Last active
April 26, 2017 16:04
-
-
Save hmarcelodn/3ca937e37e61537b5d756ce7652d1549 to your computer and use it in GitHub Desktop.
webpack2 config
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
const path = require('path') | |
const HtmlWebpackPlugin = require('html-webpack-plugin'); | |
const CopyWebpackPlugin = require('copy-webpack-plugin'); | |
const ExtractTextPlugin = require('extract-text-webpack-plugin'); | |
const { AureliaPlugin, ModuleDependenciesPlugin } = require('aurelia-webpack-plugin'); | |
const { optimize: { CommonsChunkPlugin }, ProvidePlugin } = require('webpack') | |
const { TsConfigPathsPlugin, CheckerPlugin } = require('awesome-typescript-loader'); | |
// config helpers: | |
const ensureArray = (config) => config && (Array.isArray(config) ? config : [config]) || [] | |
const when = (condition, config, negativeConfig) => | |
condition ? ensureArray(config) : ensureArray(negativeConfig) | |
// primary config: | |
const title = 'Interviewer'; | |
const outDir = path.resolve(__dirname, 'dist'); | |
const srcDir = path.resolve(__dirname, 'src'); | |
const nodeModulesDir = path.resolve(__dirname, 'node_modules'); | |
const baseUrl = '/'; | |
const cssRules = [ | |
{ loader: 'css-loader' }, | |
{ | |
loader: 'postcss-loader', | |
options: { plugins: () => [require('autoprefixer')({ browsers: ['last 2 versions'] })]} | |
} | |
] | |
module.exports = ({production, server, extractCss, coverage} = {}) => ({ | |
resolve: { | |
extensions: ['.ts', '.js', '.css', '.scss'], | |
modules: [srcDir, 'node_modules'], | |
}, | |
entry: { | |
app: ['aurelia-bootstrapper'], | |
vendor: ['bluebird', 'jquery', 'bootstrap'], | |
jqueryPlugins: | |
[ | |
'materialize-css', | |
'sweetalert/dist/sweetalert.min', | |
'ion-rangeslider', | |
'hammerjs', | |
'owl.carousel', | |
'qtip2', | |
'selectivity/jquery' | |
], | |
styles: | |
[ | |
'materialize-css/dist/css/materialize.css', | |
'font-awesome/scss/font-awesome.scss', | |
'dragula/dist/dragula.css', | |
'sweetalert/dist/sweetalert.css', | |
'ion-rangeslider/css/ion.rangeSlider.css', | |
'ion-rangeslider/css/ion.rangeSlider.skinModern.css', | |
'ion-rangeslider/css/normalize.css', | |
'owl.carousel/dist/assets/owl.carousel.css', | |
'qtip2/dist/jquery.qtip.css', | |
'flag-icon-css/css/flag-icon.css', | |
'selectivity/styles/selectivity.css', | |
'./static/site.scss' | |
] | |
}, | |
output: { | |
path: outDir, | |
publicPath: baseUrl, | |
filename: production ? '[name].[chunkhash].bundle.js' : '[name].[hash].bundle.js', | |
sourceMapFilename: production ? '[name].[chunkhash].bundle.map' : '[name].[hash].bundle.map', | |
chunkFilename: production ? '[chunkhash].chunk.js' : '[hash].chunk.js', | |
}, | |
devServer: { | |
contentBase: baseUrl, | |
// serve index.html for all 404 (required for push-state) | |
historyApiFallback: true, | |
}, | |
module: { | |
rules: [ | |
// CSS required in JS/TS files should use the style-loader that auto-injects it into the website | |
// only when the issuer is a .js/.ts file, so the loaders are not applied inside html templates | |
{ | |
test: /\.css$/i, | |
loader: 'style-loader!css-loader' | |
}, | |
{ | |
test: /\.scss$/i, | |
loader: 'style-loader!css-loader!sass-loader' | |
}, | |
{ test: /\.html$/i, loader: 'html-loader' }, | |
{ test: /\.ts$/i, loader: 'awesome-typescript-loader', exclude: nodeModulesDir }, | |
{ test: /\.json$/i, loader: 'json-loader' }, | |
// use Bluebird as the global Promise implementation: | |
{ test: /[\/\\]node_modules[\/\\]bluebird[\/\\].+\.js$/, loader: 'expose-loader?Promise' }, | |
// exposes jQuery globally as $ and as jQuery: | |
{ test: require.resolve('jquery'), loader: 'expose-loader?$!expose-loader?jQuery' }, | |
// embed small images and fonts as Data Urls and larger ones as files: | |
{ test: /\.(png|gif|jpg|cur)$/i, loader: 'url-loader', options: { limit: 8192 } }, | |
{ test: /\.woff2(\?v=[0-9]\.[0-9]\.[0-9])?$/i, loader: 'url-loader', options: { limit: 10000, mimetype: 'application/font-woff2' } }, | |
{ test: /\.woff(\?v=[0-9]\.[0-9]\.[0-9])?$/i, loader: 'url-loader', options: { limit: 10000, mimetype: 'application/font-woff' } }, | |
// load these fonts normally, as files: | |
{ test: /\.(ttf|eot|svg|otf)(\?v=[0-9]\.[0-9]\.[0-9])?$/i, loader: 'file-loader', include: nodeModulesDir }, | |
...when(coverage, { | |
test: /\.[jt]s$/i, loader: 'istanbul-instrumenter-loader', | |
include: srcDir, exclude: [/\.{spec,test}\.[jt]s$/i], | |
enforce: 'post', options: { esModules: true }, | |
}) | |
] | |
}, | |
plugins: [ | |
new AureliaPlugin(), | |
new ProvidePlugin({ | |
'Promise': 'bluebird', | |
'$': 'jquery', | |
'jQuery': 'jquery', | |
'window.jQuery': 'jquery', | |
'window.$': 'jquery', | |
'JQuery': 'jquery' | |
}), | |
new ModuleDependenciesPlugin({ | |
"aurelia-auth": [ "aurelia-auth/auth-filter" ] | |
}), | |
new TsConfigPathsPlugin(), | |
new CheckerPlugin(), | |
new HtmlWebpackPlugin({ | |
template: 'index.ejs', | |
minify: production ? { | |
removeComments: true, | |
collapseWhitespace: true | |
} : undefined, | |
metadata: { | |
// available in index.ejs // | |
title, server, baseUrl | |
}, | |
}), | |
...when(extractCss, new ExtractTextPlugin({ | |
filename: production ? '[contenthash].css' : '[id].css', | |
allChunks: true, | |
})), | |
...when(production, new CommonsChunkPlugin({ | |
name: ['common'] | |
})), | |
...when(production, new CopyWebpackPlugin([ | |
{ from: 'static/favicon.ico', to: 'favicon.ico' } | |
])) | |
], | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment