refactor: convert function expressions to declarations (#4926)

This commit is contained in:
Vladislav Shkodin
2021-02-08 20:01:21 +02:00
committed by GitHub
parent c0236536dd
commit 141a2eba56
241 changed files with 3444 additions and 2933 deletions

View File

@ -6,8 +6,8 @@ const path = require('path');
* netlify-cms-something to NetlifyCmsSomething
* @param {} string
*/
const toGlobalName = name =>
`${name}`
function toGlobalName(name) {
return `${name}`
.replace(new RegExp(/[-_/]+/, 'g'), ' ')
.replace(new RegExp(/[^\w\s]/, 'g'), '')
.replace(
@ -16,6 +16,7 @@ const toGlobalName = name =>
)
.replace(new RegExp(/\s/, 'g'), '')
.replace(new RegExp(/\w/), s => s.toUpperCase());
}
const packages = fs.readdirSync(path.resolve(__dirname, '../packages'));

View File

@ -7,42 +7,49 @@ const pkg = require(path.join(process.cwd(), 'package.json'));
const isProduction = process.env.NODE_ENV === 'production';
const isTest = process.env.NODE_ENV === 'test';
const moduleNameToPath = libName => path.resolve(__dirname, '..', 'node_modules', libName);
const rules = () => ({
js: () => ({
test: /\.(ts|js)x?$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
rootMode: 'upward',
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',
},
},
},
}),
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',
}),
});
}),
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',
}),
};
}
const plugins = () => {
function plugins() {
return {
ignoreEsprima: () => new webpack.IgnorePlugin(/^esprima$/, /js-yaml/),
ignoreMomentOptionalDeps: () => new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
friendlyErrors: () => new FriendlyErrorsWebpackPlugin(),
};
};
}
const stats = () => {
function stats() {
if (isProduction) {
return {
builtAt: false,
@ -60,12 +67,13 @@ const stats = () => {
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 = () => {
function targetOutputs() {
console.log(`Building [${pkg.name}, library: ${toGlobalName(pkg.name)}]`);
return {
umd: {
@ -93,7 +101,7 @@ const targetOutputs = () => {
libraryTarget: 'window',
},
};
};
}
const umdExternals = Object.keys(pkg.peerDependencies || {}).reduce((previous, key) => {
if (!externals[key]) throw `Missing external [${key}]`;
@ -106,45 +114,51 @@ const umdExternals = Object.keys(pkg.peerDependencies || {}).reduce((previous, k
* getting multiple configs and add the new output in targetOutputs if needed.
* Default: umd
*/
const baseConfig = ({ target = isProduction ? 'umd' : 'umddir' } = {}) => ({
context: process.cwd(),
mode: isProduction ? 'production' : 'development',
entry: './src',
output: targetOutputs()[target],
module: {
rules: flatMap(Object.values(rules()), rule => rule()),
},
resolve: {
extensions: ['.ts', '.tsx', '.js', '.json'],
alias: {
moment$: 'moment/moment.js',
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()),
},
},
plugins: Object.values(plugins()).map(plugin => plugin()),
devtool: isTest ? '' : 'source-map',
target: 'web',
resolve: {
extensions: ['.ts', '.tsx', '.js', '.json'],
alias: {
moment$: 'moment/moment.js',
},
},
plugins: Object.values(plugins()).map(plugin => plugin()),
devtool: isTest ? '' : 'source-map',
target: 'web',
/**
* 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();
},
stats: stats(),
});
/**
* Exclude peer dependencies from package bundles.
*/
externals:
target.substr(0, 3) === 'umd'
? umdExternals
: (context, request, cb) => {
const externals = Object.keys(pkg.peerDependencies || {});
const getConfig = ({ baseOnly = false } = {}) => {
function isPeerDep(dep) {
return new RegExp(`^${dep}($|/)`).test(request);
}
return externals.some(isPeerDep) ? cb(null, request) : cb();
},
stats: stats(),
};
}
function getConfig({ baseOnly = false } = {}) {
if (baseOnly) {
// netlify-cms build
return baseConfig({ target: 'umd' });
}
return [baseConfig({ target: 'umd' })];
};
}
module.exports = {
getConfig,