Skip to content

Instantly share code, notes, and snippets.

@bernardodiasc
Created March 3, 2016 13:41
Show Gist options
  • Select an option

  • Save bernardodiasc/dc029a885d18d5956a25 to your computer and use it in GitHub Desktop.

Select an option

Save bernardodiasc/dc029a885d18d5956a25 to your computer and use it in GitHub Desktop.
var path = require('path');
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
require('es6-promise').polyfill();
module.exports = function(options) {
var entry, jsLoaders, plugins, cssLoaders, sassLoaders;
// If production is true
if (options.prod) {
// Entry
entry = [
path.resolve(__dirname, 'js/app.js'), // Start with js/app.js...
"bootstrap-loader/extractStyles"
];
sassLoaders = ExtractTextPlugin.extract('style-loader', 'css-loader!postcss-loader!sass-loader');
cssLoaders = ExtractTextPlugin.extract('style-loader', 'css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!postcss-loader');
// Plugins
plugins = [// Plugins for Webpack
new webpack.optimize.UglifyJsPlugin({ // Optimize the JavaScript...
compress: {
warnings: false // ...but do not show warnings in the console (there is a lot of them)
}
}),
new HtmlWebpackPlugin({
template: 'index.html', // Move the index.html file...
minify: { // Minifying it while it is parsed
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true
},
inject: true // inject all files that are generated by webpack, e.g. bundle.js, main.css with the correct HTML tags
}),
new ExtractTextPlugin("css/main.css"),
new ExtractTextPlugin("css/setup.scss"),
new webpack.DefinePlugin({
"process.env": {
NODE_ENV: JSON.stringify("production"),
API_BASE_URL: JSON.stringify(options.apiBaseUrl),
GOOGLE_SETTINGS: JSON.stringify(options.googleSettings)
}
})
];
// If app is in development
} else {
// Entry
entry = [
"webpack-dev-server/client?http://localhost:3000", // Needed for hot reloading
"webpack/hot/only-dev-server", // See above
path.resolve(__dirname, 'js/app.js'), // Start with js/app.js...
"bootstrap-loader"
];
sassLoaders = 'style-loader!css-loader!postcss-loader!sass-loader';
cssLoaders = 'style-loader!css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!postcss-loader';
// Only plugin is the hot module replacement plugin
plugins = [
new webpack.HotModuleReplacementPlugin(), // Make hot loading work
new HtmlWebpackPlugin({
template: 'index.html', // Move the index.html file
inject: true // inject all files that are generated by webpack, e.g. bundle.js, main.css with the correct HTML tags
}),
new webpack.DefinePlugin({
"process.env": {
NODE_ENV: JSON.stringify("development"),
API_BASE_URL: JSON.stringify(options.apiBaseUrl),
GOOGLE_SETTINGS: JSON.stringify(options.googleSettings)
}
})
]
}
return {
entry: entry,
output: { // Compile into js/build.js
path: path.resolve(__dirname, 'build'),
filename: "js/bundle.js",
publicPath: "/"
},
module: {
loaders: [{
test: /\.js$/, // Transform all .js files required somewhere within an entry point...
loader: 'babel', // ...with the specified loaders...
exclude: path.join(__dirname, '/node_modules/') // ...except for the node_modules folder.
},{
test: /bootstrap\/dist\/js\/umd\//,
loader: 'imports?jQuery=jquery'
}, {
test: /\.css$/, // Transform all .css files required somewhere within an entry point...
loader: cssLoaders // ...with PostCSS
}, {
test: /\.scss$/,
loaders: sassLoaders
}, {
test: /\.jpe?g$|\.gif$|\.png$/i,
loader: "url-loader?limit=100000"
}, {
test: /\.svg$/,
loader: 'babel!svg-react'
}
]
},
plugins: plugins,
postcss: function() {
return [
require('postcss-import')({ // Import all the css files...
glob: true,
onImport: function (files) {
files.forEach(this.addDependency); // ...and add dependecies from the main.css files to the other css files...
}.bind(this) // ...so they get hot–reloaded when something changes...
}),
require('postcss-simple-vars')(), // ...then replace the variables...
require('postcss-focus')(), // ...add a :focus to ever :hover...
require('autoprefixer')({ // ...and add vendor prefixes...
browsers: ['last 2 versions', 'IE > 8'] // ...supporting the last 2 major browser versions and IE 8 and up...
}),
require('postcss-reporter')({ // This plugin makes sure we get warnings in the console
clearMessages: true
})
];
},
target: "web", // Make web variables accessible to webpack, e.g. window
stats: false, // Don't show stats in the console
progress: true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment