Skip to content

Commit

Permalink
refactor hard code node_modules
Browse files Browse the repository at this point in the history
  • Loading branch information
thescientist13 committed Sep 13, 2021
1 parent 9f16979 commit 489c695
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions packages/cli/src/plugins/resource/plugin-node-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const getPackageEntryPath = (packageJson) => {
: 'index.js'; // lastly, fallback to index.js

// use .mjs version if it exists, for packages like redux
if (!Array.isArray(entry) && fs.existsSync(`${process.cwd()}/node_modules/${packageJson.name}/${entry.replace('.js', '.mjs')}`)) {
if (!Array.isArray(entry) && fs.existsSync(`${getNodeModulesResolveLocationForPackageName(packageJson.name)}/${entry.replace('.js', '.mjs')}`)) {
entry = entry.replace('.js', '.mjs');
}

Expand All @@ -47,6 +47,7 @@ const walkModule = (module, dependency) => {
}), {
ImportDeclaration(node) {
let { value: sourceValue } = node.source;
const absoluteNodeModulesLocation = getNodeModulesResolveLocationForPackageName(dependency);

if (path.extname(sourceValue) === '' && sourceValue.indexOf('http') !== 0 && sourceValue.indexOf('./') < 0) {
if (!importMap[sourceValue]) {
Expand All @@ -55,7 +56,7 @@ const walkModule = (module, dependency) => {
updateImportMap(sourceValue, `/node_modules/${sourceValue}`);
}

walkPackageJson(path.join(process.cwd(), 'node_modules', sourceValue, 'package.json'));
walkPackageJson(path.join(absoluteNodeModulesLocation, 'package.json'));
} else if (sourceValue.indexOf('./') < 0) {
// adding a relative import
updateImportMap(sourceValue, `/node_modules/${sourceValue}`);
Expand All @@ -65,8 +66,8 @@ const walkModule = (module, dependency) => {
? `${sourceValue}.js`
: sourceValue;

if (fs.existsSync(path.join(process.cwd(), 'node_modules', dependency, sourceValue))) {
const moduleContents = fs.readFileSync(path.join(process.cwd(), 'node_modules', dependency, sourceValue));
if (fs.existsSync(path.join(absoluteNodeModulesLocation, sourceValue))) {
const moduleContents = fs.readFileSync(path.join(absoluteNodeModulesLocation, sourceValue));
walkModule(moduleContents, dependency);
}
}
Expand Down Expand Up @@ -101,6 +102,8 @@ const walkPackageJson = (packageJson = {}) => {
const isJavascriptPackage = Array.isArray(entry) || typeof entry === 'string' && entry.endsWith('.js') || entry.endsWith('.mjs');

if (isJavascriptPackage) {
const absoluteNodeModulesLocation = getNodeModulesResolveLocationForPackageName(dependency);

// https://nodejs.org/api/packages.html#packages_determining_module_system
if (Array.isArray(entry)) {
// we have an exportMap
Expand Down Expand Up @@ -158,7 +161,7 @@ const walkPackageJson = (packageJson = {}) => {
}

if (packageExport) {
const packageExportLocation = path.join(process.cwd(), 'node_modules', `${dependency}/${packageExport.replace('./', '')}`);
const packageExportLocation = path.join(absoluteNodeModulesLocation, packageExport.replace('./', ''));

// check all exports of an exportMap entry
// to make sure those deps get added to the importMap
Expand All @@ -181,7 +184,7 @@ const walkPackageJson = (packageJson = {}) => {

walkPackageJson(dependencyPackageJson);
} else {
const packageEntryPointPath = path.join(process.cwd(), './node_modules', dependency, entry);
const packageEntryPointPath = path.join(absoluteNodeModulesLocation, entry);

// sometimes a main file is actually just an empty string... :/
if (fs.existsSync(packageEntryPointPath)) {
Expand Down

0 comments on commit 489c695

Please sign in to comment.