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');
|
2019-12-16 12:17:37 -05:00
|
|
|
const { flatMap } = require('lodash');
|
2019-03-16 15:44:29 -07:00
|
|
|
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';
|
2019-09-30 14:43:06 -04:00
|
|
|
const isTest = process.env.NODE_ENV === 'test';
|
2018-07-23 12:30:15 -04:00
|
|
|
|
2021-02-08 20:01:21 +02:00
|
|
|
function moduleNameToPath(libName) {
|
|
|
|
return path.resolve(__dirname, '..', 'node_modules', libName);
|
|
|
|
}
|
|
|
|
|
|
|
|
function rules() {
|
|
|
|
return {
|
|
|
|
js: () => ({
|
|
|
|
test: /\.(ts|js)x?$/,
|
|
|
|
exclude: /node_modules/,
|
|
|
|
use: {
|
|
|
|
loader: 'babel-loader',
|
|
|
|
options: {
|
|
|
|
rootMode: 'upward',
|
|
|
|
},
|
2018-07-23 21:25:49 -04:00
|
|
|
},
|
2021-02-08 20:01:21 +02:00
|
|
|
}),
|
|
|
|
css: () => [
|
|
|
|
{
|
|
|
|
test: /\.css$/,
|
|
|
|
include: ['ol', 'redux-notifications', 'react-datetime', 'codemirror'].map(
|
|
|
|
moduleNameToPath,
|
|
|
|
),
|
|
|
|
use: ['to-string-loader', 'css-loader'],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
svg: () => ({
|
|
|
|
test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
|
|
|
|
exclude: [/node_modules/],
|
|
|
|
use: 'svg-inline-loader',
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
}
|
2018-07-23 21:25:49 -04:00
|
|
|
|
2021-02-08 20:01:21 +02:00
|
|
|
function 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
|
|
|
};
|
2021-02-08 20:01:21 +02:00
|
|
|
}
|
2018-07-23 12:30:15 -04:00
|
|
|
|
2021-02-08 20:01:21 +02:00
|
|
|
function stats() {
|
2018-07-23 12:30:15 -04:00
|
|
|
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,
|
|
|
|
};
|
2021-02-08 20:01:21 +02:00
|
|
|
}
|
2018-07-23 12:30:15 -04:00
|
|
|
|
2019-03-16 15:44:29 -07:00
|
|
|
const umdPath = path.resolve(process.cwd(), 'dist');
|
|
|
|
const umdDirPath = path.resolve(process.cwd(), 'dist/umd');
|
|
|
|
const cjsPath = path.resolve(process.cwd(), 'dist/cjs');
|
2021-02-08 20:01:21 +02:00
|
|
|
|
|
|
|
function targetOutputs() {
|
2019-03-16 15:44:29 -07:00
|
|
|
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',
|
|
|
|
},
|
|
|
|
};
|
2021-02-08 20:01:21 +02:00
|
|
|
}
|
2019-03-16 15:44:29 -07:00
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
2021-02-08 20:01:21 +02:00
|
|
|
function baseConfig({ target = isProduction ? 'umd' : 'umddir' } = {}) {
|
|
|
|
return {
|
|
|
|
context: process.cwd(),
|
|
|
|
mode: isProduction ? 'production' : 'development',
|
|
|
|
entry: './src',
|
|
|
|
output: targetOutputs()[target],
|
|
|
|
module: {
|
|
|
|
rules: flatMap(Object.values(rules()), rule => rule()),
|
2020-05-04 14:47:55 +03:00
|
|
|
},
|
2021-02-08 20:01:21 +02:00
|
|
|
resolve: {
|
|
|
|
extensions: ['.ts', '.tsx', '.js', '.json'],
|
|
|
|
alias: {
|
|
|
|
moment$: 'moment/moment.js',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
plugins: Object.values(plugins()).map(plugin => plugin()),
|
|
|
|
devtool: isTest ? '' : 'source-map',
|
|
|
|
target: 'web',
|
2018-07-23 21:25:49 -04:00
|
|
|
|
2021-02-08 20:01:21 +02:00
|
|
|
/**
|
|
|
|
* Exclude peer dependencies from package bundles.
|
|
|
|
*/
|
|
|
|
externals:
|
|
|
|
target.substr(0, 3) === 'umd'
|
|
|
|
? umdExternals
|
|
|
|
: (context, request, cb) => {
|
|
|
|
const externals = Object.keys(pkg.peerDependencies || {});
|
2018-07-23 21:25:49 -04:00
|
|
|
|
2021-02-08 20:01:21 +02:00
|
|
|
function isPeerDep(dep) {
|
|
|
|
return new RegExp(`^${dep}($|/)`).test(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
return externals.some(isPeerDep) ? cb(null, request) : cb();
|
|
|
|
},
|
|
|
|
stats: stats(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function getConfig({ baseOnly = false } = {}) {
|
2019-03-16 15:44:29 -07:00
|
|
|
if (baseOnly) {
|
|
|
|
// netlify-cms build
|
|
|
|
return baseConfig({ target: 'umd' });
|
|
|
|
}
|
|
|
|
return [baseConfig({ target: 'umd' })];
|
2021-02-08 20:01:21 +02:00
|
|
|
}
|
2019-03-16 15:44:29 -07:00
|
|
|
|
2018-07-23 21:25:49 -04:00
|
|
|
module.exports = {
|
|
|
|
getConfig,
|
|
|
|
rules: rules(),
|
|
|
|
plugins: plugins(),
|
2018-07-17 19:13:52 -04:00
|
|
|
};
|