Skip to content

Commit

Permalink
Update webpack config to support muti page.
Browse files Browse the repository at this point in the history
  • Loading branch information
erha19 committed Jan 25, 2018
1 parent acec5d9 commit 6a3427c
Show file tree
Hide file tree
Showing 9 changed files with 247 additions and 54 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ CVS

# node_modules
templates/node_modules/*
templates/release/*
node_modules
node_modules/*
node_module/.bin/*
Expand Down
2 changes: 1 addition & 1 deletion npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "weexpack-create",
"version": "0.2.15",
"version": "0.2.16",
"description": "weexpack create module. Creates new project from default or template",
"main": "index.js",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion templates/configs/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const config = {
historyApiFallback: true,
open:true,
watchContentBase: true,
openPage: 'web/preview.html',
openPage: 'web/preview.html?page=index.js',
watchOptions: {
ignored: /node_modules/,
aggregateTimeout: 300,
Expand Down
28 changes: 21 additions & 7 deletions templates/configs/webpack.dev.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,26 @@ const config = require('./config');
const utils = require('./utils');
const helper = require('./helper');

/**
* Generate multiple entrys
* @param {Array} entry
*/
const generateHtmlWebpackPlugin = (entry) => {
const entrys = Object.keys(entry);
const htmlPlugin = entrys.map(name => {

return new HtmlWebpackPlugin({
filename: name + '.html',
template: helper.rootNode(`web/index.html`),
isDevServer: true,
chunksSortMode: 'dependency',
inject: true,
chunks: [name]
})
})
return htmlPlugin;
}

/**
* Webpack configuration for browser.
*/
Expand Down Expand Up @@ -62,12 +82,7 @@ const devWebpackConfig = webpackMerge(commonConfig[0], {
*
* See: https://github.com/ampedandwired/html-webpack-plugin
*/
new HtmlWebpackPlugin({
template: helper.rootNode('web/index.html'),
isDevServer: true,
chunksSortMode: 'dependency',
inject: 'head'
}),
...generateHtmlWebpackPlugin(commonConfig[0].entry),
/*
* Plugin: ScriptExtHtmlWebpackPlugin
* Description: Enhances html-webpack-plugin functionality
Expand Down Expand Up @@ -132,7 +147,6 @@ module.exports = new Promise((resolve, reject) => {
// add port to devServer config
devWebpackConfig.devServer.port = port
devWebpackConfig.devServer.public = `${ip}:${port}`

// Add FriendlyErrorsPlugin
devWebpackConfig.plugins.push(new FriendlyErrorsPlugin({
compilationSuccessInfo: {
Expand Down
106 changes: 100 additions & 6 deletions templates/configs/webpack.prod.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,42 @@ const commonConfig = require('./webpack.common.conf');
const webpackMerge = require('webpack-merge'); // used to merge webpack configs
// tools
const ip = require('ip').address();
const os = require('os');
const chalk = require('chalk');
const path = require('path');
const webpack = require('webpack');
const helpers = require('./helper');
const helper = require('./helper');
const config = require('./config');

console.log(`${chalk.green(`Package web project at ${chalk.bold(path.resolve('./web/build'))}!`)}`)
console.log(`${chalk.green(`Package web project at ${chalk.bold(path.resolve('./release/web'))}!`)}`)
/**
* Webpack Plugins
*/
const UglifyJsparallelPlugin = require('webpack-uglify-parallel');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ScriptExtHtmlWebpackPlugin = require('script-ext-html-webpack-plugin');

/**
* Generate multiple entrys
* @param {Array} entry
*/
const generateMultipleEntrys = (entry) => {
const entrys = Object.keys(entry);
const htmlPlugin = entrys.map(name => {
return new HtmlWebpackPlugin({
filename: name + '.html',
template: helper.rootNode(`web/index.html`),
isDevServer: true,
chunksSortMode: 'dependency',
inject: true,
chunks: [name],
// production
minimize: true
})
})
return htmlPlugin;
}

/**
* Webpack configuration for browser.
*/
Expand All @@ -36,7 +60,7 @@ const productionConfig = webpackMerge(commonConfig[0], {
*
* See: http://webpack.github.io/docs/configuration.html#output-path
*/
path: helpers.rootNode('release/web'),
path: helper.rootNode('release/web'),
/**
* Specifies the name of each output file on disk.
* IMPORTANT: You must not specify an absolute path here!
Expand Down Expand Up @@ -83,9 +107,7 @@ const productionConfig = webpackMerge(commonConfig[0], {
*
* See: https://webpack.github.io/docs/list-of-plugins.html#uglifyjsplugin
*/
new webpack.optimize.UglifyJsPlugin({
minimize: true
}),
...generateMultipleEntrys(commonConfig[0].entry),
/*
* Plugin: HtmlWebpackPlugin
* Description: Simplifies creation of HTML files to serve your webpack bundles.
Expand All @@ -108,8 +130,80 @@ const productionConfig = webpackMerge(commonConfig[0], {
*/
new ScriptExtHtmlWebpackPlugin({
defaultAttribute: 'defer'
}),
/*
* Plugin: UglifyJsparallelPlugin
* Description: Identical to standard uglify webpack plugin
* with an option to build multiple files in parallel
*
* See: https://www.npmjs.com/package/webpack-uglify-parallel
*/
new UglifyJsparallelPlugin({
workers: os.cpus().length,
mangle: true,
compressor: {
warnings: false,
drop_console: true,
drop_debugger: true
}
})
]
});

/**
* Webpack configuration for weex.
*/
const weexConfig = webpackMerge(commonConfig[1], {
/**
* Options affecting the output of the compilation.
*
* See: http://webpack.github.io/docs/configuration.html#output
*/
output: {
/**
* The output directory as absolute path (required).
*
* See: http://webpack.github.io/docs/configuration.html#output-path
*/
path: helper.rootNode('dist'),
/**
* Specifies the name of each output file on disk.
* IMPORTANT: You must not specify an absolute path here!
*
* See: http://webpack.github.io/docs/configuration.html#output-filename
*/
filename: '[name].js'
},
/*
* Add additional plugins to the compiler.
*
* See: http://webpack.github.io/docs/configuration.html#plugins
*/
plugins: [
/*
* Plugin: UglifyJsparallelPlugin
* Description: Identical to standard uglify webpack plugin
* with an option to build multiple files in parallel
*
* See: https://www.npmjs.com/package/webpack-uglify-parallel
*/
new UglifyJsparallelPlugin({
workers: os.cpus().length,
mangle: true,
compressor: {
warnings: false,
drop_console: true,
drop_debugger: true
}
})
]
})

// build source to weex_bundle with watch mode.
webpack(weexConfig, (err, stats) => {
if (err) {
console.err('COMPILE ERROR:', err.stack)
}
})

module.exports = productionConfig
Loading

0 comments on commit 6a3427c

Please sign in to comment.