-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebpack.api.js
77 lines (73 loc) · 2.55 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
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
const path=require("path")
const nodeExternals=require("webpack-node-externals")
const fs=require('fs')
/**
*NOTE: since all plugins are with in we-edit project,
you should set dependencies of plugin as optionalDependencies of packages/we-edit,
so project depends on we-edit can use built plugin without big bundle into built plugin
*/
module.exports=(base,packages,args)=>{
return [
...packages.map(a=>({
...base,
module:{
...base.module,
noParse:/code-mirror-modes/,
},
entry:`./packages/${a}/src/index.js`,
output:{
filename:`${a.substr("we-edit".length+1)||"index"}.js`,
chunkFilename: "[name]",
libraryTarget:"commonjs2"
},
devtool:"inline-source-map",
plugins:[
...base.plugins,
new LocalReference(),
a!=="we-edit" ? new CopyReadme(a) : null,
a=="we-edit-representation-pagination" ? new CopyFontService() : null
].filter(a=>a),
target:"node",
externals:[nodeExternals()]
}))
]
}
class LocalReference{
apply(compiler){
compiler.hooks.emit.tap("localize we-edit-", compilation=>{
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
}
}
}
})
}
}
class CopyFontService{
apply(compiler){
compiler.hooks.emit.tap("copy font service", (compilation)=>{
fs.createReadStream(path.resolve(__dirname, 'packages/we-edit-representation-pagination/src/fonts/font-service.js'))
.pipe(fs.createWriteStream(path.join(compilation.options.output.path,"www","font-service.js")))
})
}
}
class CopyReadme{
constructor(project){
this.project=project
}
apply(compiler){
compiler.hooks.emit.tap("copy readme", (compilation)=>{
fs.createReadStream(path.resolve(__dirname, `packages/${this.project}/README.md`))
.pipe(fs.createWriteStream(path.resolve(compilation.options.output.path,this.project.replace("we-edit-","")+".md")))
})
}
}