Webpack-捆绑的node_module依赖项找不到http

时间:2020-03-30 22:12:59

标签: webpack

我有一个Webpack配置,它正在构建一组入口点。我允许捆绑依赖项(一个@architect/functions除外)。其中一个依赖项tiny-json-http使用Node中的http。无论我做什么,我都无法让Webpack提供/帮助tiny-json-http来查看内置程序。

const path = require('path');
const fs = require('fs');
const CopyPlugin = require('copy-webpack-plugin');

const lib = path.resolve(__dirname, 'lib/http');
const entry = {};
const copies = [];
const externals = {
  '@architect/functions': 'commonjs @architect/functions',
};

fs.readdirSync(lib).forEach(mod => {
  if (mod.startsWith('.')) {
    return;
  }

  const lib = `./lib/http/${mod}`;
  const src = `src/http/${mod}`;

  entry[`${src}/index`] = lib;

  // Add copying rules
  copies.push({
    from: lib,
    to: src,
    ignore: ['*.ts'],
  });
});

module.exports = {
  mode: 'development',
  entry,
  output: {
    path: __dirname,
    filename: '[name].js',
  },
  resolve: {
    extensions: ['.ts', '.tsx', '.js'],
  },
  externals,
  node: {
    fs: 'empty',
    http: 'empty', // This should work?
  },
  module: {
    rules: [
      {
        test: /\.(ts|js)x?$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
        },
      },
    ],
  },
  plugins: [new CopyPlugin(copies)],
};

错误

Error: Cannot find module 'http'
    at s (webpack:///./node_modules/tiny-json-http/bundle.js?:1:331)
    at eval (webpack:///./node_modules/tiny-json-http/bundle.js?:1:488)
    at Object.1.http (webpack:///./node_modules/tiny-json-http/bundle.js?:1:670)
    at s (webpack:///./node_modules/tiny-json-http/bundle.js?:1:437)
    at eval (webpack:///./node_modules/tiny-json-http/bundle.js?:1:488)
    at Object.3../_read (webpack:///./node_modules/tiny-json-http/bundle.js?:1:4430)
    at s (webpack:///./node_modules/tiny-json-http/bundle.js?:1:437)
    at e (webpack:///./node_modules/tiny-json-http/bundle.js?:1:603)
    at eval (webpack:///./node_modules/tiny-json-http/bundle.js?:1:623)
    at eval (webpack:///./node_modules/tiny-json-http/bundle.js?:1:130) {
  code: 'MODULE_NOT_FOUND'
}

0 个答案:

没有答案