Skip to content

Commit 1d80d8f

Browse files
author
Naoaki Yamada
committed
Fix webpack memory leaks
1 parent 1be1a80 commit 1d80d8f

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

src/index.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import WebWorkerTemplatePlugin from 'webpack/lib/webworker/WebWorkerTemplatePlug
77

88
export default function loader() {}
99

10+
const CACHE = {};
1011
const tapName = 'workerize-loader';
1112

1213
function compilationHook(compiler, handler) {
@@ -37,25 +38,24 @@ loader.pitch = function(request) {
3738

3839
const cb = this.async();
3940

40-
const filename = loaderUtils.interpolateName(this, `${options.name || '[fullhash]'}.worker.js`, {
41-
context: options.context || this.rootContext || this.options.context,
42-
regExp: options.regExp
43-
});
41+
const compilerOptions = this._compiler.options || {};
42+
43+
const filename = (options.name || '[fullhash]') + '.worker.js';
4444

4545
const worker = {};
4646

4747
worker.options = {
4848
filename,
49-
chunkFilename: `[id].${filename}`,
50-
namedChunkFilename: null
49+
chunkFilename: filename,
50+
publicPath: options.publicPath || compilerOptions.output.publicPath,
51+
globalObject: 'self'
5152
};
5253

53-
const compilerOptions = this._compiler.options || {};
5454
if (compilerOptions.output && compilerOptions.output.globalObject==='window') {
5555
console.warn('Warning (workerize-loader): output.globalObject is set to "window". It should be set to "self" or "this" to support HMR in Workers.');
5656
}
5757

58-
worker.compiler = this._compilation.createChildCompiler('worker', worker.options);
58+
worker.compiler = this._compilation.createChildCompiler(`worker ${request}`, worker.options);
5959

6060
(new WebWorkerTemplatePlugin(worker.options)).apply(worker.compiler);
6161

@@ -82,10 +82,9 @@ loader.pitch = function(request) {
8282
}).apply(worker.compiler);
8383
}
8484

85-
(new SingleEntryPlugin(this.context, `!!${path.resolve(__dirname, 'rpc-worker-loader.js')}!${request}`, 'main')).apply(worker.compiler);
85+
const bundleName = path.parse(this.resourcePath).name;
8686

87-
const subCache = `subcache ${__dirname} ${request}`;
88-
const CACHE = this._compilation.getCache(subCache);
87+
(new SingleEntryPlugin(this.context, `!!${path.resolve(__dirname, 'rpc-worker-loader.js')}!${request}`, bundleName)).apply(worker.compiler);
8988

9089
compilationHook(worker.compiler, (compilation, data) => {
9190
parseHook(data, (parser, options) => {
@@ -96,7 +95,7 @@ loader.pitch = function(request) {
9695
let entryModule =
9796
compilation.entries instanceof Map
9897
? compilation.moduleGraph.getModule(
99-
compilation.entries.get('main').dependencies[0]
98+
compilation.entries.get(bundleName).dependencies[0]
10099
)
101100
: compilation.entries[0];
102101

@@ -121,7 +120,7 @@ loader.pitch = function(request) {
121120
if (compilation.moduleGraph) {
122121
const { getEntryRuntime } = require('webpack/lib/util/runtime');
123122
const { UsageState } = require('webpack');
124-
const runtime = getEntryRuntime(compilation, 'main');
123+
const runtime = getEntryRuntime(compilation, bundleName);
125124
for (const exportName of Object.keys(exports)) {
126125
const exportInfo = compilation.moduleGraph.getExportInfo(entryModule, exportName);
127126
exportInfo.setUsed(UsageState.Used, runtime);

0 commit comments

Comments
 (0)