From 4d6b1214d3d42619727a727a53aab529d67fc55d Mon Sep 17 00:00:00 2001 From: wadezhan Date: Sat, 22 May 2021 00:57:50 +0800 Subject: [PATCH] feat: support alias config MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 基于 @rollup/plugin-alias 支持路径别名配置项 --- README.md | 20 ++++++++++++++++++++ packages/father-build/package.json | 1 + packages/father-build/src/getRollupConfig.ts | 4 ++++ packages/father-build/src/schema.ts | 3 +++ packages/father-build/src/types.d.ts | 1 + packages/father-build/yarn.lock | 12 ++++++++++++ 6 files changed, 41 insertions(+) diff --git a/README.md b/README.md index 3a930131..bf56a7f7 100644 --- a/README.md +++ b/README.md @@ -473,6 +473,26 @@ export default { } ``` +### alias + +配置需要替换的别名路径,基于 [@rollup/plugin-alias](https://github.com/rollup/plugins/tree/master/packages/alias#readme)。 + +- Type: `Object` +- Default: `{}` + +比如: + +```js +import path from 'path'; + +// dumi 中引入别名路径 +export default { + alias: { + entries: [{ find: '@', replacement: path.join(__dirname, 'src') }], + }, +}; +``` + #### inject 配置需要替换成依赖引用的全局变量 Map,基于 [rollup-plugin-inject](https://github.com/rollup/rollup-plugin-inject)。 diff --git a/packages/father-build/package.json b/packages/father-build/package.json index c424c274..43bed5c6 100644 --- a/packages/father-build/package.json +++ b/packages/father-build/package.json @@ -24,6 +24,7 @@ "@babel/preset-typescript": "7.12.1", "@babel/register": "7.12.1", "@lerna/project": "^3.21.0", + "@rollup/plugin-alias": "^3.1.2", "@rollup/plugin-babel": "5.2.1", "@rollup/plugin-commonjs": "16.0.0", "@rollup/plugin-inject": "4.0.2", diff --git a/packages/father-build/src/getRollupConfig.ts b/packages/father-build/src/getRollupConfig.ts index 2544c32d..d813f0eb 100644 --- a/packages/father-build/src/getRollupConfig.ts +++ b/packages/father-build/src/getRollupConfig.ts @@ -8,6 +8,7 @@ import commonjs from '@rollup/plugin-commonjs'; import nodeResolve from '@rollup/plugin-node-resolve'; import inject, { RollupInjectOptions } from '@rollup/plugin-inject'; import babel, { RollupBabelInputPluginOptions } from '@rollup/plugin-babel'; +import alias, { RollupAliasOptions } from '@rollup/plugin-alias'; import postcss from 'rollup-plugin-postcss'; import { terser } from 'rollup-plugin-terser'; import typescript2 from 'rollup-plugin-typescript2'; @@ -54,6 +55,7 @@ export default function(opts: IGetRollupConfigOpts): RollupOptions[] { runtimeHelpers: runtimeHelpersOpts, replace: replaceOpts, inject: injectOpts, + alias: aliasOpts, extraExternals = [], externalsExclude = [], nodeVersion, @@ -147,6 +149,8 @@ export default function(opts: IGetRollupConfigOpts): RollupOptions[] { function getPlugins(opts = {} as { minCSS: boolean; }) { const { minCSS } = opts; return [ + // @see https://github.com/umijs/father/issues/356 + ...(aliasOpts ? [alias(aliasOpts as RollupAliasOptions)] : []), url(), svgr(), postcss({ diff --git a/packages/father-build/src/schema.ts b/packages/father-build/src/schema.ts index 235faf05..4d0bb512 100644 --- a/packages/father-build/src/schema.ts +++ b/packages/father-build/src/schema.ts @@ -126,6 +126,9 @@ export default { inject: { type: 'object', }, + alias: { + type: 'object', + }, lessInRollupMode: { type: 'object' }, diff --git a/packages/father-build/src/types.d.ts b/packages/father-build/src/types.d.ts index dbc4d9c1..fe4ed833 100644 --- a/packages/father-build/src/types.d.ts +++ b/packages/father-build/src/types.d.ts @@ -44,6 +44,7 @@ export interface IBundleOptions { extractCSS?: boolean; injectCSS?: boolean | ((varname: string, filename: string) => string); inject?: Object; + alias?: Object; autoprefixer?: Object; include?: string | RegExp; runtimeHelpers?: boolean; diff --git a/packages/father-build/yarn.lock b/packages/father-build/yarn.lock index eab440c4..5df2fa33 100644 --- a/packages/father-build/yarn.lock +++ b/packages/father-build/yarn.lock @@ -1102,6 +1102,13 @@ resolved "https://registry.yarnpkg.com/@nodelib/fs.stat/-/fs.stat-1.1.3.tgz#2b5a3ab3f918cca48a8c754c08168e3f03eba61b" integrity sha512-shAmDyaQC4H92APFoIaVDHCx5bStIocgvbwQyxPRrbUY20V1EYTbSDchWbuwlMG3V17cprZhA6+78JfB+3DTPw== +"@rollup/plugin-alias@^3.1.2": + version "3.1.2" + resolved "https://mirrors.tencent.com/npm/@rollup%2fplugin-alias/-/plugin-alias-3.1.2.tgz#c585b05be4a7782d269c69d13def56f44e417772" + integrity sha512-wzDnQ6v7CcoRzS0qVwFPrFdYA4Qlr+ookA217Y2Z3DPZE1R8jrFNM3jvGgOf6o6DMjbnQIn5lCIJgHPe1Bt3uw== + dependencies: + slash "^3.0.0" + "@rollup/plugin-babel@5.2.1": version "5.2.1" resolved "https://registry.yarnpkg.com/@rollup/plugin-babel/-/plugin-babel-5.2.1.tgz#20fc8f8864dc0eaa1c5578408459606808f72924" @@ -5618,6 +5625,11 @@ slash@^2.0.0: resolved "https://registry.yarnpkg.com/slash/-/slash-2.0.0.tgz#de552851a1759df3a8f206535442f5ec4ddeab44" integrity sha512-ZYKh3Wh2z1PpEXWr0MpSBZ0V6mZHAQfYevttO11c51CaWjGTaadiKZ+wVt1PbMlDV5qhMFslpZCemhwOK7C89A== +slash@^3.0.0: + version "3.0.0" + resolved "http://mirrors.tencent.com/npm/slash/-/slash-3.0.0.tgz#6539be870c165adbd5240220dbe361f1bc4d4634" + integrity sha512-g9Q1haeby36OSStwb4ntCGGGaKsaVSjQ68fBxoQcutl5fS1vuY18H3wSt3jFyFtrkx+Kz0V1G85A4MyAdDMi2Q== + snapdragon-node@^2.0.1: version "2.1.1" resolved "https://registry.yarnpkg.com/snapdragon-node/-/snapdragon-node-2.1.1.tgz#6c175f86ff14bdb0724563e8f3c1b021a286853b"