1

我有一个需要与 Internet Explorer 11 兼容的 React 项目,在应用了一些兼容性 polyfill 后,我们仍然会收到错误消息。

SCRIPT1053:必须初始化常量

. 这似乎是由惰性函数引起的,但我在任何地方都找不到对此问题的任何参考。让我给你看一些代码:

应用程序.tsx

      const SandboxLazy = lazy(() => import('./microfronts/Sandbox'));

      export const App = () =>{
        return (
         <>
          <Header/>
          <Suspense fallback={<Progress />}>
            <Switch>
              <Route path="/sandbox">
                <MicrofrontErrorBoundary>
                  <SandboxLazy />
                </MicrofrontErrorBoundary>
              </Route>
            </Switch>
          </Suspense>
        </>
       );
      }

我们显然有比这更多的路线,如果我将 SandboxLazy 替换为 Sandbox 而没有惰性(包括它在项目中),一切正常。

这是我们的 webpack 和 babel 配置:

网页包:

const webpack = require('webpack');
const ModuleFederationPlugin = require('webpack/lib/container/ModuleFederationPlugin');
const microfront = require('./modules/microfront.jsx');
const package = require('../package.json');

module.exports = {
  getConfig(env) {
    console.log('ENV', env);
    const entries = microfront.getEntries(env);
    const shared = microfront.getSharedDeps(env);
    console.log('Current entries', entries);

    return {
      target: ['web', 'es5'],
      mode: 'development',
      entry: {
        app: [
          'react-app-polyfill/ie11',
          'react-app-polyfill/stable',
          'core-js/stable',
          './src/index.tsx',
        ],
      },
      output: {
        publicPath: '/',
      },
      devServer: {
        port: 8080,
        historyApiFallback: true,
      },
      plugins: [
        new ModuleFederationPlugin({
          name: 'container',
          remotes: entries,
          shared: shared,
        }),
        new webpack.DefinePlugin({
          ENV: JSON.stringify(env),
          CURRENT_VERSION: JSON.stringify(package.version),
          LOCALHOST_PATH: JSON.stringify(microfront.getLocalhostEntry()),
        }),
      ],
    };
  },
};

通天塔:

{
  "presets": [
    "@babel/preset-react",
    [
      "@babel/preset-env",
      {
        "corejs": {
          "version": "^3.15.2"
        },
        "useBuiltIns": "usage",
        "targets": {
          "edge": "17",
          "firefox": "60",
          "chrome": "67",
          "safari": "11.1",
          "ie": "11"
        }
      }
    ],
    "@babel/preset-typescript"
  ],
  "plugins": [
    "@babel/plugin-transform-runtime",
    "@babel/plugin-syntax-jsx",
    [
      "babel-plugin-styled-components",
      {
        "namespace": "host"
      }
    ]
  ]
}

有谁知道是什么问题?提前致谢

4

0 回答 0