From 8abb30cf21f9bb3c71fd37268766cf93469582b6 Mon Sep 17 00:00:00 2001 From: Turadg Aleahmad Date: Thu, 25 Jul 2024 17:43:17 -0700 Subject: [PATCH] feat: transpile .mts with swc strip-only --- packages/compartment-mapper/package.json | 1 + .../compartment-mapper/src/import-parsers.js | 2 + .../compartment-mapper/src/node-modules.js | 2 + packages/compartment-mapper/src/parse-mts.js | 43 +++++++++++++++++++ .../fixtures-esm-imports-cjs-define/0.mts | 4 ++ .../test/mts-imports-cjs-define.test.js | 23 ++++++++++ yarn.lock | 8 ++++ 7 files changed, 83 insertions(+) create mode 100644 packages/compartment-mapper/src/parse-mts.js create mode 100644 packages/compartment-mapper/test/fixtures-esm-imports-cjs-define/0.mts create mode 100644 packages/compartment-mapper/test/mts-imports-cjs-define.test.js diff --git a/packages/compartment-mapper/package.json b/packages/compartment-mapper/package.json index 497dfbd8c8..667867aa93 100644 --- a/packages/compartment-mapper/package.json +++ b/packages/compartment-mapper/package.json @@ -58,6 +58,7 @@ "@endo/cjs-module-analyzer": "^1.0.5", "@endo/module-source": "^0.0.0", "@endo/zip": "^1.0.5", + "@swc/wasm-typescript": "^1.7.2", "ses": "^1.5.0" }, "devDependencies": { diff --git a/packages/compartment-mapper/src/import-parsers.js b/packages/compartment-mapper/src/import-parsers.js index 99c5f04cc5..2bf42a1773 100644 --- a/packages/compartment-mapper/src/import-parsers.js +++ b/packages/compartment-mapper/src/import-parsers.js @@ -11,11 +11,13 @@ import parserText from './parse-text.js'; import parserBytes from './parse-bytes.js'; import parserCjs from './parse-cjs.js'; import parserMjs from './parse-mjs.js'; +import parserMts from './parse-mts.js'; /** @satisfies {Readonly} */ export const defaultParserForLanguage = Object.freeze( /** @type {const} */ ({ mjs: parserMjs, + mts: parserMts, cjs: parserCjs, json: parserJson, text: parserText, diff --git a/packages/compartment-mapper/src/node-modules.js b/packages/compartment-mapper/src/node-modules.js index 786d93a06d..40764196d1 100644 --- a/packages/compartment-mapper/src/node-modules.js +++ b/packages/compartment-mapper/src/node-modules.js @@ -184,6 +184,7 @@ const findPackage = async (readDescriptor, canonical, directory, name) => { const defaultLanguages = /** @type {const} */ ([ 'mjs', + 'mts', 'cjs', 'json', 'text', @@ -192,6 +193,7 @@ const defaultLanguages = /** @type {const} */ ([ const defaultUncontroversialParsers = /** @type {const} */ ({ cjs: 'cjs', mjs: 'mjs', + mts: 'mts', json: 'json', text: 'text', bytes: 'bytes', diff --git a/packages/compartment-mapper/src/parse-mts.js b/packages/compartment-mapper/src/parse-mts.js new file mode 100644 index 0000000000..760f8fdc43 --- /dev/null +++ b/packages/compartment-mapper/src/parse-mts.js @@ -0,0 +1,43 @@ +/* Provides language behavior (a parser) for importing ESM. */ + +// @ts-check + +import { transform } from '@swc/wasm-typescript'; +import { ModuleSource } from '@endo/module-source'; + +const textDecoder = new TextDecoder(); + +/** @type {import('./types.js').ParseFn} */ +export const parseMts = async ( + bytes, + _specifier, + sourceUrl, + _packageLocation, + options = {}, +) => { + const { sourceMap, sourceMapHook } = options; + const source = textDecoder.decode(bytes); + const transformed = await transform(source, { + filename: sourceUrl, + mode: 'strip-only', + module: true, + }); + const record = new ModuleSource(transformed.code, { + sourceUrl, + // XXX use transformed source map? + sourceMap, + sourceMapUrl: sourceUrl, + sourceMapHook, + }); + return { + parser: 'mts', + bytes, + record, + }; +}; + +/** @type {import('./types.js').ParserImplementation} */ +export default { + parse: parseMts, + heuristicImports: false, +}; diff --git a/packages/compartment-mapper/test/fixtures-esm-imports-cjs-define/0.mts b/packages/compartment-mapper/test/fixtures-esm-imports-cjs-define/0.mts new file mode 100644 index 0000000000..682fc4bd44 --- /dev/null +++ b/packages/compartment-mapper/test/fixtures-esm-imports-cjs-define/0.mts @@ -0,0 +1,4 @@ +import { meaning } from './1.cjs'; +if (meaning !== 42) { + throw new Error('Fail'); +} diff --git a/packages/compartment-mapper/test/mts-imports-cjs-define.test.js b/packages/compartment-mapper/test/mts-imports-cjs-define.test.js new file mode 100644 index 0000000000..2817fb126c --- /dev/null +++ b/packages/compartment-mapper/test/mts-imports-cjs-define.test.js @@ -0,0 +1,23 @@ +/* eslint-disable no-underscore-dangle */ +// import "./ses-lockdown.js"; +import 'ses'; +import test from 'ava'; + +import { scaffold } from './scaffold.js'; + +const fixture = new URL( + 'fixtures-esm-imports-cjs-define/0.mts', + import.meta.url, +).toString(); + +const assertFixture = t => t.pass(); + +const fixtureAssertionCount = 1; + +scaffold( + 'fixtures-esm-imports-cjs-define', + test, + fixture, + assertFixture, + fixtureAssertionCount, +); diff --git a/yarn.lock b/yarn.lock index 748bddba0d..17b8c43353 100644 --- a/yarn.lock +++ b/yarn.lock @@ -319,6 +319,7 @@ __metadata: "@endo/cjs-module-analyzer": "npm:^1.0.5" "@endo/module-source": "npm:^0.0.0" "@endo/zip": "npm:^1.0.5" + "@swc/wasm-typescript": "npm:^1.7.2" ava: "npm:^6.1.3" babel-eslint: "npm:^10.1.0" c8: "npm:^7.14.0" @@ -2599,6 +2600,13 @@ __metadata: languageName: node linkType: hard +"@swc/wasm-typescript@npm:^1.7.2": + version: 1.7.2 + resolution: "@swc/wasm-typescript@npm:1.7.2" + checksum: 10c0/fb84874ff21865b5b563564026cdec2de16c69c8f659b7070a4a4f15b70d2a53b1f8a8bfc40e00f90933e4c6fad537f3976209cc2fbc79be2558fd0a138785d5 + languageName: node + linkType: hard + "@tootallnate/once@npm:2": version: 2.0.0 resolution: "@tootallnate/once@npm:2.0.0"