-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathplugin.js
More file actions
174 lines (149 loc) · 4.93 KB
/
plugin.js
File metadata and controls
174 lines (149 loc) · 4.93 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
let SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin')
let MultiEntryPlugin = require('webpack/lib/MultiEntryPlugin')
let fs = require('fs')
var trimSlashes = function (string) {
return string.replace(/^\/+|\/+$/g, '')
}
var checkChunkExclusion = function (compilerOptions) {
if (!compilerOptions) return true
if (!compilerOptions.plugins) return true
let htmlPlugin = compilerOptions.plugins.find(function (i) {
return i.constructor && i.constructor.name === 'HtmlWebpackPlugin'
})
if (htmlPlugin && htmlPlugin.options) {
let exclude = htmlPlugin.options.excludeChunks
if (!exclude || exclude.indexOf('uibook') === -1) {
return false
}
}
return true
}
var getSettings = function (options) {
return {
isFixedHeader: !(options.isFixedHeader === false)
}
}
class UibookPlugin {
constructor (options) {
this.options = options
}
apply (compiler) {
let options = this.options
let isChunkExcluded = checkChunkExclusion(compiler.options)
let isHotErrored = false
compiler.plugin('entry-option', function (context, entry) {
let controllerPath = options.controller
let uibookEntry = controllerPath
let hasHMR = false
if (compiler.options && compiler.options.devServer) {
hasHMR = compiler.options.devServer.hot
}
if (options.hot) {
try {
uibookEntry = [
require.resolve('webpack-dev-server/client') + '?/',
hasHMR ? require.resolve('webpack/hot/dev-server') : null,
controllerPath
].filter(Boolean)
} catch (e) {
isHotErrored = true
}
}
let itemToPlugin = function (item, name) {
if (Array.isArray(item)) {
return new MultiEntryPlugin(context, item, name)
} else {
return new SingleEntryPlugin(context, item, name)
}
}
if (typeof entry === 'string' || Array.isArray(entry)) {
entry = {
main: entry,
uibook: uibookEntry
}
} else {
entry.uibook = uibookEntry
}
Object.keys(entry).forEach(function (name) {
compiler.apply(itemToPlugin(entry[name], name))
})
return true
})
compiler.plugin('emit', function (compilation, callback) {
let publicPath = compilation.outputOptions.publicPath
let entrypoints = []
let notices = []
let imports = ''
let files = []
if (Array.isArray(compilation.entrypoints)) {
entrypoints = compilation.entrypoints
} else if (typeof compilation.entrypoints === 'object') {
if (typeof compilation.entrypoints.forEach === 'function') {
compilation.entrypoints.forEach(function (entrypoint) {
entrypoints.push(entrypoint)
})
} else {
Object.keys(compilation.entrypoints).forEach(function (entrypoint) {
entrypoints.push(compilation.entrypoints[entrypoint])
})
}
} else {
entrypoints = [compilation.entrypoints]
}
entrypoints.forEach(function (i) {
if (i.name === 'uibook') {
i.chunks.forEach(function (chunk) {
if (chunk.files) {
files = files.concat(chunk.files)
}
})
}
})
files.forEach(function (file) {
if (file.slice(-3) === '.js') {
imports += '<script src="' + publicPath + file + '"></script>'
} else if (file.slice(-4) === '.css') {
imports += '<link rel="stylesheet" href="' + publicPath + file + '">'
}
})
if (!isChunkExcluded) {
notices.unshift('chunk')
}
if (isHotErrored) {
notices.unshift('hot')
}
notices = JSON.stringify(notices)
let outputPath = trimSlashes(options.outputPath || 'uibook')
let settings = JSON.stringify(getSettings(options))
let title = options.title || 'Uibook'
let pathHtml = require.resolve('./src/template.html')
let pathCss = require.resolve('./src/uibook.css')
let UibookHtml = fs.readFileSync(pathHtml, 'utf-8')
let UibookCss = fs.readFileSync(pathCss, 'utf-8')
UibookHtml = UibookHtml.replace(/%OUTPUT_PATH%/gm, outputPath)
UibookHtml = UibookHtml.replace(/%PUBLIC_URL%/gm, publicPath)
UibookHtml = UibookHtml.replace(/%SETTINGS%/, settings)
UibookHtml = UibookHtml.replace(/%NOTICES%/, notices)
UibookHtml = UibookHtml.replace(/%IMPORTS%/gm, imports)
UibookHtml = UibookHtml.replace(/%TITLE%/, title)
compilation.assets[outputPath + '/index.html'] = {
source: function () {
return UibookHtml
},
size: function () {
return UibookHtml.length
}
}
compilation.assets[outputPath + '/uibook.css'] = {
source: function () {
return UibookCss
},
size: function () {
return UibookCss.length
}
}
callback()
})
}
}
module.exports = UibookPlugin