meipian-common
Version:
Meipian common lib developmaent evn
128 lines (113 loc) • 2.97 kB
JavaScript
const path = require('path')
const chalk = require('chalk')
const WebpackDevServer = require('webpack-dev-server')
const webpack = require('webpack')
const merge = require('webpack-merge')
const { fspc, mpos } = require('meipian-stage-tools')
const sassdoc = require('sassdoc')
const mpjsdoc = require('meipian-jsdoc')
//Webpack base config.
const base = require('./base')
//Meipian.config.js for config's value.
const config = merge({
isAutoOpenBrowser: false,
isErrorOverlay: false,
proxy: {},
isPoll: false,
isDevtool: true,
isBackHistory: true
}, base.buildConfig)
const baseWebpack = base.webpack
const SERVER_HOST = '0.0.0.0'
const SERVER_PORT = process.env.SERVER_PORT && Number(process.env.SERVER_PORT)
exports.output = {
publicPath: '/'
}
exports.plugins = [
new webpack.DefinePlugin((() => {
for (let temp in config.define) {
config.define[temp] = JSON.stringify(
(config.define[temp])
)
}
return config.define
})()),
new webpack.HotModuleReplacementPlugin()
]
exports.optimization = {
namedModules: true,
noEmitOnErrors: true,
splitChunks: {
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
priority: -20,
chunks: 'all'
}
}
}
}
exports.devtool = config.isDevtool ? 'cheap-module-eval-source-map' : false
exports.devServer = {
clientLogLevel: 'warning',
historyApiFallback: {
rewrites: [{
from: /.*/,
to: '/'
}]
},
hot: true,
contentBase: false,
compress: true,
host: SERVER_HOST,
port: SERVER_PORT,
open: config.isAutoOpenBrowser,
overlay: config.isErrorOverlay ? {
warnings: false,
errors: true
} : false,
publicPath: '/',
proxy: config.proxy,
quiet: true,
watchOptions: {
poll: config.isPoll
}
}
//Print webpack complie content.
module.exports = new Promise(function(resolve, reject) {
Object.keys(baseWebpack.entry).map(function(value) {
console.log(chalk.yellow([
` Access url: http://${mpos.ip}:${SERVER_PORT}/${value}.${config.extName}`
].join('\n') + '\n'))
})
if (config.isAPI) {
fspc.rm('./sassdoc').then(function() {
sassdoc(path.resolve(process.cwd(), config.root)).then(function() {
console.log(chalk.green(' Sassdoc API generation completion, output dir: sassdoc\n'))
})
})
mpjsdoc(config.root, fspc.readConfig.apply(config, [
path.resolve(process.cwd(), './jsdoc.config.js')
]))
}
new WebpackDevServer(webpack(merge(baseWebpack, exports, fspc.readConfig.apply(config, [
process.env.MEIPIAN_PROGRAM_CONFIG, ['entry', 'output']
]))), {
inline: true,
compress: false,
stats: {
chunks: false,
children: false,
colors: true
},
historyApiFallback: config.isBackHistory
}).listen(SERVER_PORT, SERVER_HOST, function(err) {
if (err) {
reject(err)
}
})
}).catch(function(err) {
throw err
})