Babel Standalone Typescript Preset allExtensions not working
See original GitHub issueBug Report
Input Code
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
<body>
The content of the document......
<div id="output"></div>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<script>
Babel.registerPreset("my-preset", {
presets: [
[Babel.availablePresets["typescript"], { allExtensions: true }]
]
});
</script>
<!-- Your custom script here -->
<script src="index.ts.js" type="text/babel" data-type="module" data-presets="my-preset"></script>
</body>
</html>
// index.ts.js
import message from './message.ts.js';
document.getElementById('output').innerHTML = message;
// message.ts.js
let message: number = null;
message = 300;
export default message;
I get the error:
Uncaught SyntaxError: Unexpected token ':'
from message.ts.js in the browser.
I wish i could call the file .ts but Browser only allows loading .js files because of strict mime type checking when loading files via import statement.
Expected behavior This should work since allExtensions should parse any file as a Typescript File.
Or am i missing something here ? Documentation says:
Indicates that every file should be parsed as TS or TSX (depending on the isTSX option).
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
babel/preset-typescript
This is so that we know that the import is not a type import, and should not be removed. allExtensions. boolean , defaults...
Read more >Use typescript with Babel 7 Standalone - Stack Overflow
In order to transpile typescript with Babel standalone, a filename must be specified in the settings passed to Babel.transform , otherwise a ...
Read more >babeljs typescript Code Example - Code Grepper
"compilerOptions": { // Ensure that .d.ts files are created by tsc, but not .js files "declaration": true, "emitDeclarationOnly": true, // Ensure that Babel...
Read more >@babel/preset-typescript · Babel 中文网
Replace the function used when compiling JSX fragment expressions. This is so that we know that the import is not a type import,...
Read more >JSX | Code Cookbook - Michael Currin
You can add Babel Standalone as a package on your frontend. ... From my own testing, it looks like this approach does not...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Browsers doesn’t support hooking into
import
statements: we can only transpile files directly included with<script>
tags.@nicolo-ribaudo Thanks for clarifying. Now I have one more question, since I can’t find documentation (or even answers on Stack Overflow) on how to use
@babel/standalone
with TypeScript React code: it seems that using thetypescript
preset alone isn’t enough, even withisTSX
andallExtensions
set totrue
.Here’s an example. Ignoring the actual React code, can it be simplified somehow? I’m specifically wondering about the arguments passed to
Babel.registerPreset
in combination with the value of thedata-presets
attribute on the last<script>
tag. What’s the minimum configuration necessary to simply strip types and then transform the JSX so it runs in a modern browser (e.g. V8/Chrome)?