1 | 'use strict'
|
2 |
|
3 | const fs = require('fs')
|
4 | const path = require('path')
|
5 | const glob = require('glob')
|
6 | const devIp = require('dev-ip')
|
7 | const portscanner = require('portscanner')
|
8 | const ExtractTextPlugin = require('extract-text-webpack-plugin')
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | const appDirectory = fs.realpathSync(process.cwd())
|
16 |
|
17 | function rootPath(relativePath) {
|
18 | return path.resolve(appDirectory, relativePath)
|
19 | }
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | function getPageList(entry) {
|
26 | const entries = getEntries(`${process.cwd()}/${entry}`)
|
27 | return Object.keys(entries)
|
28 | }
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | function localIp() {
|
35 | const ip = devIp()
|
36 |
|
37 |
|
38 | return ip ? (Array.isArray(ip) ? ip[0] : ip) : '0.0.0.0'
|
39 | }
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | async function getFreePort(defPort) {
|
46 | const ceiling = Number(defPort + 20)
|
47 |
|
48 | return portscanner.findAPortNotInUse(defPort, ceiling, localIp())
|
49 | }
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | function getEntries(globPath, preDep = []) {
|
62 | const files = glob.sync(rootPath(globPath))
|
63 | const getPageName = filepath => {
|
64 | const dirname = path.dirname(path.relative('src/view/', filepath))
|
65 |
|
66 | return dirname === '..' ? 'index' : dirname
|
67 | }
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | return files.reverse().reduce((entries, filepath) => {
|
73 | const name = getPageName(filepath)
|
74 |
|
75 | entries[name] = [].concat(preDep, filepath)
|
76 |
|
77 | return entries
|
78 | }, {})
|
79 | }
|
80 |
|
81 | function getChunks(globPath, preDep = []) {
|
82 | const files = glob.sync(rootPath(globPath))
|
83 | const getTrunkName = filepath => {
|
84 | const basename = path.posix.basename(filepath, '.js')
|
85 | return basename.replace(/^index\./, '') + '.servant'
|
86 | }
|
87 |
|
88 | return files.reduce((trunks, filepath) => {
|
89 | const name = getTrunkName(filepath)
|
90 |
|
91 | trunks[name] = [].concat(preDep, filepath)
|
92 |
|
93 | return trunks
|
94 | }, {})
|
95 | }
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 | function parseDate(target) {
|
103 | const f = n => (n > 9 ? n : '0' + n)
|
104 | const date = target instanceof Date ? target : new Date(target)
|
105 | return {
|
106 | y: date.getFullYear(),
|
107 | M: f(date.getMonth() + 1),
|
108 | d: f(date.getDate()),
|
109 | h: f(date.getHours()),
|
110 | m: f(date.getMinutes()),
|
111 | s: f(date.getSeconds())
|
112 | }
|
113 | }
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 | function pubDate(dt) {
|
121 | const date = parseDate(dt)
|
122 | return `${date.y}-${date.M}-${date.d}`
|
123 | }
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 | function banner() {
|
130 | return (
|
131 | `@version ${process.env.npm_package_version}\n` +
|
132 | `@date ${pubDate(new Date())}\n` +
|
133 |
|
134 |
|
135 | `@generated`
|
136 | )
|
137 | }
|
138 |
|
139 | function nodeModulesRegExp(modules = '') {
|
140 |
|
141 |
|
142 |
|
143 | return []
|
144 | .concat(modules)
|
145 | .map(mod => new RegExp(`node_modules\\${path.sep}${mod}?`))
|
146 | }
|
147 |
|
148 | function isNotEmptyArray(target) {
|
149 | return Array.isArray(target) && target.length
|
150 | }
|
151 |
|
152 | function isObject(obj) {
|
153 | return Object.prototype.toString.call(obj) === '[object Object]'
|
154 | }
|
155 |
|
156 | function ensureSlash(path, needsSlash) {
|
157 | const hasSlash = path.endsWith('/')
|
158 | if (hasSlash && !needsSlash) {
|
159 | return path.substr(path, path.length - 1)
|
160 | } else if (!hasSlash && needsSlash) {
|
161 | return `${path}/`
|
162 | } else {
|
163 | return path
|
164 | }
|
165 | }
|
166 |
|
167 | function camelName(name) {
|
168 | return name
|
169 | }
|
170 |
|
171 |
|
172 | function buffer2String(data) {
|
173 | return data.toString().replace(/[\n\r]/g, '')
|
174 | }
|
175 |
|
176 | function write(dest, code) {
|
177 | return new Promise((resolve, reject) => {
|
178 | fs.writeFile(dest, code, err => {
|
179 | if (err) return reject(err)
|
180 | resolve()
|
181 | })
|
182 | })
|
183 | }
|
184 |
|
185 | function assetsPath(_path) {
|
186 | return path.posix.join('static', _path)
|
187 | }
|
188 |
|
189 | function cssLoaders(options) {
|
190 | options = options || {}
|
191 |
|
192 | var cssLoader = {
|
193 | loader: 'css-loader',
|
194 | options: {
|
195 | minimize: process.env.NODE_ENV === 'production',
|
196 | sourceMap: options.sourceMap
|
197 | }
|
198 | }
|
199 |
|
200 | var postcssLoader = {
|
201 | loader: 'postcss-loader',
|
202 | options: {
|
203 | sourceMap: true
|
204 | }
|
205 | }
|
206 |
|
207 | var px2rpxLoader = {
|
208 | loader: 'px2rpx-loader',
|
209 | options: {
|
210 | baseDpr: 1,
|
211 | rpxUnit: 0.5
|
212 | }
|
213 | }
|
214 |
|
215 |
|
216 | function generateLoaders(loader, loaderOptions) {
|
217 | var loaders = [cssLoader, postcssLoader, px2rpxLoader]
|
218 | if (loader) {
|
219 | loaders.push({
|
220 | loader: loader + '-loader',
|
221 | options: Object.assign({}, loaderOptions, {
|
222 | sourceMap: options.sourceMap
|
223 | })
|
224 | })
|
225 | }
|
226 |
|
227 |
|
228 |
|
229 | if (options.extract) {
|
230 | return ExtractTextPlugin.extract({
|
231 | use: loaders,
|
232 | fallback: 'vue-style-loader'
|
233 | })
|
234 | } else {
|
235 | return ['vue-style-loader'].concat(loaders)
|
236 | }
|
237 | }
|
238 |
|
239 |
|
240 | return {
|
241 | css: generateLoaders(),
|
242 | postcss: generateLoaders(),
|
243 | less: generateLoaders('less'),
|
244 | sass: generateLoaders('sass', { indentedSyntax: true }),
|
245 | scss: generateLoaders('sass'),
|
246 | stylus: generateLoaders('stylus'),
|
247 | styl: generateLoaders('stylus')
|
248 | }
|
249 | }
|
250 |
|
251 |
|
252 | function styleLoaders(options) {
|
253 | var output = []
|
254 | var loaders = cssLoaders(options)
|
255 | for (var extension in loaders) {
|
256 | var loader = loaders[extension]
|
257 | output.push({
|
258 | test: new RegExp('\\.' + extension + '$'),
|
259 | use: loader
|
260 | })
|
261 | }
|
262 | return output
|
263 | }
|
264 |
|
265 | module.exports = {
|
266 | styleLoaders,
|
267 | cssLoaders,
|
268 | assetsPath,
|
269 | isObject,
|
270 | write,
|
271 | getPageList,
|
272 | localIp,
|
273 | getFreePort,
|
274 | getEntries,
|
275 | getChunks,
|
276 | rootPath,
|
277 | parseDate,
|
278 | pubDate,
|
279 | banner,
|
280 | isNotEmptyArray,
|
281 | nodeModulesRegExp,
|
282 | ensureSlash,
|
283 | camelName,
|
284 | buffer2String
|
285 | }
|