Skip to content
This repository has been archived by the owner on May 28, 2021. It is now read-only.

antfu/vite-dynamic-import-polyfill-example

Repository files navigation

[DEPRECATED] See vitejs/vite#3434


vite-dynamic-import-polyfill-example

This is an example repo demosturates the configurations of GoogleChromeLabs/dynamic-import-polyfill with Vite v2.3.x

Procedures

You can follow this setup to install it manually in your project:

Install dynamic-import-polyfill

$ npm install dynamic-import-polyfill

Or

$ yarn add dynamic-import-polyfill

Then, add the following code in your main entry, before using any dynamic imports usage:

// main.js
import dynamicImportPolyfill from 'dynamic-import-polyfill'

// This needs to be done before any dynamic imports are used.
dynamicImportPolyfill.initialize()

Add a plugin in your vite.config.js telling rollup to transform your dynamic imports.

// vite.config.js
import { defineConfig } from 'vite'

export default defineConfig({
  plugins: [
    {
      name: 'dynamic-import-polyfill',
      renderDynamicImport() {
        return {
          left: '__import__(',
          right: ', import.meta.url)'
        }
      }
    }
  ]
})

And that's all. Run vite build and verify if the dynamic imports are properly polyfilled.

About

No description, website, or topics provided.

Resources

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published