Skip to content

Commit

Permalink
feat: support multiple config in .fatherrc.js (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc authored Sep 1, 2019
1 parent fb946f2 commit 6dfb059
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 69 deletions.
125 changes: 65 additions & 60 deletions packages/father-build/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,34 @@ 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,
files: ['src/index.tsx', 'src/index.ts', 'src/index.jsx', 'src/index.js'],
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 }) {
Expand Down Expand Up @@ -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,
});
}
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions packages/father-build/src/fixtures/build/config-array/.fatherrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

export default [
{
cjs: 'babel',
},
{
entry: 'ui/index.js',
umd: {
name: 'foo',
minFile: false,
},
},
];
Original file line number Diff line number Diff line change
@@ -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;

}));
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

export default () => 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => 1;
21 changes: 12 additions & 9 deletions packages/father-build/src/getUserConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {};
Expand Down

0 comments on commit 6dfb059

Please sign in to comment.