static-cms/scripts/webpack.js

87 lines
2.0 KiB
JavaScript
Raw Normal View History

2018-07-17 19:13:52 -04:00
const path = require('path');
2018-07-23 12:30:15 -04:00
const webpack = require('webpack');
2018-07-17 19:13:52 -04:00
const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin');
const pkg = require(path.join(process.cwd(), 'package.json'));
2018-07-23 12:30:15 -04:00
const isProduction = process.env.NODE_ENV === 'production';
2018-07-23 21:25:49 -04:00
const rules = () => ({
js: () => ({
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
configFile: path.resolve(`${__dirname}/../babel.config.js`),
},
},
}),
svg: () => ({
test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
exclude: [/node_modules/],
use: 'svg-inline-loader',
}),
});
2018-07-23 12:30:15 -04:00
const plugins = () => {
2018-07-23 23:40:48 -04:00
return {
2018-07-23 21:25:49 -04:00
friendlyErrors: () => new FriendlyErrorsWebpackPlugin()
};
2018-07-23 12:30:15 -04:00
};
const stats = () => {
if (isProduction) {
return {
builtAt: false,
2018-07-25 08:45:13 -04:00
chunks: false,
2018-07-23 12:30:15 -04:00
colors: true,
entrypoints: false,
2018-07-25 08:45:13 -04:00
errorDetails: false,
2018-07-23 12:30:15 -04:00
hash: false,
modules: false,
timings: false,
version: false,
2018-07-25 08:45:13 -04:00
warnings: false,
2018-07-23 12:30:15 -04:00
};
}
return {
all: false,
};
};
2018-07-23 21:25:49 -04:00
const getConfig = () => ({
2018-07-23 12:30:15 -04:00
mode: isProduction ? 'production' : 'development',
2018-07-17 19:13:52 -04:00
entry: './src/index.js',
output: {
path: process.cwd(),
filename: pkg.main,
library: pkg.name,
libraryTarget: 'umd',
umdNamedDefine: true,
},
module: {
2018-07-23 21:25:49 -04:00
rules: Object.values(rules()).map(rule => rule()),
2018-07-17 19:13:52 -04:00
},
2018-07-24 00:27:49 -04:00
plugins: Object.values(plugins()).map(plugin => plugin()),
2018-07-23 23:40:48 -04:00
devtool: 'source-map',
2018-07-17 19:13:52 -04:00
target: 'web',
2018-07-23 21:25:49 -04:00
2018-07-23 12:30:15 -04:00
/**
* Exclude peer dependencies from package bundles.
*/
2018-07-17 19:13:52 -04:00
externals: (context, request, cb) => {
2018-07-24 10:45:12 -04:00
const localExternals = pkg.localExternals || [];
2018-07-17 19:13:52 -04:00
const peerDeps = Object.keys(pkg.peerDependencies || {});
2018-07-24 10:45:12 -04:00
const externals = isProduction ? peerDeps : [...localExternals, ...peerDeps];
2018-07-23 12:14:53 -04:00
const isPeerDep = dep => new RegExp(`^${dep}($|/)`).test(request);
2018-07-24 10:45:12 -04:00
return externals.some(isPeerDep) ? cb(null, request) : cb();
2018-07-17 19:13:52 -04:00
},
2018-07-23 12:30:15 -04:00
stats: stats(),
2018-07-23 21:25:49 -04:00
});
module.exports = {
getConfig,
rules: rules(),
plugins: plugins(),
2018-07-17 19:13:52 -04:00
};