babel编译的代码使用绝对导入 [英] babel transpiled code use absolute imports

查看:219
本文介绍了babel编译的代码使用绝对导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Transpiling code using @babel/cli with the following command:

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

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

produces a file which require imports using absolute path from my computer. Of course, running that on the server breaks as path is not found.

以下是绝对导入的示例:

Here is an example of absolute imports:

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";

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

Is there any way for the transpiled file to include everything it needs inline?

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

I don't use any specific babel.config.js or babelrc file at the moment.

推荐答案

问题,react-app预设具有名为absoluteRuntime的选项,默认情况下设置为true.因此,请尝试将其设置为false,如下所示.

As mentioned in the issue, the react-app preset has an option called absoluteRuntime which is set to true by default. So, try setting it as false as shown below.

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

最初的答案是由 VasiliKubarka 给出的.我只是为了别人的利益而在这里复制.

The original answer was given by VasiliKubarka in that issue. I'm just reproducing here for the benefit of others.

这篇关于babel编译的代码使用绝对导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆