Coder Social home page Coder Social logo

vite-plugin-import-maps's Introduction

vite-plugin-import-maps

Use native browser import-maps in vite. What's import-maps

NPM

Usage

Simply add import-maps plugin in vite.config

// vite.config.js
const { defineConfig } = require('vite')
const { importMaps } = require('vite-plugin-import-maps')

module.exports = defineConfig({
  plugins: [
    importMaps([
      {
        imports: {
          lodash: 'https://esm.sh/[email protected]',
        },
      },
    ]),
  ],
})

Then your module will import from cdn instead of vite pre-bundle module.

Cuz it use native import-maps, itt also allow you to use module in runtime.

<!-- index.html -->
<script type="module">
  import _, { isNaN } from 'lodash'

  console.log(_.isNaN(NaN)) // true
  console.log(isNaN(NaN)) // true
</script>

Limitation

Until now, only Chrome implements the import-maps feature. But it is easy to use a polyfill with es-module-shims.

And to be ware, due to there is no way for vite to unresolve bare moduleId now, this plugin use an alia with the preifx /@import-maps/, which means import 'lodash' will transform to import '/@import-maps/lodash'.

License

MIT

vite-plugin-import-maps's People

Contributors

pakholeung37 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

1cepeak tombebb

vite-plugin-import-maps's Issues

Adjust plugin dependencies

This is a question / suggestion.

Currently the dep graph looks like this:

"dependencies": {
    "less": "*",
    "sass": "*",
    "stylus": "*",
    "vite": "2.6.14"
  }
  • less, sass, and stylus are not used, should be removed.
  • vite is only used for types, therefore should be at most a peerDependency, ot maybe just a devDependency.

The plugin fails to work in Vite@3

Describe the bug

In the development environment of Vite@2, after the plugin is executed, <script type="importmap"> is at the top of the head tag, and /@vite/client is below it, this is no problem; but in the development environment of Vite@3, because Vite adjusted the injection rules of the plugin execution result(applyHtmlTransforms function), <script type="importmap"> was under /@vite/client, and the browser could not parse the import maps.

在Vite@2的开发环境中,插件执行完后在head标签中<script type="importmap">位于最顶部,/@vite/client在它下面,这个没有问题;但是在Vite@3的开发环境中,由于Vite调整了插件执行结果的注入规则(applyHtmlTransforms函数),导致<script type="importmap">/@vite/client下面,浏览器就无法解析 Import maps 了。

System Info

Vite 3.0.8

Personal solution

enforce: 'post', // or delete it

I tested it, it works.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.