Skip to content

[BUG] Webpack bundles typescript package when ts-runtime-checks is used #39

@jirutka

Description

@jirutka

Describe the bug
I’m using Webpack to bundle a TypeScript project into a single JS file that can be executed with Node.js. When I import and use the check function from ts-runtime-checks, I end up with a huge bundle that includes even typescript.js.

Expected behavior
Development dependencies should not be bundled.

Additional context

import { check } from 'ts-runtime-checks'

const [config, errors] = check<SomeType>(obj)

Relevant devDependencies:

  • ts-loader: "^9.4.4",
  • ts-patch: "^3.0.2",
  • ts-runtime-checks: "^0.4.1",
  • typescript: "^5.2.2",
  • webpack: "^5.88.2",
  • webpack-cli: "^5.1.4",

tsconfig.json:

{
  "compilerOptions": {
    "jsx": "react",
    "jsxFactory": "h",
    "jsxFragmentFactory": "Fragment",
    "moduleResolution": "node",
    "target": "ES2022",
    "module": "ES2022",
    "strict": true,
    "esModuleInterop": true,
    "outDir": "./dist",
    "paths": {
      "react": ["./node_modules/preact/compat"],
      "react-dom": ["./node_modules/preact/compat"]
    },
    "strictNullChecks": true,
    "plugins": [
      {
        "transform": "ts-runtime-checks"
      }
    ]
  },
  "include": ["src/**/*.ts", "src/**/*.tsx"]
}

webpack.config.mjs:

// @ts-check
import * as Path from 'node:path'
import webpack from 'webpack'

/** @type {import('webpack').Configuration} */
const cli = {
  name: 'cli',
  entry: './src/main.ts',
  target: 'node',
  plugins: [
    // Don't split the output bundle into multiple files.
    new webpack.optimize.LimitChunkCountPlugin({
      maxChunks: 1,
    }),
  ],
  mode: 'production',
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: 'ts-loader',
        exclude: /node_modules/,
      },
    ],
  },
  resolve: {
    extensions: ['.tsx', '.ts', '.js'],
    alias: {
      react: 'preact/compat',
      'react-dom/test-utils': 'preact/test-utils',
      'react-dom': 'preact/compat',
    },
  },
  output: {
    filename: 'cli.js',
    path: Path.resolve('./dist'),
  },
  optimization: {
    minimize: false,
  },
}

export default [cli]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions