-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.dev.js
More file actions
77 lines (68 loc) · 2.39 KB
/
webpack.dev.js
File metadata and controls
77 lines (68 loc) · 2.39 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
const path = require("path");
const APP = path.resolve(__dirname);
const { merge } = require("webpack-merge");
const commons = require("./webpack.common.js");
const fs = require("fs");
// Output to production location
const outPath = path.join(
APP,
"..",
"..",
"web",
"assets",
"ab_plugins",
"ab_plugin_text_widget"
);
// Plugin to copy .js files as .mjs after compilation
class CopyAsMjsPlugin {
apply(compiler) {
compiler.hooks.afterEmit.tap("CopyAsMjsPlugin", (compilation) => {
const outputPath = compilation.options.output.path;
// Copy each .js file as .mjs
Object.keys(compilation.assets).forEach((filename) => {
if (filename.endsWith(".js")) {
const jsPath = path.join(outputPath, filename);
const mjsPath = path.join(outputPath, filename.replace(/\.js$/, ".mjs"));
try {
fs.copyFileSync(jsPath, mjsPath);
console.log(`✓ Copied ${filename} → ${filename.replace(/\.js$/, ".mjs")}`);
} catch (err) {
console.error(`✗ Failed to copy ${filename}:`, err.message);
}
}
});
// Also copy to dev/ subdirectory for backward compatibility
const devPath = path.join(outputPath, "dev");
if (!fs.existsSync(devPath)) {
fs.mkdirSync(devPath, { recursive: true });
}
Object.keys(compilation.assets).forEach((filename) => {
if (filename.endsWith(".js")) {
const jsPath = path.join(outputPath, filename);
const mjsDevPath = path.join(devPath, filename.replace(/\.js$/, ".mjs"));
try {
fs.copyFileSync(jsPath, mjsDevPath);
console.log(`✓ Copied ${filename} → dev/${filename.replace(/\.js$/, ".mjs")}`);
} catch (err) {
console.error(`✗ Failed to copy ${filename} to dev/:`, err.message);
}
}
});
});
}
}
let configs = null;
let myChanges = {
output: {
path: outPath,
},
mode: "development",
devtool: "source-map",
plugins: [new CopyAsMjsPlugin()],
};
if (Array.isArray(commons)) {
configs = commons.map((cfg) => merge(cfg, myChanges));
} else {
configs = merge(commons, myChanges);
}
module.exports = configs;