postinstall-build icon indicating copy to clipboard operation
postinstall-build copied to clipboard

babel presets not being used during postinstall

Open bdefore opened this issue 8 years ago • 10 comments

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 avatar Jun 22 '17 00:06 bdefore

@bdefore Thanks a ton for the bug report and repro! I spent a while debugging and have come up with a few fixes & suggestions.

  1. The first is a postinstall-build fix. It skips install of devDependencies when it thinks it already has them, one condition being that node_modules is not the parent directory. But this detection wasn't working for @scoped packages since the parent directory is the scope, not node_modules. I'll be opening a PR for that.
  2. 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-all exclude: /node_modules/ regex applies to everything under @foo/bar, because it's building in node_modules/@foo/bar – so all files are excluded by the rule. I recommend changing it to include: `${__dirname}/src` . (One hint is that it wasn't complaining about not being able to find babel-loader or any of the presets; rather, the loader wasn't operating on this file at all.)
  3. I recommend changing your resolve.modules and resolveLoader.modules settings. The ${__dirname}/node_modules entry should just be node_modules – that way, if some of your modules and loaders are flattened by npm (because they're shared with another dependency, like postinstall-build-error-dependent, or a sibling), webpack can still find them. As it's configured now, it will definitely break in some scenarios.
  4. You may have done this because of the issue in (1), but I recommend moving a bunch of those babel- dependencies to devDependencies. They'll be installed by postinstall-build when they're needed, but unless your project is some kinda JS transpilation playground, aren't needed as prod dependencies.

Incoming PR for issue (1)!

exogen avatar Jun 22 '17 05:06 exogen

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.

bdefore avatar Jun 22 '17 17:06 bdefore

I've also created a no-scope branch that builds successfully on postinstall with the library name to just bar

bdefore avatar Jun 22 '17 19:06 bdefore

@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 avatar Jun 22 '17 19:06 exogen

@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 avatar Dec 13 '17 21:12 killtheliterate

@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.

exogen avatar Dec 13 '17 21:12 exogen

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.

killtheliterate avatar Dec 13 '17 21:12 killtheliterate

@exogen scripts.prepare seems to do the trick.

killtheliterate avatar Dec 13 '17 21:12 killtheliterate

@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 avatar Dec 13 '17 22:12 exogen

@exogen no doubt, and thanks for looking into this. If there's anything else helpful I could provide for repro, lemme know.

killtheliterate avatar Dec 14 '17 04:12 killtheliterate