Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yarn Workspaces and Browserify - package.json in subfolder breaks the build

My ultimate goal is to use Yarn Workspaces in a project using Browserify and Babel 7. This is a minimal reproduction of a problem I'm having. Basically it seems that the presence of a package.json file in a subfolder (which is one of the things that you have when using Yarn Workspaces) breaks my Browserify build, and I can't figure out why.

Here's a GitHub repo with a minimal reproduction of the problem.

First, install the dependencies (you can use yarn or npm, doesn't matter):

$ npm install

Then confirm the Browserify+Babel build works:

$ npm run build

> [email protected] build /home/user/projects/browserify-babelify-yarn-workspaces
> browserify a/index.js -t babelify --outfile bundle.js

Yay, all is good! My compiled code is in bundle.js.

Now let's make a dummy package.json within the a folder:

$ echo "{}" > a/package.json

That shouldn't change the build, right? Wrong:

$ npm run build

> [email protected] build /home/user/projects/browserify-babelify-yarn-workspaces
> browserify a/index.js -t babelify --outfile bundle.js


/home/user/projects/browserify-babelify-yarn-workspaces/a/index.js:1
import lib from "./lib.js";
^
ParseError: 'import' and 'export' may appear only with 'sourceType: module'
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] build: `browserify a/index.js -t babelify --outfile bundle.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] build 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!     /home/user/.npm/_logs/2018-11-16T15_58_43_540Z-debug.log

I can't figure out why this is happening. Any help would be appreciated!

like image 362
dumbmatter Avatar asked Nov 16 '18 16:11

dumbmatter


1 Answers

I think my problem is actually described in the Babel docs. https://babeljs.io/docs/en/config-files#monorepos says that the solution is to use a babel.config.js file rather than a .babelrc file. Deleting .babelrc and putting this in babel.config.js does seem to work:

module.exports = function (api) {
  api.cache(true);

  const presets = ["@babel/preset-env"];
  const plugins = [];

  return {
    presets,
    plugins
  };
}
like image 184
dumbmatter Avatar answered Nov 04 '22 22:11

dumbmatter