在nodejs服务器端使用webpack。

时间:2022-12-09 19:40:43

I've been trying to use webpack with a nodejs application, and the client side is going fine - a reasonably good documentation on their website + links from google search.

我一直在尝试使用webpack与一个nodejs应用程序,客户端运行良好——在他们的网站+谷歌搜索的链接上有一个相当好的文档。

Has anyone used webpack on server side of nodejs? or please guide me to any useful links.

有没有人在nodejs服务器端使用webpack ?或者请引导我到任何有用的链接。

Thanks.

谢谢。

2 个解决方案

#1


22  

This might be useful: http://jlongster.com/Backend-Apps-with-Webpack--Part-I

这可能有用:http://jlongster.com/backend - apps-with- webpack-parti

Key point is to make external all third party module (in node_modules directory) in webpack config file

关键是在webpack配置文件中使所有第三方模块(在node_modules目录中)都成为外部模块

Final config file

最后一个配置文件

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

var nodeModules = {};
fs.readdirSync('node_modules')
  .filter(function(x) {
    return ['.bin'].indexOf(x) === -1;
  })
  .forEach(function(mod) {
    nodeModules[mod] = 'commonjs ' + mod;
  });

module.exports = {
  entry: './src/main.js',
  target: 'node',
  output: {
    path: path.join(__dirname, 'build'),
    filename: 'backend.js'
  },
  externals: nodeModules,
  plugins: [
    new webpack.IgnorePlugin(/\.(css|less)$/),
    new webpack.BannerPlugin('require("source-map-support").install();',
                             { raw: true, entryOnly: false })
  ],
  devtool: 'sourcemap'
}

#2


18  

A real example with webpack 2.x

I want to highlight the difference from client side config:

我想强调一下与客户端配置的区别:

1. target: 'node'

2. externals: [nodeExternals()]

for node.js, it doesn't make much sense to bundle node_modules/

为节点。js,捆绑node_modules/没有什么意义

3. output.libraryTarget: 'commonjs2'

without this, you cannot require('your-library')

没有这个,就不能要求('your-library')

webpack.config.js

import nodeExternals from 'webpack-node-externals'

const config = {
  target: 'node',
  externals: [nodeExternals()],
  entry: {
    'src/index': './src/index.js',
    'test/index': './test/index.js'
  },
  output: {
    path: __dirname,
    filename: '[name].bundle.js',
    libraryTarget: 'commonjs2'
  },
  module: {
    rules: [{
      test: /\.js$/,
      use: {
        loader: 'babel-loader',
        options: {
          presets: [
            ['env', {
              'targets': {
                'node': 'current'
              }
            }]
          ]
        }
      }
    }]
  }
}

export default [config]

#1


22  

This might be useful: http://jlongster.com/Backend-Apps-with-Webpack--Part-I

这可能有用:http://jlongster.com/backend - apps-with- webpack-parti

Key point is to make external all third party module (in node_modules directory) in webpack config file

关键是在webpack配置文件中使所有第三方模块(在node_modules目录中)都成为外部模块

Final config file

最后一个配置文件

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

var nodeModules = {};
fs.readdirSync('node_modules')
  .filter(function(x) {
    return ['.bin'].indexOf(x) === -1;
  })
  .forEach(function(mod) {
    nodeModules[mod] = 'commonjs ' + mod;
  });

module.exports = {
  entry: './src/main.js',
  target: 'node',
  output: {
    path: path.join(__dirname, 'build'),
    filename: 'backend.js'
  },
  externals: nodeModules,
  plugins: [
    new webpack.IgnorePlugin(/\.(css|less)$/),
    new webpack.BannerPlugin('require("source-map-support").install();',
                             { raw: true, entryOnly: false })
  ],
  devtool: 'sourcemap'
}

#2


18  

A real example with webpack 2.x

I want to highlight the difference from client side config:

我想强调一下与客户端配置的区别:

1. target: 'node'

2. externals: [nodeExternals()]

for node.js, it doesn't make much sense to bundle node_modules/

为节点。js,捆绑node_modules/没有什么意义

3. output.libraryTarget: 'commonjs2'

without this, you cannot require('your-library')

没有这个,就不能要求('your-library')

webpack.config.js

import nodeExternals from 'webpack-node-externals'

const config = {
  target: 'node',
  externals: [nodeExternals()],
  entry: {
    'src/index': './src/index.js',
    'test/index': './test/index.js'
  },
  output: {
    path: __dirname,
    filename: '[name].bundle.js',
    libraryTarget: 'commonjs2'
  },
  module: {
    rules: [{
      test: /\.js$/,
      use: {
        loader: 'babel-loader',
        options: {
          presets: [
            ['env', {
              'targets': {
                'node': 'current'
              }
            }]
          ]
        }
      }
    }]
  }
}

export default [config]