babel presets not being used during postinstall
I've encountered an issue where a library I've made that relies on Webpack and the Babel "react" preset, and relies on postinstall-build to build in dependent projects, fails when built there. Here's a trimmed down version that reproduces the issue:
https://github.com/bdefore/postinstall-build-error-dependent https://github.com/bdefore/postinstall-build-error-library
The library project can successfully run npm run build:react but when it does so in the postinstall step of the dependent project, it fails to parse JSX.
Here's a gist of the terminal output of the failure: https://gist.github.com/bdefore/cb4269649b7e3e4e42dcf1f52d573f9a
The full npm-debug.log is checked in here: https://raw.githubusercontent.com/bdefore/postinstall-build-error-dependent/master/npm-debug.log
@bdefore Thanks a ton for the bug report and repro! I spent a while debugging and have come up with a few fixes & suggestions.
- The first is a
postinstall-buildfix. It skips install ofdevDependencieswhen it thinks it already has them, one condition being thatnode_modulesis not the parent directory. But this detection wasn't working for@scopedpackages since the parent directory is the scope, notnode_modules. I'll be opening a PR for that. - But even with the fix from (1), the build is still broken. The problem is in your loader config on this line. Because we're building as a dependency under
node_modules, the catch-allexclude: /node_modules/regex applies to everything under@foo/bar, because it's building innode_modules/@foo/bar– so all files are excluded by the rule. I recommend changing it toinclude: `${__dirname}/src`. (One hint is that it wasn't complaining about not being able to findbabel-loaderor any of the presets; rather, the loader wasn't operating on this file at all.) - I recommend changing your
resolve.modulesandresolveLoader.modulessettings. The${__dirname}/node_modulesentry should just benode_modules– that way, if some of your modules and loaders are flattened by npm (because they're shared with another dependency, likepostinstall-build-error-dependent, or a sibling), webpack can still find them. As it's configured now, it will definitely break in some scenarios. - You may have done this because of the issue in (1), but I recommend moving a bunch of those
babel-dependencies todevDependencies. They'll be installed bypostinstall-buildwhen they're needed, but unless your project is some kinda JS transpilation playground, aren't needed as proddependencies.
Incoming PR for issue (1)!
Thanks @exogen for such a quick and thorough response.
I had a hunch that the scoping was causing path issues. I've pushed an update to the repos addressing your comments 2-4, and removed a lot of extraneous dependencies. It fails differently now, not finding Webpack, but that may be due to point 1. May serve well as a test case for your PR.
I've also created a no-scope branch that builds successfully on postinstall with the library name to just bar
@bdefore Thanks a ton for the updates! I'll be working on this today. There are also some potential issues with support for local-folder deps in npm@5, so I'm hoping to fix both in the same release.
@exogen Having a sort of similar problem, but blowing up on not being able to find the jsx loader. Interestingly, some loaders do work.
https://gist.github.com/killtheliterate/3ebec9187f2ca16f20045a0ee06b1838
This is with npm 5.6.0
@killtheliterate Thanks for the info! Could you post your package.json as well? Also just making sure: do you have your .babelrc (or wherever the config comes from) ignored by npm / in the files list? It'll need to be included in the npm package for the build step to find it.
Also I should update the README with this info, but depending on the Yarn / npm versions you'd like to support, it might be possible to begin migrating off postinstall-build entirely, because newer versions of npm basically do the equivalent of postinstall-build if you specify a prepare script (install all the devDependencies temporarily, etc.). Newer versions of npm do this, but last I checked Yarn didn't.
Ah, thanks for the prepare tip! Will explore that a bit.
Here's package.json — https://gist.github.com/killtheliterate/a9bb9a79555057b764dbd1e75d9cf8fb
I've got the .babelrc included—trying another tactic of using the inline config directly in webpack.config.
@exogen scripts.prepare seems to do the trick.
@killtheliterate Awesome. I'm still going to look at this since it should work. Just be aware that prepare might not work yet for other folks using older npm or Yarn.
@exogen no doubt, and thanks for looking into this. If there's anything else helpful I could provide for repro, lemme know.