@freshpack/webpack 1.0.0 → 1.0.2
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/CHANGELOG.md +16 -0
- package/LICENSE +13 -12
- package/denoEnv.d.ts +12 -0
- package/denoEnv.js +49 -0
- package/denoEnv.js.map +1 -0
- package/fresh.d.ts +39 -0
- package/fresh.js +245 -0
- package/fresh.js.map +1 -0
- package/importMetaResolve.d.ts +4 -0
- package/importMetaResolve.js +50 -0
- package/importMetaResolve.js.map +1 -0
- package/mod.d.ts +8 -0
- package/mod.js +9 -0
- package/mod.js.map +1 -0
- package/package.json +81 -9
- package/prefresh-forked/index.d.ts +15 -0
- package/prefresh-forked/index.js +59 -0
- package/prefresh-forked/index.js.map +1 -0
- package/prefresh-forked/loader/index.d.ts +2 -0
- package/prefresh-forked/loader/index.js +5 -0
- package/prefresh-forked/loader/index.js.map +1 -0
- package/prefresh-forked/utils/Runtime.d.ts +5 -0
- package/prefresh-forked/utils/Runtime.js +27 -0
- package/prefresh-forked/utils/Runtime.js.map +1 -0
- package/prefresh-forked/utils/constants.d.ts +8 -0
- package/prefresh-forked/utils/constants.js +12 -0
- package/prefresh-forked/utils/constants.js.map +1 -0
- package/prefresh-forked/utils/prefresh.d.ts +5 -0
- package/prefresh-forked/utils/{prefresh.ts → prefresh.js} +8 -27
- package/prefresh-forked/utils/prefresh.js.map +1 -0
- package/quasiquote.d.ts +9 -0
- package/quasiquote.js +32 -0
- package/quasiquote.js.map +1 -0
- package/rawBytesLoader.d.ts +5 -0
- package/rawBytesLoader.js +10 -0
- package/rawBytesLoader.js.map +1 -0
- package/rawImports.d.ts +13 -0
- package/rawImports.js +44 -0
- package/rawImports.js.map +1 -0
- package/requireHook.d.ts +1 -0
- package/{requireHook.ts → requireHook.js} +5 -10
- package/requireHook.js.map +1 -0
- package/resolve.d.ts +11 -0
- package/resolve.js +395 -0
- package/resolve.js.map +1 -0
- package/transpileLoader.d.ts +9 -0
- package/transpileLoader.js +46 -0
- package/transpileLoader.js.map +1 -0
- package/unmap.d.ts +9 -0
- package/unmap.js +94 -0
- package/unmap.js.map +1 -0
- package/util.d.ts +9 -0
- package/{util.ts → util.js} +13 -19
- package/util.js.map +1 -0
- package/viteComment.d.ts +8 -0
- package/viteComment.js +28 -0
- package/viteComment.js.map +1 -0
- package/buildCacheUtil.ts +0 -11
- package/denoEnv.ts +0 -78
- package/fresh.ts +0 -391
- package/mod.ts +0 -7
- package/prefresh-forked/index.ts +0 -114
- package/prefresh-forked/loader/index.ts +0 -10
- package/prefresh-forked/utils/Runtime.ts +0 -79
- package/prefresh-forked/utils/constants.ts +0 -35
- package/prefresh-forked/utils/createTemplate.ts +0 -76
- package/prefresh-forked/utils/injectEntry.ts +0 -28
- package/quasiquote.ts +0 -44
- package/rawBytesLoader.ts +0 -17
- package/rawImports.ts +0 -85
- package/resolve.ts +0 -588
- package/transpileLoader.ts +0 -59
- package/unmap.ts +0 -154
- package/viteComment.ts +0 -41
package/prefresh-forked/index.ts
DELETED
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
/* FIXME: Typings are awful in this file, the port was very crude */
|
|
2
|
-
|
|
3
|
-
import Webpack from "webpack";
|
|
4
|
-
import {
|
|
5
|
-
injectRefreshFunctions,
|
|
6
|
-
matcherOptions,
|
|
7
|
-
NAME,
|
|
8
|
-
prefreshUtils,
|
|
9
|
-
} from "./utils/constants.ts";
|
|
10
|
-
import { PrefreshRuntimeModule } from "./utils/Runtime.ts";
|
|
11
|
-
import { defineRequire } from "../requireHook.ts";
|
|
12
|
-
|
|
13
|
-
type Options = {
|
|
14
|
-
overlay?: {
|
|
15
|
-
module: string;
|
|
16
|
-
};
|
|
17
|
-
entryOptions?: string | Webpack.EntryOptions;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
const internalPrefreshLoader = defineRequire(
|
|
21
|
-
"@freshpack/webpack/internal-prefresh-loader",
|
|
22
|
-
await import("./loader/index.ts"),
|
|
23
|
-
);
|
|
24
|
-
|
|
25
|
-
export class DenoPreactRefreshPlugin {
|
|
26
|
-
matcher;
|
|
27
|
-
constructor(public options: Options) {
|
|
28
|
-
this.matcher = Webpack.ModuleFilenameHelpers.matchObject.bind(
|
|
29
|
-
undefined,
|
|
30
|
-
matcherOptions,
|
|
31
|
-
);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
webpack5(
|
|
35
|
-
compiler: Webpack.Compiler,
|
|
36
|
-
RuntimeGlobals: typeof Webpack.RuntimeGlobals,
|
|
37
|
-
) {
|
|
38
|
-
compiler.hooks.compilation.tap(
|
|
39
|
-
NAME,
|
|
40
|
-
(compilation, { normalModuleFactory }) => {
|
|
41
|
-
if (compilation.compiler !== compiler) {
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
injectRefreshFunctions(compilation);
|
|
46
|
-
|
|
47
|
-
compilation.hooks.additionalTreeRuntimeRequirements.tap(
|
|
48
|
-
NAME,
|
|
49
|
-
(chunk, runtimeRequirements) => {
|
|
50
|
-
runtimeRequirements.add(RuntimeGlobals.interceptModuleExecution);
|
|
51
|
-
compilation.addRuntimeModule(chunk, new PrefreshRuntimeModule());
|
|
52
|
-
},
|
|
53
|
-
);
|
|
54
|
-
|
|
55
|
-
normalModuleFactory.hooks.afterResolve.tap(
|
|
56
|
-
NAME,
|
|
57
|
-
({ createData: data }) => {
|
|
58
|
-
if (
|
|
59
|
-
this.matcher(data.resource!) &&
|
|
60
|
-
!data.resource!.includes("@prefresh") &&
|
|
61
|
-
!data.resource!.includes("/prefresh-forked/loader/") &&
|
|
62
|
-
!data.resource!.includes("/prefresh-forked/utils/")
|
|
63
|
-
) {
|
|
64
|
-
data.loaders!.unshift({
|
|
65
|
-
loader: internalPrefreshLoader,
|
|
66
|
-
options: undefined,
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
},
|
|
70
|
-
);
|
|
71
|
-
},
|
|
72
|
-
);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
apply(compiler: Webpack.Compiler) {
|
|
76
|
-
if (
|
|
77
|
-
compiler.options.mode === "production"
|
|
78
|
-
) {
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const provide = {
|
|
83
|
-
[prefreshUtils]: import.meta.resolve("./utils/prefresh.ts"),
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
if (this.options.overlay) {
|
|
87
|
-
(provide as any).__prefresh_errors__ = import.meta.resolve(
|
|
88
|
-
this.options.overlay.module,
|
|
89
|
-
);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
const providePlugin = new Webpack.ProvidePlugin(provide);
|
|
93
|
-
providePlugin.apply(compiler);
|
|
94
|
-
const dependency = Webpack.EntryPlugin.createDependency(
|
|
95
|
-
"@prefresh/core",
|
|
96
|
-
{ name: "@prefresh/core" },
|
|
97
|
-
);
|
|
98
|
-
compiler.hooks.make.tapAsync(NAME, (compilation, callback) => {
|
|
99
|
-
compilation.addEntry(
|
|
100
|
-
compiler.context,
|
|
101
|
-
dependency,
|
|
102
|
-
this.options.entryOptions ?? {},
|
|
103
|
-
callback as any,
|
|
104
|
-
);
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
this.webpack5(
|
|
108
|
-
compiler,
|
|
109
|
-
compiler.webpack
|
|
110
|
-
? compiler.webpack.RuntimeGlobals
|
|
111
|
-
: Webpack.RuntimeGlobals,
|
|
112
|
-
);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import type Webpack from "webpack";
|
|
2
|
-
import runtimeText from "./runtime.js" with { type: "text" };
|
|
3
|
-
|
|
4
|
-
export default function RefreshHotLoader(
|
|
5
|
-
this: any,
|
|
6
|
-
source: Webpack.sources.SourceMapSource,
|
|
7
|
-
inputSourceMap: Webpack.sources.SourceMapSource["source"],
|
|
8
|
-
) {
|
|
9
|
-
this.callback(null, source + "\n\n" + runtimeText, inputSourceMap);
|
|
10
|
-
}
|
|
@@ -1,79 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,35 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,76 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,28 +0,0 @@
|
|
|
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
|
-
}
|
package/quasiquote.ts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
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
|
-
});
|
package/rawBytesLoader.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
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
|
-
}
|