2017-07-28 23:13:58 +02:00
|
|
|
import { resolve } from 'path';
|
|
|
|
import * as BundleTracker from 'webpack-bundle-tracker';
|
|
|
|
import * as webpack from 'webpack';
|
2018-04-25 22:09:48 +02:00
|
|
|
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
|
2017-05-22 22:49:20 +02:00
|
|
|
|
2017-07-28 23:13:58 +02:00
|
|
|
const assets = require('./webpack.assets.json');
|
2017-07-28 22:53:37 +02:00
|
|
|
|
2018-04-25 22:09:48 +02:00
|
|
|
// Currently we're using style-loader for local dev
|
|
|
|
// because MiniCssExtractPlugin doesn't support
|
|
|
|
// HMR as yet. When this changes we can switch
|
|
|
|
// over to MiniCssExtractPlugin which will Also
|
|
|
|
// solve the flash of unstsyled elements on page load.
|
|
|
|
// https://github.com/webpack-contrib/mini-css-extract-plugin/issues/34
|
|
|
|
function getCSSLoader(isProd: boolean) {
|
|
|
|
if(isProd) {
|
|
|
|
return MiniCssExtractPlugin.loader
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
loader: 'style-loader',
|
|
|
|
options: {
|
|
|
|
sourceMap: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-17 21:59:17 +02:00
|
|
|
export default (env?: string) : webpack.Configuration => {
|
2017-07-28 23:13:58 +02:00
|
|
|
const production: boolean = env === "production";
|
2018-04-17 21:59:17 +02:00
|
|
|
let config: webpack.Configuration = {
|
|
|
|
mode: production ? "production" : "development",
|
2017-07-28 23:13:58 +02:00
|
|
|
context: resolve(__dirname, "../"),
|
2017-07-28 22:33:40 +02:00
|
|
|
entry: assets,
|
|
|
|
module: {
|
|
|
|
rules: [
|
|
|
|
// Run the typescript compilier on .ts files before webpack
|
|
|
|
{
|
|
|
|
test: /\.tsx?$/,
|
2018-04-17 21:59:17 +02:00
|
|
|
loader: 'ts-loader',
|
|
|
|
options: {
|
|
|
|
configFile: require.resolve('../static/ts/tsconfig.json')
|
|
|
|
}
|
2017-07-28 22:33:40 +02:00
|
|
|
},
|
|
|
|
// Uses script-loader on minified files so we don't change global variables in them.
|
|
|
|
// Also has the effect of making processing these files fast
|
2017-08-06 03:53:44 +02:00
|
|
|
// Currently the source maps don't work with these so use unminified files
|
|
|
|
// if debugging is required.
|
2017-07-28 22:33:40 +02:00
|
|
|
{
|
|
|
|
test: /(min|zxcvbn)\.js/,
|
|
|
|
use: [ 'script-loader' ],
|
|
|
|
},
|
|
|
|
// Expose Global variables to webpack
|
2017-08-06 03:53:44 +02:00
|
|
|
// Use the unminified versions of jquery and underscore so that
|
|
|
|
// Good error messages show up in production and development in the source maps
|
|
|
|
{
|
|
|
|
test: require.resolve('../static/node_modules/jquery/dist/jquery.js'),
|
|
|
|
use: [
|
|
|
|
{loader: 'expose-loader', options: '$'},
|
|
|
|
{loader: 'expose-loader', options: 'jQuery'},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: require.resolve('../node_modules/underscore/underscore.js'),
|
|
|
|
use: [
|
|
|
|
{loader: 'expose-loader', options: '_'},
|
|
|
|
],
|
|
|
|
},
|
2017-07-28 22:33:40 +02:00
|
|
|
{
|
|
|
|
test: require.resolve('../static/js/debug.js'),
|
|
|
|
use: [
|
|
|
|
{loader: 'expose-loader', options: 'debug'},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: require.resolve('../static/js/blueslip.js'),
|
|
|
|
use: [
|
|
|
|
{loader: 'expose-loader', options: 'blueslip'},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: require.resolve('../static/js/common.js'),
|
|
|
|
use: [
|
|
|
|
{loader: 'expose-loader', options: 'common'},
|
|
|
|
],
|
|
|
|
},
|
2018-04-25 22:09:48 +02:00
|
|
|
// regular css files
|
|
|
|
{
|
|
|
|
test: /\.css$/,
|
|
|
|
use: [
|
|
|
|
getCSSLoader(production),
|
|
|
|
{
|
|
|
|
loader: 'css-loader',
|
|
|
|
options: {
|
|
|
|
sourceMap: true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
// sass / scss loader
|
|
|
|
{
|
|
|
|
test: /\.(sass|scss)$/,
|
|
|
|
use: [
|
|
|
|
getCSSLoader(production),
|
|
|
|
{
|
|
|
|
loader: 'css-loader',
|
|
|
|
options: {
|
|
|
|
sourceMap: true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
loader: 'sass-loader',
|
|
|
|
options: {
|
|
|
|
sourceMap: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
},
|
|
|
|
// load fonts and files
|
|
|
|
{
|
|
|
|
test: /\.(woff(2)?|ttf|eot|svg|otf)(\?v=\d+\.\d+\.\d+)?$/,
|
|
|
|
use: [{
|
|
|
|
loader: 'file-loader',
|
|
|
|
options: {
|
|
|
|
name: '[name].[ext]',
|
|
|
|
outputPath: 'fonts/'
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
}
|
2017-07-28 22:33:40 +02:00
|
|
|
],
|
|
|
|
},
|
|
|
|
output: {
|
2017-07-28 23:13:58 +02:00
|
|
|
path: resolve(__dirname, '../static/webpack-bundles'),
|
2017-07-28 22:53:37 +02:00
|
|
|
filename: production ? '[name]-[hash].js' : '[name].js',
|
2017-07-28 22:33:40 +02:00
|
|
|
},
|
|
|
|
resolve: {
|
2018-04-25 22:09:48 +02:00
|
|
|
extensions: [".tsx", ".ts", ".js", ".json", ".scss", ".css"],
|
2017-07-28 22:33:40 +02:00
|
|
|
},
|
2018-04-18 16:29:08 +02:00
|
|
|
// We prefer cheap-module-eval-source-map over eval because
|
|
|
|
// currently eval has trouble setting breakpoints per line
|
|
|
|
// in Google Chrome. There's almost no difference
|
|
|
|
// between the compilation time for the two and could be
|
|
|
|
// re-evaluated as the size of files grows
|
|
|
|
devtool: production ? 'source-map' : 'cheap-module-eval-source-map',
|
2017-07-28 22:33:40 +02:00
|
|
|
};
|
2017-07-28 22:53:37 +02:00
|
|
|
if (production) {
|
|
|
|
config.plugins = [
|
|
|
|
new BundleTracker({filename: 'webpack-stats-production.json'}),
|
2018-04-25 22:09:48 +02:00
|
|
|
// Extract CSS from files
|
|
|
|
new MiniCssExtractPlugin({
|
2018-05-02 01:27:48 +02:00
|
|
|
filename: (data) => {
|
|
|
|
// This is a special case in order to produce
|
|
|
|
// a static CSS file to be consumed by
|
|
|
|
// static/html/5xx.html
|
|
|
|
if(data.chunk.name === 'error-styles') {
|
|
|
|
return 'error-styles.css';
|
|
|
|
}
|
|
|
|
return '[name].[contenthash].css';
|
|
|
|
},
|
2018-04-25 22:09:48 +02:00
|
|
|
chunkFilename: "[id].css"
|
|
|
|
})
|
2017-07-28 22:53:37 +02:00
|
|
|
];
|
|
|
|
} else {
|
|
|
|
// Out JS debugging tools
|
2017-07-28 23:13:58 +02:00
|
|
|
config.entry['common'].push('./static/js/debug.js');
|
2017-07-28 22:53:37 +02:00
|
|
|
|
|
|
|
config.output.publicPath = '/webpack/';
|
|
|
|
config.plugins = [
|
|
|
|
new BundleTracker({filename: 'var/webpack-stats-dev.json'}),
|
|
|
|
// Better logging from console for hot reload
|
|
|
|
new webpack.NamedModulesPlugin(),
|
|
|
|
// script-loader should load sourceURL in dev
|
|
|
|
new webpack.LoaderOptionsPlugin({debug: true}),
|
2018-04-25 22:09:48 +02:00
|
|
|
// Extract CSS from files
|
|
|
|
new MiniCssExtractPlugin({
|
|
|
|
filename: "[name].css",
|
|
|
|
chunkFilename: "[id].css"
|
|
|
|
})
|
2017-07-28 22:53:37 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
config.devServer = {
|
2018-04-25 21:16:08 +02:00
|
|
|
clientLogLevel: "error",
|
|
|
|
stats: "errors-only",
|
2017-07-28 22:53:37 +02:00
|
|
|
watchOptions: {
|
2018-04-25 21:16:08 +02:00
|
|
|
poll: 100
|
|
|
|
}
|
2017-07-28 22:53:37 +02:00
|
|
|
};
|
|
|
|
}
|
2017-07-28 22:33:40 +02:00
|
|
|
return config;
|
2015-10-26 17:11:44 +01:00
|
|
|
};
|