Created
August 20, 2016 16:46
-
-
Save lavezzi1/9cc0e58cd7d2b8f2470e703022a41db7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
var chunks = Object.keys(entries); | |
module.exports = { | |
entry: entries, | |
output: { | |
path: path.resolve(__dirname, 'public'), | |
publicPath: '/public/', | |
filename: '[name].js', | |
chunkFilename: '[id].js' | |
}, | |
devServer: { | |
quiet: false, | |
noInfo: false, | |
stats: { | |
assets: true, | |
colors: true, | |
version: false, | |
hash: false, | |
timings: false, | |
chunks: false, | |
chunkModules: false | |
} | |
}, | |
resolve: { | |
alias: { | |
'@blocks': path.resolve(__dirname, './source/blocks'), | |
'@library': path.resolve(__dirname, './source/library'), | |
'@layouts': path.resolve(__dirname, './source/layouts'), | |
'@source': path.resolve(__dirname, './source'), | |
'@styles': path.resolve(__dirname, './source/styles'), | |
'@images': path.resolve(__dirname, './source/images'), | |
'@helpers': path.resolve(__dirname, './source/resources/helpers'), | |
'@directives': path.resolve(__dirname, './source/resources/directives'), | |
'@mixins': path.resolve(__dirname, './source/resources/mixins'), | |
'@aero': path.resolve(__dirname, './source/styles/aero') | |
}, | |
extensions: ['', '.js', '.jsx'] | |
}, | |
module: { | |
loaders: [ | |
{ | |
test: /\.css$/, | |
loader: ExtractTextPlugin.extract('style-loader', 'css-loader!postcss-loader') | |
}, | |
{ | |
test: /\.js?$/, | |
loader: 'babel', | |
exclude: /node_modules/ | |
}, | |
{ | |
test: /\.(png|jpg|gif|svg)$/, | |
loader: 'url-loader', | |
query: { | |
limit: 10000, | |
name: './static/[name].[ext]' | |
} | |
} | |
] | |
}, | |
babel: { | |
presets: ['es2015'], | |
plugins: ['transform-runtime'] | |
}, | |
plugins: [ | |
new webpack.optimize.CommonsChunkPlugin({ | |
name: 'common', | |
chunks: chunks, | |
minChunks: chunks.length | |
}), | |
new ExtractTextPlugin('[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([ | |
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) { | |
var conf = { | |
filename: pathname + '.html', | |
template: pages[pathname], | |
inject: true, | |
minify: { | |
removeComments: true, | |
collapseWhitespace: false | |
} | |
}; | |
if (pathname in module.exports.entry) { | |
conf.chunks = ['common', pathname]; | |
conf.hash = false; | |
} | |
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