-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
executable file
·107 lines (87 loc) · 2.56 KB
/
webpack.config.js
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
const defaultConfig = require("@wordpress/scripts/config/webpack.config");
const path = require("path");
const fs = require("fs");
const copy = require("rollup-plugin-copy");
const createTranform = require("rollup-copy-transform-css").createTranform;
const glob = require("glob");
const rename = () => {
const { join } = path;
const blockJsonFiles = glob.sync(
join(process.cwd(), "build", "**", "block.json")
);
if (blockJsonFiles) {
blockJsonFiles.forEach((filePath) => {
let blockJson = require(filePath);
console.log("hi", blockJson);
if (blockJson?.editorScript) {
blockJson.editorScript = blockJson.editorScript.replace(".tsx", ".js");
}
if (blockJson?.script) {
blockJson.script = blockJson.script.replace(".tsx", ".js");
}
if (blockJson?.viewScript) {
blockJson.viewScript = blockJson.viewScript.replace(".tsx", ".js");
}
if (blockJson?.editorStyle) {
blockJson.editorStyle = blockJson.editorStyle.replace(".scss", ".css");
}
if (blockJson?.style) {
blockJson.style = blockJson.style.replace(".scss", ".css");
}
fs.writeFile(
filePath,
JSON.stringify(blockJson, null, 2),
function writeJSON(error) {
if (error) {
return console.log(error);
}
}
);
});
}
const cssFiles = glob.sync(join(process.cwd(), "dist", "*.css"));
cssFiles.push(...(glob.sync(join(process.cwd(), "dist", "*.css.map"))));
cssFiles.push(...(glob.sync(join(process.cwd(), "dist", "*inject.es-*.js"))));
cssFiles.push(
...(glob.sync(join(process.cwd(), "dist", "*inject.es-*.js.map")))
);
console.log("cssFiles", cssFiles)
if (cssFiles) {
cssFiles.forEach((filePath) => {
// copy to build
console.log("filePath", filePath)
fs.copyFile(filePath, filePath.replace("dist", "build"), (err) => {
if (err) throw err;
console.log("source.txt was copied to destination.txt");
});
});
}
};
module.exports = (env) => {
// copy dist/src/*.css and map to build
return {
...defaultConfig,
module: {
...defaultConfig.module,
rules: [
...defaultConfig.module.rules,
{
// handle source maps
test: /\.js$/,
use: ["source-map-loader"],
},
],
},
entry: {
index: "./dist/index.js",
},
plugins: [
...defaultConfig.plugins,
{
apply: (compiler) => {
compiler.hooks.afterEmit.tap("rename", rename);
},
},
],
};
};