question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

When transpileOnly is enabled, ts-loader leaves in re-exports of types, causing webpack 4 to warn about export not being found

See original GitHub issue

Expected Behaviour

For a project like this:

// bar.ts
export {IFoo, FOO} from "./foo";
export type IBar = {};
// foo.ts
export type IFoo = {};
export const FOO = 1;
// index.ts
import {FOO, IFoo, IBar} from "./bar";

declare function foo(i: number): IFoo;
declare function bar(): IBar;
foo(FOO);
bar();

Regardless of whether transpileOnly is enabled, the output for bar.ts should be (assuming module=ESNEXT):

export var FOO = 1;

Actual Behaviour

The actual output is:

export { IFoo } from "./foo";
export var FOO = 1;

which causes webpack to complain:

WARNING in ./src/bar.ts
1:0-34 "export 'IFoo' was not found in './foo'
 @ ./src/bar.ts
 @ ./src/index.ts

When transpileOnly is off, the module is correctly emitted. This may very well be a compiler bug, but I’m not sure how to use tsc to reproduce this.

Steps to Reproduce the Problem

git clone https://github.com/univerio/ts-loader-type-export-issue
cd ts-loader-type-export-issue
yarn
./node_modules/webpack-cli/bin/webpack.js

Location of a Minimal Repository that Demonstrates the Issue.

https://github.com/univerio/ts-loader-type-export-issue

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:15
  • Comments:22 (11 by maintainers)

github_iconTop GitHub Comments

11reactions
univeriocommented, Mar 26, 2018

I think I understand it now. I think it has to do with the fact that transpileOnly uses ts.transpileModule, which is essentially equivalent to turning on isolatedModules. When compiling each module separately, the compiler does not have enough information to know whether an import is a type or not (see Microsoft/TypeScript#15231), so while it eliminates imports that are never used in a value position (which works for imported types that are not exported), it cannot know whether an export is a type or not. So, fundamentally, ts.transpileModule is incompatible with type re-exports.

This issue has cropped up now because webpack 4 warns about these invalid exports, but the final bundle otherwise works fine. Perhaps there’s an option in webpack to silence these warnings?

5reactions
FourwingsYcommented, Apr 11, 2018

@univerio Oh yes i got it. I’ve set config.module.strictExportPresence = true on webpack config. Thanks a lot!

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript Warning: Export, reexported as, was not found ...
Either way the solution is that your type needs to be exported differently. Updating your code to the following should fix your issue....
Read more >
TypeScript loader for webpack - ts-loader - npm
Start using ts-loader in your project by running `npm i ts-loader` ... If you enable this option, webpack 4 will give you "export...
Read more >
node_modules/ts-loader - GitLab
If you enable this option, webpack 4 will give you "export not found" warnings any time you re-export a type: WARNING in ./src/bar.ts...
Read more >
Does it matter if a shipment to an embassy would require a ...
For purposes of the EAR, shipments to embassies abroad are considered exports or reexports to the host country. License requirements for exports or ......
Read more >
Content Types - ESBuild
This loader is enabled by default for .js , .cjs , and .mjs files. ... TypeScript, Webpack, and esbuild) and setting the default...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found