Files
KaTeX/webpack.common.js
ylemkimon 30854eb866 Enable hot module replacement(HMR) (#1100)
* Use ES6 in static/main.js

+ Lint static/main.js

* Enable hot module replacement(HMR) in webpack-dev-server

* Disable no-console in static/main.js

* Use seperate entry point/bundle for the test page(/static/main.js)
2018-02-01 13:33:49 -05:00

123 lines
3.4 KiB
JavaScript

// @flow
const path = require('path');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const webpack = require('webpack');
/*::
type Target = {|
name: string, // the name of output JS/CSS
entry: string, // the path to the entry point
library?: string // the name of the exported module
|};
*/
/**
* List of targets to build
*/
const targets /*: Array<Target> */ = [
{
name: 'katex',
entry: './katex.webpack.js',
library: 'katex',
},
{
name: 'contrib/auto-render',
entry: './contrib/auto-render/auto-render.js',
library: 'renderMathInElement',
},
{
name: 'contrib/copy-tex',
entry: './contrib/copy-tex/copy-tex.js',
},
{
name: 'contrib/mathtex-script-type',
entry: './contrib/mathtex-script-type/mathtex-script-type.js',
},
];
/**
* Create a webpack config for given target
*/
function createConfig(target /*: Target */, dev /*: boolean */,
minimize /*: boolean */) /*: Object */ {
const cssLoader = {
loader: 'css-loader',
options: {
minimize, // cssnano
},
};
return {
context: __dirname,
entry: {
[target.name]: target.entry,
},
output: {
filename: minimize ? '[name].min.js' : '[name].js',
library: target.library,
libraryTarget: 'umd',
libraryExport: 'default',
path: path.resolve(__dirname, 'build'),
publicPath: dev ? '/' : '',
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: 'babel-loader',
},
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [cssLoader],
}),
},
{
test: /\.less$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [cssLoader, {
loader: 'less-loader',
}],
}),
},
{
test: /\.(ttf|woff|woff2)$/,
use: [{
loader: 'file-loader',
options: {
name: 'fonts/[name].[ext]',
},
}],
},
],
},
externals: 'katex',
plugins: [
new webpack.EnvironmentPlugin({
NODE_ENV: dev ? 'development' : 'production',
}),
dev && new webpack.NamedModulesPlugin(),
minimize && new UglifyJsPlugin({
uglifyOptions: {
output: {
ascii_only: true,
},
},
}),
new ExtractTextPlugin({
filename: minimize ? '[name].min.css' : '[name].css',
disable: dev,
}),
].filter(Boolean),
devtool: dev && 'inline-source-map',
};
}
module.exports = {
targets,
createConfig,
};