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

僤鯓⒐⒋嵵緔 提交于 2019-12-08 16:37:31

问题


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

> browserify-babelify-yarn-workspaces@1.0.0 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

> browserify-babelify-yarn-workspaces@1.0.0 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! browserify-babelify-yarn-workspaces@1.0.0 build: `browserify a/index.js -t babelify --outfile bundle.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the browserify-babelify-yarn-workspaces@1.0.0 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!


回答1:


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
  };
}



回答2:


This method will prevent the loading of your babelrc file, because your file is not in the root package. You can simply change your npm script to use following :

"scripts": {
    "build": "browserify a/index.js -t [babelify --presets [@babel/preset-env] ] --outfile bundle.js"
  }


来源:https://stackoverflow.com/questions/53341651/yarn-workspaces-and-browserify-package-json-in-subfolder-breaks-the-build

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!