Skip to content

Commit 7cbec4a

Browse files
committed
test: add tests for enforce esm feature
1 parent 614dfc5 commit 7cbec4a

File tree

8 files changed

+550
-14
lines changed

8 files changed

+550
-14
lines changed

Diff for: package-lock.json

+477
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"prettier": "^1.19.1",
7676
"standard-version": "^7.0.1",
7777
"webpack": "^4.41.4",
78+
"webpack-5": "npm:[email protected]",
7879
"webpack-cli": "^3.3.6",
7980
"webpack-dev-server": "^3.7.2"
8081
},

Diff for: src/loader.js

+8-9
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@ import NativeModule from 'module';
33
import path from 'path';
44

55
import loaderUtils from 'loader-utils';
6-
import NodeTemplatePlugin from 'webpack/lib/node/NodeTemplatePlugin';
6+
import webpack from 'webpack';
77
import NodeTargetPlugin from 'webpack/lib/node/NodeTargetPlugin';
8-
import LibraryTemplatePlugin from 'webpack/lib/LibraryTemplatePlugin';
9-
import SingleEntryPlugin from 'webpack/lib/SingleEntryPlugin';
10-
import LimitChunkCountPlugin from 'webpack/lib/optimize/LimitChunkCountPlugin';
118
import validateOptions from 'schema-utils';
129

1310
import CssDependency from './CssDependency';
@@ -84,13 +81,15 @@ export function pitch(request) {
8481
outputOptions
8582
);
8683

87-
new NodeTemplatePlugin(outputOptions).apply(childCompiler);
88-
new LibraryTemplatePlugin(null, 'commonjs2').apply(childCompiler);
84+
new webpack.node.NodeTemplatePlugin(outputOptions).apply(childCompiler);
85+
new webpack.LibraryTemplatePlugin(null, 'commonjs2').apply(childCompiler);
8986
new NodeTargetPlugin().apply(childCompiler);
90-
new SingleEntryPlugin(this.context, `!!${request}`, pluginName).apply(
87+
new webpack.SingleEntryPlugin(this.context, `!!${request}`, pluginName).apply(
88+
childCompiler
89+
);
90+
new webpack.optimize.LimitChunkCountPlugin({ maxChunks: 1 }).apply(
9191
childCompiler
9292
);
93-
new LimitChunkCountPlugin({ maxChunks: 1 }).apply(childCompiler);
9493

9594
childCompiler.hooks.thisCompilation.tap(
9695
`${pluginName} loader`,
@@ -210,7 +209,7 @@ export function pitch(request) {
210209
locals
211210
)};`
212211
: esModule
213-
? `export {};`
212+
? `\nexport {};`
214213
: '';
215214

216215
let resultSource = `// extracted by ${pluginName}`;

Diff for: test/__mocks__/webpack.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import webpack from 'webpack-5';
2+
3+
export default webpack;

Diff for: test/__mocks__/webpack/lib/node/NodeTargetPlugin.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import NodeTargetPlugin from 'webpack-5/lib/node/NodeTargetPlugin';
2+
3+
export default NodeTargetPlugin;

Diff for: test/enforce-esm.test.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { getCompiler, compile } from './helpers';
2+
3+
jest.mock('webpack');
4+
jest.mock('webpack/lib/node/NodeTargetPlugin');
5+
6+
it('should enforce esm for empty module with options.esModule', async (done) => {
7+
const compiler = getCompiler(
8+
'./esm.js',
9+
{ esModule: true },
10+
{
11+
mode: 'production',
12+
optimization: { minimize: false },
13+
}
14+
);
15+
const stats = await compile(compiler);
16+
expect(stats.hasErrors()).toBe(false);
17+
18+
const { chunks } = stats.toJson();
19+
const [chunk] = chunks;
20+
21+
expect(JSON.stringify(chunk.sizes)).toMatchInlineSnapshot(
22+
// javascript size is mostly a webpack info comments
23+
`"{\\"javascript\\":73,\\"unknown\\":23,\\"runtime\\":0}"`
24+
);
25+
done();
26+
});
27+
28+
it('should keep module without options.esModule', async (done) => {
29+
const compiler = getCompiler(
30+
'./esm.js',
31+
{},
32+
{
33+
mode: 'production',
34+
optimization: { minimize: false },
35+
}
36+
);
37+
const stats = await compile(compiler);
38+
expect(stats.hasErrors()).toBe(false);
39+
40+
const { chunks } = stats.toJson();
41+
const [chunk] = chunks;
42+
43+
expect(JSON.stringify(chunk.sizes)).toMatchInlineSnapshot(
44+
// javascript size is mostly a webpack info comments
45+
`"{\\"javascript\\":62,\\"unknown\\":23,\\"runtime\\":657}"`
46+
);
47+
done();
48+
});

Diff for: test/fixtures/esm.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import "./simple.css";

Diff for: test/helpers/getCompiler.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import { createFsFromVolume, Volume } from 'memfs';
66
import MiniCssExtractPlugin from '../../src';
77

88
export default (fixture, loaderOptions = {}, config = {}) => {
9+
const { outputFileSystem, ...cnfg } = config;
10+
911
const fullConfig = {
1012
mode: 'development',
11-
devtool: config.devtool || false,
13+
devtool: cnfg.devtool || false,
1214
context: path.resolve(__dirname, '../fixtures'),
1315
entry: path.resolve(__dirname, '../fixtures', fixture),
1416
output: {
@@ -40,16 +42,18 @@ export default (fixture, loaderOptions = {}, config = {}) => {
4042
chunkFilename: '[id].css',
4143
}),
4244
],
43-
...config,
45+
...cnfg,
4446
};
4547

4648
const compiler = webpack(fullConfig);
4749

48-
if (!config.outputFileSystem) {
49-
const outputFileSystem = createFsFromVolume(new Volume());
50+
if (!outputFileSystem) {
51+
const outputFS = createFsFromVolume(new Volume());
5052
// Todo remove when we drop webpack@4 support
51-
outputFileSystem.join = path.join.bind(path);
53+
outputFS.join = path.join.bind(path);
5254

55+
compiler.outputFileSystem = outputFS;
56+
} else {
5357
compiler.outputFileSystem = outputFileSystem;
5458
}
5559

0 commit comments

Comments
 (0)