-
Notifications
You must be signed in to change notification settings - Fork 1
/
LoadScriptRuntimeModule.js
67 lines (58 loc) · 1.91 KB
/
LoadScriptRuntimeModule.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
/*
MIT License http://www.opensource.org/licenses/mit-license.php
modify base on https://github.com/webpack/webpack/blob/v5.36.2/lib/runtime/LoadScriptRuntimeModule.js
make change to support load script in hippy
*/
"use strict";
const { SyncWaterfallHook } = require("tapable");
const Compilation = require("webpack/lib/Compilation");
const RuntimeGlobals = require("webpack/lib/RuntimeGlobals");
const Template = require("webpack/lib/Template");
const HelperRuntimeModule = require("webpack/lib/runtime/HelperRuntimeModule");
/** @typedef {import("../Chunk")} Chunk */
/** @typedef {import("../Compiler")} Compiler */
/**
* @typedef {Object} LoadScriptCompilationHooks
* @property {SyncWaterfallHook<[string, Chunk]>} createScript
*/
/** @type {WeakMap<Compilation, LoadScriptCompilationHooks>} */
const compilationHooksMap = new WeakMap();
class LoadScriptRuntimeModule extends HelperRuntimeModule {
/**
* @param {Compilation} compilation the compilation
* @returns {LoadScriptCompilationHooks} hooks
*/
static getCompilationHooks(compilation) {
if (!(compilation instanceof Compilation)) {
throw new TypeError(
"The 'compilation' argument must be an instance of Compilation"
);
}
let hooks = compilationHooksMap.get(compilation);
if (hooks === undefined) {
hooks = {
createScript: new SyncWaterfallHook(["source", "chunk"])
};
compilationHooksMap.set(compilation, hooks);
}
return hooks;
}
constructor() {
super("load script for hippy");
}
/**
* @returns {string} runtime code
*/
generate() {
const { compilation } = this;
const { runtimeTemplate, outputOptions } = compilation;
const fn = RuntimeGlobals.loadScript;
return Template.asString([
"// loadScript function to load a script via script tag",
`${fn} = ${runtimeTemplate.basicFunction("url, done, key, chunkId", [
"global.dynamicLoad(url, done)",
])};`
]);
}
}
module.exports = LoadScriptRuntimeModule;