@babel/plugin-transform-runtime 能否不要在编译后的文件中单独使用 import 引入 呢?

@babel/plugin-transform-runtime 能否不要在编译后的文件中单独使用 import 引入 呢?

场景

使用 babel-cli 对传统前端中的 js 文件进行编译,然而在吾辈引入了 @babel/plugin-transform-runtime 插件后却发现编译后的 js 文件顶部使用 import 引入了 helpers 中的函数,问题是吾辈不可能使用模块化,所以有什么解决方案么?

babel 配置

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "modules": false
      }
    ]
  ],
  "plugins": [
    "@babel/plugin-proposal-class-properties",
    "@babel/plugin-transform-runtime"
  ]
}

源代码

/**
 * 公共的 JavaScript 代码片段
 * @author rxliuli
 */
const global = new class Global {
}()

编译后的代码

import _classCallCheck from "@babel/runtime/helpers/classCallCheck";

/**
 * 公共的 JavaScript 代码片段
 * @author rxliuli
 */
var global = new function Global() {
  _classCallCheck(this, Global);
}();

吾辈不想在源文件中使用 import 引入函数,而更想要在公共的 html 头文件中使用 script 引入 helpers 下面用到的函数,有什么解决方法么?

理想的效果

全局引入 js 文件

<!doctype html>
<html
  lang="zh-CN"
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:th="http://www.thymeleaf.org"
>
<body>
    <!--全局引入帮助函数 js 文件-->
    <script src="/static/js/lib/babel/babel-runtime.min.js"></script>
</body>
</html>

编译后的代码

/**
 * 公共的 JavaScript 代码片段
 * @author rxliuli
 */
var global = new function Global() {
  _classCallCheck(this, Global);
}();
阅读 2k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题