简体   繁体   中英

Webpack bundling but not resolving typescript from node_modules package

Question guys, I've tried about every different way I can think of to solve this.

We have two folders in a makeshift mono-repo (no yarn workspace). One called Mgt-Shared one called Server . We reference shared from Servers package.json with "mgt-shared": "file:../mgt-shared" , and I run a simple npm install to get our shared into Servers node_modules . Both projects are mixed typescript and javascript. When I run webpack to attempt to bundle server, it works fine, the Typescript files from shared show up in the bundle list but when I run the node dist/app.bundle.js - it can't resolve some of the modules (specifically all the typescript ones). When I run webpack --json I find this (which seems like it's resolving properly):

    {
      "id": "mgt-shared/discounts/discount_reasons",
      "identifier": "external \"mgt-shared/discounts/discount_reasons\"",
      "name": "external \"mgt-shared/discounts/discount_reasons\"",
      "index": 23,
      "index2": 15,
      "size": 42,
      "built": true,
      "optional": false,
      "prefetched": false,
      "chunks": [
        "main"
      ],
    ...
      "failed": false,
      "errors": 0,
      "warnings": 0,
      "assets": [],
      "reasons": [
        {
          "moduleId": "./src/discounts/discount.ts",
          "moduleIdentifier": "/home/mygastank/WebstormProjects/mygastank/server/node_modules/babel-loader/lib/index.js!/home/mygastank/WebstormProjects/mygastank/server/src/discounts/discount.ts",
          "module": "./src/discounts/discount.ts",
          "moduleName": "./src/discounts/discount.ts",
          "type": "cjs require",
          "userRequest": "mgt-shared/discounts/discount_reasons",
          "loc": "20:24-72"
        },
        {...
      ],
      "providedExports": null,
      "optimizationBailout": [],
      "depth": 6
    },

My webpack config is this:

const nodeExternals = require('webpack-node-externals')
module.exports = {
  mode: 'development',
  target: 'node',
  // This forces resolution of native node modules
  externals: [nodeExternals({ modulesFromFile: true })],
  entry: './src/index',
  output: {
    filename: 'app.bundle.js'
  },
  resolve: {
    extensions: ['.ts', '.tsx', '.js', '.json'],
  },
  module: {
    rules: [
      {
      // Include ts, tsx, js, and jsx files.
      test: /\.(ts|js)?$/,
      loader: 'babel-loader',
      include: [/node_modules\/mgt-shared/],
      exclude: [/node_modules/],
      options: {
        presets: [
          [
            'env',
            {
              'targets': {
                'node': 'current'
              }
            }
          ],
          ['@babel/preset-typescript',
            { 'allExtensions': true, 'isTSX': true }]
        ]
      }
    }]
  }
}

I found that there was a way to whitelist other directories.

I needed to add the whitelist key to the options for nodeExternals like so:

  externals: [nodeExternals({
    whitelist: [/mgt-shared/],
    modulesFromFile: true
  })],

Everything else was able to stay the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM