static-cms/scripts/webpack.js

142 lines
3.7 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 { toGlobalName, externals } = require('./externals');
2018-07-17 19:13:52 -04:00
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`),
},
},
}),
css: () => ({
test: /\.css$/,
include: [/(ol|redux-notifications|react-datetime)/],
use: ['to-string-loader', 'css-loader'],
}),
2018-07-23 21:25:49 -04:00
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-25 11:00:55 -04:00
ignoreEsprima: () => new webpack.IgnorePlugin(/^esprima$/, /js-yaml/),
ignoreMomentOptionalDeps: () => new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
friendlyErrors: () => new FriendlyErrorsWebpackPlugin(),
2018-07-23 21:25:49 -04:00
};
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,
};
};
const umdPath = path.resolve(process.cwd(), 'dist');
const umdDirPath = path.resolve(process.cwd(), 'dist/umd');
const cjsPath = path.resolve(process.cwd(), 'dist/cjs');
const targetOutputs = () => {
console.log(`Building [${pkg.name}, library: ${toGlobalName(pkg.name)}]`);
return {
umd: {
path: umdPath,
filename: `${pkg.name}.js`,
library: toGlobalName(pkg.name),
libraryTarget: 'umd',
libraryExport: toGlobalName(pkg.name),
umdNamedDefine: true,
globalObject: 'window',
},
umddir: {
path: umdDirPath,
filename: `index.js`,
library: toGlobalName(pkg.name),
libraryTarget: 'umd',
libraryExport: toGlobalName(pkg.name),
umdNamedDefine: true,
globalObject: 'window',
},
cjs: {
path: cjsPath,
filename: 'index.js',
library: toGlobalName(pkg.name),
libraryTarget: 'window',
},
};
};
const umdExternals = Object.keys(pkg.peerDependencies || {}).reduce((previous, key) => {
if (!externals[key]) throw `Missing external [${key}]`;
previous[key] = externals[key] || null;
return previous;
}, {});
/**
* Use [getConfig({ target:'umd' }), getConfig({ target:'cjs' })] for
* getting multiple configs and add the new output in targetOutputs if needed.
* Default: umd
*/
const baseConfig = ({ target = isProduction ? 'umd' : 'umddir' } = {}) => ({
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: targetOutputs()[target],
2018-07-17 19:13:52 -04:00
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.
*/
externals:
target.substr(0, 3) === 'umd'
? umdExternals
: (context, request, cb) => {
const externals = Object.keys(pkg.peerDependencies || {});
const isPeerDep = dep => new RegExp(`^${dep}($|/)`).test(request);
return externals.some(isPeerDep) ? cb(null, request) : cb();
},
2018-07-23 12:30:15 -04:00
stats: stats(),
2018-07-23 21:25:49 -04:00
});
const getConfig = ({ baseOnly = false } = {}) => {
if (baseOnly) {
// netlify-cms build
return baseConfig({ target: 'umd' });
}
return [baseConfig({ target: 'umd' })];
};
2018-07-23 21:25:49 -04:00
module.exports = {
getConfig,
rules: rules(),
plugins: plugins(),
2018-07-17 19:13:52 -04:00
};