6

我开发了一个使用 ES2015 代码和 babel 作为编译器的 javascript CLI 应用程序。(babel-require 钩子)

该应用程序在本地完美运行,但是当我在 npm 上发布时,它停止工作(babel 似乎不再编译 ES2015 文件)

设置:

示例./bootstrap.js(ES5):

require('babel-register');
require('./src/app.js');

样本./src/app.js(ES2015):

import package from 'package';
...

样品./bin/myapp

#!/usr/bin/env node
require('../bootstrap.js');

在本地运行有效:

appdir$ ./bin/myapp
I'm running fine!
appdir$

全局运行(在 npm install 之后)中断:

$ sudo npm install -g myapp
└── myapp@0.1.0
$ myapp
/usr/local/lib/node_modules/myapp/src/app.js:1
(function (exports, require, module, __filename, __dirname) { import package from 'package';
                                                              ^^^^^^

SyntaxError: Unexpected token import
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:404:25)
    at Module._extensions..js (module.js:432:10)
    at Object.require.extensions.(anonymous function) [as .js] (/usr/local/lib/node_modules/myapp/node_modules/babel-register/lib/node.js:138:7)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:313:12)
    at Module.require (module.js:366:17)
    at require (module.js:385:17)
    at Object.<anonymous> (/usr/local/lib/node_modules/myapp/server.js:9:12)
    at Module._compile (module.js:425:26)   

版本:

    ├─┬ babel-register@6.4.3
    │ │ ├─┬ babel-core@6.4.5
    │ │ │ ├─┬ babel-code-frame@6.3.13
    │ │ │ ├─┬ babel-generator@6.4.5
    │ │ │ ├── babel-helpers@6.4.5
    │ │ ├── babel-runtime@5.8.35

我试过的:

  • 添加ignore: false我的.babelrc文件,希望它能启用编译
  • 在需要钩子参数中使用选项而不是.babelrc文件

没运气 :/

4

1 回答 1

12

好的,我弄清楚了问题所在。我在正确的轨道上认为有一些事情禁止编译发生。

TL;博士

babel-registerhook 不从文件中获取ignoreonly选项.babelrc,但它从其参数中获取。

修复./bootstrap.js

require('babel-register')({
  ignore: [],
  only: [/src/],
});
require('./src/app.js');
  • ignore开关将禁用忽略node_modules在其路径中匹配的文件,这是每个全局模块的情况。
  • 然后,该开关启用我项目目录only的文件的编译。src

现在,如果您有兴趣,我将描述我为解决问题所采取的步骤,因为我认为我做对了(这次:))...

故障排除的故事:

  • 首先,我需要安装node-debug命令行工具

    $ sudo npm install -g node-inspector
    
  • 然后用它启动我的应用程序(--debug-brk开关要求在第一行停止执行)

    $ node-debug --debug-brk myapp
    
  • 在提供的本地 URL 上打开我的浏览器,node-debug瞧瞧的应用程序代码

  • require('./scr/app.js');在语句正上方的行放置一个断点
  • 单击“播放”以继续执行流程,直到该行
  • 这里开始使用“越过”和“进入”按钮攀登错误跟踪的繁琐任务:当下一个语句位于错误跟踪的“外部”时(请参阅问题中的上述那个),我可以“越过” . 如果下一条语句是跟踪的功能之一,则“步入”。
  • 然后我意识到,通过查看“范围变量”手表,当在Object.require.extension方法中时,在文件babel-register/lib/node.js中,ignore变量onlyundefined,尽管我希望它们被定义!
  • 我进入shouldIgnore了同一个文件的方法,这证实了我的恐惧:这个函数node_modules在路径中检查是否匹配,如果匹配则!ignore && !only返回true(忽略文件)。这最终导致我的 ES2015 文件无法编译。

    babel-register/lib/node.js:120:
    function shouldIgnore(filename) {
      if (!ignore && !only) {  // here `ignore` and `only` were null, despite .babelrc values
        return getRelativePath(filename).split(_path2["default"].sep).indexOf("node_modules") >= 0;
      } else {
        return _babelCore.util.shouldIgnore(filename, ignore || [], only);
      }
    }
    
  • 然后我猜想我必须直接在babe-register参数中指定这些开关。

于 2016-01-31T23:20:45.970 回答