代码之家  ›  专栏  ›  技术社区  ›  Salman

在中包含node\u模块时,当前未启用对实验语法“jsx”的支持babel.config.js文件

  •  0
  • Salman  · 技术社区  · 5 年前

    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.
    

    我用的是下面的堆栈

    与世博会互动 通过Expo对web作出反应

    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;
    };
    
    
    0 回复  |  直到 5 年前
        1
  •  0
  •   Salman    5 年前

    把插件放在我的网页包里对我有用

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