-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
155 lines (134 loc) · 4.26 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
"use strict";
const { generate } = require("gas-entry-generator");
const { SourceMapSource, RawSource } = require("webpack-sources");
const { RuntimeGlobals, Dependency } = require("webpack");
const { minimatch } = require("minimatch");
const path = require("path");
const slash = require("slash");
const defaultOptions = {
comment: true,
autoGlobalExportsFiles: [],
include: ["**/*"],
};
function GasPlugin(options) {
this.options = Object.assign({}, defaultOptions, options || {});
}
function gasify(compilation, chunk, filename, entryFunctions) {
const asset = compilation.assets[filename];
let source, map;
if (asset.sourceAndMap) {
let sourceAndMap = asset.sourceAndMap();
source = sourceAndMap.source;
map = sourceAndMap.map;
} else {
source = asset.source();
map = typeof asset.map === "function" ? asset.map() : null;
}
const entries = compilation.chunkGraph
.getChunkModules(chunk)
.filter((module) => !!entryFunctions.get(module.rootModule || module))
.map(
(module) =>
entryFunctions.get(module.rootModule || module).entryPointFunctions
)
.filter((entries) => !!entries)
.join("\n");
const gasify = entries + source;
compilation.assets[filename] = map
? new SourceMapSource(gasify, filename, map)
: new RawSource(gasify);
}
class GasDependency extends Dependency {
constructor(m) {
super();
this.m = m;
}
}
GasDependency.Template = class GasDependencyTemplate {
constructor(options) {
this.comment = options.comment;
this.autoGlobalExportsFilePatterns = options.autoGlobalExportsFilePatterns;
this.includePatterns = options.includePatterns;
this.entryFunctions = new Map();
}
match(target, pattern) {
return minimatch(slash(target), slash(pattern));
}
apply(dep, source) {
const module = dep.m;
if (
!this.includePatterns.some((file) => this.match(module.resource, file))
) {
return;
}
const options = {
comment: this.comment,
autoGlobalExports:
module.resource &&
this.autoGlobalExportsFilePatterns.some((file) =>
this.match(module.resource, file)
),
exportsIdentifierName: RuntimeGlobals.exports,
globalIdentifierName: RuntimeGlobals.global,
};
const originalSource =
typeof source.original === "function"
? source.original().source()
: source.source();
const output = generate(originalSource, options);
this.entryFunctions.set(module, output);
if (output.globalAssignments) {
source.insert(originalSource.length, output.globalAssignments);
}
}
};
GasPlugin.prototype.apply = function (compiler) {
const context = compiler.options.context;
const autoGlobalExportsFilePatterns = this.options.autoGlobalExportsFiles.map(
(file) => (path.isAbsolute(file) ? file : path.resolve(context, file))
);
const includePatterns = this.options.include.map((file) =>
path.isAbsolute(file) ? file : path.resolve(context, file)
);
const plugin = { name: "GasPlugin" };
const compilationHook = (compilation, { normalModuleFactory }) => {
const gasDependencyTemplate = new GasDependency.Template({
comment: this.options.comment,
autoGlobalExportsFilePatterns,
includePatterns,
});
compilation.dependencyTemplates.set(GasDependency, gasDependencyTemplate);
const handler = (parser) => {
parser.hooks.program.tap(plugin, () => {
parser.state.current.addDependency(
new GasDependency(parser.state.current)
);
});
};
normalModuleFactory.hooks.parser
.for("javascript/auto")
.tap("GasPlugin", handler);
normalModuleFactory.hooks.parser
.for("javascript/dynamic")
.tap("GasPlugin", handler);
normalModuleFactory.hooks.parser
.for("javascript/esm")
.tap("GasPlugin", handler);
compilation.hooks.chunkAsset.tap(plugin, (chunk, filename) => {
gasify(
compilation,
chunk,
filename,
gasDependencyTemplate.entryFunctions
);
});
compilation.hooks.additionalModuleRuntimeRequirements.tap(
plugin,
(_module, set) => {
set.add(RuntimeGlobals.global);
}
);
};
compiler.hooks.compilation.tap(plugin, compilationHook);
};
module.exports = GasPlugin;