【问题标题】:webpack 1 build peerDependencies cannot be foundwebpack 1 build peerDependencies 找不到
【发布时间】:2017-08-31 19:34:18
【问题描述】:

我有一个 Rails 应用程序,它在前端包含许多 React 组件,最近在尝试添加另一个东西时,由于多个版本的 React 开始出现问题。

返回并更新旧代码,以便所有代码都可以在react@15.x 上使用,我决定使用 peerDependencies,以便每个功能都不会加载到它自己的 react 库实例中。

在开发中peerDependencies 工作正常,但在尝试构建生产时出现错误:

Cannot resolve module 'react' in /Users/path/to/project/lib/toaster @ ...

我错过了什么?

package.json:

{
  "name": "my-package",
  // ... omitted ...
  "scripts": {
    "test": "NODE_ENV=development testem ci",
    "testem": "testem -g",
    "start": "webpack --watch",
    "build": "webpack -p --config ./webpack.production.config.js --progress --profile --colors --preserve-symlinks"
  },
  "repository": { // ... omitted ... },
  "devDependencies": {
    "babel-core": "^6.4.5",
    "babel-loader": "^6.2.1",
    "babel-preset-es2015": "^6.3.13",
    "babel-preset-react": "^6.23.0",
    "browser-sync": "^2.17.2",
    "browser-sync-webpack-plugin": "^1.1.3",
    "bundle-collapser": "^1.1.1",
    "del": "^1.1.1",
    "envify": "^3.2.0",
    "es6-promise": "^2.0.1",
    "tape": "^4.0.0",
    "testem": "^0.6.35",
    "webpack": "^1.13.2"
  },
  "peerDependencies": {
    "react": "^15.3.2",
    "react-dom": "^15.3.2"
  },
  "dependencies": {
    "empty": "^0.10.0",
    "lodash": "^3.9.1",
    "react-addons-css-transition-group": "^15.4.2"
  }
}

webpack.production.config.js

var path = require('path');
var webpack = require('webpack');

module.exports = {
  entry: [
    ...
  ],
  devtool: 'eval',
  output: {
    path: path.join(__dirname, "output"),
    filename: 'index.js',
    library: 'my-package',
    libraryTarget: 'umd'
  },
  resolveLoader: {
    modules: ['..', 'node_modules']
  },
  plugins: [
    new webpack.DefinePlugin({
      // This has effect on the react lib size.
      "process.env": {
        NODE_ENV: JSON.stringify("production")
      }
    }),
    new webpack.IgnorePlugin(/vertx/),
    new webpack.IgnorePlugin(/configs/),
    new webpack.IgnorePlugin(/un~$/),
    new webpack.optimize.DedupePlugin(),
    new webpack.optimize.UglifyJsPlugin(),
  ],
  resolve: {
    extensions: ['.js', '.jsx']
  },
  module: {
    loaders: [
      {
        test: /.jsx?$/,
        loader: 'babel-loader',
        exclude: /node_modules/,
        query: {
          presets: ['es2015', 'react']
        }
      }
    ]
  }
};

【问题讨论】:

    标签: reactjs npm webpack jsx


    【解决方案1】:

    我想通了……

    这与peerDependencies 无关。问题出在我的 webpack.production.config.js 文件中 resolve

    resolve: {
      extensions: ['.js', '.jsx']
    }
    

    需要

    resolve: {
      extensions: ['', '.js', '.jsx']
    }
    

    它在我的普通 webpack.config.js 文件中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-11
      相关资源
      最近更新 更多