首页 > 解决方案 > 如何强制禁用 Babeljs 生成的“使用严格”模式

问题描述

我正在尝试设置将 ES6 和更高版本的 Javascript 语法转换为 ES5 的 Babel 编译器。但是,我遇到了一个奇怪的问题,当它转换为 ES5 时,在行首添加了“use strict”,因为整个项目不是以严格模式编写的,因此会导致错误。我想禁用此功能。我在网上看到了几个解决方案,但没有一个对我有用。我按照这些步骤链接但没有工作。

这就是我的package.json样子:

{
  "name": "webplatform",
  "version": "1.0.0",
  "description": "Web interface Platform",
  "scripts": {
    "build": "babel htdocs/es6 -d htdocs/js"
  },
  "author": "harry",
  "license": "ISC",
  "devDependencies": {
    "@babel/cli": "^7.4.3",
    "@babel/core": "^7.4.3",
    "@babel/plugin-transform-strict-mode": "^7.2.0",
    "@babel/preset-env": "^7.4.3"
  }
}

.babelrc在这里

{
  "presets": ["@babel/preset-env"],
  "plugins": [
    ["@babel/plugin-transform-strict-mode", {
      "strict": false
    }]
  ]
}

当我运行命令npm run build时,它会转换一切都很好,只是“使用严格”把我弄乱了:(

标签: javascriptbabeljs

解决方案


推荐阅读