Skip to content

Incorrect webpack --watch behavior for type-only imports when using yarn workspaces #611

Open
@jtbandes

Description

@jtbandes

Current behavior

Type-only imports (import type X, or import X where X is only used as a type) result in incorrect webpack --watch behavior. When files residing in workspace packages are modified, the importing code does not always get recompiled.

This is a similar issue to #36 and TypeStrong/ts-loader#1138, but that issue has been fixed and the recommendation to use "importsNotUsedAsValues": "preserve" has been removed in #516. However, issues still arise when using yarn workspaces.

It seems like enabling "importsNotUsedAsValues": "preserve" and avoiding import type might be a viable workaround for this issue, but that's not great, because import type is a core TS feature that should ideally be supported properly.

Expected behavior

Modifying a file in a workspace package should cause webpack to recompile the bundle, even if it's only used via type-only imports.

Steps to reproduce the issue

  1. Check out https://github.com/jtbandes/ts-workspaces-repro. This repo uses Yarn workspaces and has two sub-packages under packages/pkg1 and packages/pkg2. The main file src/index.ts imports from these packages.

  2. Run yarn install && yarn webpack --watch --progress --mode=development

  3. Modify type Example1 in packages/pkg1/index.ts. Observe that webpack does not recompile src/index.ts nor display new type errors. ❌

  4. Modify function Example2 in packages/pkg2/index.ts. Observe that webpack does recompile src/index.ts and display new type errors (this is not a type-only import). ✅

  5. Add
    "compilerOptions": { "importsNotUsedAsValues": "preserve" } to tsconfig.json. Now, observe that changing pkg1/index.ts does cause webpack to recompile. ✅

    • Change import {Example1} from 'pkg1' to import type {Example1} from 'pkg1' in src/index.ts. Observe that changing pkg1/index.ts again does not result in a recompile. ❌ (This is because "importsNotUsedAsValues": "preserve" does not affect explicit import type imports, which are always removed.)

Issue reproduction repository

https://github.com/jtbandes/ts-workspaces-repro

Environment

  • fork-ts-checker-webpack-plugin: 6.2.6
  • typescript: 4.2.4
  • ts-loader: 9.1.2
  • webpack: 5.37.0
  • os: macOS 11.3.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions