forked from HarryChen0506/react-markdown-editor-lite
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwebpack.overwrite.js
119 lines (114 loc) · 3.47 KB
/
webpack.overwrite.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
108
109
110
111
112
113
114
115
116
117
118
119
const dts = require('dts-bundle');
const fs = require('fs');
const glob = require('glob');
const path = require('path');
function hasArgument(name) {
for (const it of process.argv) {
if (it === `-${name}`) {
return true;
}
}
return false;
}
function tryRemoveDir(dir) {
fs.rmdir(dir, (err) => {
if (err === null) {
tryRemoveDir(dir.substr(0, dir.lastIndexOf('/')));
}
});
}
module.exports = config => {
// Alias
if (config.resolve) {
if (typeof (config.resolve.alias) === "undefined") {
config.resolve.alias = {};
}
config.resolve.alias.src = path.resolve(__dirname, "src");
}
// 启用静态文件支持
if (config.module && config.module.rules) {
config.module.rules.push({
test: /\.(png|svg|jpg|gif|eot|woff|ttf)$/,
use: [{
loader: 'url-loader',
options: {
limit: 20000,
}
}]
});
}
// 不生成bundle分析
if (config.mode === 'production' && hasArgument('o')) {
for (const k in config.plugins) {
const it = config.plugins[k];
if (typeof(it) === 'object' && it.__proto__.constructor.name === 'BundleAnalyzerPlugin') {
config.plugins.splice(k, 1);
break;
}
}
}
// 生成模块化
if (config.output) {
config.output.libraryTarget = 'umd';
config.output.library = 'ReactMarkdownEditorLite';
}
// Build的情况下,把React给External掉
if (config.output && typeof config.devServer === 'undefined') {
config.externals = {
react: {
root: 'React',
commonjs2: 'react',
commonjs: 'react',
amd: 'react'
}
};
}
// 聚合d.ts文件
if (config.mode === 'production' && config.plugins) {
config.plugins.push({
apply: (compiler) => {
compiler.hooks.afterEmit.tap('BundleDTS', () => {
// TS生成的dts不会自动转换paths,这里手动进行
const libPath = path.resolve(__dirname, 'lib');
glob("./lib/**/*.d.ts", null, function (er, files) {
files.forEach(filePath => {
let content = fs.readFileSync(filePath, { encoding: "utf8" });
if (content.includes("from 'src/")) {
content = content.replace(/from 'src\/(.*?)'/g, "from '" + libPath + "/$1'");
fs.writeFileSync(filePath, content, { encoding: "utf8" })
}
});
dts.bundle({
name: "@erda-ui/react-markdown-editor-lite",
main: "lib/index.d.ts",
baseDir: "lib",
out: "temp_dts.tmp",
newLine: "\n",
indent: " ",
verbose: false,
outputAsModuleFolder: false
});
console.log('bundle dts finished');
// 移除其他d.ts文件
files.forEach(it => {
fs.unlinkSync(it);
tryRemoveDir(it.substr(0, it.lastIndexOf('/')));
});
fs.copyFileSync('./src/editor/html.css', './lib/html.css');
// 改名回来
fs.renameSync('./lib/temp_dts.tmp', './lib/index.d.ts');
// 移除掉文件中的less引用
let content = fs.readFileSync('./lib/index.d.ts', {
encoding: "utf8"
});
content = content.replace(/(\s*)import '(.*?)\.less';/gi, "");
fs.writeFileSync('./lib/index.d.ts', content, {
encoding: "utf8"
});
});
});
}
})
}
return config;
};