-
Notifications
You must be signed in to change notification settings - Fork 1k
[ssr] Improve module loader and add a module cache that track imports. #2288
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
Conversation
🦋 Changeset detectedLatest commit: 881d471 The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
|
||
| const modulePromise = (async () => { | ||
| const source = await fs.readFile(modulePath, 'utf-8'); | ||
| // TODO: store and re-use cachedData: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment still relevant? It is cached now, right? Or if there's another layer of caching we could do, maybe elaborate on the comment (there is nothing called cachedData).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is referring to Node's vm module cachedData, which should speed up repeated module creation in cases where we create fresh vm contexts. It's not related to our module cache. I updated the link and it seems like they added cachedData for modules now, so we can do this.
| ) { | ||
| // Override where we resolve lit-html from so that we always resolve to | ||
| // a single version of lit-html. | ||
| referrer = import.meta.url; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this force one version because it causes us to resolve relative to the top-level node_modules directory instead of a nested one?
What if there are two conflicting versions of Lit?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.
We have a bit of work to do here to test behavior with multiple versions of Lit. Since Lit 2.0 we don't require instanceof anymore, so we might not even need this. It wouldn't be used in the "global" mode (where we path Node's global and don't load modules like this) anyway, but I don't want to change this quite yet.
| // in their package.json to differentiate their ES module version. | ||
| packageFilter: (packageJson: PackageJSON) => { | ||
| packageJson.main = | ||
| packageJson.module ?? packageJson['jsnext:main'] ?? packageJson.main!; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think ?? index.js should be the final fallback?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolve should already implement that then. Here we're just adding in some non-standard behavior to look at the module and jsnext:main fields.
| referrer = import.meta.url; | ||
| } | ||
| const referencingModulePath = new URL(referrer).pathname; | ||
| const modulePath = resolve.sync(specifier, { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this handle package exports?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know... I don't think this package has been updated in a while.
| packages/labs/ssr/lib/ | ||
| packages/labs/ssr/node_modules/ | ||
| packages/labs/ssr/test/ | ||
| packages/labs/ssr/test/**/*.js |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these changes needed? Maybe they belong in another PR that adds tests? Same for .prettierignore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR does add tests. Some of your comments were on older version files though, so maybe you didn't see them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops yeah I missed them somehow.
|
@aomarks PTAL |
This make the module loader a class so that it can hold instance state about the cache, current vmContextID. This way more than one loader could be instantiated, which would be important for hosting multiple apps. This is working towards being able to introspect and modify the module cache too.