Skip to content

Module not found when using raw-loader and project references #1127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
trbabb opened this issue Jun 1, 2020 · 7 comments
Closed

Module not found when using raw-loader and project references #1127

trbabb opened this issue Jun 1, 2020 · 7 comments
Labels

Comments

@trbabb
Copy link

trbabb commented Jun 1, 2020

Expected Behavior

When using Webpack's raw-loader with TypeScript project references— For any given module X.ts, I'd expect import paths relative to X.ts to refer to the same resources whether X is imported from within its own project, or from a module in a different project.

Specifically, if X.ts successfully finds a resource Y, both contained in project Z when Z is compiled separately, then I wouldn't expect Y to suddenly be considered missing when Z is imported or referenced by another project.

Clone https://github.com/trbabb/rawloader-repro.git, and after npm install, run npx webpack. I'd expect src/Foo.ts to find ./shad/foo.frg and import it normally.

Actual Behavior

npx webpack in the above repo yields:

ERROR in /project/path/src/Foo.ts
./src/Foo.ts
[tsl] ERROR in /project/path/src/Foo.ts(1,19)
      TS2307: Cannot find module './shad/foo.frg' or its corresponding type declarations.

This is not the case if src/Foo.ts is compiled from within its own project. This can be seen if you:

> mv webpack.config.js webpack.config.js-broken
> mv webpack.config.js-working webpack.config.js
> npx webpack

This compiles normally, and the contents of foo.frg can be found inside the bundle as expected.

Somehow, the existence of project reference breaks the relative path in src/Foo.ts.

Code / How to reproduce

git clone https://github.com/trbabb/rawloader-repro.git
npm install
npx run webpack

The raw-loader team informs me that this is likely a ts-loader issue. I'm not well versed in how different loaders interact with each other— please let me know if there's relevant information to pass back to the raw-loader team, or if there's anything I need to configure differently to get this to work.

Thanks!

@appzuka
Copy link
Member

appzuka commented Jun 5, 2020

You are not actually using project references in this example. You are just building using webpack and the first command fails because in test/tsconfig.json you only include "*". If you change the include statement to:

    "include" : ["*", "../src/**/*"],

then it builds as expected.

If you want to use project references you will set it up so that when the project builds ts-loader will cause tsc to build dependent projects. Then webpack will use the output of those projects in its build without rebuilding them, unless necessary.

In your repo, src is the referenced project. Test is not a project reference; it references src. tsconfig in src should look something like:

{
    "extends": "../tsconfig-base.json",
    "compilerOptions": {
        "outDir": "./out",
        "composite": true,
   },
    "include": [
        "./**/*", 
    ],
}

When you run

npx tsc -b src

tsc will build the project src and the output will be in src/out. Your test project can then import the files from src/out. ts-loader should automatically rebuild referenced projects if they change (although there is currently an issue with this. See #1114).

This will only happen if you set projectReferences: true in the ts-loader options in your webpack config.

When you build src with the tsc command above it does build the project and put the output in src/out. However, tsc does not know about raw-loader so it just includes the import statement

import stuff from './shad/foo.frg';

in the emitted file. It ignores the .frg and .vtx files as tsc does not understand these. Then, when webpack builds test using the pre-built output in src/out it throws an error because ./shad/foo.frg is not in src/out.

If you want to use webpack loaders in dependent projects you will need to build them as sub-projects using webpack.

You can see a simple repo using project references at the repo in issue 1114:

https://github.com/jedster1111/ts-loader-project-references

This shows how project references work with ts-loader. There is currently an issue where the project needs to be built twice to succeed although that is being worked on.

@tom-spalding
Copy link

tom-spalding commented Jul 3, 2020

@trbabb @appzuka this appears like a solution https://stackoverflow.com/questions/50469875/configuring-typescript-to-work-with-html-modules

edit: actually it would be nice to have a solution that doesnt require naming the imported modules differently.

@stale
Copy link

stale bot commented Sep 5, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Sep 5, 2020
@tom-spalding
Copy link

afaik still an issue stale-bot.

@stale stale bot removed the wontfix label Sep 6, 2020
@appzuka
Copy link
Member

appzuka commented Sep 6, 2020

@nebulousdog, please explain what is still an issue, with a minimal repo if possible.

@stale
Copy link

stale bot commented Nov 7, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Nov 7, 2020
@stale
Copy link

stale bot commented Nov 29, 2020

Closing as stale. Please reopen if you'd like to work on this further.

@stale stale bot closed this as completed Nov 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants