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: fix host package dependencies problem #1109

Merged
merged 2 commits into from
Jun 18, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions packages/preset-dumi/src/transformer/demo/dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import {
getModuleResolvePkg,
getModuleResolvePath,
getModuleResolveContent,
getPackageInfo,
getHostPkgPath,
getHostModuleResolvePkg,
} from '../../utils/moduleResolver';
import FileCache from '../../utils/cache';
import type { IDemoOpts } from './options';
Expand Down Expand Up @@ -53,6 +56,8 @@ export const LOCAL_MODULE_EXT = [...LOCAL_DEP_EXT, '.json'];
// local dependency extensions which will be collected
export const PLAIN_TEXT_EXT = [...LOCAL_MODULE_EXT, '.less', '.css', '.scss', '.sass', '.styl'];

export const isHostPackage = (packageName: string) => getHostPkgPath(packageName);

function analyzeDeps(
raw: string,
{
Expand Down Expand Up @@ -99,9 +104,10 @@ function analyzeDeps(
});
const resolvePathParsed = path.parse(resolvePath);

if (resolvePath.includes('node_modules')) {
const isHostPkg = isHostPackage(requireStr);
if (resolvePath.includes('node_modules') || isHostPkg) {
// save external deps
const pkg = getModuleResolvePkg({
const pkg = isHostPkg ? getHostModuleResolvePkg(requireStr) : getModuleResolvePkg({
basePath: fileAbsPath,
sourcePath: resolvePath,
extensions: LOCAL_MODULE_EXT,
Expand Down
8 changes: 8 additions & 0 deletions packages/preset-dumi/src/utils/moduleResolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
getModuleResolvePath,
getModuleResolvePkg,
getModuleResolveContent,
getHostModuleResolvePkg,
} from './moduleResolver';
import ctx from '../context';

Expand Down Expand Up @@ -54,4 +55,11 @@ describe('moduleResolver', () => {

expect(content).toEqual(expectedContent);
});

it('get expected host module version', () => {
const { version } = getHostModuleResolvePkg('@umijs/preset-dumi');
const { version: expectedVersion } = require(path.join(__dirname, '../../package.json'));

expect(version).toEqual(expectedVersion);
});
});
37 changes: 26 additions & 11 deletions packages/preset-dumi/src/utils/moduleResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const getPkgPathsFromPath = (identifier: string) => {
* get package root path if it is a local package
* @param pkg package name
*/
const getHostPkgPath = (() => {
export const getHostPkgPath = (() => {
let cache: ReturnType<typeof getHostPkgAlias>;

return (pkg: string) => {
Expand Down Expand Up @@ -78,20 +78,12 @@ export const getModuleResolvePath = ({
};

/**
* resolve module version
* get package info by modulePath
*/
export const getModuleResolvePkg = ({
basePath,
sourcePath,
extensions = DEFAULT_EXT,
}: IModuleResolverOpts) => {
export const getPackageInfo = (modulePath: string) => {
let version: string | null;
let name: string | null;
let peerDependencies: any | null;
const resolvePath = getModuleResolvePath({ basePath, sourcePath, extensions });
const { pkgName, absPkgModulePath } = getPkgPathsFromPath(resolvePath);
// use project path as module path for local packages
const modulePath = getHostPkgPath(pkgName) || absPkgModulePath;
const pkgPath = path.join(modulePath, 'package.json');

if (modulePath && fs.existsSync(pkgPath)) {
Expand All @@ -107,6 +99,22 @@ export const getModuleResolvePkg = ({
return { name, version, peerDependencies };
};

/**
* resolve module version
*/
export const getModuleResolvePkg = ({
basePath,
sourcePath,
extensions = DEFAULT_EXT,
}: IModuleResolverOpts) => {
const resolvePath = getModuleResolvePath({ basePath, sourcePath, extensions });
const { pkgName, absPkgModulePath } = getPkgPathsFromPath(resolvePath);
// use project path as module path for local packages
const modulePath = getHostPkgPath(pkgName) || absPkgModulePath;

return getPackageInfo(modulePath);
};

/**
* resolve module content
*/
Expand All @@ -119,3 +127,10 @@ export const getModuleResolveContent = ({

return resolvePath ? fs.readFileSync(resolvePath, 'utf8').toString() : '';
};

/**
* resolve host module version
*/
export const getHostModuleResolvePkg = (requireStr: string) => {
return getPackageInfo(getHostPkgPath(requireStr));
};