Skip to content
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: transpile .mts with swc strip-only #2382

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/compartment-mapper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 2 additions & 0 deletions packages/compartment-mapper/src/import-parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<ParserForLanguage>} */
export const defaultParserForLanguage = Object.freeze(
/** @type {const} */ ({
mjs: parserMjs,
mts: parserMts,
cjs: parserCjs,
json: parserJson,
text: parserText,
Expand Down
2 changes: 2 additions & 0 deletions packages/compartment-mapper/src/node-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ const findPackage = async (readDescriptor, canonical, directory, name) => {

const defaultLanguages = /** @type {const} */ ([
'mjs',
'mts',
'cjs',
'json',
'text',
Expand All @@ -192,6 +193,7 @@ const defaultLanguages = /** @type {const} */ ([
const defaultUncontroversialParsers = /** @type {const} */ ({
cjs: 'cjs',
mjs: 'mjs',
mts: 'mts',
json: 'json',
text: 'text',
bytes: 'bytes',
Expand Down
43 changes: 43 additions & 0 deletions packages/compartment-mapper/src/parse-mts.js
Original file line number Diff line number Diff line change
@@ -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,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { meaning } from './1.cjs';
if (meaning !== 42) {
throw new Error('Fail');
}
23 changes: 23 additions & 0 deletions packages/compartment-mapper/test/mts-imports-cjs-define.test.js
Original file line number Diff line number Diff line change
@@ -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,
);
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
Loading