Skip to content

Commit

Permalink
re-using import.meta.resolve in more places
Browse files Browse the repository at this point in the history
  • Loading branch information
thescientist13 committed Nov 21, 2024
1 parent d1a7876 commit 8eeab7c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 21 deletions.
29 changes: 27 additions & 2 deletions packages/cli/src/lib/node-modules-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createRequire } from 'module';
import { checkResourceExists } from './resource-utils.js';
import fs from 'fs/promises';

// TODO delete me

Check warning on line 5 in packages/cli/src/lib/node-modules-utils.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected 'todo' comment: 'TODO delete me'

Check warning on line 5 in packages/cli/src/lib/node-modules-utils.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected ' TODO' comment: 'TODO delete me'

Check warning on line 5 in packages/cli/src/lib/node-modules-utils.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected 'todo' comment: 'TODO delete me'

Check warning on line 5 in packages/cli/src/lib/node-modules-utils.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected ' TODO' comment: 'TODO delete me'
async function getNodeModulesLocationForPackage(packageName) {
let nodeModulesUrl;

Expand Down Expand Up @@ -29,6 +30,28 @@ async function getNodeModulesLocationForPackage(packageName) {
return nodeModulesUrl;
}

function resolveBareSpecifier(specifier) {
let resolvedPath;

// sometimes a package.json has no main field :/
// https://unpkg.com/browse/@types/trusted-types@2.0.7/package.json
try {
resolvedPath = import.meta.resolve(specifier);
} catch (e) {
// TODO console.log(`WARNING: unable to resolve specifier \`${specifier}\``);

Check warning on line 41 in packages/cli/src/lib/node-modules-utils.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected 'todo' comment: 'TODO console.log(`WARNING: unable to...'

Check warning on line 41 in packages/cli/src/lib/node-modules-utils.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected ' TODO' comment: 'TODO console.log(`WARNING: unable to...'

Check warning on line 41 in packages/cli/src/lib/node-modules-utils.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected 'todo' comment: 'TODO console.log(`WARNING: unable to...'

Check warning on line 41 in packages/cli/src/lib/node-modules-utils.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected ' TODO' comment: 'TODO console.log(`WARNING: unable to...'
}

return resolvedPath;
}

function resolveRootForSpecifier(specifier) {
const resolved = resolveBareSpecifier(specifier);

if (resolved) {
return `${resolved.split(specifier)[0]}${specifier}/`;
}
}

// extract the package name from a URL like /node_modules/<some>/<package>/index.js
function getPackageNameFromUrl(url) {
const packagePathPieces = url.split('node_modules/')[1].split('/'); // double split to handle node_modules within nested paths
Expand All @@ -42,7 +65,7 @@ function getPackageNameFromUrl(url) {
return packageName;
}

async function getPackageJson({ userWorkspace, projectDirectory }) {
async function getPackageJsonForProject({ userWorkspace, projectDirectory }) {
const monorepoPackageJsonUrl = new URL('./package.json', userWorkspace);
const topLevelPackageJsonUrl = new URL('./package.json', projectDirectory);
const hasMonorepoPackageJson = await checkResourceExists(monorepoPackageJsonUrl);
Expand All @@ -56,7 +79,9 @@ async function getPackageJson({ userWorkspace, projectDirectory }) {
}

export {
resolveBareSpecifier,
resolveRootForSpecifier,
getPackageJsonForProject,
getNodeModulesLocationForPackage,
getPackageJson,
getPackageNameFromUrl
};
20 changes: 9 additions & 11 deletions packages/cli/src/lib/walker-package-ranger2.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'fs/promises';
import { resolveBareSpecifier } from './node-modules-utils.js';

/* eslint-disable max-depth,complexity */
const importMap = {};
Expand Down Expand Up @@ -80,35 +81,32 @@ async function walkPackageForExports(dependency, packageJson, resolvedRoot) {
async function walkPackageJson(packageJson = {}) {
try {
for (const dependency of Object.keys(packageJson.dependencies || {})) {
// TODO why empty main :/
// https://unpkg.com/browse/@types/trusted-types@2.0.7/package.json
if (dependency !== '@types/trusted-types') {
const resolved = import.meta.resolve(dependency);
const resolved = resolveBareSpecifier(dependency);

if (resolved) {
const resolvedRoot = new URL(`./${dependency}/`, resolved.split(dependency)[0]);
const resolvedPackageJson = (await import(new URL('./package.json', resolvedRoot), { with: { type: 'json' } })).default;

walkPackageForExports(dependency, resolvedPackageJson, resolvedRoot);

if (resolvedPackageJson.dependencies) {
for (const dependency in resolvedPackageJson.dependencies) {
// TODO why empty main
// https://unpkg.com/browse/@types/trusted-types@2.0.7/package.json
if (dependency !== '@types/trusted-types') {
// TODO do we have to duplicate this look here and above??
const resolved = import.meta.resolve(dependency);
const resolved = resolveBareSpecifier(dependency);

if (resolved) {
const resolvedRoot = new URL(`./${dependency}/`, resolved.split(dependency)[0]);
const resolvedPackageJson = (await import(new URL('./package.json', resolvedRoot), { with: { type: 'json' } })).default;

walkPackageForExports(dependency, resolvedPackageJson, resolvedRoot);
// console.log('### resolve direct dependency', { dependency, resolved, resolvedRoot: resolvedRoot.href, resolvedPackageJson })

await walkPackageJson(resolvedPackageJson);
}
}
}
}
}
} catch (e) {
console.error('Error building up import map', e);
// TODO console.error('Error building up import map', e);
}

// console.log({ importMap });
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/plugins/resource/plugin-node-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { checkResourceExists } from '../../lib/resource-utils.js';
import fs from 'fs/promises';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import replace from '@rollup/plugin-replace';
import { getNodeModulesLocationForPackage, getPackageJson, getPackageNameFromUrl } from '../../lib/node-modules-utils.js';
import { getNodeModulesLocationForPackage, getPackageJsonForProject, getPackageNameFromUrl } from '../../lib/node-modules-utils.js';
import { resolveForRelativeUrl } from '../../lib/resource-utils.js';
import { ResourceInterface } from '../../lib/resource-interface.js';
import { mergeImportMap } from '../../lib/walker-package-ranger.js';
Expand Down Expand Up @@ -86,7 +86,7 @@ class NodeModulesResource extends ResourceInterface {
body = body.replace(/\<head>(.*)<\/head>/s, contents.replace(/\$/g, '$$$')); // https://github.com/ProjectEvergreen/greenwood/issues/656);
}

const userPackageJson = await getPackageJson(context);
const userPackageJson = await getPackageJsonForProject(context);

// if there are dependencies and we haven't generated the importMap already
// walk the project's package.json for all its direct dependencies
Expand Down
12 changes: 6 additions & 6 deletions packages/plugin-polyfills/src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getNodeModulesLocationForPackage } from '@greenwood/cli/src/lib/node-modules-utils.js';
import { resolveRootForSpecifier } from '@greenwood/cli/src/lib/node-modules-utils.js';
import { ResourceInterface } from '@greenwood/cli/src/lib/resource-interface.js';

class PolyfillsResource extends ResourceInterface {
Expand Down Expand Up @@ -79,18 +79,18 @@ const greenwoodPluginPolyfills = (options = {}) => {
provider: async (compilation) => {
const { outputDir } = compilation.context;
const polyfillPackageName = '@webcomponents/webcomponentsjs';
const polyfillNodeModulesLocation = await getNodeModulesLocationForPackage(polyfillPackageName);
const litNodeModulesLocation = await getNodeModulesLocationForPackage('lit');
const polyfillNodeModulesLocation = resolveRootForSpecifier(polyfillPackageName);
const litNodeModulesLocation = resolveRootForSpecifier('lit');

const standardPolyfills = [{
from: new URL('./webcomponents-loader.js', new URL(`file://${polyfillNodeModulesLocation}/`)),
from: new URL('./webcomponents-loader.js', polyfillNodeModulesLocation),
to: new URL('./webcomponents-loader.js', outputDir)
}, {
from: new URL('./bundles/', new URL(`file://${polyfillNodeModulesLocation}/`)),
from: new URL('./bundles/', polyfillNodeModulesLocation),
to: new URL('./bundles/', outputDir)
}];
const litPolyfills = [{
from: new URL('./polyfill-support.js', new URL(`file://${litNodeModulesLocation}/`)),
from: new URL('./polyfill-support.js', litNodeModulesLocation),
to: new URL('./polyfill-support.js', outputDir)
}];

Expand Down

0 comments on commit 8eeab7c

Please sign in to comment.