2017-07-28 23:13:58 +02:00
|
|
|
import { resolve } from 'path';
|
|
|
|
import * as BundleTracker from 'webpack-bundle-tracker';
|
|
|
|
import * as webpack from 'webpack';
|
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
|
|
|
|
2017-07-28 23:13:58 +02:00
|
|
|
// Only use the Webpack 2.0 Rule definitions.
|
|
|
|
interface Config extends webpack.Configuration {
|
|
|
|
module: {
|
|
|
|
rules: webpack.NewUseRule[]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default (env?: string) : Config => {
|
|
|
|
const production: boolean = env === "production";
|
|
|
|
let config: Config = {
|
|
|
|
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?$/,
|
2017-07-28 23:13:58 +02:00
|
|
|
use: 'ts-loader',
|
2017-07-28 22:33:40 +02:00
|
|
|
},
|
|
|
|
// This loads and transforms sourcemap files from other compiliers.
|
2017-07-28 22:53:37 +02:00
|
|
|
// The typescript comilier will generate a sourcemap and
|
|
|
|
// source-map-loader will output the correct sourcemap from that.
|
2017-07-28 22:33:40 +02:00
|
|
|
{
|
|
|
|
enforce: 'pre',
|
|
|
|
test: /\.js$/,
|
2017-07-28 23:13:58 +02:00
|
|
|
use: "source-map-loader",
|
2017-07-28 22:33:40 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
enforce: 'pre',
|
|
|
|
test: /\.tsx?$/,
|
|
|
|
use: "source-map-loader",
|
|
|
|
},
|
|
|
|
// 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'},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
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: {
|
|
|
|
extensions: [".tsx", ".ts", ".js", ".json"],
|
|
|
|
},
|
2017-07-28 22:53:37 +02:00
|
|
|
devtool: production ? 'source-map' : 'eval',
|
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'}),
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
// Built webpack dev asset reloader
|
2017-07-28 23:13:58 +02:00
|
|
|
config.entry['common'].unshift('webpack/hot/dev-server');
|
2017-07-28 22:53:37 +02:00
|
|
|
// Use 0.0.0.0 so that we can set a port but still use the host
|
|
|
|
// the browser is connected to.
|
2017-07-28 23:13:58 +02:00
|
|
|
config.entry['common'].unshift('webpack-dev-server/client?http://0.0.0.0:9994');
|
2017-07-28 22:53:37 +02:00
|
|
|
|
|
|
|
// 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'}),
|
|
|
|
// Hot Reload of code in development
|
|
|
|
new webpack.HotModuleReplacementPlugin(),
|
|
|
|
// Better logging from console for hot reload
|
|
|
|
new webpack.NamedModulesPlugin(),
|
|
|
|
// script-loader should load sourceURL in dev
|
|
|
|
new webpack.LoaderOptionsPlugin({debug: true}),
|
|
|
|
];
|
|
|
|
|
|
|
|
config.devServer = {
|
|
|
|
clientLogLevel: "warning",
|
|
|
|
hot: true,
|
|
|
|
inline: false,
|
|
|
|
stats: "errors-only",
|
|
|
|
watchOptions: {
|
|
|
|
aggregateTimeout: 300,
|
|
|
|
poll: 1000,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2017-07-28 22:33:40 +02:00
|
|
|
return config;
|
2015-10-26 17:11:44 +01:00
|
|
|
};
|