Skip to content
This repository has been archived by the owner on Mar 28, 2023. It is now read-only.

Commit

Permalink
feat: support to generate deps pre-bundle config
Browse files Browse the repository at this point in the history
  • Loading branch information
PeachScript committed Jun 17, 2022
1 parent 9655667 commit 44f633f
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/commands/prebundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { logger } from '@umijs/utils';
import { IApi } from '../types';

export default (api: IApi) => {
// to avoid conflict with schema
api.describe({ key: 'prebundle-command' });

api.registerCommand({
name: 'prebundle',
description: 'prebundle',
Expand Down
9 changes: 9 additions & 0 deletions src/features/configPlugins/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,14 @@ export function getSchemas(): Record<string, (Joi: Root) => any> {
externals: Joi.object().pattern(Joi.string(), Joi.string()),
chainWebpack: Joi.function().optional(),
}),
prebundle: (Joi) =>
Joi.object({
deps: Joi.alternatives()
.try(Joi.array().items(Joi.string()), Joi.object())
.optional(),
extraExternals: Joi.object()
.pattern(Joi.string(), Joi.string())
.optional(),
}),
};
}
74 changes: 74 additions & 0 deletions src/prebundler/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import path from 'path';
import { winPath } from '@umijs/utils';
import { IApi, IFatherPreBundleConfig } from '../types';

const DEFAULT_OUTPUT_DIR = './compiled';

export function getConfig(opts: {
userConfig: IFatherPreBundleConfig;
cwd: string;
pkg: IApi['pkg'];
}) {
// external project dependencies by default
const pkgExternals: IFatherPreBundleConfig['extraExternals'] = Object.keys(
Object.assign({}, opts.pkg.dependencies, opts.pkg.peerDependencies),
).reduce((r, dep) => ({ ...r, [dep]: dep }), {});
const depExternals: IFatherPreBundleConfig['extraExternals'] = {};
const config: { deps: Record<string, any> } = { deps: {} };
const { extraExternals = {} } = opts.userConfig;
let { deps = {} } = opts.userConfig;

// process deps config
Object.entries(deps).forEach(([dep, depConfig]) => {
// handle array deps
const depName = Array.isArray(deps) ? deps[parseInt(dep)] : dep;
depConfig = Array.isArray(deps) ? {} : depConfig;

const depEntryPath = require.resolve(depName, { paths: [opts.cwd] });

// generate build config
config.deps[depEntryPath] = {
ncc: {
minify: depConfig.minify || true,
target: 'es5',
quiet: true,
externals: {},
},
pkg: require(require.resolve(`${depName}/package.json`, {
paths: [opts.cwd],
})),
output: path.resolve(
opts.cwd,
depConfig.output || `${DEFAULT_OUTPUT_DIR}/${depName}/index.js`,
),
};

// prepare deps externals
depExternals[depName] = config.deps[depEntryPath].output;
});

// process externals for deps
Object.values(config.deps).forEach((depConfig) => {
const rltDepExternals = Object.entries(depExternals).reduce<
Record<string, string>
>((r, [dep, target]) => {
// skip self
if (dep !== depConfig.pkg.name) {
// transform dep externals path to relative path
r[dep] = winPath(
path.relative(path.dirname(depConfig.output), path.dirname(target)),
);
}

return r;
}, {});

depConfig.ncc.externals = {
...pkgExternals,
...rltDepExternals,
...extraExternals,
};
});

return config;
}
18 changes: 18 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,19 @@ export interface IFatherBundleConfig extends IFatherBaseConfig {
chainWebpack?: (args: any) => any;
}

export interface IFatherPreBundleConfig {
/**
* dependencies or entries need to be pre-bundled
*/
deps: string[] | Record<string, { output?: string; minify?: boolean }>;

/**
* extra dependencies & declarations need to be externalized
* @note all deps & package.json dependencies will be added to externals by default
*/
extraExternals: Record<string, string>;
}

export interface IFatherConfig extends IFatherBaseConfig {
/**
* bundler config (umd)
Expand Down Expand Up @@ -164,4 +177,9 @@ export interface IFatherConfig extends IFatherBaseConfig {
*/
output?: string;
};

/**
* deps pre-bundle config
*/
prebundle?: IFatherPreBundleConfig;
}

0 comments on commit 44f633f

Please sign in to comment.