Skip to content

Commit

Permalink
fix (labs/ssr): move to enhanced-resolve for package exports support
Browse files Browse the repository at this point in the history
The resolve package doesn't support package exports and various other
more modern resolution features. It seems `enhanced-resolve` (from
webpack) introduces this support.
  • Loading branch information
43081j committed Oct 15, 2022
1 parent 2906369 commit 4b83ea5
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 46 deletions.
91 changes: 63 additions & 28 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions packages/labs/ssr/package.json
Expand Up @@ -134,9 +134,9 @@
"@open-wc/testing-karma": "^4.0.9",
"@types/command-line-args": "^5.0.0",
"@types/koa": "^2.0.49",
"@types/koa__router": "^8.0.2",
"@types/koa-cors": "*",
"@types/koa-static": "^4.0.1",
"@types/koa__router": "^8.0.2",
"@types/parse5": "^6.0.1",
"@types/resolve": "^1.20.2",
"@webcomponents/template-shadowroot": "^0.1.0",
Expand All @@ -151,12 +151,12 @@
"@lit-labs/ssr-client": "^1.0.0",
"@lit/reactive-element": "^1.4.0",
"@types/node": "^16.0.0",
"enhanced-resolve": "^5.10.0",
"lit": "^2.3.0",
"lit-element": "^3.1.0",
"lit-html": "^2.3.0",
"node-fetch": "^3.2.8",
"parse5": "^6.0.1",
"resolve": "^1.10.1"
"parse5": "^6.0.1"
},
"engines": {
"node": ">=13.9.0"
Expand Down
23 changes: 8 additions & 15 deletions packages/labs/ssr/src/lib/module-loader.ts
Expand Up @@ -8,11 +8,9 @@ import * as path from 'path';
import {promises as fs} from 'fs';
import {URL, fileURLToPath, pathToFileURL} from 'url';
import * as vm from 'vm';
import resolveAsync from 'resolve';
import enhancedResolve from 'enhanced-resolve';
import {builtinModules} from 'module';

type PackageJSON = {main?: string; module?: string; 'jsnext:main'?: string};

const builtIns = new Set(builtinModules);

const specifierMatches = (specifier: string, match: string) =>
Expand Down Expand Up @@ -276,17 +274,10 @@ export const resolveSpecifier = async (
// a single version.
referrerPath = fileURLToPath(import.meta.url);
}
const modulePath = await resolve(specifier, {
basedir: path.dirname(referrerPath),
moduleDirectory: ['node_modules'],
const modulePath = await resolve(specifier, path.dirname(referrerPath), {
modules: ['node_modules'],
extensions: ['.js'],
// Some packages use a non-standard alternative to the "main" field
// in their package.json to differentiate their ES module version.
packageFilter: (packageJson: PackageJSON) => {
packageJson.main =
packageJson.module ?? packageJson['jsnext:main'] ?? packageJson.main;
return packageJson;
},
mainFields: ['module', 'jsnext:main', 'main'],
});
return pathToFileURL(modulePath);
}
Expand All @@ -301,10 +292,12 @@ const initializeImportMeta = (meta: {url: string}, module: vm.Module) => {

const resolve = async (
id: string,
opts: resolveAsync.AsyncOpts
path: string,
opts: Partial<enhancedResolve.ResolveOptions>
): Promise<string> => {
const resolver = enhancedResolve.create(opts);
return new Promise((res, rej) => {
resolveAsync(id, opts, (err, resolved) => {
resolver({}, path, id, {}, (err: unknown, resolved?: string) => {
if (err != null) {
rej(err);
} else {
Expand Down

0 comments on commit 4b83ea5

Please sign in to comment.