简体   繁体   English

babel 转译代码使用绝对导入

[英]babel transpiled code use absolute imports

Transpiling code using @babel/cli with the following command:使用@babel/cli和以下命令转译代码:

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?有什么方法可以让转译的文件内联包含它需要的所有内容?

I don't use any specific babel.config.js or babelrc file at the moment.我目前不使用任何特定的babel.config.jsbabelrc文件。

As mentioned in the issue , the react-app preset has an option called absoluteRuntime which is set to true by default.issue中所述,react-app 预设有一个名为absoluteRuntime的选项,默认设置为true So, try setting it as false as shown below.因此,尝试将其设置为false ,如下所示。

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

The original answer was given by VasiliKubarka in that issue. VasiliKubarka在该问题中给出了原始答案。 I'm just reproducing here for the benefit of others.我只是为了他人的利益在这里复制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM