Skip to content
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

Vite build error: TypeError: PluginCritical is not a function #12

Closed
Kretiss opened this issue Jun 21, 2023 · 5 comments
Closed

Vite build error: TypeError: PluginCritical is not a function #12

Kretiss opened this issue Jun 21, 2023 · 5 comments
Labels
bug Something isn't working

Comments

@Kretiss
Copy link

Kretiss commented Jun 21, 2023

Describe the bug

Cannot build Vite project when using this plugin in plugins section in vite.config.ts. It says TypeError: PluginCritical is not a function. What am I doing wrong? I tried rollup.config.js, but it looks like Vite is ignoring that file.

Steps to reproduce

  • Install plugin
  • Import plugin in vite.config.ts with import PluginCritical from 'rollup-plugin-critical'
  • use plugin in config
/// <reference types="vite/client" />
import { defineConfig, loadEnv, splitVendorChunkPlugin } from 'vite'
import react from '@vitejs/plugin-react-swc'
import svgr from 'vite-plugin-svgr'
import eslintPlugin from 'vite-plugin-eslint'
import { sentryVitePlugin } from '@sentry/vite-plugin'
import { visualizer } from 'rollup-plugin-visualizer'
import viteCompression from 'vite-plugin-compression'
import { ViteImageOptimizer } from 'vite-plugin-image-optimizer'
import VitePluginInjectPreload from 'vite-plugin-inject-preload'
import PluginCritical from 'rollup-plugin-critical'


// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd(), '')

  return {
    build: {
      // required for sentry: tells vite to create source maps
      sourcemap: true,
      // cssCodeSplit: true,
    },
    server: {
      port: 3000,
      host: true,
    },
    plugins: [
      react(),
      svgr(),
      eslintPlugin(),
      sentryVitePlugin({
        url: env.VITE_SENTRY_URL,
        authToken: env.VITE_SENTRY_AUTH_TOKEN,
        org: env.VITE_SENTRY_ORG,
        project: env.VITE_SENTRY_PROJECT,
        release: {
          create: !!env.SENTRY_DEPLOY_ENV,
          deploy: {
            env: env.SENTRY_DEPLOY_ENV || 'Not specified',
          },
          setCommits: {
            auto: true,
            ignoreMissing: true,
          },
        },
        // telemetry: false,
        // debug: true,
      }),
      visualizer({
        template: 'treemap', // or sunburst
        // open: true,
        gzipSize: true,
        brotliSize: true,
        filename: 'analyse.html', // will be saved in project's root
      }),
      splitVendorChunkPlugin(),
      viteCompression(),
      ViteImageOptimizer(),
      VitePluginInjectPreload({
        files: [
          {
            match: /hero-[a-zA-Z0-9]*.webp$/,
          },
        ],
      }),
      PluginCritical({
        criticalUrl: '',
        criticalBase: './',
        criticalPages: [{ uri: '', template: 'index' }],
        criticalConfig: {},
      }),
    ],
  }
})

Screenshots

obrazek

Versions

  • Plugin version: 1.0.12
  • Vite version: 4.3.9
@Kretiss Kretiss added the bug Something isn't working label Jun 21, 2023
@ElPrudi
Copy link

ElPrudi commented Oct 4, 2023

Got it to work with a workaround:

      (PluginCritical as any).default({
        criticalUrl: '',
        criticalBase: './',
        criticalPages: [{ uri: '', template: 'index' }],
        criticalConfig: {},
      }),

The any is needed or else typescript will throw an error.

@davidhellmann
Copy link

Got it to work with a workaround:

      (PluginCritical as any).default({
        criticalUrl: '',
        criticalBase: './',
        criticalPages: [{ uri: '', template: 'index' }],
        criticalConfig: {},
      }),

The any is needed or else typescript will throw an error.

Thanks! Works!

@khalwat
Copy link
Contributor

khalwat commented Nov 22, 2023

hmmmm. I'm going to see if there's a way I can solve this at the plugin level

@khalwat
Copy link
Contributor

khalwat commented Nov 22, 2023

Okay so I was already properly generating a CJS and ESM variants of the library, but I needed to add Conditional Exports as a hint to help with module resolution:

https://nodejs.org/dist/latest-v18.x/docs/api/packages.html#conditional-exports

So what was happening is it was doing an import of the CJS version of the library, which worked likely because of the esModuleInterop in your project's tsconfig.json until the project was changed to "type": "module"

Adding this to the project's package.json fixes it:

  "exports": {
    "types": "./dist/index.d.ts",
    "import": "./dist/index.mjs",
    "require": "./dist/index.js"
  },

I'll roll out an update shortly.

khalwat added a commit that referenced this issue Nov 22, 2023
… would fail because conditional exports were not defined in `"exports"` ([#12](#12))
@khalwat
Copy link
Contributor

khalwat commented Nov 22, 2023

@khalwat khalwat closed this as completed Nov 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants