@hyext/builder-zabin
Version:
builder of hyext
55 lines (47 loc) • 1.45 kB
JavaScript
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CopyPlugin = require('copy-webpack-plugin')
module.exports = function({mode, config = {}, inputPath, outputPath, publicPath}) {
const {endpoints = {}} = config
const entries = {}
let plugins = [
new CopyPlugin([
{ from: path.join(inputPath, 'assets'), to: path.join(outputPath, 'assets') }
], { logLevel: 'debug' })
]
Object.keys(endpoints)
.filter(name => {
const {entry = '', template = ''} = (endpoints[name] || {})
if (entry && template) {
entries[name] = path.resolve(inputPath, entry)
plugins.push(
createHtmlPlugin(
`${name}.html`,
path.resolve(inputPath, template),
name
)
)
}
})
if ( Object.keys(entries).length == 0 ) {
throw new Error('endpoints 为空')
}
return {
entry: entries,
plugins
}
}
function createHtmlPlugin(filename, template, entryName) {
return new HtmlWebpackPlugin({
filename,
template,
inject: true,
minify: {
removeComments: true,
collapseWhitespace: true,
removeAttributeQuotes: true
},
chunksSortMode: 'dependency',
chunks: ([entryName]).concat(['vendors'])
})
}