简体   繁体   English

lerna + webpack + babel-loader问题

[英]lerna + webpack + babel-loader issue

Here's where the issue is recreated: 这是重新创建问题的地方:

https://github.com/reggi/webpack-lerna-babel-loader-issue https://github.com/reggi/webpack-lerna-babel-loader-issue

Goal: I'm trying to build ./packages/usage/index.js with webpack. 目标:我试图建立./packages/usage/index.js用的WebPack。

  • I have 4 packages alpha , beta , gamma , usage . 我有4个包alphabetagammausage
  • alpha , beta , gamma are not using babel and should just be consumed using index.js alphabetagamma不使用babel,只应使用index.js消费
  • usage is using babel and should be converted using babel-loader then the traversal should contunue usage是使用babel并且应该使用babel-loader进行转换然后遍历应该是contunue

When I try and compile babel is oddly being request for the other deps. 当我尝试编译babel奇怪的是请求其他deps。

$ lerna bootstrap
Lerna v2.0.0-beta.20
Linking all dependencies
Successfully bootstrapped 4 packages.
$ cd packages/usage
$ npm run webpack

> @reggi/usage@1.0.0 webpack /Users/thomasreggi/Desktop/webpack-issue/packages/usage
> webpack

Hash: 27e6d9d1d4147417b516
Version: webpack 1.13.1
Time: 429ms
    Asset     Size  Chunks             Chunk Names
bundle.js  2.11 kB       0  [emitted]  main
    + 3 hidden modules

ERROR in ../alpha/index.js
Module not found: Error: Cannot resolve module 'babel' in /Users/thomasreggi/Desktop/webpack-issue/packages/alpha
 @ ../alpha/index.js 1:11-33

And with { exclude: /(node_modules|bower_components)/ } enabled I get this. 并且启用了{ exclude: /(node_modules|bower_components)/ }我得到了这个。

$ npm run webpack

> @reggi/usage@1.0.0 webpack /Users/thomasreggi/Desktop/webpack-issue/packages/usage
> webpack

Hash: 99d08ad8b664833bba1c
Version: webpack 1.13.1
Time: 401ms
    Asset     Size  Chunks             Chunk Names
bundle.js  2.25 kB       0  [emitted]  main
    + 4 hidden modules

ERROR in ../alpha/~/@reggi/beta/index.js
Module not found: Error: Cannot resolve module 'babel' in /Users/thomasreggi/Desktop/webpack-issue/packages/alpha/node_modules/@reggi/beta
 @ ../alpha/~/@reggi/beta/index.js 1:17-82

Types of bundles 捆绑的类型

Without babel-loader excluding node_modules 没有babel-loader不包括node_modules

https://github.com/reggi/webpack-lerna-babel-loader-issue/blob/master/packages/usage/bundle-without-exclude.js#L70 https://github.com/reggi/webpack-lerna-babel-loader-issue/blob/master/packages/usage/bundle-without-exclude.js#L70

Cannot find module \\"@reggi/beta\\" 找不到模块“@reggi / beta \\”

With babel-loader excluding node_modules 使用babel-loader不包括node_modules

https://github.com/reggi/webpack-lerna-babel-loader-issue/blob/master/packages/usage/bundle-with-exclude.js#L77 https://github.com/reggi/webpack-lerna-babel-loader-issue/blob/master/packages/usage/bundle-with-exclude.js#L77

Cannot find module \\"/Users/thomasreggi/Desktop/webpack-issue/packages/beta\\" 找不到模块\\“/ Users / thomasreggi / Desktop / webpack-issue / packages / beta \\”

Without babel-loader & switching code to require 没有babel-loader和切换代码require

https://github.com/reggi/webpack-lerna-babel-loader-issue/blob/master/packages/usage/bundle-without-babel.js#L97 https://github.com/reggi/webpack-lerna-babel-loader-issue/blob/master/packages/usage/bundle-without-babel.js#L97

Includes everything perfectly. 完美包含一切。

One nice thing about using Lerna is you can pull devDependencies up to the repo root to factor them out of the individual packages. 使用Lerna的一个devDependencies是你可以将devDependencies repo root,从而将它们从各个包中devDependencies出来。 This can significantly improve bootstrap time if devDependencies are used in many packages. 如果在许多包中使用devDependencies这可以显着改善引导时间。 It can also , in this case, help you avoid this strange babel behavior. 可以,在这种情况下,帮助你避免这种奇怪的巴贝尔行为。

If you move the devDependencies from packages/usage up to the root: 如果将devDependenciespackages/usage移动到root:

diff --git a/package.json b/package.json
index d3728db..e9b313b 100644
--- a/package.json
+++ b/package.json
@@ -1,5 +1,8 @@
 {
   "devDependencies": {
-    "lerna": "2.0.0-beta.20"
+    "babel-loader": "^6.2.4",
+    "babel-preset-es2015": "^6.9.0",
+    "lerna": "2.0.0-beta.20",
+    "webpack": "^1.13.1"
   }
 }
diff --git a/packages/usage/package.json b/packages/usage/package.json
index 7186ddf..387ddc4 100644
--- a/packages/usage/package.json
+++ b/packages/usage/package.json
@@ -17,10 +17,5 @@
     "presets": [
       "es2015"
     ]
-  },
-  "devDependencies": {
-    "babel-loader": "^6.2.4",
-    "babel-preset-es2015": "^6.9.0",
-    "webpack": "^1.13.1"
   }
 }

Then re-run npm install at your repo root. 然后在您的repo root重新运行npm install After that your npm run webpack in packages/usage should succeed. 之后你的npm run webpackpackages/usage npm run webpack应该会成功。

Note also that with Lerna you can simplify your workflow here using lerna run webpack . 另请注意,使用Lerna,您可以使用lerna run webpack简化您的工作流程。

Looking at this error here: 看这个错误:

ERROR in ../alpha/~/@reggi/beta/index.js
Module not found: Error: Cannot resolve module 'babel' in /Users/thomasreggi/Desktop/webpack-issue/packages/alpha/node_modules/@reggi/beta

It looks like the problem is finding the babel loader because require.resolve isnt going to look in sibling directory node_modules to find it. 看起来问题是找到babel加载器因为require.resolve在兄弟目录node_modules中查找它。

Try configuring webpack to tell it where to resolve the babel loader (within the usage project): 尝试配置webpack以告诉它在哪里解析babel加载器(在usage项目中):

resolveLoader: {
  modulesDirectories: [
    path.resolve(__dirname, 'node_modules'),
  ],
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM