core icon indicating copy to clipboard operation
core copied to clipboard

Type file missing when using vue-tsc

Open HUST-SE-LY opened this issue 1 year ago • 10 comments

Describe the bug

The bug happened when I use webpack with module federation 2.0 to build my Vue App. When I run pnpm start in container folder, A type declaration file is missing from @mf-types.zip in the dist directory.

Then I read the source code, I find in packages/dts-plugin/src/core/lib/typeScriptCompiler.ts, function createHost rewrites the writeFile of typescript,but this looks useless under vue-tsc. Whether it is a component or a function, the following code doesn't seem to execute, and the target .d.ts file cannot be generated. By the way, in vue-tsc, sourceFile.fileName seems to be App.vue.ts, not App.vue, so the following code may be wrong.

// packages/dts-plugin/src/core/lib/typeScriptCompiler.ts
for (const sourceFile of sourceFiles || []) {
      const sourceEntry = mapExposeToEntry[normalize(sourceFile.fileName)];
      if (sourceEntry) {
        const mfeTypeEntry = join(
          mfTypePath,
          `${sourceEntry}${DEFINITION_FILE_EXTENSION}`,
        );
        const mfeTypeEntryDirectory = dirname(mfeTypeEntry);
        const relativePathToOutput = relative(mfeTypeEntryDirectory, filepath)
          .replace(DEFINITION_FILE_EXTENSION, '')
          .replace(STARTS_WITH_SLASH, '')
          .split(sep) // Windows platform-specific file system path fix
          .join('/');
        originalWriteFile(
          mfeTypeEntry,
          `export * from './${relativePathToOutput}';\nexport { default } from './${relativePathToOutput}';`,
          writeOrderByteMark,
        );
      }

Reproduction

https://github.com/HUST-SE-LY/mf-vue3-demo

Used Package Manager

pnpm

System Info

System:
    OS: macOS 14.4.1
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 2.64 GB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.20.1 - ~/.nvm/versions/node/v18.20.1/bin/node
    Yarn: 1.22.22 - /usr/local/bin/yarn
    npm: 10.5.0 - ~/.nvm/versions/node/v18.20.1/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  Browsers:
    Chrome: 125.0.6422.142
    Safari: 17.4.1

Validations

HUST-SE-LY avatar Jun 17 '24 07:06 HUST-SE-LY

@2heal1 do you have any idea here why it is not detected?

ScriptedAlchemy avatar Jun 21 '24 22:06 ScriptedAlchemy

Hello,

i have a similar issue by using module federation quick start rsbuild example converted to vue3

WeetA34 avatar Jul 01 '24 12:07 WeetA34

let me check this

2heal1 avatar Jul 02 '24 08:07 2heal1

as mentioned in issue 2685 HUST-SE-LY's comment , it works with latest vue-tsc 1.x (1.8.27). Regarding rsbuild, it doesn't provide vue-tsc by default for vue3 typescript projects as it uses tsc directly to build. So, it would be nice to get the types generated with tsc directly.

WeetA34 avatar Jul 02 '24 09:07 WeetA34

I find [email protected] will not use writeFile anymore , so this is the reason why our logic not work .

And the vue-tsc@<1.7.0 also has some questions , as @WeetA34 say , i will try tsc instead to see if it can work

2heal1 avatar Jul 02 '24 10:07 2heal1

I check again and find it can not use tsc to compile .vue file .

I will fix the issue by re-implement the ts compile.

2heal1 avatar Jul 02 '24 11:07 2heal1

upgrade @module-federation/[email protected] , the issue should be fixed

2heal1 avatar Jul 22 '24 11:07 2heal1

I'll test that soon. thank you

WeetA34 avatar Jul 23 '24 19:07 WeetA34

It works fine with vue-tsc 2.0.28 and @module-federation/[email protected] and the following dts config:

          dts: {
            generateTypes: {
              compilerInstance: 'vue-tsc',
            },
          },

WeetA34 avatar Jul 23 '24 20:07 WeetA34

Unable to compile federated types, Error: compile TS failed, the original command is 'npx vue-tsc --project node_modules/.federation/tsconfig.a7b0ede872d63bdbfb5ee8bcadd8473a.json'

ckken avatar Jul 30 '24 07:07 ckken

Stale issue message

github-actions[bot] avatar Sep 28 '24 15:09 github-actions[bot]