Skip to content

Instantly share code, notes, and snippets.

@lavezzi1
Last active October 7, 2016 10:12
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save lavezzi1/3b599b8e2adec9561e4bf1f3dffb5505 to your computer and use it in GitHub Desktop.
var path = require('path');
var webpack = require('webpack');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var glob = require('glob');
var entries = getEntries('./source/pages/', 'js'); // Obtain entry js file
var chunks = Object.keys(entries);
module.exports = {
entry: entries,
output: {
path: path.resolve(__dirname, 'public'), // Output path html, css, js, image files and other resources, and all the resources files in the Public directory
publicPath: '/public/', // Path html, css, js, image files and other resources on the server
filename: 'js/[name].js', // Js file is generated for each entry configuration
chunkFilename: '[id].js'
},
devServer: {
quiet: false,
noInfo: false,
stats: {
assets: true,
colors: true,
version: false,
hash: false,
timings: true,
chunks: false,
chunkModules: false
}
},
resolve: {
alias: {
'@components': path.resolve(__dirname, './source/components'),
'@styles': path.resolve(__dirname, './source/styles')
},
extensions: ['', '.js', '.vue']
},
module: {
loaders: [
{
test: /\.css$/,
loader: ExtractTextPlugin.extract('style-loader', 'css-loader')
},
{
test: /\.vue$/,
loader: 'vue'
},
{
test: /\.js$/,
loader: 'babel',
exclude: /node_modules/
},
{
test: /\.(png|jpg|gif|svg)$/,
loader: 'url',
query: {
limit: 10000,
name: './static/[name].[ext]'
}
}
]
},
vue: {
loaders: {
js: 'babel',
css: ExtractTextPlugin.extract("vue-style-loader", "css-loader"),
}
},
plugins: [
// Extraction common module
new webpack.optimize.CommonsChunkPlugin({
name: 'common', // The name of the common module
chunks: chunks, // chunks need to extract module
minChunks: chunks.length
}),
// Configured to extract style files
new ExtractTextPlugin('css/[name].css')
]
};
var prod = process.env.NODE_ENV === 'production';
module.exports.plugins = (module.exports.plugins || []);
if (prod) {
module.exports.devtool = 'source-map';
module.exports.plugins = module.exports.plugins.concat([
// Environment Configuration
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"'
}
}),
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
}
}),
new webpack.optimize.OccurenceOrderPlugin()
]);
} else {
module.exports.devtool = 'eval-source-map';
module.exports.output.publicPath = '/';
}
var pages = getEntries('./source/pages/', 'html');
for (var pathname in pages) {
// Configured to generate the html file, define paths, etc.
var conf = {
filename: pathname + '.html', // html output pathname
template: pages[pathname], // Template path
inject: true, // js insertion
favicon: './source/pages/static/favicon.png',
minify: {
removeComments: true,
collapseWhitespace: false
}
};
if (pathname in module.exports.entry) {
conf.chunks = ['common', pathname];
conf.hash = false;
}
// We need to generate a html file on several HtmlWebpackPlugin object configuration
module.exports.plugins.push(new HtmlWebpackPlugin(conf));
}
function getEntries(context, extension) {
if (context[context.length - 1] !== '/') {
context += '/';
}
extension = '.' + extension;
var files = glob.sync(context + '**/*' + extension),
entries = {};
files.forEach(function (file) {
entries[file.replace(context, '').replace(extension, '')] = file;
});
return entries;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment