ค้นหาเว็บไซต์

node.js - รวมแอปพลิเคชัน NodeJS http2 ด้วย webpack, ไม่พบ http2


เกี่ยวกับเอกสาร webpack สำหรับไลบรารีภายนอกและ nodejs มีข้อมูลว่าควรสามารถนำเข้าโมดูลของ node ได้ ซึ่งทำงานได้เป็นอย่างดี ยกเว้นเมื่อฉันต้องการนำเข้า http2 ระบบจะแสดงข้อผิดพลาดดังต่อไปนี้:

ERROR in ./src/index.ts
Module not found: Error: Can't resolve 'http2' in 'absolutepath/to/src'

ไฟล์ typescript สำหรับการเริ่มต้นโดยสรุปมีลักษณะดังนี้:

import { readFileSync } from 'fs';
import { createSecureServer } from 'http2';

const server = createSecureServer({...});

และไฟล์การกำหนดค่า webpack มีลักษณะดังนี้:

const path = require('path');

module.exports = {
  entry: './src/index.ts',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'index.js'
  },
  node: {
    global: true,
    process: true,
    fs: 'empty',
    http2: 'empty'
  },
  module: {
    rules: [
      { test: /\.ts$/, use: 'ts-loader' }
    ]
  },
  resolve: {
    extensions: [".ts", ".tsx", ".js"],
    modules: [path.resolve(__dirname, '/src'), 'node_modules/'],
    descriptionFiles: ['package.json']
  },
  mode: 'production'
}

และไฟล์ tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "experimentalDecorators": true,
    "target": "es5",
    "module": "commonjs",
    "emitDecoratorMetadata": true,
    "outDir": "dist",
    "declaration": false,
    "sourceMap": true,
    "removeComments": true,
    "moduleResolution": "node",
    "strict": true,
    "noImplicitAny": false,
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017"
    ]
  }
}

ตัวอย่างเช่น การนำเข้า fs ทำงานโดยไม่มีข้อผิดพลาด ฉันยังเคยเห็นปลั๊กอิน node-libs-browser ซึ่งโมดูล http2 ไม่ได้ถูกระบุ แต่ฉันไม่ต้องการใช้ http2 ในเบราว์เซอร์ เพียงแค่ใช้สำหรับการบันเดิลแอปพลิเคชันเบื้องหลังของฉัน มีคำแนะนำหรือไอเดียบ้างไหม? ขอบคุณและสวัสดี!