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

Babel standalone type module #8410

Closed

Conversation

lukejacksonn
Copy link

@lukejacksonn lukejacksonn commented Jul 31, 2018

Q                       A
Fixed Issues? Fixes babel/babel-standalone#96
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature? Yes
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes? No
License MIT

I have been building an in browser based dev environment that takes advantage of script type="module" with native static import statements. Everything worked great until I tried building an app that uses JSX. For that to work in the browser it needs to be transpiled.

I employed @babel/standalone to do this for me in the browser. It was at this point that I realised a conflict in script types; I needed <script type="text/babel"> to flag to babel standalone that the code was to be transpiled but also required <script type="module"> in order for native imports to work correctly.

I searched the babel-standalone repo for similar issues and found this babel/babel-standalone#96. So I added this comment babel/babel-standalone#96 (comment) questioning wether use of the type attribute was required. Some others replied saying they had similar issues but we got no reply for any maintainers and the issue was subsequently archived a week or so later.

So here is my suggestion but in working code. The changes are minimal but have the desired effect; if you mark a script with type="text/jsx/module" then babel standalone picks up that the script needs to be transpiled and adds type="module" to the resultant script tag which gets appended to the head.

Thanks for your time 🙇

@babel-bot
Copy link
Collaborator

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/8737/

1 similar comment
@babel-bot
Copy link
Collaborator

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/8737/

<pre id="output">Loading...</pre>

<script src="../babel.js"></script>
<script type="text/babel/module" data-presets='es2016'>
Copy link
Member

@loganfsmyth loganfsmyth Sep 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to do this, we should use a mimetype parameter like text/babel; goal=module and ideally parse the type a bit rather than doing a general .indexOf

Copy link
Author

@lukejacksonn lukejacksonn Sep 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be happy with that. I used indexOf because I saw it used here https://github.com/babel/babel/pull/8410/files#diff-7a71ff75920619cbc0e67883df263ca8L188. Is there anywhere in the codebase (or elsewhere) you can point me toward regard parsing something like the mimetype you suggest?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha, yeah that at least does type.split(";") so it's not as generic. I'd vote for

script.type.split(';').slice(1).some(meta => /^\s*type\s*=\s*module\s*$/.test(meta))

probably.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, thanks! I will give it a go 👍

@toqueteos
Copy link

I just found this PR, any status updates?

@nicolo-ribaudo
Copy link
Member

We added support for data-type="module", so that you can use native modules. Even if we went with another design, thanks for the PR!

#11466

@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Feb 10, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 10, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Change type attribute to other to use with native esm type="module"
5 participants