2017-07-28 23:13:58 +02:00
|
|
|
import { resolve } from 'path';
|
|
|
|
import * as BundleTracker from 'webpack-bundle-tracker';
|
|
|
|
import * as webpack from 'webpack';
|
2019-06-02 06:39:34 +02:00
|
|
|
// The devServer member of webpack.Configuration is managed by the
|
|
|
|
// webpack-dev-server package. We are only importing the type here.
|
|
|
|
import * as _webpackDevServer from 'webpack-dev-server';
|
2019-06-04 21:11:31 +02:00
|
|
|
import { getExposeLoaders, getImportLoaders, cacheLoader } from './webpack-helpers';
|
2019-03-25 15:55:39 +01:00
|
|
|
import * as MiniCssExtractPlugin from '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
|
|
|
|
2019-03-25 15:55:39 +01:00
|
|
|
export default (env?: string): webpack.Configuration => {
|
2017-07-28 23:13:58 +02:00
|
|
|
const production: boolean = env === "production";
|
2019-03-24 11:54:17 +01:00
|
|
|
const config: webpack.Configuration = {
|
2018-04-17 21:59:17 +02:00
|
|
|
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: {
|
2019-03-24 11:54:17 +01:00
|
|
|
configFile: require.resolve('../static/js/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
|
|
|
{
|
2018-05-28 08:09:49 +02:00
|
|
|
// We dont want to match admin.js
|
|
|
|
test: /(\.min|min\.|zxcvbn)\.js/,
|
2019-06-04 21:11:31 +02:00
|
|
|
use: [cacheLoader, 'script-loader'],
|
2017-07-28 22:33:40 +02:00
|
|
|
},
|
2018-04-25 22:09:48 +02:00
|
|
|
// regular css files
|
|
|
|
{
|
|
|
|
test: /\.css$/,
|
2019-06-06 11:01:44 +02:00
|
|
|
use: [
|
|
|
|
{
|
|
|
|
loader: MiniCssExtractPlugin.loader,
|
|
|
|
options: {
|
|
|
|
hmr: !production,
|
|
|
|
},
|
|
|
|
},
|
2019-06-04 21:11:31 +02:00
|
|
|
cacheLoader,
|
2018-04-25 22:09:48 +02:00
|
|
|
{
|
|
|
|
loader: 'css-loader',
|
|
|
|
options: {
|
2019-03-24 11:54:17 +01:00
|
|
|
sourceMap: true,
|
2018-05-28 08:09:49 +02:00
|
|
|
},
|
2018-04-25 22:09:48 +02:00
|
|
|
},
|
2019-06-06 11:01:44 +02:00
|
|
|
],
|
2018-04-25 22:09:48 +02:00
|
|
|
},
|
|
|
|
// sass / scss loader
|
|
|
|
{
|
|
|
|
test: /\.(sass|scss)$/,
|
2019-06-06 11:01:44 +02:00
|
|
|
use: [
|
|
|
|
{
|
|
|
|
loader: MiniCssExtractPlugin.loader,
|
|
|
|
options: {
|
|
|
|
hmr: !production,
|
|
|
|
},
|
|
|
|
},
|
2019-06-04 21:11:31 +02:00
|
|
|
cacheLoader,
|
2018-04-25 22:09:48 +02:00
|
|
|
{
|
|
|
|
loader: 'css-loader',
|
|
|
|
options: {
|
2019-03-24 11:54:17 +01:00
|
|
|
sourceMap: true,
|
|
|
|
},
|
2018-04-25 22:09:48 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
loader: 'sass-loader',
|
|
|
|
options: {
|
2019-03-24 11:54:17 +01:00
|
|
|
sourceMap: true,
|
|
|
|
},
|
|
|
|
},
|
2019-06-06 11:01:44 +02:00
|
|
|
],
|
2018-04-25 22:09:48 +02:00
|
|
|
},
|
|
|
|
// load fonts and files
|
|
|
|
{
|
2018-05-01 22:27:05 +02:00
|
|
|
test: /\.(woff(2)?|ttf|eot|svg|otf|png)(\?v=\d+\.\d+\.\d+)?$/,
|
2018-04-25 22:09:48 +02:00
|
|
|
use: [{
|
|
|
|
loader: 'file-loader',
|
|
|
|
options: {
|
|
|
|
name: '[name].[ext]',
|
2019-03-24 11:54:17 +01:00
|
|
|
outputPath: 'files/',
|
|
|
|
},
|
|
|
|
}],
|
|
|
|
},
|
2017-07-28 22:33:40 +02:00
|
|
|
],
|
|
|
|
},
|
|
|
|
output: {
|
2017-07-28 23:13:58 +02:00
|
|
|
path: resolve(__dirname, '../static/webpack-bundles'),
|
2018-06-02 02:56:15 +02:00
|
|
|
filename: production ? '[name].[chunkhash].js' : '[name].js',
|
2017-07-28 22:33:40 +02:00
|
|
|
},
|
|
|
|
resolve: {
|
2018-05-28 08:09:49 +02:00
|
|
|
modules: [
|
|
|
|
resolve(__dirname, "../static"),
|
2019-04-22 07:47:05 +02:00
|
|
|
"node_modules",
|
2018-05-28 08:09:49 +02:00
|
|
|
resolve(__dirname, "../"),
|
|
|
|
],
|
2018-04-25 22:09:48 +02:00
|
|
|
extensions: [".tsx", ".ts", ".js", ".json", ".scss", ".css"],
|
2017-07-28 22:33:40 +02:00
|
|
|
},
|
2018-05-22 01:50:25 +02:00
|
|
|
// We prefer cheap-module-source-map over any eval-** options
|
|
|
|
// because the eval-options currently don't support being
|
|
|
|
// source mapped in error stack traces
|
|
|
|
// We prefer it over eval since eval has trouble setting
|
|
|
|
// breakpoints in chrome.
|
|
|
|
devtool: production ? 'source-map' : 'cheap-module-source-map',
|
2017-07-28 22:33:40 +02:00
|
|
|
};
|
2018-05-28 08:09:49 +02:00
|
|
|
|
|
|
|
// Add variables within modules
|
|
|
|
// Because of code corecing the value of window within the libraries
|
|
|
|
var importsOptions = [
|
|
|
|
{
|
|
|
|
path: "../static/third/spectrum/spectrum.js",
|
2019-03-24 11:54:17 +01:00
|
|
|
args: "this=>window",
|
|
|
|
},
|
2018-05-28 08:09:49 +02:00
|
|
|
];
|
|
|
|
config.module.rules.push(...getImportLoaders(importsOptions));
|
|
|
|
|
|
|
|
// Expose Global variables for third party libraries to webpack modules
|
|
|
|
// Use the unminified versions of jquery and underscore so that
|
|
|
|
// Good error messages show up in production and development in the source maps
|
|
|
|
var exposeOptions = [
|
2019-06-21 03:16:48 +02:00
|
|
|
{ path: "blueimp-md5/js/md5.js" },
|
|
|
|
{ path: "clipboard/dist/clipboard.js", name: "ClipboardJS" },
|
|
|
|
{ path: "xdate/src/xdate.js", name: "XDate" },
|
|
|
|
{ path: "simplebar/dist/simplebar.js"},
|
2018-05-28 08:09:49 +02:00
|
|
|
{ path: "../static/third/marked/lib/marked.js" },
|
|
|
|
{ path: "../static/generated/emoji/emoji_codes.js" },
|
|
|
|
{ path: "../static/generated/pygments_data.js" },
|
|
|
|
{ path: "../static/js/debug.js" },
|
|
|
|
{ path: "../static/js/blueslip.js" },
|
|
|
|
{ path: "../static/js/common.js" },
|
2019-06-21 03:16:48 +02:00
|
|
|
{ path: "jquery/dist/jquery.js", name: ['$', 'jQuery'] },
|
|
|
|
{ path: "underscore/underscore.js", name: '_' },
|
|
|
|
{ path: "handlebars/dist/handlebars.runtime.js", name: 'Handlebars' },
|
|
|
|
{ path: "to-markdown/dist/to-markdown.js", name: 'toMarkdown' },
|
|
|
|
{ path: "sortablejs/Sortable.js"},
|
|
|
|
{ path: "winchan/winchan.js", name: 'WinChan'},
|
2018-05-28 08:09:49 +02:00
|
|
|
];
|
|
|
|
config.module.rules.push(...getExposeLoaders(exposeOptions));
|
|
|
|
|
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
|
2019-03-24 11:54:17 +01:00
|
|
|
if (data.chunk.name === 'error-styles') {
|
2018-05-02 01:27:48 +02:00
|
|
|
return 'error-styles.css';
|
|
|
|
}
|
|
|
|
return '[name].[contenthash].css';
|
|
|
|
},
|
2019-03-24 11:54:17 +01:00
|
|
|
chunkFilename: "[chunkhash].css",
|
|
|
|
}),
|
2017-07-28 22:53:37 +02:00
|
|
|
];
|
|
|
|
} else {
|
|
|
|
// Out JS debugging tools
|
2019-03-24 11:54:17 +01:00
|
|
|
config.entry['common'].push('./static/js/debug.js'); // eslint-disable-line dot-notation
|
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",
|
2019-03-24 11:54:17 +01:00
|
|
|
chunkFilename: "[chunkhash].css",
|
2018-05-03 00:16:32 +02:00
|
|
|
}),
|
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: {
|
2019-03-24 11:54:17 +01: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
|
|
|
};
|