-
Notifications
You must be signed in to change notification settings - Fork 72
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
feat(ses,module-source): Dynamic import #2639
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,8 @@ const fixtureOptionalDepsCjs = new URL( | |
|
||
scaffold( | ||
'optionalDependencies/esm', | ||
// this test fails because it relies on dynamic import | ||
// fails for archives because dynamic import cannot reach modules not | ||
// discovered during archival | ||
test, | ||
fixtureOptionalDepsEsm, | ||
async (t, { namespace }) => { | ||
|
@@ -41,7 +42,7 @@ scaffold( | |
); | ||
}, | ||
4, | ||
{ knownFailure: true }, | ||
{ knownArchiveFailure: true }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (not a polite way to say you should, just a note) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, this is a sign things are getting weedy. We might refactor |
||
); | ||
|
||
scaffold( | ||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,6 +2,10 @@ User-visible changes in `@endo/module-source`: | |||||||||
|
||||||||||
# Next release | ||||||||||
|
||||||||||
- Supports dynamic `import` within a `ModuleSource` in conjunction with | ||||||||||
a related change in `ses`. | ||||||||||
Comment on lines
+5
to
+6
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
For example, `await import(specifier)` can now call through to the | ||||||||||
surrounding compartment's `importHook` to load and evaluate further modules. | ||||||||||
- Provides an XS-specific variant of `@endo/module-source` that adapts the | ||||||||||
native `ModuleSource` instead of entraining Babel. | ||||||||||
|
||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
/** @import {ModuleExportsNamespace} from '../types.js' */ | ||
|
||
import { assert } from './error/assert.js'; | ||
import { getDeferredExports } from './module-proxy.js'; | ||
import { | ||
|
@@ -131,13 +133,15 @@ export const makeModuleInstance = ( | |
__fixedExportMap__: fixedExportMap = {}, | ||
__liveExportMap__: liveExportMap = {}, | ||
__reexportMap__: reexportMap = {}, | ||
__needsImport__: needsImport = false, | ||
__needsImportMeta__: needsImportMeta = false, | ||
__syncModuleFunctor__, | ||
} = moduleSource; | ||
|
||
const compartmentFields = weakmapGet(privateFields, compartment); | ||
|
||
const { __shimTransforms__, importMetaHook } = compartmentFields; | ||
const { __shimTransforms__, resolveHook, importMetaHook, compartmentImport } = | ||
compartmentFields; | ||
|
||
const { exportsProxy, exportsTarget, activate } = getDeferredExports( | ||
compartment, | ||
|
@@ -171,6 +175,14 @@ export const makeModuleInstance = ( | |
importMetaHook(moduleSpecifier, importMeta); | ||
} | ||
|
||
/** @type {(fullSpecifier: string) => Promise<ModuleExportsNamespace>} */ | ||
let dynamicImport; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. needs |
||
if (needsImport) { | ||
/** @param {string} importSpecifier */ | ||
dynamicImport = async importSpecifier => | ||
compartmentImport(resolveHook(importSpecifier, moduleSpecifier)); | ||
} | ||
|
||
// {_localName_: [{get, set, notify}]} used to merge all the export updaters. | ||
const localGetNotify = create(null); | ||
|
||
|
@@ -462,6 +474,7 @@ export const makeModuleInstance = ( | |
imports: freeze(imports), | ||
onceVar: freeze(onceVar), | ||
liveVar: freeze(liveVar), | ||
import: dynamicImport, | ||
importMeta, | ||
}), | ||
); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if you added a test for CommonJS? Or would that be of low value? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Middling value. We don’t test CJS directly at this layer of abstraction so much as we test the mechanisms for supporting it, which are virtual module sources and |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
/* eslint max-lines: 0 */ | ||
|
||
import test from 'ava'; | ||
import { ModuleSource } from '@endo/module-source'; | ||
import '../index.js'; | ||
import { resolveNode, makeNodeImporter } from './_node.js'; | ||
import { makeImporter, makeStaticRetriever } from './_import-commons.js'; | ||
|
@@ -600,3 +601,21 @@ test('importMetaHook and meta from record', async t => { | |
const { default: metaurl } = await compartment.import('./index.js'); | ||
t.is(metaurl, 'https://example.com/index.js?foo'); | ||
}); | ||
|
||
test('dynamic import from source', async t => { | ||
const c = new Compartment({ | ||
__options__: true, | ||
__noNamespaceBox__: true, | ||
Comment on lines
+607
to
+608
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Out of curiosity: wtf are these? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inadequately documented: https://github.com/endojs/endo/blob/master/packages/ses/NEWS.md#v160-2024-07-30 The legacy signature of The legacy signature of |
||
resolveHook: s => s, | ||
modules: { | ||
'-': { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this have a specific meaning, or could it have been There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It could have been |
||
source: new ModuleSource(` | ||
export const dynamic = import('-'); | ||
`), | ||
}, | ||
}, | ||
}); | ||
const namespace = await c.import('-'); | ||
const namespace2 = await namespace.dynamic; | ||
t.is(namespace, namespace2); | ||
}); |
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 dynamic require have the same limitation?
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.