簡體   English   中英

在 babel.config.js 中包含 node_modules 時,當前未啟用對實驗語法 'jsx' 的支持

[英]Support for the experimental syntax 'jsx' isn't currently enabled when including node_modules in babel.config.js

我正在嘗試將我的應用程序中的所有代碼從 es6 轉換為 es5,因為我遇到了 Safari 9 和 IE11 的問題。

但是,當我在 babel.config.js 中包含 node_modules 時,出現以下錯誤

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 預設,但我仍然得到相同的錯誤,嘗試添加 plugin-syntax-jsx 時相同

我正在使用以下堆棧

使用 EXPO 反應本機 使用 Expo 反應本機

babel.config.js

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

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;
};

在我的 webpack 中包含插件對我有用

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;
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM