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 issueExpected 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.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:15
- Comments:22 (11 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I think I understand it now. I think it has to do with the fact that
transpileOnly
usests.transpileModule
, which is essentially equivalent to turning onisolatedModules
. 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?
@univerio Oh yes i got it. I’ve set
config.module.strictExportPresence = true
on webpack config. Thanks a lot!