Pan*_*olo 5 node.js npm babeljs babel-register
我开发了一个javascript CLI应用程序,它使用ES2015代码和babel作为编译器.(babel-require hook)
该应用程序在本地完美运行,但当我在npm上发布时,它停止工作(babel似乎不再编译ES2015文件)
建立:
样本./bootstrap.js(ES5):
require('babel-register');
require('./src/app.js');
Run Code Online (Sandbox Code Playgroud)
样本./src/app.js(ES2015):
import package from 'package';
...
Run Code Online (Sandbox Code Playgroud)
样品./bin/myapp:
#!/usr/bin/env node
require('../bootstrap.js');
Run Code Online (Sandbox Code Playgroud)
在本地运行:
appdir$ ./bin/myapp
I'm running fine!
appdir$
Run Code Online (Sandbox Code Playgroud)
全局运行(在npm安装之后)中断:
$ 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)
Run Code Online (Sandbox Code Playgroud)
版本:
??? 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
Run Code Online (Sandbox Code Playgroud)
我尝试过的:
ignore: false我的.babelrc文件希望它能够启用编译.babelrc文件没运气 :/
Pan*_*olo 12
好的,我想出了问题所在.我在正确的轨道上认为有些东西禁止编译发生.
TL; DR
babel-registerhook不从文件中获取ignore和only选项.babelrc,但它从其参数中获取.
修复./bootstrap.js:
require('babel-register')({
ignore: [],
only: [/src/],
});
require('./src/app.js');
Run Code Online (Sandbox Code Playgroud)
ignore开关将禁用忽略匹配哪些文件node_modules在他们的道路,这是对的情况下每个全局模块.only交换机可以编译项目src目录的文件.现在,如果你有兴趣,我会描述我为解决这个问题所采取的步骤,因为我认为我做得对(这一次:))...
故障排除故事:
首先,我需要安装node-debug命令行工具
$ sudo npm install -g node-inspector
Run Code Online (Sandbox Code Playgroud)然后用它启动我的应用程序(--debug-brk开关要求在第一行停止执行)
$ node-debug --debug-brk myapp
Run Code Online (Sandbox Code Playgroud)在所提供的本地URL打开我的浏览器node-debug,并瞧看到我的应用程序代码
require('./scr/app.js');语句正上方Object.require.extension方法中,在文件中babel-register/lib/node.js,那个ignore和only变量undefined,尽管我希望它们被定义!我介入shouldIgnore了同一个文件的方法,这证实了我的恐惧:这个函数node_modules在路径中检查,if !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);
}
}
Run Code Online (Sandbox Code Playgroud)然后我猜想我必须直接在babe-register参数中指定thoses开关.
| 归档时间: |
|
| 查看次数: |
1617 次 |
| 最近记录: |