Skip to content

Commit

Permalink
Extract getModuleParts
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic committed Aug 24, 2022
1 parent abe52c8 commit bbbfd55
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 41 deletions.
20 changes: 2 additions & 18 deletions packages/core/package-manager/src/NodeResolverBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {ResolveResult} from './types';
import Module from 'module';
import path from 'path';
import invariant from 'assert';
import {normalizeSeparators} from '@parcel/utils';
import {getModuleParts} from '@parcel/utils';

const builtins = {pnpapi: true};
for (let builtin of Module.builtinModules) {
Expand Down Expand Up @@ -102,22 +102,6 @@ export class NodeResolverBase<T> {
});
}

getModuleParts(name: string): [FilePath, ?string] {
name = path.normalize(name);
let splitOn = name.indexOf(path.sep);
if (name.charAt(0) === '@') {
splitOn = name.indexOf(path.sep, splitOn + 1);
}
if (splitOn < 0) {
return [normalizeSeparators(name), undefined];
} else {
return [
normalizeSeparators(name.substring(0, splitOn)),
name.substring(splitOn + 1) || undefined,
];
}
}

isBuiltin(name: DependencySpecifier): boolean {
return !!(builtins[name] || name.startsWith('node:'));
}
Expand All @@ -135,7 +119,7 @@ export class NodeResolverBase<T> {
};
}

let [moduleName, subPath] = this.getModuleParts(id);
let [moduleName, subPath] = getModuleParts(id);
let dir = path.dirname(sourceFile);
let moduleDir = this.fs.findNodeModule(moduleName, dir);

Expand Down
23 changes: 23 additions & 0 deletions packages/core/utils/src/getModuleParts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// @flow strict-local
import path from 'path';

import {normalizeSeparators} from './path';

/**
* Returns the package name and the optional subpath
*/
export default function getModuleParts(_name: string): [string, ?string] {
let name = path.normalize(_name);
let splitOn = name.indexOf(path.sep);
if (name.charAt(0) === '@') {
splitOn = name.indexOf(path.sep, splitOn + 1);
}
if (splitOn < 0) {
return [normalizeSeparators(name), undefined];
} else {
return [
normalizeSeparators(name.substring(0, splitOn)),
name.substring(splitOn + 1) || undefined,
];
}
}
1 change: 1 addition & 0 deletions packages/core/utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export {default as countLines} from './countLines';
export {default as generateBuildMetrics} from './generateBuildMetrics';
export {default as generateCertificate} from './generateCertificate';
export {default as getCertificate} from './getCertificate';
export {default as getModuleParts} from './getModuleParts';
export {default as getRootDir} from './getRootDir';
export {default as isDirectoryInside} from './isDirectoryInside';
export {default as isURL} from './is-url';
Expand Down
31 changes: 8 additions & 23 deletions packages/utils/node-resolver-core/src/NodeResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
findAlternativeNodeModules,
findAlternativeFiles,
loadConfig,
getModuleParts,
globToRegex,
isGlobMatch,
} from '@parcel/utils';
Expand Down Expand Up @@ -387,7 +388,7 @@ export default class NodeResolver {

if (resolved === undefined && process.versions.pnp != null && parent) {
try {
let [moduleName, subPath] = this.getModuleParts(filename);
let [moduleName, subPath] = getModuleParts(filename);
// $FlowFixMe[prop-missing]
let pnp = _Module.findPnpApi(path.dirname(parent));

Expand Down Expand Up @@ -418,7 +419,7 @@ export default class NodeResolver {

// If we couldn't resolve the node_modules path, just return the module name info
if (resolved === undefined) {
let [moduleName, subPath] = this.getModuleParts(filename);
let [moduleName, subPath] = getModuleParts(filename);
resolved = ({
moduleName,
subPath,
Expand Down Expand Up @@ -458,12 +459,12 @@ export default class NodeResolver {
}

if (Array.isArray(includeNodeModules)) {
let [moduleName] = this.getModuleParts(name);
let [moduleName] = getModuleParts(name);
return includeNodeModules.includes(moduleName);
}

if (includeNodeModules && typeof includeNodeModules === 'object') {
let [moduleName] = this.getModuleParts(name);
let [moduleName] = getModuleParts(name);
let include = includeNodeModules[moduleName];
if (include != null) {
return !!include;
Expand All @@ -476,7 +477,7 @@ export default class NodeResolver {
name: string,
ctx: ResolverContext,
) {
let [moduleName] = this.getModuleParts(name);
let [moduleName] = getModuleParts(name);
let pkg = await this.findPackage(sourceFile, ctx);
if (!pkg) {
return;
Expand Down Expand Up @@ -771,7 +772,7 @@ export default class NodeResolver {
sourceFile: FilePath,
ctx: ResolverContext,
): ?Module {
let [moduleName, subPath] = this.getModuleParts(filename);
let [moduleName, subPath] = getModuleParts(filename);

ctx.invalidateOnFileCreate.push({
fileName: `node_modules/${moduleName}`,
Expand Down Expand Up @@ -1182,7 +1183,7 @@ export default class NodeResolver {
alias = this.lookupAlias(aliases, normalizeSeparators(filename));
if (alias == null) {
// If it didn't match, try only the module name.
let [moduleName, subPath] = this.getModuleParts(filename);
let [moduleName, subPath] = getModuleParts(filename);
alias = this.lookupAlias(aliases, moduleName);
if (typeof alias === 'string' && subPath) {
let isRelative = alias.startsWith('./');
Expand Down Expand Up @@ -1320,22 +1321,6 @@ export default class NodeResolver {
return this.resolveAliases(filename, env, pkg);
}
getModuleParts(name: string): [FilePath, ?string] {
name = path.normalize(name);
let splitOn = name.indexOf(path.sep);
if (name.charAt(0) === '@') {
splitOn = name.indexOf(path.sep, splitOn + 1);
}
if (splitOn < 0) {
return [normalizeSeparators(name), undefined];
} else {
return [
normalizeSeparators(name.substring(0, splitOn)),
name.substring(splitOn + 1) || undefined,
];
}
}
hasSideEffects(filePath: FilePath, pkg: InternalPackageJSON): boolean {
switch (typeof pkg.sideEffects) {
case 'boolean':
Expand Down

0 comments on commit bbbfd55

Please sign in to comment.