From 6dfb059431effa64accce615af69c07f96963be1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?chencheng=20=28=E4=BA=91=E8=B0=A6=29?= Date: Sun, 1 Sep 2019 19:27:46 +0800 Subject: [PATCH] feat: support multiple config in .fatherrc.js (#96) --- packages/father-build/src/build.ts | 125 +++++++++--------- .../.fatherrc.js | 0 .../expected/lib/foo.js | 0 .../expected/lib/index.js | 0 .../src/foo.js | 0 .../src/index.js | 0 .../fixtures/build/config-array/.fatherrc.js | 13 ++ .../build/config-array/expected/index.umd.js | 13 ++ .../build/config-array/expected/lib/index.js | 12 ++ .../fixtures/build/config-array/src/index.js | 2 + .../fixtures/build/config-array/ui/index.js | 1 + packages/father-build/src/getUserConfig.ts | 21 +-- 12 files changed, 118 insertions(+), 69 deletions(-) rename packages/father-build/src/fixtures/build/{babel-cjs-lazy-only => babel-cjs-lazy}/.fatherrc.js (100%) rename packages/father-build/src/fixtures/build/{babel-cjs-lazy-only => babel-cjs-lazy}/expected/lib/foo.js (100%) rename packages/father-build/src/fixtures/build/{babel-cjs-lazy-only => babel-cjs-lazy}/expected/lib/index.js (100%) rename packages/father-build/src/fixtures/build/{babel-cjs-lazy-only => babel-cjs-lazy}/src/foo.js (100%) rename packages/father-build/src/fixtures/build/{babel-cjs-lazy-only => babel-cjs-lazy}/src/index.js (100%) create mode 100644 packages/father-build/src/fixtures/build/config-array/.fatherrc.js create mode 100644 packages/father-build/src/fixtures/build/config-array/expected/index.umd.js create mode 100644 packages/father-build/src/fixtures/build/config-array/expected/lib/index.js create mode 100644 packages/father-build/src/fixtures/build/config-array/src/index.js create mode 100644 packages/father-build/src/fixtures/build/config-array/ui/index.js diff --git a/packages/father-build/src/build.ts b/packages/father-build/src/build.ts index 0104c20a..2cdcad32 100644 --- a/packages/father-build/src/build.ts +++ b/packages/father-build/src/build.ts @@ -12,7 +12,7 @@ import registerBabel from './registerBabel'; import { getExistFile } from './utils'; import getUserConfig, { CONFIG_FILES } from './getUserConfig'; -export function getBundleOpts(opts: IOpts): IBundleOptions { +export function getBundleOpts(opts: IOpts): IBundleOptions[] { const { cwd, buildArgs = {} } = opts; const entry = getExistFile({ cwd, @@ -20,23 +20,26 @@ export function getBundleOpts(opts: IOpts): IBundleOptions { returnRelative: true, }); const userConfig = getUserConfig({ cwd }); - const bundleOpts = merge( - { - entry, - }, - userConfig, - buildArgs, - ); - - // Support config esm: 'rollup' and cjs: 'rollup' - if (typeof bundleOpts.esm === 'string') { - bundleOpts.esm = { type: bundleOpts.esm }; - } - if (typeof bundleOpts.cjs === 'string') { - bundleOpts.cjs = { type: bundleOpts.cjs }; - } + const userConfigs = Array.isArray(userConfig) ? userConfig : [userConfig]; + return (userConfigs as any).map(userConfig => { + const bundleOpts = merge( + { + entry, + }, + userConfig, + buildArgs, + ); - return bundleOpts; + // Support config esm: 'rollup' and cjs: 'rollup' + if (typeof bundleOpts.esm === 'string') { + bundleOpts.esm = { type: bundleOpts.esm }; + } + if (typeof bundleOpts.cjs === 'string') { + bundleOpts.cjs = { type: bundleOpts.cjs }; + } + + return bundleOpts; + }); } function validateBundleOpts(bundleOpts: IBundleOptions, { cwd, rootPath }) { @@ -103,58 +106,60 @@ export async function build(opts: IOpts, extraOpts: IExtraBuildOpts = {}) { } // Get user config - const bundleOpts = getBundleOpts(opts); - validateBundleOpts(bundleOpts, { cwd, rootPath }); - - // Clean dist - log(`Clean dist directory`); - rimraf.sync(join(cwd, 'dist')); - - // Build umd - if (bundleOpts.umd) { - log(`Build umd`); - await rollup({ - cwd, - type: 'umd', - entry: bundleOpts.entry, - watch, - bundleOpts, - }); - } + const bundleOptsArray = getBundleOpts(opts); + for (const bundleOpts of bundleOptsArray) { + validateBundleOpts(bundleOpts, { cwd, rootPath }); - // Build cjs - if (bundleOpts.cjs) { - const cjs = bundleOpts.cjs as IBundleTypeOutput; - log(`Build cjs with ${cjs.type}`); - if (cjs.type === 'babel') { - await babel({ cwd, rootPath, watch, type: 'cjs', bundleOpts }); - } else { + // Clean dist + log(`Clean dist directory`); + rimraf.sync(join(cwd, 'dist')); + + // Build umd + if (bundleOpts.umd) { + log(`Build umd`); await rollup({ cwd, - type: 'cjs', + type: 'umd', entry: bundleOpts.entry, watch, bundleOpts, }); } - } - // Build esm - if (bundleOpts.esm) { - const esm = bundleOpts.esm as IEsm; - log(`Build esm with ${esm.type}`); - const importLibToEs = esm && esm.importLibToEs; - if (esm && esm.type === 'babel') { - await babel({ cwd, rootPath, watch, type: 'esm', importLibToEs, bundleOpts }); - } else { - await rollup({ - cwd, - type: 'esm', - entry: bundleOpts.entry, - importLibToEs, - watch, - bundleOpts, - }); + // Build cjs + if (bundleOpts.cjs) { + const cjs = bundleOpts.cjs as IBundleTypeOutput; + log(`Build cjs with ${cjs.type}`); + if (cjs.type === 'babel') { + await babel({ cwd, rootPath, watch, type: 'cjs', bundleOpts }); + } else { + await rollup({ + cwd, + type: 'cjs', + entry: bundleOpts.entry, + watch, + bundleOpts, + }); + } + } + + // Build esm + if (bundleOpts.esm) { + const esm = bundleOpts.esm as IEsm; + log(`Build esm with ${esm.type}`); + const importLibToEs = esm && esm.importLibToEs; + if (esm && esm.type === 'babel') { + await babel({ cwd, rootPath, watch, type: 'esm', importLibToEs, bundleOpts }); + } else { + await rollup({ + cwd, + type: 'esm', + entry: bundleOpts.entry, + importLibToEs, + watch, + bundleOpts, + }); + } } } } diff --git a/packages/father-build/src/fixtures/build/babel-cjs-lazy-only/.fatherrc.js b/packages/father-build/src/fixtures/build/babel-cjs-lazy/.fatherrc.js similarity index 100% rename from packages/father-build/src/fixtures/build/babel-cjs-lazy-only/.fatherrc.js rename to packages/father-build/src/fixtures/build/babel-cjs-lazy/.fatherrc.js diff --git a/packages/father-build/src/fixtures/build/babel-cjs-lazy-only/expected/lib/foo.js b/packages/father-build/src/fixtures/build/babel-cjs-lazy/expected/lib/foo.js similarity index 100% rename from packages/father-build/src/fixtures/build/babel-cjs-lazy-only/expected/lib/foo.js rename to packages/father-build/src/fixtures/build/babel-cjs-lazy/expected/lib/foo.js diff --git a/packages/father-build/src/fixtures/build/babel-cjs-lazy-only/expected/lib/index.js b/packages/father-build/src/fixtures/build/babel-cjs-lazy/expected/lib/index.js similarity index 100% rename from packages/father-build/src/fixtures/build/babel-cjs-lazy-only/expected/lib/index.js rename to packages/father-build/src/fixtures/build/babel-cjs-lazy/expected/lib/index.js diff --git a/packages/father-build/src/fixtures/build/babel-cjs-lazy-only/src/foo.js b/packages/father-build/src/fixtures/build/babel-cjs-lazy/src/foo.js similarity index 100% rename from packages/father-build/src/fixtures/build/babel-cjs-lazy-only/src/foo.js rename to packages/father-build/src/fixtures/build/babel-cjs-lazy/src/foo.js diff --git a/packages/father-build/src/fixtures/build/babel-cjs-lazy-only/src/index.js b/packages/father-build/src/fixtures/build/babel-cjs-lazy/src/index.js similarity index 100% rename from packages/father-build/src/fixtures/build/babel-cjs-lazy-only/src/index.js rename to packages/father-build/src/fixtures/build/babel-cjs-lazy/src/index.js diff --git a/packages/father-build/src/fixtures/build/config-array/.fatherrc.js b/packages/father-build/src/fixtures/build/config-array/.fatherrc.js new file mode 100644 index 00000000..695d856f --- /dev/null +++ b/packages/father-build/src/fixtures/build/config-array/.fatherrc.js @@ -0,0 +1,13 @@ + +export default [ + { + cjs: 'babel', + }, + { + entry: 'ui/index.js', + umd: { + name: 'foo', + minFile: false, + }, + }, +]; diff --git a/packages/father-build/src/fixtures/build/config-array/expected/index.umd.js b/packages/father-build/src/fixtures/build/config-array/expected/index.umd.js new file mode 100644 index 00000000..c701118a --- /dev/null +++ b/packages/father-build/src/fixtures/build/config-array/expected/index.umd.js @@ -0,0 +1,13 @@ +(function (global, factory) { + typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : + typeof define === 'function' && define.amd ? define(factory) : + (global = global || self, global.foo = factory()); +}(this, function () { 'use strict'; + + var index = (function () { + return 1; + }); + + return index; + +})); diff --git a/packages/father-build/src/fixtures/build/config-array/expected/lib/index.js b/packages/father-build/src/fixtures/build/config-array/expected/lib/index.js new file mode 100644 index 00000000..2e3e2ae4 --- /dev/null +++ b/packages/father-build/src/fixtures/build/config-array/expected/lib/index.js @@ -0,0 +1,12 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = void 0; + +var _default = function _default() { + return 1; +}; + +exports.default = _default; \ No newline at end of file diff --git a/packages/father-build/src/fixtures/build/config-array/src/index.js b/packages/father-build/src/fixtures/build/config-array/src/index.js new file mode 100644 index 00000000..be26f6dd --- /dev/null +++ b/packages/father-build/src/fixtures/build/config-array/src/index.js @@ -0,0 +1,2 @@ + +export default () => 1; diff --git a/packages/father-build/src/fixtures/build/config-array/ui/index.js b/packages/father-build/src/fixtures/build/config-array/ui/index.js new file mode 100644 index 00000000..807a7975 --- /dev/null +++ b/packages/father-build/src/fixtures/build/config-array/ui/index.js @@ -0,0 +1 @@ +export default () => 1; diff --git a/packages/father-build/src/getUserConfig.ts b/packages/father-build/src/getUserConfig.ts index dfbe72f2..440edcad 100644 --- a/packages/father-build/src/getUserConfig.ts +++ b/packages/father-build/src/getUserConfig.ts @@ -34,20 +34,23 @@ export default function({ cwd }): IBundleOptions { } const userConfig = testDefault(require(configFile)); // eslint-disable-line + const userConfigs = Array.isArray(userConfig) ? userConfig : [userConfig]; + userConfigs.forEach(userConfig => { const ajv = new AJV({ allErrors: true }); - const isValid = ajv.validate(schema, userConfig); - if (!isValid) { - const errors = ajv.errors.map(({ dataPath, message }, index) => { - return `${index + 1}. ${dataPath}${dataPath ? ' ' : ''}${message}`; - }); - throw new Error( - ` + const isValid = ajv.validate(schema, userConfig); + if (!isValid) { + const errors = ajv.errors.map(({ dataPath, message }, index) => { + return `${index + 1}. ${dataPath}${dataPath ? ' ' : ''}${message}`; + }); + throw new Error( + ` Invalid options in ${slash(relative(cwd, configFile))} ${errors.join('\n')} `.trim(), - ); - } + ); + } + }); return userConfig; } else { return {};