Skip to content

Commit

Permalink
Fix alias dirname error (umijs#171)
Browse files Browse the repository at this point in the history
* fix: alias dirname error

* chore: code style

* feat: getRealFile

* chore: code style
  • Loading branch information
xiaohuoni authored Nov 10, 2021
1 parent 79be231 commit d525119
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 33 deletions.
1 change: 1 addition & 0 deletions packages/bundler-esbuild/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"dependencies": {
"@umijs/bundler-utils": "4.0.0-beta.5",
"@umijs/utils": "4.0.0-beta.5",
"enhanced-resolve": "5.8.3",
"less": "4.1.2"
},
"devDependencies": {
Expand Down
12 changes: 10 additions & 2 deletions packages/bundler-esbuild/src/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@ const EXISTS = '1';

const expects: Record<string, Function> = {
alias({ files }: IOpts) {
expect(files['index.js']).toContain(`var a = "react";`);
expect(files['index.js']).toContain(`var something = "happy";`);
// import('foo') > import ('/path/foo.js')
expect(files['index.js']).toContain(`console.log("foo");`);
// import('dir') > import ('/path/dir/index.js')
expect(files['index.js']).toContain(`console.log("dir");`);
// import('dir/bar') > import ('/path/dir/bar.js')
expect(files['index.js']).toContain(`console.log("bar");`);
// import('less') > import ('/path/less.ts')
expect(files['index.js']).toContain(`console.log("less");`);
// import('less/bower') > import ('less/bower.json') 没匹配上走默认
expect(files['index.js']).toContain(`"dist/less.js"`);
},
externals({ files }: IOpts) {
expect(files['index.js']).toContain(`module.export = React;`);
Expand Down
2 changes: 1 addition & 1 deletion packages/bundler-esbuild/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export async function build(opts: IOpts) {
javascriptEnabled: true,
...opts.config.lessLoader,
}),
alias(opts.config.alias),
alias({ cwd: opts.cwd, ...opts.config.alias }),
externals(opts.config.externals),
],
define: {
Expand Down
5 changes: 3 additions & 2 deletions packages/bundler-esbuild/src/fixtures/alias/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { join } from 'path';
export default {
alias: {
react: require.resolve('./react.ts'),
some: join(__dirname,'some'),
foo: './path/foo',
dir: join(__dirname, 'path', 'dir'),
less$: join(__dirname, 'path', 'less'),
},
};
17 changes: 13 additions & 4 deletions packages/bundler-esbuild/src/fixtures/alias/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
// @ts-ignore
import react from 'react';
import less from 'less';
// @ts-ignore
import something from 'some/thing.ts';
console.log(react);
console.log(something);
import bower from 'less/bower';
// @ts-ignore
import foo from 'foo';
// @ts-ignore
import dir from 'dir';
// @ts-ignore
import bar from 'dir/bar';
console.log(less);
console.log(bower);
console.log(foo);
console.log(bar);
console.log(dir);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('bar');
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('dir');
1 change: 1 addition & 0 deletions packages/bundler-esbuild/src/fixtures/alias/path/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('foo');
1 change: 1 addition & 0 deletions packages/bundler-esbuild/src/fixtures/alias/path/less.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('less');
2 changes: 0 additions & 2 deletions packages/bundler-esbuild/src/fixtures/alias/react.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/bundler-esbuild/src/fixtures/alias/some/thing.ts

This file was deleted.

75 changes: 57 additions & 18 deletions packages/bundler-esbuild/src/plugins/alias.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,41 @@
import { Plugin } from '@umijs/bundler-utils/compiled/esbuild';
import { winPath } from '@umijs/utils';
import assert from 'assert';
import enhancedResolve from 'enhanced-resolve';
import { dirname } from 'path';

const getRealFile = async (cwd: string, file: string) => {
try {
return await resolve(cwd, file);
} catch (e) {
return null;
}
};
const resolver = enhancedResolve.create({
mainFields: ['module', 'browser', 'main'],
extensions: [
'.json',
'.js',
'.jsx',
'.ts',
'.tsx',
'.cjs',
'.mjs',
'.cjsx',
'.mjsx',
],
// TODO: support exports
// tried to add exports, but it don't work with swr
exportsFields: [],
});

async function resolve(context: string, path: string): Promise<string> {
return new Promise((resolve, reject) => {
resolver(context, path, (err: Error, result: string) =>
err ? reject(err) : resolve(result),
);
});
}

// https://esbuild.github.io/plugins/#resolve-callbacks
export default (options?: Record<string, string>): Plugin => {
Expand All @@ -10,24 +46,27 @@ export default (options?: Record<string, string>): Plugin => {
return;
}
Object.keys(options).forEach((key) => {
// import react from 'react';
onResolve({ filter: new RegExp(`^${key}$`) }, (args) => {
return {
path: winPath(args.path).replace(
new RegExp(`^${key}$`),
options[key],
),
};
});
// import abc from 'react/abc';
// import abc from 'react/abc.js';
onResolve({ filter: new RegExp(`^${key}\\/.*$`) }, (args) => {
return {
path: winPath(args.path).replace(
new RegExp(`^${key}`),
options[key],
),
};
onResolve({ filter: new RegExp(`^${key}`) }, async (args) => {
const dir = dirname(args.path);
try {
const realFile = await getRealFile(
options.cwd,
winPath(args.path).replace(new RegExp(`^${key}`), options[key]),
);
assert(realFile, `filePath not found of ${args.path}`);
return {
path: realFile,
};
} catch (error: any) {
return {
errors: [
{
text: error.message,
},
],
resolveDir: dir,
};
}
});
});
},
Expand Down
7 changes: 5 additions & 2 deletions pnpm-lock.yaml

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

0 comments on commit d525119

Please sign in to comment.