-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
76 lines (63 loc) · 2.55 KB
/
index.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
const { ConcatSource } = require('webpack-sources');
const ALT_RUNTIMES = ['alt', 'alt-client', 'alt-server']; // all modules we will intercept
const ALT_ID = 'alt'; // the variable name to import the alt:V runtime as
const NATIVES_ID = 'natives'; // the variable name to import the natives runtime as
class AltvPlugin {
apply(compiler) {
const options = compiler.options;
const externals = Array.isArray(options.externals) ? options.externals : [options.externals];
// make all alt:V runtimes external so Webpack doesn't try to bundle them in
const altExternals = {
natives: NATIVES_ID
};
ALT_RUNTIMES.forEach(id => altExternals[id] = ALT_ID);
externals.push(altExternals);
options.externals = externals;
compiler.hooks.compilation.tap('AltvPlugin', compilation => {
compilation.hooks.optimizeChunkAssets.tap('AltvPlugin', chunks => {
for (const chunk of chunks) {
// alt
if (ALT_RUNTIMES.some(module => doesChunkImport(chunk, module))) {
for (const fileName of chunk.files) {
addImportHeader(compilation, fileName, ALT_ID, 'alt');
}
}
// alt
if (doesChunkImport(chunk, 'natives')) {
for (const fileName of chunk.files) {
addImportHeader(compilation, fileName, NATIVES_ID, 'natives');
}
}
}
});
});
}
}
/**
* Adds the ESM import for the alt:V runtime to the top of the file.
*
* @param {object} compilation - webpack's compilation object
* @param {string} fileName - the name of the output file in a chunk
* @param {string} importName - the name of the imported variable
* @param {string} moduleName - the name of the module to import
*/
function addImportHeader(compilation, fileName, importName, moduleName) {
const currentSource = compilation.assets[fileName];
compilation.assets[fileName] = new ConcatSource(
`import ${importName} from '${moduleName}';\n`,
currentSource
);
}
/**
* Checks if a chunk imports a module.
*
* @param {object} chunk - webpack compilation chunk
* @param {string} moduleName - the name of the module.
*/
function doesChunkImport(chunk, moduleName) {
for (const module of chunk._modules) {
if (module.id === moduleName) return true;
}
return false;
}
module.exports = AltvPlugin;