+
Skip to content

fix(vite-plugin-angular): reenables test bed and jit mode for partial compilations #1675

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

Merged
merged 2 commits into from
Apr 7, 2025
Merged

fix(vite-plugin-angular): reenables test bed and jit mode for partial compilations #1675

merged 2 commits into from
Apr 7, 2025

Conversation

shannonmoeller
Copy link
Contributor

@shannonmoeller shannonmoeller commented Apr 7, 2025

PR Checklist

When trying to build with "complationMode": "partial" set in tsconfig.json, the following errors occur:

vite v6.2.5 building for production...
✓ 0 modules transformed.
✗ Build failed in 1.19s
error during build:
[@analogjs/vite-plugin-angular] TestBed support ("supportTestBed" option) cannot be disabled in partial compilation mode.
    at NgCompiler.makeCompilation (file:///Users/.../node_modules/@angular/compiler-cli/bundles/chunk-UJU2GLGZ.js:4224:13)
    at file:///Users/.../node_modules/@angular/compiler-cli/bundles/chunk-UJU2GLGZ.js:3838:31
    at ActivePerfRecorder.inPhase (file:///Users/.../node_modules/@angular/compiler-cli/bundles/chunk-Q2WE7ECN.js:142:14)
    at NgCompiler.analyzeAsync (file:///Users/.../node_modules/@angular/compiler-cli/bundles/chunk-UJU2GLGZ.js:3837:29)
    at buildAndAnalyze (file:///Users/.../node_modules/@analogjs/vite-plugin-angular/src/lib/angular-vite-plugin.js:593:35)
    at Object.buildStart (file:///Users/.../node_modules/@analogjs/vite-plugin-angular/src/lib/angular-vite-plugin.js:200:23)
    at Object.handler (file:///Users/.../node_modules/vite/dist/node/chunks/dep-Pj_jxEzN.js:51802:15)
    at file:///Users/.../node_modules/rollup/dist/es/shared/node-entry.js:21835:40
    at async Promise.all (index 5)
    at async PluginDriver.hookParallel (file:///Users/.../node_modules/rollup/dist/es/shared/node-entry.js:21763:9)
vite v6.2.5 building for production...
✓ 0 modules transformed.
✗ Build failed in 1.07s
error during build:
[@analogjs/vite-plugin-angular] JIT mode support ("supportJitMode" option) cannot be disabled in partial compilation mode.
    at NgCompiler.makeCompilation (file:///Users/.../node_modules/@angular/compiler-cli/bundles/chunk-UJU2GLGZ.js:4227:13)
    at file:///Users/.../node_modules/@angular/compiler-cli/bundles/chunk-UJU2GLGZ.js:3838:31
    at ActivePerfRecorder.inPhase (file:///Users/.../node_modules/@angular/compiler-cli/bundles/chunk-Q2WE7ECN.js:142:14)
    at NgCompiler.analyzeAsync (file:///Users/.../node_modules/@angular/compiler-cli/bundles/chunk-UJU2GLGZ.js:3837:29)
    at buildAndAnalyze (file:///Users/.../node_modules/@analogjs/vite-plugin-angular/src/lib/angular-vite-plugin.js:593:35)
    at Object.buildStart (file:///Users/.../node_modules/@analogjs/vite-plugin-angular/src/lib/angular-vite-plugin.js:200:23)
    at Object.handler (file:///Users/.../node_modules/vite/dist/node/chunks/dep-Pj_jxEzN.js:51802:15)
    at file:///Users/.../node_modules/rollup/dist/es/shared/node-entry.js:21835:40
    at async Promise.all (index 5)
    at async PluginDriver.hookParallel (file:///Users/.../node_modules/rollup/dist/es/shared/node-entry.js:21763:9)

What is the new behavior?

This PR ensures these options are enabled when the tsCompilerOptions.compilationMode is resolved to partial.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

This change is useful when building component libraries you wish to share via npm that should support a range of Angular versions.

What gif best describes this PR or how it makes you feel?

glorious-purpose

… compilations

When trying to build with `"complationMode": "partial"` set in `tsconfig.json`, the following errors occur:

```
vite v6.2.5 building for production...
✓ 0 modules transformed.
✗ Build failed in 1.19s
error during build:
[@analogjs/vite-plugin-angular] TestBed support ("supportTestBed" option) cannot be disabled in partial compilation mode.
    at NgCompiler.makeCompilation (file:///Users/.../node_modules/@angular/compiler-cli/bundles/chunk-UJU2GLGZ.js:4224:13)
    at file:///Users/.../node_modules/@angular/compiler-cli/bundles/chunk-UJU2GLGZ.js:3838:31
    at ActivePerfRecorder.inPhase (file:///Users/.../node_modules/@angular/compiler-cli/bundles/chunk-Q2WE7ECN.js:142:14)
    at NgCompiler.analyzeAsync (file:///Users/.../node_modules/@angular/compiler-cli/bundles/chunk-UJU2GLGZ.js:3837:29)
    at buildAndAnalyze (file:///Users/.../node_modules/@analogjs/vite-plugin-angular/src/lib/angular-vite-plugin.js:593:35)
    at Object.buildStart (file:///Users/.../node_modules/@analogjs/vite-plugin-angular/src/lib/angular-vite-plugin.js:200:23)
    at Object.handler (file:///Users/.../node_modules/vite/dist/node/chunks/dep-Pj_jxEzN.js:51802:15)
    at file:///Users/.../node_modules/rollup/dist/es/shared/node-entry.js:21835:40
    at async Promise.all (index 5)
    at async PluginDriver.hookParallel (file:///Users/.../node_modules/rollup/dist/es/shared/node-entry.js:21763:9)
```

```
vite v6.2.5 building for production...
✓ 0 modules transformed.
✗ Build failed in 1.09s
error during build:
[@analogjs/vite-plugin-angular] TestBed support ("supportTestBed" option) cannot be disabled in partial compilation mode.
    at NgCompiler.makeCompilation (file:///Users/.../node_modules/@angular/compiler-cli/bundles/chunk-UJU2GLGZ.js:4224:13)
    at file:///Users/.../node_modules/@angular/compiler-cli/bundles/chunk-UJU2GLGZ.js:3838:31
    at ActivePerfRecorder.inPhase (file:///Users/.../node_modules/@angular/compiler-cli/bundles/chunk-Q2WE7ECN.js:142:14)
    at NgCompiler.analyzeAsync (file:///Users/.../node_modules/@angular/compiler-cli/bundles/chunk-UJU2GLGZ.js:3837:29)
    at buildAndAnalyze (file:///Users/.../node_modules/@analogjs/vite-plugin-angular/src/lib/angular-vite-plugin.js:593:35)
    at Object.buildStart (file:///Users/.../node_modules/@analogjs/vite-plugin-angular/src/lib/angular-vite-plugin.js:200:23)
    at Object.handler (file:///Users/.../node_modules/vite/dist/node/chunks/dep-Pj_jxEzN.js:51802:15)
    at file:///Users/.../node_modules/rollup/dist/es/shared/node-entry.js:21835:40
    at async Promise.all (index 5)
    at async PluginDriver.hookParallel (file:///Users/.../node_modules/rollup/dist/es/shared/node-entry.js:21763:9)
```

This diff ensures these options are enabled when the `tsCompilerOptions.compilationMode` is resolved to `partial`.
Copy link

netlify bot commented Apr 7, 2025

Deploy Preview for analog-docs ready!

Name Link
🔨 Latest commit 6b09e61
🔍 Latest deploy log https://app.netlify.com/sites/analog-docs/deploys/67f3ee3fb89c3e000809948c
😎 Deploy Preview https://deploy-preview-1675--analog-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented Apr 7, 2025

Deploy Preview for analog-blog ready!

Name Link
🔨 Latest commit 6b09e61
🔍 Latest deploy log https://app.netlify.com/sites/analog-blog/deploys/67f3ee3fe41c8c000838a185
😎 Deploy Preview https://deploy-preview-1675--analog-blog.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented Apr 7, 2025

Deploy Preview for analog-app ready!

Name Link
🔨 Latest commit 6b09e61
🔍 Latest deploy log https://app.netlify.com/sites/analog-app/deploys/67f3ee3f8fe7e80008d039ef
😎 Deploy Preview https://deploy-preview-1675--analog-app.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented Apr 7, 2025

Deploy Preview for analog-ng-app ready!

Name Link
🔨 Latest commit 6b09e61
🔍 Latest deploy log https://app.netlify.com/sites/analog-ng-app/deploys/67f3ee3ffee27c000823d241
😎 Deploy Preview https://deploy-preview-1675--analog-ng-app.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@shannonmoeller
Copy link
Contributor Author

I'm not sure why Prettier is failing, but it's unrelated to my changes as the failures are in files this PR doesn't modify.

@brandonroberts brandonroberts merged commit 2d9999d into analogjs:beta Apr 7, 2025
21 of 22 checks passed
@brandonroberts
Copy link
Member

Thanks @shannonmoeller! @allcontributors add @shannonmoeller for code.

Copy link
Contributor

@brandonroberts

I've put up a pull request to add @shannonmoeller! 🎉

@shannonmoeller
Copy link
Contributor Author

Thank you for reviewing so quickly and accepting the change! This will make my life a lot easier.

@shannonmoeller shannonmoeller deleted the shannonmoeller-fix-partial-compilation-mode branch April 7, 2025 16:49
@brandonroberts
Copy link
Member

No problem @shannonmoeller, thanks for the PR! Are you using Vite instead of ng-packagr to build these Angular libraries?

@shannonmoeller
Copy link
Contributor Author

Yep! Exactly. I would share a link to the code, but it's in a private work repo. The component library is developed within a monorepo that has adapter libraries for Angular, React, and Vue. Both React and Vue were already using vite for storybook and production builds. Angular was the odd one out and we had to have a bunch of exceptions in our publish scripts to account for ng-packagr weirdness. Your vite plugin allowed us to unify everything and keep the Angular-specific stuff inside the Angular directory. Huge quality of life improvement for the team all around.

@brandonroberts
Copy link
Member

That's great to hear.

Is there anything else special about building the Angular component lib with Vite as far as the Vite configuration? Maybe we can add a guide on this if it's mostly standard configuration + the plugin.

@shannonmoeller
Copy link
Contributor Author

shannonmoeller commented Apr 7, 2025

There were some pain points, yes. The storybook and production builds have slightly different tsconfig and vite plugin config. In order to make that work properly we had to use Storybook's viteFinal API to remove the default vite plugins and replace them with the storybook-specific version of the plugins. We also had to expose all of the angularCompilerOptions as STORYBOOK_ANGULAR_OPTIONS and set process.env.FORCE_SIMILAR_INSTEAD_OF_MAP which are both really annoying and only discovered as necessary by iteratively addressing runtime errors. I'll share what I can here.

tsconfig.json and tsconfig.build.json

We have a base tsconfig.json file which gets used by type checking and storybook, then a tsconfig.build.json file that extends the base file and adds "compilationMode": "partial" to the angularCompilerOptions.

vite.config.js

import fg from 'fast-glob';
import { defineConfig } from 'vite';
import angular from '@analogjs/vite-plugin-angular';

export default defineConfig({
  plugins: [angular({ tsconfig: 'tsconfig.build.json' })],
  resolve: {
    mainFields: ['module'],
  },
  build: {
    target: ['esnext'],
    emptyOutputDir: false,
    lib: {
      entry: fg.globSync('src/**/index.ts'),
      formats: ['es'],
      fileName: (_format, entryName) => `${entryName}.js`,
    },
    rollupOptions: {
      external: [/^[@\w]/],
      output: {
        preserveModules: true,
      },
    },
  },
});

.storybook/main.ts

export default {
  // ...

  core: {
    // ...
    builder: {
      name: '@storybook/builder-vite',
      options: {
        viteConfigPath: undefined,
      },
    },
  },

  async viteFinal(config) {
    const { default: angular } = await import('@analogjs/vite-plugin-angular');
    const { default: tsconfig } = await import('../tsconfig.json', { with: { type: 'json' } });

    config.plugins = [
      ...(config.plugins || []).filter((plugin) => {
        return !plugin || !('name' in plugin) || !plugin.name.includes('analogjs');
      }),
      ...angular({
        jit: true,
        tsconfig: 'tsconfig.json',
      }),
    ];

    config.define = {
      ...config.define,
      'process.env.FORCE_SIMILAR_INSTEAD_OF_MAP': false,
      STORYBOOK_ANGULAR_OPTIONS: JSON.stringify(tsconfig.angularCompilerOptions),
    };

    return config;
  }

  // ...
};

@brandonroberts
Copy link
Member

Thanks for the configs 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载