毛线工作区和Browserify-子文件夹中的package.json破坏了构建

时间:2018-11-16 16:13:53

标签: javascript browserify babel babelify yarn-workspaces

我的最终目标是在使用Browserify和Babel 7的项目中使用Yarn工作区。这是我所遇到问题的最小再现。基本上看来,子文件夹中存在package.json文件(这是您使用Yarn Workspaces时所拥有的东西之一)破坏了我的Browserify构建,而我不知道为什么。

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

首先,安装依赖项(可以使用yarn或npm,没关系):

$ npm install

然后确认Browserify + Babel构建工作正常:

$ 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

是的,一切都很好!我的编译代码在bundle.js中。

现在让我们在a文件夹中创建一个虚拟package.json:

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

那不应该改变版本,对吗?错误:

$ 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

我不知道为什么会这样。任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:4)

我认为我的问题实际上在Babel文档中有所描述。 https://babeljs.io/docs/en/config-files#monorepos说,解决方案是使用babel.config.js文件而不是.babelrc文件。删除.babelrc并将其放入babel.config.js似乎确实有效:

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

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

  return {
    presets,
    plugins
  };
}

答案 1 :(得分:0)

method将阻止加载babelrc文件,因为您的文件不在根软件包中。您只需更改npm脚本即可使用以下内容:

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