Yarn Workspaces和Browserify - 子文件夹中的package.json会破坏构建 [英] Yarn Workspaces and Browserify - package.json in subfolder breaks the build
问题描述
我的最终目标是在使用Browserify和Babel 7的项目中使用纱线工作区。这是我遇到的问题的最小复制品。基本上似乎在子文件夹中存在一个package.json文件(这是使用Yarn Workspaces时的一个东西)会破坏我的Browserify构建,我无法弄清楚原因。
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.
首先,安装依赖项(你可以使用yarn或npm,无所谓):
First, install the dependencies (you can use yarn or npm, doesn't matter):
$ npm install
然后确认Browserify + Babel构建工作:
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
是的,一切都很好!我编译的代码在bundle.js中。
Yay, all is good! My compiled code is in bundle.js.
现在让我们在 a
文件夹中创建一个虚拟的package.json:
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!
推荐答案
我认为我的问题实际上是在Babel文档中描述的。 https://babeljs.io/docs/en/config-files#monorepos 说解决方案是使用babel.config.js文件而不是.babelrc文件。删除.babelrc并将其放入babel.config.js似乎确实有效:
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
};
}
这篇关于Yarn Workspaces和Browserify - 子文件夹中的package.json会破坏构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!