Skip to content

Commit

Permalink
feat: support pre-bundle for legacy built-in-modules (#671)
Browse files Browse the repository at this point in the history
  • Loading branch information
nanianlisao committed Jul 20, 2023
1 parent d9b3356 commit 611f00c
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/prebundler/config.ts
Expand Up @@ -10,6 +10,7 @@ import {
getDepPkgPath,
getDtsInfoForPkgPath,
getNestedTypeDepsForPkg,
isBuiltInModule,
} from '../utils';

export interface IPreBundleConfig {
Expand Down Expand Up @@ -131,7 +132,8 @@ export function getConfig(opts: {
let depName = Array.isArray(deps) ? deps[parseInt(dep)] : dep;
depConfig = Array.isArray(deps) ? {} : depConfig;

const depEntryPath = require.resolve(depName, { paths: [opts.cwd] });
const depPath = isBuiltInModule(depName) ? `${depName}/` : depName;
const depEntryPath = require.resolve(depPath, { paths: [opts.cwd] });
const depPkgPath = getDepPkgPath(depName, opts.cwd);
const depTypeInfo =
depConfig.dts !== false ? getDtsInfoForPkgPath(depPkgPath) : null;
Expand Down
7 changes: 7 additions & 0 deletions src/utils.ts
@@ -1,5 +1,6 @@
import { chalk, logger as umiLogger, pkgUp } from '@umijs/utils';
import Cache from 'file-system-cache';
import { builtinModules } from 'module';
import path, { isAbsolute } from 'path';
import { CACHE_PATH } from './constants';
import { IApi } from './types';
Expand Down Expand Up @@ -69,6 +70,12 @@ export function getDtsInfoForPkgPath(pkgPath: string) {
return info;
}

/**
* Determine if it is a native module
*/
export const isBuiltInModule = (pkgName: string) =>
builtinModules.includes(pkgName.replace(/^node:/, ''));

/**
* get package.json path for specific NPM package
* @see https://github.com/nodejs/node/issues/33460
Expand Down
5 changes: 5 additions & 0 deletions tests/fixtures/prebundle/builtin-module/.fatherrc.ts
@@ -0,0 +1,5 @@
export default {
prebundle: {
deps: ['url'],
},
};
5 changes: 5 additions & 0 deletions tests/fixtures/prebundle/builtin-module/expect.ts
@@ -0,0 +1,5 @@
export default (files: Record<string, string>) => {
// check url/index.js is exists
expect(files['url/index.js']).not.toBeUndefined();
expect(files['url/index.d.ts']).toBeUndefined();
};
5 changes: 5 additions & 0 deletions tests/fixtures/prebundle/builtin-module/package.json
@@ -0,0 +1,5 @@
{
"devDependencies": {
"url": "0.11.1"
}
}
13 changes: 12 additions & 1 deletion tests/utils.test.ts
@@ -1,5 +1,10 @@
import { logger as umiLogger } from '@umijs/utils';
import { getDepPkgName, isFilePath, logger } from '../src/utils';
import {
getDepPkgName,
isBuiltInModule,
isFilePath,
logger,
} from '../src/utils';

jest.mock('@umijs/utils', () => {
const originalModule = jest.requireActual('@umijs/utils');
Expand Down Expand Up @@ -94,4 +99,10 @@ describe('logger', () => {
expect(getDepPkgName('./name/test', { name: 'test' })).toBe('test');
});
});

test(isBuiltInModule.name, () => {
expect(isBuiltInModule('node:path')).toBeTruthy();
expect(isBuiltInModule('path')).toBeTruthy();
expect(isBuiltInModule('minimatch')).toBeFalsy();
});
});

0 comments on commit 611f00c

Please sign in to comment.