static-cms/scripts/webpack.js

97 lines
2.1 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 21:25:49 -04:00
const defaultPlugins = {
friendlyErrors: () => new FriendlyErrorsWebpackPlugin()
};
2018-07-23 12:30:15 -04:00
if (isProduction) {
2018-07-23 21:25:49 -04:00
return {
2018-07-23 12:30:15 -04:00
...defaultPlugins,
2018-07-23 21:25:49 -04:00
sourcemap: (config = {}) => new webpack.SourceMapDevToolPlugin({
2018-07-23 12:30:15 -04:00
filename: '[file].map',
moduleFilenameTemplate: info => path.posix.normalize(`../src/${info.resourcePath}`),
noSources: true,
}),
2018-07-23 21:25:49 -04:00
};
2018-07-23 12:30:15 -04:00
}
2018-07-23 21:25:49 -04:00
return {
2018-07-23 12:30:15 -04:00
...defaultPlugins,
2018-07-23 21:25:49 -04:00
}
2018-07-23 12:30:15 -04:00
};
const stats = () => {
if (isProduction) {
return {
builtAt: false,
colors: true,
entrypoints: false,
hash: false,
modules: false,
timings: false,
version: false,
};
}
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-23 21:25:49 -04:00
plugins: Object.values(plugins()).map(plugin => plugin),
2018-07-23 12:30:15 -04:00
devtool: !isProduction && '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) => {
const peerDeps = Object.keys(pkg.peerDependencies || {});
2018-07-23 12:14:53 -04:00
const isPeerDep = dep => new RegExp(`^${dep}($|/)`).test(request);
2018-07-17 19:13:52 -04:00
return peerDeps.some(isPeerDep) ? cb(null, request) : cb();
},
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
};