Skip to content

Commit e59a462

Browse files
test: check minimize-json assets from the config
futureDefaults emits an ESM bundle, which reaches fs through createRequire and so fails on Node 10; read the emitted files in afterEmit instead.
1 parent 4212177 commit e59a462

4 files changed

Lines changed: 47 additions & 45 deletions

File tree

‎test/configCases/optimization/minimize-json/__snapshots__/ConfigCacheTest.snap‎

Lines changed: 0 additions & 7 deletions
This file was deleted.

‎test/configCases/optimization/minimize-json/__snapshots__/ConfigTest.snap‎

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,7 @@
11
import url from "./data.json";
22
import notJsonUrl from "./not-json.json";
33

4-
const fs = require("fs");
5-
const path = require("path");
6-
7-
/**
8-
* @param {string} file emitted or source file
9-
* @returns {string} its content
10-
*/
11-
const read = (file) => fs.readFileSync(file, "utf8");
12-
13-
const sourceDirectory = path.resolve(
14-
__dirname,
15-
"../../../../configCases/optimization/minimize-json"
16-
);
17-
18-
it("should minify an emitted JSON asset only when `minimizeOptions.json` is on", () => {
19-
const source = read(path.resolve(__dirname, url));
20-
if (EXPECTED === "minified") {
21-
expect(source).toMatchSnapshot();
22-
} else if (EXPECTED === "user") {
23-
expect(source).toBe(JSON.stringify(JSON.parse(source), null, 1));
24-
} else {
25-
expect(source).toBe(read(path.resolve(sourceDirectory, "data.json")));
26-
}
27-
});
28-
29-
it("should leave a JSON asset that does not parse as written", () => {
30-
expect(read(path.resolve(__dirname, notJsonUrl))).toBe(
31-
read(path.resolve(sourceDirectory, "not-json.json"))
32-
);
4+
it("should emit both JSON assets", () => {
5+
expect(url).toMatch(/-data\.json$/);
6+
expect(notJsonUrl).toMatch(/-not-json\.json$/);
337
});

‎test/configCases/optimization/minimize-json/webpack.config.js‎

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
"use strict";
22

3+
const fs = require("fs");
4+
const path = require("path");
35
const MinimizerPlugin = require("minimizer-webpack-plugin");
4-
const webpack = require("../../../../");
6+
7+
/**
8+
* @param {string} file fixture file name
9+
* @returns {string} its content as written
10+
*/
11+
const readFixture = (file) =>
12+
fs.readFileSync(path.join(__dirname, file), "utf8");
13+
14+
const DATA = readFixture("data.json");
15+
const NOT_JSON = readFixture("not-json.json");
16+
const MINIFIED =
17+
'{"name":"webpack","list":[1,2,3],"nested":{"empty":{}},"id":9007199254740993,"precise":1.50,"huge":1e400,"text":"keep \\"these\\" spaces\\\\"}';
518

619
// Declares JSON through `include`, but its `test` keeps it to JavaScript, so the
720
// plugin never hands it a JSON asset and webpack's own minimizer must.
@@ -44,7 +57,36 @@ const config = (name, expected, options) => ({
4457
},
4558
plugins: [
4659
...(options.plugins || []),
47-
new webpack.DefinePlugin({ EXPECTED: JSON.stringify(expected) })
60+
/**
61+
* @param {import("../../../../").Compiler} compiler compiler
62+
*/
63+
(compiler) => {
64+
// Checked here rather than in the bundle: futureDefaults emits ESM,
65+
// which reaches `fs` through `createRequire`, missing on Node 10.
66+
compiler.hooks.afterEmit.tap("CheckJsonAssets", (compilation) => {
67+
/**
68+
* @param {string} file asset name suffix
69+
* @returns {string} the emitted content
70+
*/
71+
const emitted = (file) =>
72+
fs.readFileSync(
73+
path.join(
74+
/** @type {string} */ (compilation.outputOptions.path),
75+
`${name}-${file}`
76+
),
77+
"utf8"
78+
);
79+
const data = emitted("data.json");
80+
if (expected === "minified") {
81+
expect(data).toBe(MINIFIED);
82+
} else if (expected === "user") {
83+
expect(data).toBe(JSON.stringify(JSON.parse(data), null, 1));
84+
} else {
85+
expect(data).toBe(DATA);
86+
}
87+
expect(emitted("not-json.json")).toBe(NOT_JSON);
88+
});
89+
}
4890
]
4991
});
5092

0 commit comments

Comments
 (0)