Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support npm alias name for prebundle #663

Merged
merged 5 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/prebundler/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export function getConfig(opts: {
pkg: depPkg,
output: path.resolve(
opts.cwd,
`${output || DEFAULT_OUTPUT_DIR}/${depPkg.name}/index.js`,
`${output || DEFAULT_OUTPUT_DIR}/${depName}/index.js`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

直接使用用户的配置值可能会存在 Breaking Change,目前想到这两种 case:

  1. 配置值可能是 NPM 包的文件路径(类似 pkg/lib/other
  2. 配置值可能是项目的本地文件(类似 Umi 仓库中 bundler-utils 子包的情况,虽然没有直接使用 father)

想到的解法你看看是否合适,创建 getDepPkgName(depName, depPkg) 的工具方法,有两层逻辑:

  1. 如果 depName 不是绝对路径或 . 开头的相对路径,则尝试截取 depName 中包名的部分,比如 pkg/lib/other => pkg@org/pkg/index => @org/pkg
  2. 反之兜底到 depPkg.name,同时需要看下 issue 里提到的 minimatch 获取 pkg 失败的问题,按理说 getDepPkg 有使用 pkgUp 处理,应该能拿到才是

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

很好我处理下,我没留意还有其他用法

),
};

Expand All @@ -167,7 +167,7 @@ export function getConfig(opts: {
}

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

// process extraDtsDeps config
Expand Down
5 changes: 5 additions & 0 deletions tests/fixtures/prebundle/npm-alias/.fatherrc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
prebundle: {
deps: ['a', 'a-alias'],
},
};
12 changes: 12 additions & 0 deletions tests/fixtures/prebundle/npm-alias/expect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default (files: Record<string, string>) => {
expect(Object.keys(files)).toMatchInlineSnapshot(`
Array [
"a/index.d.ts",
"a/index.js",
"a/package.json",
"a-alias/index.d.ts",
"a-alias/index.js",
"a-alias/package.json",
]
`);
};

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/fixtures/prebundle/npm-alias/node_modules/a/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions tests/fixtures/prebundle/npm-alias/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "npm-alias",
"version": "0.0.0",
"devDependencies": {
"a": "0.0.1",
"a-alias": "npm:[email protected]"
}
}