最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using ES Modules with babel-standalone - Stack Overflow

programmeradmin2浏览0评论

Quoting babel docs :

"If you want to use your browser's native support for ES Modules, you'd normally need to set a type="module" attribute on your script tag. With @babel/standalone, set a data-type="module" attribute instead"

For some reason though, when including my main index.js file (which imports other js / jsx files using import), it seems like babel is converting my import statements to require statements because I get the ReferenceError: require is not defined.

The only way around this I found was to use the transform-modules-umd plugin and include all my js files as scripts. Not sure if this is a bug where data-type="module" doesn't work or if I'm missing something.

These are my scripts tags in index.html

<script src="@16/umd/react.development.js" crossorigin></script>

<script src="@16/umd/react-dom.development.js" crossorigin></script>

<script src="/@babel/standalone/babel.min.js"></script>

<script src="index.js" type="text/babel" data-type="module"></script>

Thanks for the help

Quoting babel docs https://babeljs.io/docs/en/babel-standalone#usage :

"If you want to use your browser's native support for ES Modules, you'd normally need to set a type="module" attribute on your script tag. With @babel/standalone, set a data-type="module" attribute instead"

For some reason though, when including my main index.js file (which imports other js / jsx files using import), it seems like babel is converting my import statements to require statements because I get the ReferenceError: require is not defined.

The only way around this I found was to use the transform-modules-umd plugin and include all my js files as scripts. Not sure if this is a bug where data-type="module" doesn't work or if I'm missing something.

These are my scripts tags in index.html

<script src="https://unpkg./react@16/umd/react.development.js" crossorigin></script>

<script src="https://unpkg./react-dom@16/umd/react-dom.development.js" crossorigin></script>

<script src="https://unpkg./@babel/standalone/babel.min.js"></script>

<script src="index.js" type="text/babel" data-type="module"></script>

Thanks for the help

Share Improve this question asked Sep 14, 2020 at 11:49 George KamarGeorge Kamar 1541 silver badge12 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

For (my) future reference, here's a plete working example.

You can just put the code below into index.html and run it in a web browser and you'll have a working React app with JSX.

source: https://codesandbox.io/s/dikoh?file=/index.html

  <!DOCTYPE html>
  <html lang="en">
    <head>
      <meta charset="UTF-8" />
      <meta name="viewport" content="width=device-width, initial-scale=1.0" />
      <meta http-equiv="X-UA-Compatible" content="ie=edge" />
      <title>Static Template</title>
    </head>
    <body>
      <div id="main"></div>
  
      <script src="https://unpkg./@babel/standalone/babel.min.js"></script>
      <script type="text/babel" data-presets="react" data-type="module">
        import {
          ChakraProvider,
          Box,
          Text
        } from "https://cdn.skypack.dev/@chakra-ui/react";
        import React, { useRef } from "https://cdn.skypack.dev/react";
        import ReactDOM from "https://cdn.skypack.dev/react-dom";
  
        const MyText = ({ color, ...props }) => {
          return <Text fontWeight="bold" as="span" {...props} />;
        };
  
        function Foo() {
          return (
            <div>
              Foo
            </div>
          );
        }
  
        function App() {
          return (
            <Box>
              Hello <MyText>Skypack</MyText>
              <Foo/>
            </Box>
          );
        }
  
        ReactDOM.render(
          <ChakraProvider>
            <App />
          </ChakraProvider>,
          document.getElementById("main")
        );
      </script>
    </body>
  </html>

My question was answered by Amareis on babel's github page: https://github./babel/babel/discussions/12059

The problem is babel doesn't transpile modules imported through ES modules, they have to explicitly be included as scripts with the "type" set to "text/babel". So a JSX file imported through ES modules in the index.js script is imported after index.js is transpiled by babel, and is not itself transpiled.

Also got a suggestion to fetch and execute my scripts with a service worker for development purposes. babel-standalone transforms them after fetching

发布评论

评论列表(0)

  1. 暂无评论