Skip to content

Commit

Permalink
fix: support ts passthrough for no typestripping (#459)
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford authored Jan 27, 2025
1 parent 6ca5bff commit 471c32f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
5 changes: 4 additions & 1 deletion chompfile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ ${amaroSource}
const amaroTransformSync = module.exports.transformSync;
export function transform(source, url) {
try {
return amaroTransformSync(source, { filename: url, transform: { mode: 'strip-only', noEmptyExport: true } }).code;
const transformed = amaroTransformSync(source, { filename: url, transform: { mode: 'strip-only', noEmptyExport: true } }).code;
// Importantly, return undefined when there is no work to do
if (transformed !== source)
return transformed;
} catch (e) {
// This is needed pending figuring out why filename option above isn't working
throw new SyntaxError(e.message.replace(',-', url + ' - '));
Expand Down
15 changes: 8 additions & 7 deletions src/es-module-shims.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ async function fetchModule(url, fetchOpts, parent) {
const source = await res.text();
if (!esmsTsTransform) await initTs();
const transformed = esmsTsTransform(source, url);
return { r, s: transformed || source, t: transformed ? 'ts' : 'js' };
return { r, s: transformed === undefined ? source : transformed, t: transformed !== undefined ? 'ts' : 'js' };
} else
throw Error(
`Unsupported Content-Type "${contentType}" loading ${url}${fromParent(parent)}. Modules must be served with a valid MIME type like application/javascript.`
Expand Down Expand Up @@ -796,15 +796,16 @@ function processScript(script, ready = readyStateCompleteCnt > 0) {
if (script.lang === 'ts' && !script.src) {
const source = script.innerHTML;
return initTs()
.then(() =>
topLevelLoad(
.then(() => {
const transformed = esmsTsTransform(source, pageBaseUrl);
return topLevelLoad(
pageBaseUrl,
getFetchOpts(script),
esmsTsTransform(source, pageBaseUrl) || source,
undefined,
transformed === undefined ? source : transformed,
transformed === undefined,
undefined
)
)
);
})
.catch(throwError);
}
if (self.ESMS_DEBUG) console.info(`es-module-shims: checking script ${script.src || '<inline>'}`);
Expand Down
5 changes: 5 additions & 0 deletions test/test-ts.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@
window.inlineTypescript as test = true;
</script>

<script type="module" lang="ts">
window.inlineTypescriptNoWork = window.inlineTypescriptNoWork || 0;
window.inlineTypescriptNoWork++;
</script>

<div id="mocha"></div>
10 changes: 8 additions & 2 deletions test/typescript.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
suite('TypeScript loading tests', () => {
const timeoutInitPromise = new Promise(resolve => setTimeout(resolve, 1000));
test('Inline lang=ts support', async function () {
await new Promise(resolve => setTimeout(resolve, 1000));
await timeoutInitPromise;
assert.ok(globalThis.inlineTypescriptNoWork === 1);
});

test('Inline lang=ts support for no work', async function () {
await timeoutInitPromise;
assert.ok(globalThis.inlineTypescript === true);
});

test('Static TS script', async function () {
await new Promise(resolve => setTimeout(resolve, 1000));
await timeoutInitPromise;
assert.ok(globalThis.executedTs === true);
});

Expand Down

0 comments on commit 471c32f

Please sign in to comment.