forked from wilesun/we-edit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.api.js
46 lines (44 loc) · 1.33 KB
/
webpack.api.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
const path=require("path")
const webpack=require("webpack")
const nodeExternals=require("webpack-node-externals")
module.exports=base=>{
return require("fs")
.readdirSync("./packages")
.filter(a=>a.startsWith("we-edit"))
.map(a=>({
...base,
entry:`./packages/${a}/src/index.js`,
output:{
filename:`${a.substr("we-edit".length+1)||"index"}.js`,
path:path.resolve(__dirname, 'packages/we-edit'),
libraryTarget:"commonjs2"
},
plugins:[
...base.plugins,
new LocalReference(),
],
target:"node",
externals:[nodeExternals()]
}))
}
class LocalReference{
apply(compiler){
compiler.plugin("emit",function(compilation,done){
let fileName=compilation.options.output.filename
let asset=compilation.assets[fileName]
let content=asset.source()
let revised=content.replace(/require\("we-edit-/g,'require("./')
if(content!=revised){
compilation.assets[fileName]={
source(){
return revised
},
size(){
return revised.length
}
}
}
done()
})
}
}