首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在将node_modules包括在babel.config.js中时,目前还没有启用对实验性语法'jsx‘的支持

在将node_modules包括在babel.config.js中时,目前还没有启用对实验性语法'jsx‘的支持
EN

Stack Overflow用户
提问于 2020-11-27 06:27:10
回答 1查看 1.6K关注 0票数 0

我试图将应用程序中的所有代码从es6转到es5,因为我对Safari 9和IE11有问题。

但是,当我在我的node_modules中包含babel.config.js时,我会得到以下错误

代码语言:javascript
复制
SyntaxError: /Users/salmanmohamed/Documents/apps/rapioUserApp/App.js: Support for the experimental syntax 'jsx' isn't currently enabled (41:5):

  39 |   //return <Text>Hii</Text>;
  40 |   return loading ? (
> 41 |     <LoadingLayout>
     |     ^
  42 |       <LoadingText>{copy.loading.texts.fonts}</LoadingText>
  43 |       <ActivityIndicator />
  44 |     </LoadingLayout>

Add @babel/preset-react (https://git.io/JfeDR) to the 'presets' section of your Babel config to enable transformation.
If you want to leave it as-is, add @babel/plugin-syntax-jsx (https://git.io/vb4yA) to the 'plugins' section to enable parsing.

我尝试过添加react预置,但仍然会出现相同的错误,在添加插件语法-jsx时也是如此。

我正在使用以下堆栈

对世博的反应与世博的本土化

babel.config.js

代码语言:javascript
复制
module.exports = function (api) {
  api.cache(true);
  return {
    //exclude: [/\bcore-js\b/, /\bwebpack\/buildin\b/,/\bnode_modules/],
    include: /(node_modules)/,
    test: /\.(tsx?)|(js)$|(jsx)$/,

    presets: [
      ["@babel/react"],
      ["@babel/preset-typescript"],
      [
        "@babel/preset-env",
        {
          corejs: { version: 3 },
          useBuiltIns: "usage",
          targets: {
            esmodules: true,
            browsers: [
              "last 5 versions",
              "ie >= 9",
              " safari >= 7",
              "ios_saf >= 9",
            ],
          },
          loose: true,
        },
      ],
      [
        "babel-preset-expo",
        {
          corejs: { version: 3 },
          targets: {
            esmodules: true,
            browsers: [
              "last 5 versions",
              "ie >= 9",
              "safari >= 7",
              "ios_saf >= 9",
            ],
          },
        },
      ],
    ],
    plugins: [
      "@babel/plugin-proposal-class-properties",
      "@babel/plugin-transform-computed-properties",
      [
        "module-resolver",
        {
          alias: {
            "@Components": "./components",
            "@Containers": "./containers",
            "@Hooks": "./hooks",
            "@Controllers": "./controllers",
            "@Assets": "./assets",
            "@Helpers": "./helpers",
            "@Actions": "./actions",
            "@Services": "./services",
            "@Utils": "./utils",
          },
        },
      ],
    ],
  };
};

webpack.config.js

代码语言:javascript
复制
const createExpoWebpackConfigAsync = require("@expo/webpack-config");
const { getExpoBabelLoader } = require("@expo/webpack-config/utils");

const modulesToTranspile = [
  "ansi-regex",
  "ansi-styles",
  "chalk",
  "react-dev-utils",
];

module.exports = async function (env, argv) {
  env.babel = { dangerouslyAddModulePathsToTranspile: modulesToTranspile };

  const config = await createExpoWebpackConfigAsync(env, argv);
  // Customize the config before returning it.
  const loader = getExpoBabelLoader(config);
  if (loader) {
    // Modify the loader...
    loader.include("@babel/polyfill");
    loader.include("react-app-polyfill");
    loader.include("react-app-polyfill/ie9");
    loader.include("react-app-polyfill/ie11");

    loader.include("core-js");
    // console.warn(loader)
  }
  return config;
};
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-27 10:20:49

,包括我webpack的插件在内,为我工作过

代码语言:javascript
复制
const createExpoWebpackConfigAsync = require("@expo/webpack-config");
const { getExpoBabelLoader } = require("@expo/webpack-config/utils");

const modulesToTranspile = [
  "ansi-regex",
  "ansi-styles",
  "chalk",
  "react-dev-utils",
  "@react-navigation",
  "styled-components",
  "node_modules",
];

module.exports = async function (env, argv) {
  env.babel = { dangerouslyAddModulePathsToTranspile: modulesToTranspile };

  const config = await createExpoWebpackConfigAsync(env, argv);
  // Customize the config before returning it.
  const loader = getExpoBabelLoader(config);
  if (loader) {
    loader.include("@babel/plugin-proposal-class-properties");
    loader.include("@babel/plugin-transform-arrow-functions");
    loader.include("@babel/plugin-transform-block-scoping");
    loader.include("@babel/plugin-transform-sticky-regex");
    loader.include("@babel/plugin-transform-unicode-regex");
    loader.include("@babel/plugin-transform-dotall-regex");
    loader.include("@babel/plugin-transform-named-capturing-groups-regex");
    loader.include("@babel/plugin-transform-runtime");

    // console.warn(loader)
  }
  return config;
};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65032925

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档