1
1
mirror of https://github.com/c8r/x0.git synced 2024-08-17 01:10:32 +03:00
x0/lib/dev.js

102 lines
2.4 KiB
JavaScript
Raw Permalink Normal View History

const fs = require('fs')
2018-05-20 00:06:12 +03:00
const path = require('path')
const webpack = require('webpack')
const serve = require('webpack-serve')
const history = require('connect-history-api-fallback')
const convert = require('koa-connect')
const MiniHTMLWebpackPlugin = require('mini-html-webpack-plugin')
const merge = require('webpack-merge')
const baseConfig = require('./config')
const createTemplate = require('./createTemplate')
const dev = {
hot: true,
logLevel: 'error',
clientLogLevel: 'none',
stats: 'errors-only'
}
module.exports = async (opts) => {
2018-05-27 23:37:06 +03:00
if (opts.basename) delete opts.basename
2018-05-20 00:06:12 +03:00
const config = merge(baseConfig, opts.webpack)
const template = createTemplate(opts)
config.mode = 'development'
2018-05-20 22:59:08 +03:00
config.context = opts.dirname
2018-06-19 18:00:22 +03:00
config.entry = opts.entry || path.join(__dirname, '../src/entry')
config.output = {
2018-05-20 00:06:12 +03:00
path: path.join(process.cwd(), 'dev'),
filename: 'dev.js',
publicPath: '/'
}
config.resolve.modules.unshift(
opts.dirname,
path.join(opts.dirname, 'node_modules')
)
if (config.resolve.alias) {
const whcAlias = config.resolve.alias['webpack-hot-client/client']
if (!fs.existsSync(whcAlias)) {
const whcPath = path.dirname(require.resolve('webpack-hot-client/client'))
config.resolve.alias['webpack-hot-client/client'] = whcPath
}
}
2018-05-20 00:06:12 +03:00
config.plugins.push(
new webpack.DefinePlugin({
DEV: JSON.stringify(true),
OPTIONS: JSON.stringify(opts),
DIRNAME: JSON.stringify(opts.dirname),
2018-06-19 18:41:18 +03:00
MATCH: JSON.stringify(opts.match)
2018-05-20 00:06:12 +03:00
})
)
config.plugins.push(
new MiniHTMLWebpackPlugin({
context: opts,
template
})
)
2018-06-24 01:26:36 +03:00
if (opts.analyze) {
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer')
const analyzerPort = typeof opts.analyze === 'string'
? opts.analyze
: 8888
config.plugins.push(
new BundleAnalyzerPlugin({
analyzerPort
})
)
}
2018-05-22 06:58:57 +03:00
if (opts.debug) {
config.stats = 'verbose'
// todo: enable other logging
}
2018-05-20 00:06:12 +03:00
const serveOpts = {
config,
dev,
logLevel: 'error',
2018-06-04 02:46:04 +03:00
content: opts.dirname,
2018-05-20 00:06:12 +03:00
port: opts.port,
hot: { logLevel: 'error' },
add: (app, middleware, options) => {
app.use(convert(history({})))
}
}
return new Promise((resolve, reject) => {
serve(serveOpts)
.then(server => {
2018-05-25 17:32:58 +03:00
server.compiler.hooks.done.tap({ name: 'x0' }, (stats) => {
resolve({ server, stats })
2018-05-20 00:06:12 +03:00
})
})
.catch(reject)
})
}