@freshpack/webpack 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +20 -0
- package/buildCacheUtil.ts +11 -0
- package/denoEnv.ts +78 -0
- package/fresh.ts +391 -0
- package/mod.ts +7 -0
- package/package.json +19 -3
- package/prefresh-forked/index.ts +114 -0
- package/prefresh-forked/loader/index.ts +10 -0
- package/prefresh-forked/loader/runtime.js +70 -0
- package/prefresh-forked/utils/Runtime.ts +79 -0
- package/prefresh-forked/utils/constants.ts +35 -0
- package/prefresh-forked/utils/createTemplate.ts +76 -0
- package/prefresh-forked/utils/injectEntry.ts +28 -0
- package/prefresh-forked/utils/prefresh.ts +59 -0
- package/quasiquote.ts +44 -0
- package/rawBytesLoader.ts +17 -0
- package/rawImports.ts +85 -0
- package/requireHook.ts +22 -0
- package/resolve.ts +588 -0
- package/transpileLoader.ts +59 -0
- package/unmap.ts +154 -0
- package/util.ts +58 -0
- package/viteComment.ts +41 -0
- package/README.md +0 -3
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// declare const __prefresh_utils__: any;
|
|
2
|
+
// declare const __prefresh_errors__: any;
|
|
3
|
+
// declare const __webpack_module__: any;
|
|
4
|
+
// declare const __webpack_modules__: any;
|
|
5
|
+
// declare const error: any;
|
|
6
|
+
|
|
7
|
+
const isPrefreshComponent = __prefresh_utils__.shouldBind(__webpack_module__);
|
|
8
|
+
|
|
9
|
+
// `@vanilla-extract/webpack` does some custom preprocessing where
|
|
10
|
+
// `module.hot` is partially replaced. This leads to our injected
|
|
11
|
+
// code being executed although it shouldn't be:
|
|
12
|
+
//
|
|
13
|
+
// Intermediate result:
|
|
14
|
+
//
|
|
15
|
+
// if (true) { // <- inlined by intermediate compile step
|
|
16
|
+
// const previousHotModuleExports = module.hot.data && ...
|
|
17
|
+
// // Crash happens here ---^
|
|
18
|
+
//
|
|
19
|
+
// It crashes at that line because some intermediate compiler isn't
|
|
20
|
+
// running in hot mode, but the overall guard condition was compiled
|
|
21
|
+
// down to being truthy. By moving `module.hot` outside of the
|
|
22
|
+
// condition of the if-statement, it will be left as is.
|
|
23
|
+
const moduleHot = __webpack_module__.hot;
|
|
24
|
+
|
|
25
|
+
if (moduleHot) {
|
|
26
|
+
const currentExports = __prefresh_utils__.getExports(__webpack_module__);
|
|
27
|
+
const previousHotModuleExports = moduleHot.data &&
|
|
28
|
+
moduleHot.data.moduleExports;
|
|
29
|
+
|
|
30
|
+
__prefresh_utils__.registerExports(currentExports, __webpack_module__.id);
|
|
31
|
+
|
|
32
|
+
if (isPrefreshComponent) {
|
|
33
|
+
if (previousHotModuleExports) {
|
|
34
|
+
try {
|
|
35
|
+
__prefresh_utils__.flush();
|
|
36
|
+
if (
|
|
37
|
+
typeof __prefresh_errors__ !== "undefined" &&
|
|
38
|
+
__prefresh_errors__ &&
|
|
39
|
+
__prefresh_errors__.clearRuntimeErrors
|
|
40
|
+
) {
|
|
41
|
+
__prefresh_errors__.clearRuntimeErrors();
|
|
42
|
+
}
|
|
43
|
+
} catch (e) {
|
|
44
|
+
// Only available in newer webpack versions.
|
|
45
|
+
if (moduleHot.invalidate) {
|
|
46
|
+
moduleHot.invalidate();
|
|
47
|
+
} else {
|
|
48
|
+
console.log("[PREFRESH] Failed to flush updates:", e);
|
|
49
|
+
self.location.reload();
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
moduleHot.dispose((data) => {
|
|
55
|
+
data.moduleExports = __prefresh_utils__.getExports(__webpack_module__);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
moduleHot.accept(function errorRecovery() {
|
|
59
|
+
if (
|
|
60
|
+
typeof __prefresh_errors__ !== "undefined" &&
|
|
61
|
+
__prefresh_errors__ &&
|
|
62
|
+
__prefresh_errors__.handleRuntimeError
|
|
63
|
+
) {
|
|
64
|
+
__prefresh_errors__.handleRuntimeError(error);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
__webpack_modules__[__webpack_module__.id].hot.accept(errorRecovery);
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import Webpack from "webpack";
|
|
2
|
+
|
|
3
|
+
const NAMESPACE = "__PREFRESH__";
|
|
4
|
+
|
|
5
|
+
const beforeModule = `
|
|
6
|
+
self.$RefreshSig$ = function() {
|
|
7
|
+
var status = 'begin';
|
|
8
|
+
var savedType;
|
|
9
|
+
|
|
10
|
+
return function(type, key, forceReset, getCustomHooks) {
|
|
11
|
+
if (!savedType) savedType = type;
|
|
12
|
+
status = self.${NAMESPACE}.sign(type || savedType, key, forceReset, getCustomHooks, status);
|
|
13
|
+
return type;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
`;
|
|
17
|
+
|
|
18
|
+
export class PrefreshRuntimeModule extends Webpack.RuntimeModule {
|
|
19
|
+
constructor() {
|
|
20
|
+
super("prefresh", 5);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
override generate() {
|
|
24
|
+
const { runtimeTemplate } = this.compilation as any;
|
|
25
|
+
const declare = runtimeTemplate.supportsConst() ? "const" : "var";
|
|
26
|
+
|
|
27
|
+
return Webpack.Template.asString([
|
|
28
|
+
`${Webpack.RuntimeGlobals.interceptModuleExecution}.push(${
|
|
29
|
+
runtimeTemplate.basicFunction("options", [
|
|
30
|
+
`${declare} originalFactory = options.factory;`,
|
|
31
|
+
`options.factory = ${
|
|
32
|
+
runtimeTemplate.basicFunction(
|
|
33
|
+
"moduleObject, moduleExports, webpackRequire",
|
|
34
|
+
[
|
|
35
|
+
`if (typeof self !== "undefined" && typeof self.${NAMESPACE} !== "undefined") {`,
|
|
36
|
+
Webpack.Template.indent([
|
|
37
|
+
`${declare} prevRefreshReg = self.$RefreshReg$;`,
|
|
38
|
+
`${declare} prevRefreshSig = self.$RefreshSig$;`,
|
|
39
|
+
beforeModule,
|
|
40
|
+
`${declare} reg = ${
|
|
41
|
+
runtimeTemplate.basicFunction(
|
|
42
|
+
"currentModuleId",
|
|
43
|
+
[
|
|
44
|
+
"self.$RefreshReg$ = function(type, id) {",
|
|
45
|
+
Webpack.Template.indent(
|
|
46
|
+
`self.${NAMESPACE}.register(type, currentModuleId + ' ' + id);`,
|
|
47
|
+
),
|
|
48
|
+
"};",
|
|
49
|
+
],
|
|
50
|
+
)
|
|
51
|
+
}`,
|
|
52
|
+
"reg()",
|
|
53
|
+
"try {",
|
|
54
|
+
Webpack.Template.indent(
|
|
55
|
+
"originalFactory.call(this, moduleObject, moduleExports, webpackRequire);",
|
|
56
|
+
),
|
|
57
|
+
"} finally {",
|
|
58
|
+
Webpack.Template.indent(
|
|
59
|
+
"self.$RefreshReg$ = prevRefreshReg;",
|
|
60
|
+
),
|
|
61
|
+
Webpack.Template.indent(
|
|
62
|
+
"self.$RefreshSig$ = prevRefreshSig;",
|
|
63
|
+
),
|
|
64
|
+
"}",
|
|
65
|
+
]),
|
|
66
|
+
"} else {",
|
|
67
|
+
Webpack.Template.indent(
|
|
68
|
+
"originalFactory.call(this, moduleObject, moduleExports, webpackRequire);",
|
|
69
|
+
),
|
|
70
|
+
"}",
|
|
71
|
+
],
|
|
72
|
+
)
|
|
73
|
+
}`,
|
|
74
|
+
])
|
|
75
|
+
})`,
|
|
76
|
+
"",
|
|
77
|
+
]);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import Webpack from "webpack";
|
|
2
|
+
|
|
3
|
+
export const prefreshUtils = "__prefresh_utils__";
|
|
4
|
+
export const NAME = "PrefreshWebpackPlugin";
|
|
5
|
+
|
|
6
|
+
export const matcherOptions = {
|
|
7
|
+
include: /\.([jt]sx?)$/,
|
|
8
|
+
exclude: /node_modules|\/deno\/npm\//,
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export function injectRefreshFunctions(compilation: Webpack.Compilation) {
|
|
12
|
+
const hookVars = compilation.mainTemplate.hooks.localVars;
|
|
13
|
+
|
|
14
|
+
hookVars.tap(
|
|
15
|
+
"ReactFreshWebpackPlugin",
|
|
16
|
+
(source) =>
|
|
17
|
+
Webpack.Template.asString([
|
|
18
|
+
source,
|
|
19
|
+
"",
|
|
20
|
+
"// noop fns to prevent runtime errors during initialization",
|
|
21
|
+
'if (typeof self !== "undefined") {',
|
|
22
|
+
Webpack.Template.indent("self.$RefreshReg$ = function () {};"),
|
|
23
|
+
Webpack.Template.indent("self.$RefreshSig$ = function () {"),
|
|
24
|
+
Webpack.Template.indent(
|
|
25
|
+
Webpack.Template.indent("return function (type) {"),
|
|
26
|
+
),
|
|
27
|
+
Webpack.Template.indent(
|
|
28
|
+
Webpack.Template.indent(Webpack.Template.indent("return type;")),
|
|
29
|
+
),
|
|
30
|
+
Webpack.Template.indent(Webpack.Template.indent("};")),
|
|
31
|
+
Webpack.Template.indent("};"),
|
|
32
|
+
"}",
|
|
33
|
+
]),
|
|
34
|
+
);
|
|
35
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
const { Template } = require("webpack");
|
|
2
|
+
|
|
3
|
+
const NAMESPACE = "__PREFRESH__";
|
|
4
|
+
|
|
5
|
+
const beforeModule = `
|
|
6
|
+
var prevRefreshReg = self.$RefreshReg$;
|
|
7
|
+
var prevRefreshSig = self.$RefreshSig$;
|
|
8
|
+
|
|
9
|
+
self.$RefreshSig$ = function() {
|
|
10
|
+
var status = 'begin';
|
|
11
|
+
var savedType;
|
|
12
|
+
return function(type, key, forceReset, getCustomHooks) {
|
|
13
|
+
if (!savedType) savedType = type;
|
|
14
|
+
status = self.${NAMESPACE}.sign(type || savedType, key, forceReset, getCustomHooks, status);
|
|
15
|
+
return type;
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
self.$RefreshReg$ = function(type, id) {
|
|
20
|
+
self.${NAMESPACE}.register(type, module.id + ' ' + id);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
try {
|
|
24
|
+
`;
|
|
25
|
+
|
|
26
|
+
const afterModule = `
|
|
27
|
+
} finally {
|
|
28
|
+
self.$RefreshReg$ = prevRefreshReg;
|
|
29
|
+
self.$RefreshSig$ = prevRefreshSig;
|
|
30
|
+
}
|
|
31
|
+
`;
|
|
32
|
+
|
|
33
|
+
export function createRefreshTemplate(
|
|
34
|
+
source,
|
|
35
|
+
chunk,
|
|
36
|
+
hash,
|
|
37
|
+
mainTemplate,
|
|
38
|
+
options,
|
|
39
|
+
) {
|
|
40
|
+
let filename = mainTemplate.outputOptions.filename;
|
|
41
|
+
if (typeof filename === "function") {
|
|
42
|
+
filename = filename({
|
|
43
|
+
chunk,
|
|
44
|
+
hash,
|
|
45
|
+
contentHashType: "javascript",
|
|
46
|
+
hashWithLength: (length) =>
|
|
47
|
+
mainTemplate.renderCurrentHashCode(hash, length),
|
|
48
|
+
noChunkHash: mainTemplate.useChunkHash(chunk),
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (!filename || !filename.includes(".js")) {
|
|
53
|
+
return source;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const lines = source.split("\n");
|
|
57
|
+
|
|
58
|
+
// Webpack generates this line whenever the mainTemplate is called
|
|
59
|
+
const moduleInitializationLineNumber = lines.findIndex((line) =>
|
|
60
|
+
options.runsInNextJs
|
|
61
|
+
? line.includes("modules[moduleId].call(")
|
|
62
|
+
: line.startsWith("modules[moduleId].call")
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
if (moduleInitializationLineNumber === -1) {
|
|
66
|
+
return source;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return Template.asString([
|
|
70
|
+
...lines.slice(0, moduleInitializationLineNumber),
|
|
71
|
+
beforeModule,
|
|
72
|
+
Template.indent(lines[moduleInitializationLineNumber]),
|
|
73
|
+
afterModule,
|
|
74
|
+
...lines.slice(moduleInitializationLineNumber + 1, lines.length),
|
|
75
|
+
]);
|
|
76
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export function injectEntry(originalEntry: any): any {
|
|
2
|
+
const entryInjects = [require.resolve("@prefresh/core")];
|
|
3
|
+
|
|
4
|
+
if (typeof originalEntry === "string") {
|
|
5
|
+
return [...entryInjects, originalEntry];
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
if (Array.isArray(originalEntry)) {
|
|
9
|
+
return [...entryInjects, ...originalEntry];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (typeof originalEntry === "object") {
|
|
13
|
+
return Object.entries(originalEntry).reduce(
|
|
14
|
+
(acc, [curKey, curEntry]) => ({
|
|
15
|
+
...acc,
|
|
16
|
+
[curKey]: injectEntry(curEntry),
|
|
17
|
+
}),
|
|
18
|
+
{},
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (typeof originalEntry === "function") {
|
|
23
|
+
return (...args: any[]) =>
|
|
24
|
+
Promise.resolve(originalEntry(...args)).then(injectEntry);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
throw new Error("Can't detect valid entry point.");
|
|
28
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { flush, isComponent } from "@prefresh/utils";
|
|
2
|
+
|
|
3
|
+
// eslint-disable-next-line
|
|
4
|
+
export const getExports = (m: any) => m.exports || m.__proto__.exports;
|
|
5
|
+
|
|
6
|
+
function isSafeExport(key: string) {
|
|
7
|
+
return (
|
|
8
|
+
key === "__esModule" ||
|
|
9
|
+
key === "__N_SSG" ||
|
|
10
|
+
key === "__N_SSP" ||
|
|
11
|
+
key === "config"
|
|
12
|
+
);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function registerExports(moduleExports: any, moduleId: string) {
|
|
16
|
+
if (
|
|
17
|
+
typeof self !== "undefined" && typeof self["__PREFRESH__"] !== "undefined"
|
|
18
|
+
) {
|
|
19
|
+
self["__PREFRESH__"].register(moduleExports, moduleId + " %exports%");
|
|
20
|
+
if (moduleExports == null || typeof moduleExports !== "object") return;
|
|
21
|
+
|
|
22
|
+
for (const key in moduleExports) {
|
|
23
|
+
if (isSafeExport(key)) continue;
|
|
24
|
+
const exportValue = moduleExports[key];
|
|
25
|
+
const typeID = moduleId + " %exports% " + key;
|
|
26
|
+
self["__PREFRESH__"].register(exportValue, typeID);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const shouldBind = (m: any) => {
|
|
32
|
+
let isCitizen = false;
|
|
33
|
+
const moduleExports = getExports(m);
|
|
34
|
+
|
|
35
|
+
if (isComponent(moduleExports)) {
|
|
36
|
+
isCitizen = true;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (
|
|
40
|
+
moduleExports === undefined ||
|
|
41
|
+
moduleExports === null ||
|
|
42
|
+
typeof moduleExports !== "object"
|
|
43
|
+
) {
|
|
44
|
+
isCitizen = isCitizen || false;
|
|
45
|
+
} else {
|
|
46
|
+
for (const key in moduleExports) {
|
|
47
|
+
if (key === "__esModule") continue;
|
|
48
|
+
|
|
49
|
+
const exportValue = moduleExports[key];
|
|
50
|
+
if (isComponent(exportValue)) {
|
|
51
|
+
isCitizen = isCitizen || true;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return isCitizen;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export { flush };
|
package/quasiquote.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export type JsRaw = { _jsRaw: string };
|
|
2
|
+
type ToJsRaw =
|
|
3
|
+
| string
|
|
4
|
+
| JsRaw
|
|
5
|
+
| (ToJsRaw[])
|
|
6
|
+
| Record<string, string | JsRaw>
|
|
7
|
+
| Map<string, JsRaw>
|
|
8
|
+
| undefined;
|
|
9
|
+
export function toJsRaw(v: ToJsRaw): JsRaw {
|
|
10
|
+
if (typeof v === "string") {
|
|
11
|
+
return { _jsRaw: JSON.stringify(v) };
|
|
12
|
+
}
|
|
13
|
+
if (v instanceof Array) {
|
|
14
|
+
return {
|
|
15
|
+
_jsRaw: "[" + v.map((v) =>
|
|
16
|
+
toJsRaw(v)._jsRaw
|
|
17
|
+
).join(v.length > 5 ? ",\n" : ",") +
|
|
18
|
+
"]",
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
if (v instanceof Map) {
|
|
22
|
+
return js`new Map(${
|
|
23
|
+
v.entries().map(([name, v]) => js`[${name},${v}]`).toArray()
|
|
24
|
+
})`;
|
|
25
|
+
}
|
|
26
|
+
if (typeof v === "object" && !("_jsRaw" in v)) {
|
|
27
|
+
const entries = Object.entries(v);
|
|
28
|
+
return {
|
|
29
|
+
_jsRaw: "{" + entries.map(([name, v]) =>
|
|
30
|
+
`${name}:${toJsRaw(v)._jsRaw}`
|
|
31
|
+
).join(entries.length > 10 ? ",\n" : ",") + "}",
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
if (typeof v === "undefined") {
|
|
35
|
+
return js`undefined`;
|
|
36
|
+
}
|
|
37
|
+
if (v === null) {
|
|
38
|
+
return js`null`;
|
|
39
|
+
}
|
|
40
|
+
return v as JsRaw;
|
|
41
|
+
}
|
|
42
|
+
export const js = (template: TemplateStringsArray, ...subs: ToJsRaw[]) => ({
|
|
43
|
+
_jsRaw: String.raw(template, ...subs.map((v) => toJsRaw(v)._jsRaw)),
|
|
44
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { LoaderContext } from "webpack";
|
|
2
|
+
|
|
3
|
+
type Context = LoaderContext<Record<never, never>>;
|
|
4
|
+
|
|
5
|
+
export function pitch(this: Context): string {
|
|
6
|
+
const resource = this.resourcePath;
|
|
7
|
+
this.addDependency(resource);
|
|
8
|
+
const request = `${resource}.webpack[asset/resource]!=!${resource}`;
|
|
9
|
+
return [
|
|
10
|
+
`import url from ${JSON.stringify(request)};`,
|
|
11
|
+
`export default new Uint8Array(await (await fetch(url)).arrayBuffer());`,
|
|
12
|
+
].join("\n");
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export default function rawBytesLoader(this: Context, source: string): string {
|
|
16
|
+
return source;
|
|
17
|
+
}
|
package/rawImports.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type { Compiler } from "webpack";
|
|
2
|
+
import { defineRequire } from "./requireHook.ts";
|
|
3
|
+
|
|
4
|
+
const NAME = "RawImportsPlugin";
|
|
5
|
+
|
|
6
|
+
const internalRawBytesLoader = defineRequire(
|
|
7
|
+
"@freshpack/webpack/internal-raw-bytes-loader",
|
|
8
|
+
await import("./rawBytesLoader.ts"),
|
|
9
|
+
);
|
|
10
|
+
|
|
11
|
+
type ModuleFactory = {
|
|
12
|
+
getParser(type: string): unknown;
|
|
13
|
+
getGenerator(type: string): unknown;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
type CreateData = {
|
|
17
|
+
type: string;
|
|
18
|
+
settings: { type?: string };
|
|
19
|
+
parser?: unknown;
|
|
20
|
+
parserOptions?: unknown;
|
|
21
|
+
generator?: unknown;
|
|
22
|
+
generatorOptions?: unknown;
|
|
23
|
+
loaders?: {
|
|
24
|
+
loader: string;
|
|
25
|
+
options: unknown;
|
|
26
|
+
ident: string | null;
|
|
27
|
+
type: string | null;
|
|
28
|
+
}[];
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export interface RawImportsOptions {
|
|
32
|
+
/**
|
|
33
|
+
* "resource" (default) emits a bytes import as a standalone asset fetched at runtime,
|
|
34
|
+
* "inline" embeds it into the importing chunk as base64.
|
|
35
|
+
*/
|
|
36
|
+
bytes?: "resource" | "inline";
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function setModuleType(
|
|
40
|
+
factory: ModuleFactory,
|
|
41
|
+
data: CreateData,
|
|
42
|
+
type: string,
|
|
43
|
+
) {
|
|
44
|
+
data.type = type;
|
|
45
|
+
data.settings.type = type;
|
|
46
|
+
data.parser = factory.getParser(type);
|
|
47
|
+
data.parserOptions = undefined;
|
|
48
|
+
data.generator = factory.getGenerator(type);
|
|
49
|
+
data.generatorOptions = undefined;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export class RawImportsPlugin {
|
|
53
|
+
constructor(public options: RawImportsOptions = {}) {}
|
|
54
|
+
|
|
55
|
+
apply(compiler: Compiler) {
|
|
56
|
+
compiler.hooks.compilation.tap(
|
|
57
|
+
NAME,
|
|
58
|
+
(_compilation, { normalModuleFactory }) => {
|
|
59
|
+
normalModuleFactory.hooks.afterResolve.tap(NAME, (resolveData) => {
|
|
60
|
+
const attributes = (resolveData.dependencies[0] as undefined | {
|
|
61
|
+
attributes?: { type?: string };
|
|
62
|
+
})?.attributes;
|
|
63
|
+
const data = resolveData.createData as unknown as CreateData;
|
|
64
|
+
if (attributes?.type === "text") {
|
|
65
|
+
setModuleType(normalModuleFactory, data, "asset/source");
|
|
66
|
+
data.loaders = [];
|
|
67
|
+
} else if (attributes?.type === "bytes") {
|
|
68
|
+
if (this.options.bytes === "inline") {
|
|
69
|
+
setModuleType(normalModuleFactory, data, "asset/bytes");
|
|
70
|
+
data.loaders = [];
|
|
71
|
+
} else {
|
|
72
|
+
setModuleType(normalModuleFactory, data, "javascript/esm");
|
|
73
|
+
data.loaders = [{
|
|
74
|
+
loader: internalRawBytesLoader,
|
|
75
|
+
options: {},
|
|
76
|
+
ident: null,
|
|
77
|
+
type: null,
|
|
78
|
+
}];
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
},
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
}
|
package/requireHook.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import Module from "node:module";
|
|
2
|
+
|
|
3
|
+
const overrides = new Map<string, Record<string, unknown>>();
|
|
4
|
+
let overrideId = 0;
|
|
5
|
+
|
|
6
|
+
const originalLoad = Module.prototype.require;
|
|
7
|
+
Module.prototype.require = function hookedRequire(id: string) {
|
|
8
|
+
const override = overrides.get(id);
|
|
9
|
+
if (override) {
|
|
10
|
+
return override;
|
|
11
|
+
}
|
|
12
|
+
return originalLoad.apply(this, arguments as any);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export function defineRequire(
|
|
16
|
+
name: string,
|
|
17
|
+
module: Record<string, unknown>,
|
|
18
|
+
): string {
|
|
19
|
+
const generatedName = `override:${name}/${overrideId++}`;
|
|
20
|
+
overrides.set(generatedName, module);
|
|
21
|
+
return generatedName;
|
|
22
|
+
}
|