问题 webpack绑定代码为node.js的代码


我用 webpack 用于捆绑客户端并希望将其用于构建node / npm库。我看到我可以指定目标为 node 为了这。来自 DOC

"node" Compile for usage in a node.js-like environment (use require to load chunks)

但问题是 react.js 捆绑在编译输出中。我只想要我的源文件和列出的任何依赖项 package.json 被包括。我已将反应指定为peerDependency,如

"peerDependencies": {
    "react": ">=0.13",
    "react-tap-event-plugin": ">=0.1.3"
  },

我也尝试定义反应 externals 期望它可能只是创建符号而不包括库本身,但它仍然包括 react 在编译输出中。

 target: "node",
    externals: [{
        'react' : 'React',
    }]

那么,有没有办法使用 webpack 按服务器端/节点代码捆绑,但也指定不捆绑某些依赖项(可以定义为 peerDependencies 要么 devDependencies)?


8376
2018-05-09 22:44


起源

在你身上 webpack.config.js 加 target: 'node'。 - Rubens Mariuzzo


答案:


詹姆斯已经写了3部分剧集。

http://jlong​​ster.com/Backend-Apps-with-Webpack--Part-I

遵循他的代码, externals 被设定为

{ 'babel-core': 'commonjs babel-core',
  'babel-loader': 'commonjs babel-loader',
  classnames: 'commonjs classnames',
  react: 'commonjs react',
...
}

哪个很棒。


15
2018-05-10 03:56



这不再是必要的了。正如@ rubens-mariuzzo所提到的,webpack.config.js中的一个简单的“target:'node'”应该可以解决问题。 - the.fabricio