Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gulpfile.babel.js does not compile imported es6 modules from node_modules #1234

Closed
shadowmint opened this issue Sep 7, 2015 · 12 comments
Closed

Comments

@shadowmint
Copy link

tldr; When running in 'babel' mode, gulp compiles local files from es6 to js to run them.
However, modules in node_modules are assumed to already be compiled and are not modified.

What's up with that?

long version

Repo showing the issue: https://github.com/shadowmint/tmp-gulp-test-babel

The error:

doug:gulptmp doug$ gulp
[11:17:18] Requiring external module babel-core/register
/Users/doug/gulptmp/node_modules/foo/index.js:1
(function (exports, require, module, __filename, __dirname) { export default f
                                                              ^^^^^^
SyntaxError: Unexpected reserved word
    at exports.runInThisContext (vm.js:73:16)
    at Module._compile (module.js:443:25)
    at Module._extensions..js (module.js:478:10)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/doug/gulptmp/node_modules/babel-core/lib/api/register/node.js:214:7)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (/Users/doug/gulptmp/gulpfile.babel.js:2:30)
    at Module._compile (module.js:460:26)

Modules in node_modules seem to not be compiled?

Although this... kind of makes sense, it's annoying in a very real sense.

This is the situation I currently have:

  • Project A has a module gulp setup with a variety of tasks defined in gulp/taskA.js, gulp/taskB.js, etc.
  • Project B uses Project A as a library.

Ideally, B could npm install A, and then use this from the local gulpfile.babel.js:

import * as scripts from 'A/gulp/tasks/scripts';

gulp.task('child-scripts', ['scripts']);

...and indeed, if A is in raw javascript, it works. However, if A uses es6 gulp files, the import fails (as shown in the linked example).

The solution is to clone A as a submodule in B, and that works too... but it's not ideal, for all the reasons that using npm at all makes sense.

So, two questions:

  1. Is there a good reason not to invoke babel on node_modules?

  2. Is this a 'bug' or a 'feature'; in that, will it ever be fixed, or is this something that I need to find a long term work around for?

...or am I just 'doing it wrong'? The documentation on gulpfile.bable.js is pretty sparse, but as far as I can see I would expect what I'm doing to work...?

@phated
Copy link
Member

phated commented Sep 7, 2015

All node modules should be compiled before publishing to npm. What you are doing is an anti-pattern.

@phated phated closed this as completed Sep 7, 2015
@shadowmint
Copy link
Author

It's not on npm, its installed using an npm git url.

@shadowmint
Copy link
Author

I'd also like to point out that what I'm doing here is not making package A in es6; package A is compiled from es6, using its own gulpfile.

What I'm trying to do is use package A's gulp config which is in es6.

@phated
Copy link
Member

phated commented Sep 7, 2015

Your prepublish script might run on install from github, if not, you can use an install script. No matter what, it is a packaging problem (thus the npm client) not a gulp problem.

@shadowmint
Copy link
Author

So you're suggesting the work around here is to have package A's gulpfile compile it's own gulpfile.babel.js to js and put that into some dist/gulp folder?

Seems like a hack, but fair enough. /shrug

@callumacrae
Copy link
Member

https://babeljs.io/docs/usage/require/

NOTE: By default all requires to node_modules will be ignored.

This isn't a gulp issue

@shadowmint
Copy link
Author

It actually reads:

NOTE: By default all requires to node_modules will be ignored.
You can override this by passing an ignore regex via:

Is there some way to control how gulp imports babel to customize this from
gulp then?

On Mon, Sep 7, 2015 at 5:12 PM, Callum Macrae notifications@github.com
wrote:

https://babeljs.io/docs/usage/require/

NOTE: By default all requires to node_modules will be ignored.

This isn't a gulp issue


Reply to this email directly or view it on GitHub
#1234 (comment).

@callumacrae
Copy link
Member

Could try this #830 (comment). Also read the other comments

@shadowmint
Copy link
Author

oh, thats quite a reasonable way of doing it~
thanks!

On Monday, September 7, 2015, Callum Macrae notifications@github.com
wrote:

Could try this #830 (comment)
#830 (comment)


Reply to this email directly or view it on GitHub
#1234 (comment).

@dsilva
Copy link

dsilva commented Mar 19, 2016

You could use the "ignore" option in your .babelrc but it's broken right now -- neither babel-cli nor babel-register are reading it: https://phabricator.babeljs.io/T6726

@akhilshastri
Copy link

@shadowmint & All, I am facing the same issue..? May I know what is the good approach..? I am bit confused by above comment..

The situation is same as @shadowmint has discussed, "dev-tools" is lib which has gulp task and "CAPP" imports the tasks from "dev-tools" module in gulpfile.babel.js when I run the gulp build it shows below error:

`C:\projects\app\CAPP>gulp build
[22:29:54] Requiring external module babel-register
C:\projects\app\CAPP\node_modules\dev-tools\index.js:6
import config from './tasks/config';
^^^^^^

SyntaxError: Unexpected token import
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:387:25)
at Module._extensions..js (module.js:422:10)
`

@phated
Copy link
Member

phated commented Mar 21, 2016

Support questions should be directed to StackOverflow. I am going to lock this issue, as it has become a dumping ground for all comments about babel.

@gulpjs gulpjs locked and limited conversation to collaborators Mar 21, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants