-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.security.config.cjs
More file actions
121 lines (114 loc) · 2.76 KB
/
Copy pathwebpack.security.config.cjs
File metadata and controls
121 lines (114 loc) · 2.76 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const path = require("path");
require("tsx/cjs");
const webpack = require("webpack");
const {
WebpackSandboxPlugin,
} = require("./webpack-sandbox-plugin/src/plugin.ts");
const scenarios = {
lodash: "./src/lodash.js",
"strip-ansi": "./src/strip-ansi.js",
"image-js": "./src/image-js.js",
};
module.exports = (env = {}) => {
const scenario = env.scenario;
const mode = env.mode;
const entry = scenarios[scenario];
if (!entry) {
throw new Error(
`Unknown security scenario "${scenario ?? ""}". Expected one of: ${Object.keys(scenarios).join(", ")}.`,
);
}
if (!["native", "sandbox"].includes(mode)) {
throw new Error(
`Unknown security mode "${mode ?? ""}". Expected native or sandbox.`,
);
}
const projectRoot = __dirname;
const securityApp = path.resolve(
projectRoot,
"lib-test/security/app",
);
const securityConfigPath = path.resolve(
securityApp,
`sandbox-configs/${scenario}.js`,
);
const plugins = [
new webpack.DefinePlugin({
__SECURITY_MODE__: JSON.stringify(mode),
}),
new webpack.NormalModuleReplacementPlugin(/^node:/, (resource) => {
resource.request = resource.request.replace(/^node:/, "");
}),
];
if (mode === "sandbox") {
plugins.unshift(
new WebpackSandboxPlugin({
configPath: `./sandbox-configs/${scenario}.js`,
}),
new webpack.NormalModuleReplacementPlugin(
/sandbox\.config\.js$/,
(resource) => {
resource.request = securityConfigPath;
},
),
);
}
return {
context: securityApp,
entry,
output: {
filename: "bundle.js",
path: path.resolve(projectRoot, "dist"),
clean: false,
},
mode: "development",
target: ["web", "es2022"],
experiments: {
topLevelAwait: true,
asyncWebAssembly: true,
},
plugins,
devServer: {
static: {
directory: projectRoot,
},
compress: true,
port: 9000,
},
resolve: {
modules: [
path.resolve(securityApp, "node_modules"),
path.resolve(projectRoot, "node_modules"),
],
fallback: {
path: false,
fs: false,
url: false,
},
alias: {
"quickjs-emscripten-sync": path.resolve(
projectRoot,
"node_modules/quickjs-emscripten-sync/dist/quickjs-emscripten-sync.js",
),
"ml-xsadd$": path.resolve(
securityApp,
"src/shims/ml-xsadd.js",
),
"bresenham-zingl": path.resolve(
securityApp,
"node_modules/bresenham-zingl/dist/index.cjs",
),
},
},
module: {
rules: [
{
test: /\.m?js$/,
resolve: {
fullySpecified: false,
},
},
],
},
};
};