我的最终目标是在使用Browserify和Babel 7的项目中使用Yarn Workspaces.这是我所遇到的问题的最小复制.基本上似乎在子文件夹中存在一个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中.
现在让我们在一个文件夹中创建一个虚拟的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
我无法弄清楚为什么会这样.任何帮助,将不胜感激!
解决方法
我认为我的问题实际上是在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 }; }