webpack.plugin.js
1.77 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
const fs = require("fs");
const path = require("path");
const process = require("process");
const WebpackBar = require("webpackbar");
const CopyPlugin = require("copy-webpack-plugin");
const NodePolyfillPlugin = require("node-polyfill-webpack-plugin");
const { merge } = require("webpack-merge");
const prodConfig = require("./webpack.prod");
module.exports = function ({ entryDir }) {
const extensions = ["ts", "tsx"];
delete prodConfig.plugins;
delete prodConfig.optimization;
const idx = prodConfig.module.rules.findIndex(i => i.test.toString() === /\.css$/i.toString());
prodConfig.module.rules.splice(idx, 1)
const cwd = process.cwd();
const directoryName = path.basename(entryDir);
let extension;
extensions.forEach((i) => {
if (fs.existsSync(path.resolve(entryDir, `index.${i}`))) {
extension = i;
}
});
const outputPath = path.resolve(process.cwd(), `./dist/${directoryName}`);
const entryPath = path.resolve(entryDir, `./index.${extension}`);
console.log("entryPath", entryPath);
console.log("outputPath", outputPath);
return merge(prodConfig, {
output: {
path: outputPath,
filename: "index.js",
libraryTarget: "system",
clean: true,
},
entry: entryPath,
plugins: [
new CopyPlugin({
patterns: [
{
from: path.resolve(cwd, "./plugin.json"),
to: path.resolve(cwd, "./dist/plugin.json"),
},
{
from: path.resolve(cwd, "./static"),
to: path.resolve(cwd, "./dist/static"),
},
{
from: path.resolve(entryDir, "./locale"),
to: path.resolve(cwd, `./dist/${directoryName}/locale`),
},
],
}),
new WebpackBar(),
new NodePolyfillPlugin()
],
});
};