Skip to content

Commit 14edacc

Browse files
stephencookdevStephen Cook
authored andcommitted
Set up SMP to work as expected with multi-configs, and generated configs
1 parent bae989e commit 14edacc

20 files changed

Lines changed: 24051 additions & 17614 deletions

File tree

__tests__/common/smp.test.js

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,70 +3,101 @@ const webpack = require("webpack");
33
const { readFileSync } = require("fs");
44
const webpackConfig = require("./webpack.config");
55

6-
const genSmpWebpackConfig = smp =>
7-
smp.wrap(
8-
Object.assign({}, webpackConfig, {
9-
output: {
10-
path: webpackConfig.output.path + "/_smp_" + new Date().getTime(),
11-
},
12-
})
6+
const getStandardConf = conf => {
7+
if (typeof conf === "function") conf = conf();
8+
const arr = Array.isArray(conf) ? conf : [conf];
9+
10+
return arr.map(
11+
subConf => (typeof subConf === "function" ? subConf() : subConf)
1312
);
13+
};
14+
15+
let i = 0;
16+
const prepareSmpWebpackConfig = conf => {
17+
if (Array.isArray(conf)) return conf.map(prepareSmpWebpackConfig);
18+
if (typeof conf === "function")
19+
return (...args) => prepareSmpWebpackConfig(conf(...args));
20+
21+
return Object.assign({}, conf, {
22+
output: {
23+
path: conf.output.path + "/_smp_" + i++ + "_" + new Date().getTime(),
24+
},
25+
});
26+
};
27+
28+
const genSmpWebpackConfig = smp =>
29+
smp.wrap(prepareSmpWebpackConfig(webpackConfig));
1430

1531
const runWebpack = config =>
1632
new Promise((resolve, reject) => {
17-
webpack(config, (err, stats) => {
33+
const standardConf = getStandardConf(config);
34+
webpack(standardConf, (err, stats) => {
1835
if (err || stats.hasErrors()) return reject(err || stats);
19-
resolve(readFileSync(config.output.path + "/bundle.js").toString());
36+
const fileContent = standardConf.map(conf =>
37+
readFileSync(conf.output.path + "/bundle.js").toString()
38+
);
39+
resolve(fileContent.join("\n///////// new file /////////\n"));
2040
});
2141
});
2242

2343
jest.setTimeout(20000);
2444

25-
describe("smp - " + __dirname.split("/").pop(), () => {
26-
let distApp;
27-
beforeAll(() => runWebpack(webpackConfig).then(file => (distApp = file)));
45+
const testRef = {};
2846

29-
describe("vanilla config", () => {
30-
let smpOutput;
31-
let smpDistApp;
47+
describe("smp", () => {
48+
beforeAll(() =>
49+
runWebpack(webpackConfig).then(file => (testRef.distApp = file))
50+
);
51+
52+
describe(__dirname.split("/").pop(), () => {
3253
const smp = new SpeedMeasurePlugin({
33-
outputTarget: output => (smpOutput = output),
54+
outputTarget: output => (testRef.smpOutput = output),
3455
});
3556
const smpWebpackConfig = genSmpWebpackConfig(smp);
3657

3758
beforeAll(() =>
38-
runWebpack(smpWebpackConfig).then(file => (smpDistApp = file))
59+
runWebpack(smpWebpackConfig).then(file => (testRef.smpDistApp = file))
3960
);
4061

4162
it("should generate the same app.js content", () => {
42-
expect(smpDistApp).toEqual(distApp);
63+
expect(testRef.smpDistApp).toEqual(testRef.distApp);
4364
});
4465

4566
it("should generate the same app.js content after 2 runs", () => {
4667
const dupSmpWebpackConfig = genSmpWebpackConfig(smp);
4768

4869
return runWebpack(dupSmpWebpackConfig).then(dupSmpDistApp => {
49-
expect(dupSmpDistApp).toEqual(smpDistApp);
50-
expect(dupSmpDistApp).toEqual(distApp);
70+
expect(dupSmpDistApp).toEqual(testRef.smpDistApp);
71+
expect(dupSmpDistApp).toEqual(testRef.distApp);
5172
});
5273
});
5374

5475
it("should state the time taken overall", () => {
55-
expect(smpOutput).toMatch(
76+
expect(testRef.smpOutput).toMatch(
5677
/General output time took .*([0-9]+ mins? )?[0-9]+(\.[0-9]+)? secs/
5778
);
5879
});
5980

6081
it("should state the time taken by the plugins", () => {
61-
expect(smpOutput).toMatch(
82+
expect(testRef.smpOutput).toMatch(
6283
/DefinePlugin.* took .*([0-9]+ mins? )?[0-9]+(\.[0-9]+)? secs/
6384
);
6485
});
6586

6687
it("should state the time taken by the loaders", () => {
67-
expect(smpOutput).toMatch(
88+
expect(testRef.smpOutput).toMatch(
6889
/babel-loader.* took .*([0-9]+ mins? )?[0-9]+(\.[0-9]+)? secs.*\n\s+module count\s+= [0-9]+/
6990
);
7091
});
92+
93+
let customTests;
94+
try {
95+
customTests = require("./customTests");
96+
} catch (_) {
97+
// do nothing, we expect `require` to fail if no tests exist
98+
}
99+
if (customTests) {
100+
describe("custom tests", () => customTests(testRef));
101+
}
71102
});
72103
});

__tests__/setups/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ styles.css
44
smp.test.js
55
jest.config.js
66
dist
7+
dist2

0 commit comments

Comments
 (0)