Skip to content

Commit 7fde4cc

Browse files
committed
Adapt to Webpack 5
1 parent 7f649ef commit 7fde4cc

File tree

1 file changed

+51
-12
lines changed

1 file changed

+51
-12
lines changed

src/index.js

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import loaderUtils from 'loader-utils';
44
import NodeTargetPlugin from 'webpack/lib/node/NodeTargetPlugin';
55
import SingleEntryPlugin from 'webpack/lib/SingleEntryPlugin';
66
import WebWorkerTemplatePlugin from 'webpack/lib/webworker/WebWorkerTemplatePlugin';
7+
import { getEntryRuntime } from 'webpack/lib/util/runtime';
8+
import { UsageState } from 'webpack';
79

810
export default function loader() {}
911

@@ -38,7 +40,7 @@ loader.pitch = function(request) {
3840

3941
const cb = this.async();
4042

41-
const filename = loaderUtils.interpolateName(this, `${options.name || '[hash]'}.worker.js`, {
43+
const filename = loaderUtils.interpolateName(this, `${options.name || '[fullhash]'}.worker.js`, {
4244
context: options.context || this.rootContext || this.options.context,
4345
regExp: options.regExp
4446
});
@@ -89,22 +91,37 @@ loader.pitch = function(request) {
8991

9092
compilationHook(worker.compiler, (compilation, data) => {
9193
if (compilation.cache) {
92-
if (!compilation.cache[subCache]) compilation.cache[subCache] = {};
93-
94-
compilation.cache = compilation.cache[subCache];
94+
let cache;
95+
if (compilation.cache instanceof Map) {
96+
cache = compilation.cache.get(subCache);
97+
if (!cache) {
98+
cache = new Map();
99+
compilation.cache.set(subCache, cache);
100+
}
101+
}
102+
else if (!compilation.cache[subCache]) {
103+
cache = compilation.cache[subCache] = {};
104+
}
105+
106+
compilation.cache = cache;
95107
}
96108
parseHook(data, (parser, options) => {
97109
exportDeclarationHook(parser, expr => {
98-
let decl = expr.declaration || expr,
99-
{ compilation, current } = parser.state,
100-
entry = compilation.entries[0].resource;
110+
let decl = expr.declaration || expr;
111+
let { compilation, current } = parser.state;
112+
113+
let entryModule =
114+
compilation.entries instanceof Map
115+
? compilation.moduleGraph.getModule(
116+
compilation.entries.get('main').dependencies[0]
117+
)
118+
: compilation.entries[0];
101119

102120
// only process entry exports
103-
if (current.resource!==entry) return;
121+
if (current.resource!==entryModule.resource) return;
104122

105123
let key = current.nameForCondition();
106124
let exports = CACHE[key] || (CACHE[key] = {});
107-
108125
if (decl.id) {
109126
exports[decl.id.name] = true;
110127
}
@@ -116,6 +133,17 @@ loader.pitch = function(request) {
116133
else {
117134
console.warn('[workerize] unknown export declaration: ', expr);
118135
}
136+
137+
if (compilation.moduleGraph) {
138+
const runtime = getEntryRuntime(compilation, 'main');
139+
for (const exportName of Object.keys(exports)) {
140+
const exportInfo = compilation.moduleGraph.getExportInfo(entryModule, exportName);
141+
exportInfo.setUsed(UsageState.Used, runtime);
142+
exportInfo.canMangleUse = false;
143+
exportInfo.canMangleProvide = false;
144+
}
145+
compilation.moduleGraph.addExtraReason(entryModule, 'used by workerize-loader');
146+
}
119147
});
120148
});
121149
});
@@ -124,9 +152,20 @@ loader.pitch = function(request) {
124152
if (err) return cb(err);
125153

126154
if (entries[0]) {
127-
worker.file = entries[0].files[0];
128-
129-
let key = entries[0].entryModule.nameForCondition();
155+
worker.file = Array.from(entries[0].files)[0];
156+
const entryModules =
157+
compilation.chunkGraph &&
158+
compilation.chunkGraph.getChunkEntryModulesIterable
159+
? Array.from(
160+
compilation.chunkGraph.getChunkEntryModulesIterable(entries[0])
161+
)
162+
: null;
163+
const entryModule =
164+
entryModules && entryModules.length > 0
165+
? entryModules[0]
166+
: entries[0].entryModule;
167+
168+
let key = entryModule.nameForCondition();
130169
let contents = compilation.assets[worker.file].source();
131170
let exports = Object.keys(CACHE[key] || {});
132171

0 commit comments

Comments
 (0)