The build should failed when Cannot find name error

1
closed
wizardnet972
wizardnet972
Posted 4 months ago

The build should failed when Cannot find name error #236

Bug description

When I run webpack with esbuild loader I got successfully message:

asset main.bundle.js 4.96 KiB [emitted] (name: main)
runtime modules 670 bytes 3 modules
cacheable modules 142 bytes
  ./src/index.ts 67 bytes [built] [code generated]
  ./src/build.ts 75 bytes [built] [code generated]
webpack 5.65.0 compiled successfully in 97 ms

But in the build.ts there is a function I didn't add import: thit_not_exist_should_throw_exception():

export const build = () => {
  thit_not_exist_should_throw_exception();
};

So the message should be:

Cannot find name 'thit_not_exist_should_throw_exception'.ts(2304)

And the webpack build should fail.

// index.ts
import { build } from './build';

console.log('in index');

build();
// build.ts
export const build = () => {
  thit_not_exist_should_throw_exception();
};

webpack:

 module: {
    rules: [
      {
        test: /\.ts?$/,
        use: [
          {
            loader: "esbuild-loader",
            options: {
              loader: "ts",
              target: "es2015",
              tsconfigRaw: require("./tsconfig.json"),
            },
          },
        ],
      },
    ],
  },

Reproduction

$ git clone https://github.com/wizardnet972/esbuild-bug.git
$ npm install
$ npm run build

Node.js package manager

npm

Environment

System:
    OS: macOS 12.1
    CPU: (8) arm64 Apple M1
    Memory: 99.47 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.13.0 - /usr/local/bin/node
    Yarn: 1.22.11 - /usr/local/bin/yarn
    npm: 8.1.2 - /usr/local/bin/npm

Can you contribute a fix?

  • I’m interested in opening a pull request for this issue.
privatenumber
privatenumber
Created 4 months ago

Thanks for the bug reproduction. It was helpful to understand what you were describing.

Webpack doesn't throw an error if there's an undeclared variable because it's possible that it's available at runtime as a global variable.

I believe you're expecting TypeScript type checking to run that check and throw an error through Webpack. If so, esbuild-loader does not do any type checking.

You can typecheck via IDE or tsc --noEmit. I have not used this before, but there also seems to be https://github.com/TypeStrong/fork-ts-checker-webpack-plugin