feat: add the foundations for support of import.meta
MODIFY
packages/compartment-mapper/src/bundle.js
packages/compartment-mapper/src/bundle.js
@@ -332,6 +332,9 @@ ${importsCellSetter(__liveExportMap__, index)}\
onceVar: {
${importsCellSetter(__fixedExportMap__, index)}\
},
+ metaVar: {
+ url: '${packageLocation}'\
+ },
});
`,
),

MODIFY
packages/ses/src/module-instance.js
packages/ses/src/module-instance.js
@@ -158,6 +158,17 @@ export const makeModuleInstance = (
// both initialize and update live bindings.
const liveVar = create(null);
+ const metaVar = create(null);
+ try {
+ metaVar.url = new URL(
+ moduleSpecifier,
+ compartmentFields.name || '.',
+ ).toString();
+ } catch (e) {
+ metaVar.url = moduleSpecifier;
+ console.error('Wont URL:', moduleSpecifier, compartmentFields.name);
+ }
+
// {_localName_: [{get, set, notify}]} used to merge all the export updaters.
const localGetNotify = create(null);
@@ -438,6 +449,7 @@ export const makeModuleInstance = (
imports: freeze(imports),
onceVar: freeze(onceVar),
liveVar: freeze(liveVar),
+ metaVar: freeze(metaVar),
}),
);
} catch (e) {

MODIFY
packages/ses/test/test-import.js
packages/ses/test/test-import.js
@@ -556,3 +556,31 @@ test('child compartments are modular', async t => {
t.is(meaning, 42, 'child compartments have module support');
});
+
+test.only('import.meta.url points to the module', async t => {
+ t.plan(1);
+
+ const makeImportHook = makeNodeImporter({
+ 'https://example.com/index.js': `
+ const myloc = import.meta.url;
+ export default myloc;
+ `,
+ });
+
+ const compartment = new Compartment(
+ { t },
+ {},
+ {
+ name: 'https://example.com',
+ resolveHook: resolveNode,
+ importHook: makeImportHook('https://example.com'),
+ },
+ );
+
+ const {
+ namespace:{
+ default: metaurl
+ }
+ } = await compartment.import('./index.js');
+ t.is(metaurl, 'https://example.com/index.js');
+});

MODIFY
packages/static-module-record/src/babelPlugin.js
packages/static-module-record/src/babelPlugin.js
@@ -309,6 +309,11 @@ function makeModulePlugins(options) {
};
const moduleVisitor = (doAnalyze, doTransform) => ({
+ MetaProperty(path){
+ if(path.node.meta.name==='import' && doTransform){
+ path.replaceWithMultiple([replace(path.node, t.identifier(h.HIDDEN_META))]);
+ }
+ },
// We handle all the import and export productions.
ImportDeclaration(path) {
if (doAnalyze) {

MODIFY
packages/static-module-record/src/hidden.js
packages/static-module-record/src/hidden.js
@@ -5,6 +5,7 @@ export const HIDDEN_IMPORT = `${HIDDEN_PREFIX}import`;
export const HIDDEN_IMPORT_SELF = `${HIDDEN_PREFIX}importSelf`;
export const HIDDEN_IMPORTS = `${HIDDEN_PREFIX}imports`;
export const HIDDEN_ONCE = `${HIDDEN_PREFIX}once`;
+export const HIDDEN_META = `${HIDDEN_PREFIX}meta`;
export const HIDDEN_LIVE = `${HIDDEN_PREFIX}live`;
export const HIDDEN_IDENTIFIERS = [
HIDDEN_A,
@@ -12,5 +13,6 @@ export const HIDDEN_IDENTIFIERS = [
HIDDEN_IMPORT_SELF,
HIDDEN_IMPORTS,
HIDDEN_ONCE,
+ HIDDEN_META,
HIDDEN_LIVE,
];

MODIFY
packages/static-module-record/src/transform-analyze.js
packages/static-module-record/src/transform-analyze.js
@@ -113,6 +113,7 @@ const makeCreateStaticRecord = transformSource =>
imports: ${h.HIDDEN_IMPORTS}, \
liveVar: ${h.HIDDEN_LIVE}, \
onceVar: ${h.HIDDEN_ONCE}, \
+ metaVar: ${h.HIDDEN_META}, \
}) => { \
${preamble} \
${scriptSource}

MODIFY
packages/static-module-record/test/test-static-module-record.js
packages/static-module-record/test/test-static-module-record.js
@@ -152,6 +152,7 @@ function initialize(t, source, options = {}) {
imports: updateImports,
liveVar: liveUpdaters,
onceVar: onceUpdaters,
+ metaVar: { url: 'file://meta.url' },
});
return { record, namespace, log, updaters };
@@ -556,6 +557,17 @@ test('import for side-effect', t => {
t.deepEqual(record.__liveExportMap__, {});
t.deepEqual(record.imports, ['module']);
});
+test('import meta', t => {
+ t.notThrows(() => initialize(t, `const a = import.meta.url`));
+});
+test.failing('import meta in export', t => {
+ let namespace = {};
+ t.notThrows(() => {
+ namespace = initialize(t, `export const a = 'ok ' + import.meta.url`)
+ .namespace;
+ });
+ t.is(namespace.result, 'ok file://meta.url');
+});
test('export names', t => {
const { namespace } = initialize(

feat: add the foundations for support of import.meta