babel: cli and register: sub-packages are not compiled, .babelrc ignored
Bug Report
Current Behavior Using the following directory structure:
package.json
server.js
components/cmp1/package.json
components/cmp1/.babelrc
components/cmp1/core.js
Babel CLI and babel register ignore the sub-package in components/cmp1. For some reason, the only way to get it to work is if the contents of .babelrc are passed directly to @babel/register. Below you will find the code, but first the detailed errors and outputs.
It is not possible to compile core.js from the root directory using the CLI:
$ babel components/cmp1/core.js
import path from 'path';
console.log(path.basename("test.js"));
This should have transformed the import statement.
Neither is it possible to require the file using @babel/register if you don’t pass any options to it:
$ npm start
> babel-bug@ start /babel-bug
> node server.js
/babel-bug/components/cmp1/core.js:1
(function (exports, require, module, __filename, __dirname) { import path from 'path';
^^^^
SyntaxError: Unexpected identifier
at new Script (vm.js:73:7)
at createScript (vm.js:245:10)
at Object.runInThisContext (vm.js:297:10)
at Module._compile (internal/modules/cjs/loader.js:657:28)
at Module._compile (/babel-bug/node_modules/pirates/lib/index.js:83:24)
at Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Object.newLoader [as .js] (/babel-bug/node_modules/pirates/lib/index.js:88:7)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! babel-bug@ start: `node server.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the babel-bug@ start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /.npm/_logs/2018-08-27T11_02_54_837Z-debug.log
Input Code
server.js:
require('@babel/register')
// ({ // BUG: without options, core.js is not transformed! Even thought there is a .babelrc
// "presets": [
// [
// "@babel/preset-env",
// {
// "targets": {
// "node": "current",
// "browsers": "> 0.5%, not dead"
// }
// }
// ]
// ]
// });
require('./components/cmp1/core.js');
components/cmp1/core.js:
import path from 'path';
console.log(path.basename("test.js"));
Expected behavior/code I would expect the following:
@babel/registerlooks into the closest.babelrcand/orpackage.jsonand uses that config- babel CLI works from the root directory and compiles the given file(s) according to the nearest config
After I cd components/cmp1, babel CLI correctly complains about multiple configuration files (package.json and .babelrc), so I would also expect it to do the same when called from the root, but it doesn’t, it just ignores both.
Babel Configuration (.babelrc, package.json, cli command)
package.json
{
"name": "babel-bug",
"scripts": {
"start": "node server.js"
},
"devDependencies": {
"@babel/core": "7.0.0-rc.3",
"@babel/preset-env": "7.0.0-rc.3",
"@babel/register": "7.0.0-rc.3"
},
"babel": {
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "current",
"browsers": "> 0.5%, not dead"
}
}
]
]
}
}
components/cmp1/package.json
{
"name": "component",
"devDependencies": {
"@babel/core": "7.0.0-rc.3",
"@babel/register": "7.0.0-rc.3",
"@babel/preset-env": "7.0.0-rc.3"
},
"babel": {
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "current",
"browsers": "> 0.5%, not dead"
}
}
]
]
}
}
components/cmp1/.babelrc
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "current",
"browsers": "> 0.5%, not dead"
}
}
]
]
}
Environment
- Babel version(s): v7.0.0-rc.3
- Node/npm version: Node 10.9/npm 6.2
- OS: Linux
- How you are using Babel:
cli,register
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 16 (4 by maintainers)
workaround run babel with:
My 0.02 EUR
Nice release candidate guys! I’m having the same problem too