productionViewImport.spec.js
2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const fs = require("fs");
const os = require("os");
const path = require("path");
const webpack = require("webpack");
const PROJECT_ROOT = path.resolve(__dirname, "../../..");
const VIEW_IMPORTER = path.resolve(PROJECT_ROOT, "src/libs/util.import.production.js");
globalThis.setImmediate ||= (callback, ...args) => setTimeout(callback, 0, ...args);
globalThis.clearImmediate ||= handle => clearTimeout(handle);
function writeFixture(filePath, content) {
fs.mkdirSync(path.dirname(filePath), { recursive: true });
fs.writeFileSync(filePath, content, "utf8");
}
function compileProductionViewContext() {
const fixtureRoot = fs.mkdtempSync(path.join(os.tmpdir(), "dj-view-context-"));
const sourceRoot = path.join(fixtureRoot, "src");
const outputPath = path.join(fixtureRoot, "dist");
writeFixture(
path.join(sourceRoot, "libs/util.import.production.js"),
fs.readFileSync(VIEW_IMPORTER, "utf8"),
);
writeFixture(
path.join(sourceRoot, "views/realPage.js"),
"module.exports = 'real page';\n",
);
writeFixture(
path.join(sourceRoot, "views/buildBreaker.spec.js"),
"module.exports = require('fs');\n",
);
writeFixture(
path.join(sourceRoot, "views/buildBreaker.test.js"),
"module.exports = require('path');\n",
);
writeFixture(
path.join(fixtureRoot, "entry.js"),
"const loadView = require('./src/libs/util.import.production');\nloadView('realPage');\n",
);
return new Promise((resolve, reject) => {
webpack({
mode: "development",
target: "web",
cache: false,
entry: path.join(fixtureRoot, "entry.js"),
output: {
path: outputPath,
filename: "bundle.js",
},
resolve: {
alias: {
"@": sourceRoot,
},
extensions: [".js"],
},
optimization: {
minimize: false,
},
}, (error, stats) => {
const result = stats?.toJson({ all: false, errors: true });
fs.rmSync(fixtureRoot, { recursive: true, force: true });
if (error) {
reject(error);
return;
}
if (result.errors.length) {
reject(new Error(result.errors.map(item => item.message).join("\n")));
return;
}
resolve();
});
});
}
describe("production view import context", () => {
it("excludes spec and test modules from the browser bundle", async () => {
await expect(compileProductionViewContext()).resolves.toBeUndefined();
});
});