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

fix(module-tools): add namespace when resolve result is false #4856

Merged
merged 1 commit into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions .changeset/cold-timers-teach.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@modern-js/module-tools': patch
---

fix(module-tools): Add namespace when resolve result is false. Build failed in windows because `\empty-stub` is treated as a non-absolute path by esbuild.
fix(module-tools): 当 resovle 的结果是 false 时,添加 namespace,因为在 windows 系统下,`\empty-stub` 会被 esbuild 当成一个非绝对路径导致构建失败。
17 changes: 9 additions & 8 deletions packages/solutions/module-tools/src/builder/esbuild/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { dirname, resolve, extname } from 'path';
import { dirname, resolve, extname, sep } from 'path';
import module from 'module';
import { ImportKind, Loader, Plugin } from 'esbuild';
import { fs, isString } from '@modern-js/utils';
Expand Down Expand Up @@ -194,8 +194,9 @@ export const adapterPlugin = (compiler: ICompiler): Plugin => {
// we may get false when resolve browser field, in this case, we set it a empty object
debugResolve('resolve false:', args);
return {
path: '/empty-stub',
path: `${sep}empty-stub`,
sideEffects: false,
namespace: 'resolve-false',
};
}
const sideEffects = await getSideEffects(resultPath, isExternal);
Expand All @@ -220,6 +221,12 @@ export const adapterPlugin = (compiler: ICompiler): Plugin => {
};
}

if (args.namespace === 'resolve-false') {
return {
contents: 'module.exports = {}',
};
}

if (args.suffix) {
args.path += args.suffix;
}
Expand All @@ -228,12 +235,6 @@ export const adapterPlugin = (compiler: ICompiler): Plugin => {
return;
}

if (args.path === '/empty-stub') {
return {
contents: 'module.exports = {}',
};
}

compiler.addWatchFile(args.path);
let result = await compiler.hooks.load.promise(args);
if (!result) {
Expand Down