Skip to content
This repository was archived by the owner on Apr 9, 2024. It is now read-only.

Commit 92e225d

Browse files
authored
Refcatopr child compiler to remove deprecation warnings (#164)
1 parent 1375f5e commit 92e225d

File tree

4 files changed

+90
-42
lines changed

4 files changed

+90
-42
lines changed

.changeset/blue-shoes-own.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'treat': patch
3+
---
4+
5+
Fix webpack 5 deprecation warnings

packages/treat/src/webpack-plugin/compat.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const webpack4 = {
2+
isWebpack5: false,
23
isModuleUsed: (_compilation, module) =>
34
typeof module.used === 'boolean' ? module.used : true,
45
getDependencyModule: (_compilation, dependency) => dependency.module,
@@ -29,9 +30,15 @@ const webpack4 = {
2930
return Object.keys(watcher.mtimes);
3031
},
3132
getModuleIssuer: (_compilation, module) => module.issuer,
33+
getNodeTemplatePlugin: () => require('webpack/lib/node/NodeTemplatePlugin'),
34+
getNodeTargetPlugin: () => require('webpack/lib/node/NodeTargetPlugin'),
35+
getLimitChunkCountPlugin: () =>
36+
require('webpack/lib/optimize/LimitChunkCountPlugin'),
37+
getExternalsPlugin: () => require('webpack/lib/ExternalsPlugin'),
3238
};
3339

3440
const webpack5 = {
41+
isWebpack5: true,
3542
isModuleUsed: (compilation, module) => {
3643
const exportsInfo = compilation.moduleGraph.getExportsInfo(module);
3744

@@ -61,6 +68,11 @@ const webpack5 = {
6168
watchCompiler.modifiedFiles ? Array.from(watchCompiler.modifiedFiles) : [],
6269
getModuleIssuer: (compilation, module) =>
6370
compilation.moduleGraph.getIssuer(module),
71+
getNodeTemplatePlugin: (compiler) => compiler.webpack.node.NodeTemplatePlugin,
72+
getNodeTargetPlugin: (compiler) => compiler.webpack.node.NodeTargetPlugin,
73+
getLimitChunkCountPlugin: (compiler) =>
74+
compiler.webpack.optimize.LimitChunkCountPlugin,
75+
getExternalsPlugin: (compiler) => compiler.webpack.ExternalsPlugin,
6476
};
6577

6678
export default (isWebpack5) => {

packages/treat/src/webpack-plugin/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ export class TreatPlugin {
4242
} = options;
4343

4444
this.store = store();
45-
this.treatCompiler = makeTreatCompiler();
4645

4746
this.test = test;
4847
this.minify = minify;
@@ -59,11 +58,12 @@ export class TreatPlugin {
5958
apply(compiler) {
6059
const isWebpack5 = Boolean(compiler.webpack && compiler.webpack.version);
6160
const compat = createCompat(isWebpack5);
61+
const treatCompiler = makeTreatCompiler(compat);
6262

6363
compiler.hooks.watchRun.tap(TWP, (watchCompiler) => {
6464
const modifiedFiles = compat.getModifiedFiles(watchCompiler);
6565

66-
this.treatCompiler.expireCache(modifiedFiles);
66+
treatCompiler.expireCache(modifiedFiles);
6767
});
6868

6969
compiler.hooks.thisCompilation.tap(TWP, (compilation) => {
@@ -399,7 +399,7 @@ export class TreatPlugin {
399399
prod: '[hash:base64:4]',
400400
}),
401401
store: this.store,
402-
treatCompiler: this.treatCompiler,
402+
treatCompiler,
403403
},
404404
},
405405
],

packages/treat/src/webpack-plugin/treatCompiler.js

Lines changed: 70 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
import intersection from 'lodash/intersection';
22
import once from 'lodash/once';
3-
import NodeTemplatePlugin from 'webpack/lib/node/NodeTemplatePlugin';
4-
import NodeTargetPlugin from 'webpack/lib/node/NodeTargetPlugin';
5-
import LibraryTemplatePlugin from 'webpack/lib/LibraryTemplatePlugin';
6-
import SingleEntryPlugin from 'webpack/lib/SingleEntryPlugin';
7-
import LimitChunkCountPlugin from 'webpack/lib/optimize/LimitChunkCountPlugin';
8-
import ExternalsPlugin from 'webpack/lib/ExternalsPlugin';
93
import Promise from 'bluebird';
104
import chalk from 'chalk';
115
import dedent from 'dedent';
@@ -29,7 +23,7 @@ const logMultiWebpackError = once(() => {
2923
);
3024
});
3125

32-
export default () => {
26+
export default (compat) => {
3327
const cache = new Map();
3428

3529
const expireCache = (changedFiles) => {
@@ -51,7 +45,7 @@ export default () => {
5145
);
5246
};
5347

54-
const getSource = async (loader, request) => {
48+
const getSource = async (loader) => {
5549
const identifier = loader._module.identifier();
5650
trace('Get compiled source: %i', identifier);
5751

@@ -63,17 +57,16 @@ export default () => {
6357
}
6458

6559
trace('No cached source. Compiling: %i', identifier);
66-
const compilationResult = await compileTreatSource(loader, request);
60+
const compilationResult = await compileTreatSource(loader, compat);
6761

6862
cache.set(identifier, compilationResult);
6963

7064
return compilationResult;
7165
};
7266

73-
const getCompiledSource = async (loader, request) => {
67+
const getCompiledSource = async (loader) => {
7468
const { source, fileDependencies, contextDependencies } = await getSource(
7569
loader,
76-
request,
7770
);
7871

7972
// Set loader dependencies to dependecies of the child compiler
@@ -106,48 +99,86 @@ function getRootCompilation(loader) {
10699
return compilation;
107100
}
108101

109-
function compileTreatSource(loader) {
102+
function compileTreatSource(loader, compat) {
110103
return new Promise((resolve, reject) => {
111104
// Child compiler will compile treat files to be evaled during compilation
112105
const outputOptions = { filename: loader.resourcePath };
113106

114107
const childCompiler = getRootCompilation(loader).createChildCompiler(
115108
TWL,
116109
outputOptions,
117-
[
118-
new NodeTemplatePlugin(outputOptions),
119-
new LibraryTemplatePlugin(null, 'commonjs2'),
120-
new NodeTargetPlugin(),
121-
new SingleEntryPlugin(loader.context, loader.resourcePath),
122-
new LimitChunkCountPlugin({ maxChunks: 1 }),
123-
new ExternalsPlugin('commonjs', 'treat'),
124-
],
125110
);
126111

127-
const subCache = 'subcache ' + __dirname + ' ' + loader.resourcePath;
128-
childCompiler.hooks.compilation.tap(TWL, (compilation) => {
129-
if (compilation.cache) {
130-
if (!compilation.cache[subCache]) compilation.cache[subCache] = {};
131-
compilation.cache = compilation.cache[subCache];
132-
}
133-
});
112+
const NodeTemplatePlugin = compat.getNodeTemplatePlugin(loader._compiler);
113+
const NodeTargetPlugin = compat.getNodeTargetPlugin(loader._compiler);
114+
const LimitChunkCountPlugin = compat.getLimitChunkCountPlugin(
115+
loader._compiler,
116+
);
117+
const ExternalsPlugin = compat.getExternalsPlugin(loader._compiler);
118+
119+
new NodeTemplatePlugin(outputOptions).apply(childCompiler);
120+
new NodeTargetPlugin().apply(childCompiler);
121+
122+
if (compat.isWebpack5) {
123+
const {
124+
EntryOptionPlugin,
125+
library: { EnableLibraryPlugin },
126+
} = loader._compiler.webpack;
127+
128+
new EnableLibraryPlugin('commonjs2').apply(childCompiler);
129+
130+
EntryOptionPlugin.applyEntryOption(childCompiler, loader.context, {
131+
child: {
132+
library: {
133+
type: 'commonjs2',
134+
},
135+
import: [loader.resourcePath],
136+
},
137+
});
138+
} else {
139+
// Webpack 4 code. Remove once support is removed
140+
const { LibraryTemplatePlugin, SingleEntryPlugin } = require('webpack');
141+
142+
new LibraryTemplatePlugin(null, 'commonjs2').apply(childCompiler);
143+
new SingleEntryPlugin(loader.context, loader.resourcePath).apply(
144+
childCompiler,
145+
);
146+
}
134147

135-
let source;
148+
new LimitChunkCountPlugin({ maxChunks: 1 }).apply(childCompiler);
149+
new ExternalsPlugin('commonjs', 'treat').apply(childCompiler);
136150

137-
childCompiler.hooks.afterCompile.tapAsync(TWL, (compilation, callback) => {
138-
source =
139-
compilation.assets[loader.resourcePath] &&
140-
compilation.assets[loader.resourcePath].source();
151+
let source;
141152

142-
// Remove all chunk assets
143-
compilation.chunks.forEach((chunk) => {
144-
chunk.files.forEach((file) => {
145-
delete compilation.assets[file];
153+
if (compat.isWebpack5) {
154+
childCompiler.hooks.compilation.tap(TWL, (compilation) => {
155+
compilation.hooks.processAssets.tap(TWL, () => {
156+
source =
157+
compilation.assets[loader.resourcePath] &&
158+
compilation.assets[loader.resourcePath].source();
159+
160+
// Remove all chunk assets
161+
compilation.chunks.forEach((chunk) => {
162+
chunk.files.forEach((file) => {
163+
compilation.deleteAsset(file);
164+
});
165+
});
146166
});
147167
});
148-
149-
callback();
150-
});
168+
} else {
169+
childCompiler.hooks.afterCompile.tap(TWL, (compilation) => {
170+
source =
171+
compilation.assets[loader.resourcePath] &&
172+
compilation.assets[loader.resourcePath].source();
173+
174+
// Remove all chunk assets
175+
compilation.chunks.forEach((chunk) => {
176+
chunk.files.forEach((file) => {
177+
delete compilation.assets[file];
178+
});
179+
});
180+
});
181+
}
151182

152183
try {
153184
childCompiler.runAsChild((err, entries, compilation) => {

0 commit comments

Comments
 (0)