【问题标题】:SCRIPT1028: SCRIPT1028: Expected identifier, string or numberSCRIPT1028:SCRIPT1028:预期的标识符、字符串或数字
【发布时间】:2019-11-11 13:00:59
【问题描述】:

我正在开发一个 React 应用程序,但我无法在 Safari 和 IE 中打开此应用程序。

对于IE-11: SCRIPT1028: SCRIPT1028: Expected identifier, string or number

对于Safari(10.1.2) : SyntaxError: Unexpected token '...'. Expected a property name.

我的 package.json 文件是..

"dependencies": {
    "@babel/core": "^7.0.0",
    "@babel/plugin-proposal-object-rest-spread": "^7.6.2",
    "@storybook/addon-backgrounds": "^3.4.8",
    "@storybook/react": "^4.0.0-alpha.4",
    "@types/d3": "^5.0.0",
    "@types/debug": "^0.0.30",
    "@types/enzyme": "^3.1.9",
    "@types/jest": "^23.1.4",
    "@types/moment-timezone": "^0.5.4",
    "@types/react": "^16.9.11",
    "@types/react-dom": "^16.9.3",
    "@types/react-intl": "^2.3.16",
    "@types/react-mentions": "2.4.0",
    "@types/react-redux": "^7.1.5",
    "@types/react-router": "^5.1.2",
    "@types/react-router-dom": "^5.1.0",
    "@types/react-select": "^1.2.6",
    "@types/react-show-more": "^2.0.1",
    "@types/reactour": "^1.13.1",
    "@types/redux": "^3.6.0",
    "@types/redux-form": "^7.3.1",
    "@types/redux-logger": "^3.0.6",
    "@types/storybook__addon-backgrounds": "^3.2.0",
    "@types/storybook__react": "^3.0.8",
    "@types/styled-components": "^4.1.19",
    "awesome-typescript-loader": "^5.2.0",
    "axios": "^0.18.0",
    "babel-engine-plugin": "^0.3.0",
    "babel-loader": "^7.0.0",
    "babel-register": "^6.26.0",
    "connected-react-router": "^4.4.1",
    "cosed": "^1.1.8",
    "d3": "^5.7.0",
    "date-fns": "^1.29.0",
    "debug": "^3.1.0",
    "duplicate-package-checker-webpack-plugin": "^3.0.0",
    "emoji-mart": "^2.6.1",
    "enzyme": "^3.3.0",
    "enzyme-adapter-react-16": "^1.1.1",
    "gen-tester": "^3.1.2",
    "history": "^4.10.1",
    "html-loader": "^0.5.5",
    "html-webpack-plugin": "^3.1.0",
    "husky": "^0.14.3",
    "immer": "^1.3.1",
    "jest": "^23.3.0",
    "jest-environment-enzyme": "^6.0.2",
    "jest-enzyme": "^6.0.2",
    "jest-styled-components": "^6.0.0",
    "lint-staged": "^7.2.0",
    "moment-timezone": "^0.5.14",
    "prettier": "^1.13.7",
    "prop-types": "^15.7.2",
    "react": "^16.11.0",
    "react-cosed": "^1.0.9",
    "react-dom": "^16.11.0",
    "react-intl": "^2.8.0",
    "react-redux": "^5.1.1",
    "react-router": "^5.1.2",
    "react-router-dom": "^5.1.2",
    "react-select": "^1.2.1",
    "reactour": "^1.15.4",
    "redux": "^4.0.0",
    "redux-cosed": "^1.0.1",
    "redux-form": "^8.2.6",
    "redux-persist": "^5.10.0",
    "redux-thunk": "^2.3.0",
    "styled-components": "^4.4.1",
    "ts-jest": "^23.0.0",
    "tslint": "^5.9.1",
    "tslint-config-prettier": "^1.13.0",
    "tslint-no-circular-imports": "^0.5.0",
    "typescript": "^3.0.1",
    "uglifyjs-webpack-plugin": "^1.2.7",
    "url-loader": "^1.0.1",
    "webpack": "^4.15.1",
    "webpack-cli": "^3.0.8",
    "webpack-dev-server": "^3.1.5",
    "webpack-merge": "^4.1.3"
  },
  "devDependencies": {
    "@types/lodash.debounce": "^4.0.4",
    "firebase-tools": "^6.1.0",
    "lodash.debounce": "^4.0.8"
  }

如果需要更多详细信息,请告诉我。

请建议,我需要做什么......谢谢

我的webpack.js 文件是......

const path = require('path');
const HtmlWebPackPlugin = require('html-webpack-plugin');
const {
  CheckerPlugin,
  TsConfigPathsPlugin,
} = require('awesome-typescript-loader');
const DuplicatePackageCheckerPlugin = require('duplicate-package-checker-webpack-plugin');

const ROOT = path.resolve(__dirname, '..');

module.exports = {
  module: {
    rules: [
      {
        test: /.tsx?$/,
        use: 'awesome-typescript-loader',
        exclude: /node_modules/,
      },
      {
        test: /.(png|jpg|gif|svg)$/,
        use: [
          {
            loader: 'url-loader',
            options: {
              limit: 25000,
            },
          },
        ],
      },
      {
        test: /.html$/,
        use: 'html-loader',
      },
    ],
  },
  resolve: {
    extensions: ['.tsx', '.ts', '.js'],
    modules: [path.resolve(ROOT, 'shared'), 'node_modules'],
    plugins: [new TsConfigPathsPlugin()],
  },
  plugins: [
    new HtmlWebPackPlugin({
      template: path.join(ROOT, 'shared', 'index.html'),
      filename: './index.html',
    }),
    new CheckerPlugin(),
    new DuplicatePackageCheckerPlugin(),
  ],
};

我认为babel-loader 可以工作,但找不到任何使用 babel-loader 的方法。我需要这个应用程序也可以在 ie11 和 safari 上运行。

请提出一些解决方案...

【问题讨论】:

  • 请尝试参考this article安装react-app-polyfill包,并使react应用支持IE浏览器。您可以查看Babel Loader document 来使用它。另外,这里有一个similar thread,你可以参考一下,尝试添加“@babel/preset-flow”预设。

标签: reactjs internet-explorer webpack safari babel-loader


【解决方案1】:

问题在于,默认情况下,应用程序在 IE7 上呈现,即 - 不在支持转译构建的 IE11/Edge 上。所以我不得不提到元信息让浏览器知道预期的浏览器是 IE11/Edge。将此添加到 index.html 的 head 部分:

<meta http-equiv="X-UA-Compatible" content="IE=edge">

现在您可能会在控制台中看到一些错误,显示为 SCRIPT5009: 'Set' is undefined - 请参阅以下与 Set 集合类型的 polyfill 相关的链接:

React JavaScript Environment Requirements

有关该问题的更多信息,请参阅:

React app not rendering in IE11 and below

【讨论】:

    【解决方案2】:

    因为您是在 Microsoft Edge 中打开它,所以请尝试在最新的 chrome 版本中打开它......问题将得到解决......这适用于 ReactJs 项目

    【讨论】:

    • 在 IE、Edge 等设备上使用 React 应用程序很常见。这并不是一个真正有建设性的答案。
    猜你喜欢
    • 1970-01-01
    • 2020-09-05
    • 2019-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多