This repository was archived by the owner on Nov 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
121 lines (111 loc) · 4 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const path = require("path")
const webpack = require("webpack")
const fs = require("fs")
module.exports.getEntrySet = function (compiler) {
const extensions = (compiler.options.resolve || {}).extensions || []
extensions.unshift("")
function entryToStringArray (entry) {
if (entry instanceof Array) {
return entry.map(entryItem => entryToStringArray(entryItem))
.reduce((p, n) => p.concat(n))
} else if (typeof entry === "object") {
return Object.keys(entry).map(name => entryToStringArray(entry[name]))
.reduce((p, n) => p.concat(n))
} else if (typeof entry === "string") {
return [entry]
}
}
return new Set(
entryToStringArray(compiler.options.entry)
.map(entryPath => /^(\/|[^/\\]+\:)/.test(entryPath) ? entryPath : path.join(compiler.context, entryPath))
.map(url => url.replace(/(\?|\!|\#).*$/, ""))
.map(url => {
for (let index = 0; index < extensions.length; index++) {
const extension = extensions[index];
if (fs.existsSync(url + extension) && fs.statSync(url + extension).isFile()) {
return url + extension
}
}
})
)
}
module.exports.setEntry = function (optionsEntry, cb) {
function entryToStringArray (entry) {
if (entry instanceof Array) {
return entry.map(entryItem => entryToStringArray(entryItem))
.reduce((p, n) => cb(p) | cb(n))
} else if (typeof entry === "object") {
return Object.keys(entry).forEach(name => {
entry[name] = cb(entry[name])
})
} else if (typeof entry === "string") {
return [entry]
}
}
}
module.exports.stringifyHasFun = function (obj) {
return JSON.parse(JSON.stringify({
...obj,
toJSON () {
const funJSON = []
let hasFunction = false
let hasOther = false
Object.keys(obj).forEach(key => {
if (typeof obj[key] !== "function") {
hasOther = true
return
}
hasFunction = true
const [args, body] = obj[key].toString().match(/\(([.\s\S]*?\))|(\{[.\s\S]*)\}/g)
funJSON.push(`"${key}": function ${args} ${body}`)
})
return JSON.stringify(obj).replace(/\}$/, `${hasFunction && hasOther ? "," : ""}${funJSON.join(",")}}`)
}
}))
}
/**
* @callback MatchObject
* @param {string} [str]
* @returns {boolean}
*/
/**
* @typedef {Object} InjectLoaderOptions
* @property {MatchObject} match A function to include/exclude files to be processed.
* @property {import('../../loader/types').ReactRefreshLoaderOptions} [options] Options passed to the loader.
*/
/**
* Injects refresh loader to all JavaScript-like and user-specified files.
* @param {*} moduleData Module factory creation data.
* @param {InjectLoaderOptions} injectOptions Options to alter how the loader is injected.
* @returns {*} The injected module factory creation data.
*/
function injectRefreshLoader(moduleData, injectOptions, resolvedLoader) {
const { match, options } = injectOptions;
if (
match(moduleData) &&
// Exclude files referenced as assets
!moduleData.type.includes('asset') &&
// Check to prevent double injection
!moduleData.loaders.find(({ loader }) => loader === resolvedLoader)
) {
// As we inject runtime code for each module,
// it is important to run the injected loader after everything.
// This way we can ensure that all code-processing have been done,
// and we won't risk breaking tools like Flow or ESLint.
moduleData.loaders.unshift({
loader: resolvedLoader,
options,
});
}
return moduleData;
}
module.exports.getUsedWpmPackages = function getUsedWpmPackages (source) {
const wpmPackagesPattern = /\_\_wpm\_\_importWpmLoader\_\_wpmPackagesTag([.\s\S]+?)\_\_wpm\_\_importWpmLoader\_\_wpmPackagesTag/g
let used = []
let currentUsed
while (currentUsed = wpmPackagesPattern.exec(source)) {
used = used.concat(currentUsed[1].split(" "))
}
return [...new Set(used)]
}
module.exports.injectRefreshLoader = injectRefreshLoader;