Skip to content

Commit d439b8a

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

File tree

1 file changed

+52
-12
lines changed

1 file changed

+52
-12
lines changed

src/index.js

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ 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 { UsageState } from 'webpack';
78

89
export default function loader() {}
910

@@ -38,7 +39,7 @@ loader.pitch = function(request) {
3839

3940
const cb = this.async();
4041

41-
const filename = loaderUtils.interpolateName(this, `${options.name || '[hash]'}.worker.js`, {
42+
const filename = loaderUtils.interpolateName(this, `${options.name || '[fullhash]'}.worker.js`, {
4243
context: options.context || this.rootContext || this.options.context,
4344
regExp: options.regExp
4445
});
@@ -89,22 +90,37 @@ loader.pitch = function(request) {
8990

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

102119
// only process entry exports
103-
if (current.resource!==entry) return;
120+
if (current.resource!==entryModule.resource) return;
104121

105122
let key = current.nameForCondition();
106123
let exports = CACHE[key] || (CACHE[key] = {});
107-
108124
if (decl.id) {
109125
exports[decl.id.name] = true;
110126
}
@@ -116,6 +132,19 @@ loader.pitch = function(request) {
116132
else {
117133
console.warn('[workerize] unknown export declaration: ', expr);
118134
}
135+
136+
// This is for Webpack 5: mark the exports as used so it does not get tree-shaken away on production build
137+
if (compilation.moduleGraph) {
138+
const { getEntryRuntime } = require('webpack/lib/util/runtime');
139+
const runtime = getEntryRuntime(compilation, 'main');
140+
for (const exportName of Object.keys(exports)) {
141+
const exportInfo = compilation.moduleGraph.getExportInfo(entryModule, exportName);
142+
exportInfo.setUsed(UsageState.Used, runtime);
143+
exportInfo.canMangleUse = false;
144+
exportInfo.canMangleProvide = false;
145+
}
146+
compilation.moduleGraph.addExtraReason(entryModule, 'used by workerize-loader');
147+
}
119148
});
120149
});
121150
});
@@ -124,9 +153,20 @@ loader.pitch = function(request) {
124153
if (err) return cb(err);
125154

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

0 commit comments

Comments
 (0)