-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
81 lines (77 loc) · 2.52 KB
/
index.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
function matchRegexp(o) {
const no = []
Object.keys(o).forEach(k => {
const kreg = new RegExp(k)
no.push([
kreg, o[k]
])
})
return (pattern, ...rest) => {
for (let [r, cb] of no) {
let m = pattern.match(r)
if (m) {
return cb(pattern, m, ...rest)
}
}
}
}
function mkProjExts(matchlist, externalModules) {
const matcher = matchRegexp(matchlist)
return name => {
if (['vue', 'vuex', 'vue-router'].includes(name)) {
return
} else if (name in externalModules) {
console.log(`[used] ${name}`)
return `${name}.${externalModules[name]}`
} else {
let comp = matcher(name)
if (comp) {
if (comp in externalModules) {
console.log(`[used] ${name} --> ${comp}`)
return `${comp}.${externalModules[comp]}`
} else {
console.log(`[matched] ${name} --> ${comp}`)
return comp
}
} else {
return
}
}
}
}
function mkImport(registry, loader) {
return (name, ori, reg, ld) => {
let rv = `() => ${ld || loader}('${reg || registry}','${name}')`
console.log('[importStmt] ', rv)
return rv
}
}
import Axios from 'axios'
import path from 'path'
export default async function extsModule({ manifest, registry, index, verbose }) {
console.log(`[info] register plugin externalComponent`)
this.addPlugin(path.resolve(__dirname, 'lib', 'externalComponent.js'))
const externalModules = registry ? (await Axios.get(`${registry}/${index || 'latest.json'}`)).data : {}
if (verbose) Object.keys(externalModules).forEach(k => {
console.log(`${k} ==> ${externalModules[k]}`)
})
const projExts = mkProjExts(require(manifest), externalModules)
const impStmt = mkImport(registry, 'externalComponent')
this.extendBuild((config, { isClient, isServer }) => {
if (isClient) {
config.externals = (context, request, callback) => {
let r = projExts(request)
if (r) {
callback(null, impStmt(r, request))
} else {
callback()
}
}
} else {
const nodeExtrenals = require('webpack-node-externals')
config.externals = nodeExtrenals({
whitelist: /\.css$/
})
}
})
}