babel翻译代码使用绝对导入

时间:2018-10-03 06:42:12

标签: javascript babeljs

使用@babel/cli通过以下命令来转译代码:

babel --plugins @babel/plugin-transform-arrow-functions www/js/origin.js --out-file www/js/result.js

生成一个文件,该文件要求使用我计算机中的绝对路径导入。当然,在服务器上运行该命令会因为找不到路径而中断。

以下是绝对导入的示例:

import _slicedToArray from "/Users/myself/project/node_modules/@babel/runtime/helpers/builtin/es6/slicedToArray";
import _createClass from "/Users/myself/project/node_modules/@babel/runtime/helpers/builtin/es6/createClass";
import _inherits from "/Users/myself/project/node_modules/@babel/runtime/helpers/builtin/es6/inherits";
import _possibleConstructorReturn from "/Users/myself/project/node_modules/@babel/runtime/helpers/builtin/es6/possibleConstructorReturn";
import _getPrototypeOf from "/Users/myself/project/node_modules/@babel/runtime/helpers/builtin/es6/getPrototypeOf";
import _classCallCheck from "/Users/myself/project/node_modules/@babel/runtime/helpers/builtin/es6/classCallCheck";
import _typeof from "/Users/myself/project/node_modules/@babel/runtime/helpers/builtin/es6/typeof";

转译后的文件是否可以以任何方式内联包含它需要的所有内容?

我目前不使用任何特定的babel.config.jsbabelrc文件。

1 个答案:

答案 0 :(得分:1)

issue中所述,react-app预设具有一个名为absoluteRuntime的选项,默认情况下设置为true。因此,请尝试将其设置为false,如下所示。

 "presets": [
["react-app", { "absoluteRuntime": false }],]

该问题的原始答案由VasiliKubarka给出。我只是为了别人的利益而在这里复制。

相关问题