Skip to content

Commit

Permalink
feat: importMetaHook on instantiation if import.meta uttered by module
Browse files Browse the repository at this point in the history
  • Loading branch information
naugtur committed May 17, 2022
1 parent 0ddcd45 commit 63a0a28
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 21 deletions.
2 changes: 2 additions & 0 deletions packages/ses/src/compartment-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ export const makeCompartmentConstructor = (
resolveHook,
importHook,
moduleMapHook,
importMetaHook,
} = options;
const globalTransforms = [...transforms, ...__shimTransforms__];

Expand Down Expand Up @@ -316,6 +317,7 @@ export const makeCompartmentConstructor = (
importHook,
moduleMap,
moduleMapHook,
importMetaHook,
moduleRecords,
__shimTransforms__,
deferredExports,
Expand Down
14 changes: 5 additions & 9 deletions packages/ses/src/module-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
mapGet,
weakmapGet,
reflectHas,
assign,
} from './commons.js';
import { compartmentEvaluate } from './compartment-evaluate.js';

Expand Down Expand Up @@ -128,11 +129,12 @@ export const makeModuleInstance = (
__syncModuleProgram__: functorSource,
__fixedExportMap__: fixedExportMap = {},
__liveExportMap__: liveExportMap = {},
__usesImportMeta__: usesImportMeta = false,
} = staticModuleRecord;

const compartmentFields = weakmapGet(privateFields, compartment);

const { __shimTransforms__ } = compartmentFields;
const { __shimTransforms__, importMetaHook } = compartmentFields;

const { exportsProxy, proxiedExports, activate } = getDeferredExports(
compartment,
Expand All @@ -159,14 +161,8 @@ export const makeModuleInstance = (
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);
if (usesImportMeta) {
assign(metaVar, importMetaHook(moduleSpecifier));
}

// {_localName_: [{get, set, notify}]} used to merge all the export updaters.
Expand Down
7 changes: 4 additions & 3 deletions packages/ses/test/test-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -574,13 +574,14 @@ test.only('import.meta.url points to the module', async t => {
name: 'https://example.com',
resolveHook: resolveNode,
importHook: makeImportHook('https://example.com'),
importMetaHook: specifier => ({
url: new URL(specifier, 'https://example.com/').href,
}),
},
);

const {
namespace:{
default: metaurl
}
namespace: { default: metaurl },
} = await compartment.import('./index.js');
t.is(metaurl, 'https://example.com/index.js');
});
20 changes: 15 additions & 5 deletions packages/static-module-record/src/babelPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function makeModulePlugins(options) {
importDecls,
importSources,
liveExportMap,
importMetaProperties,
} = options;

if (sourceType !== 'module') {
Expand Down Expand Up @@ -308,13 +309,22 @@ 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))]);
const moduleVisitor = (doAnalyze, doTransform) => ({
MetaProperty(path) {
if (path.node.meta.name === 'import') {
if (doAnalyze && path.parentPath.isMemberExpression()) {
importMetaProperties.add(path.parentPath.node.property.name);
}
if (doTransform) {
// I saw meta added to import somewhere, but it was dead code. Leaving this here in case there's a reason to switch to meta as a field on import.
// path.replaceWith(replace(path.node, t.metaProperty(hiddenIdentifier(h.HIDDEN_IMPORT),t.identifier('meta'))));
path.replaceWithMultiple([
replace(path.node, hiddenIdentifier(h.HIDDEN_META)),
]);
}
}
},
// We handle all the import and export productions.
// We handle all the import and export productions.
ImportDeclaration(path) {
if (doAnalyze) {
const specs = path.node.specifiers;
Expand Down
2 changes: 1 addition & 1 deletion packages/static-module-record/src/hidden.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +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_META = `${HIDDEN_PREFIX}import_meta`;
export const HIDDEN_LIVE = `${HIDDEN_PREFIX}live`;
export const HIDDEN_IDENTIFIERS = [
HIDDEN_A,
Expand Down
5 changes: 5 additions & 0 deletions packages/static-module-record/src/static-module-record.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export function StaticModuleRecord(source, url) {
liveExportMap,
fixedExportMap,
exportAlls,
importMetaProperties,
} = analyzeModule({ string: source, url });
this.imports = freeze([...keys(imports)].sort());
this.exports = freeze(
Expand All @@ -60,5 +61,9 @@ export function StaticModuleRecord(source, url) {
this.__syncModuleProgram__ = functorSource;
this.__liveExportMap__ = liveExportMap;
this.__fixedExportMap__ = fixedExportMap;
// this.__usesImport
this.__usesImportMeta__ = importMetaProperties.length > 0;
// useful for knowing if resolve or any other expensive features are used
this.__usesImportMetaProperties__ = importMetaProperties;
freeze(this);
}
10 changes: 7 additions & 3 deletions packages/static-module-record/src/transform-analyze.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const makeTransformSource = (babel = null) => {
compact: true,
verbatim: true,
});
// console.log(`transformed`, transformedCode);
// console.error(`transformed`, transformedCode);
return transformedCode;
};

Expand All @@ -65,6 +65,8 @@ const makeCreateStaticRecord = transformSource =>
hoistedDecls: [],
importSources: Object.create(null),
importDecls: [],
// enables passing import.meta usage hints up.
importMetaProperties: new Set(),
};
if (moduleSource.startsWith('#!')) {
// Comment out the shebang lines.
Expand Down Expand Up @@ -123,12 +125,14 @@ const makeCreateStaticRecord = transformSource =>
if (url) {
functorSource += `//# sourceURL=${url}\n`;
}

const moduleAnalysis = freeze({
exportAlls: freeze(sourceOptions.exportAlls),
imports: freeze(sourceOptions.imports),
liveExportMap: freeze(sourceOptions.liveExportMap),
fixedExportMap: freeze(sourceOptions.fixedExportMap),
importMetaProperties: freeze(
Array.from(sourceOptions.importMetaProperties),
),
functorSource,
});
return moduleAnalysis;
Expand Down Expand Up @@ -198,7 +202,7 @@ export const makeModuleTransformer = (babel, importer) => {
? maybeSource.slice(0, -1)
: maybeSource;

// console.log(ss.isExpr, `generated`, src, `from`, ast);
console.log(ss.isExpr, `generated`, src, `from`, ast);
return { ...ss, endowments, src: actualSource };
},
};
Expand Down

0 comments on commit 63a0a28

Please sign in to comment.