• This repository has been archived on 29/Jul/2022
  • Stars
    star
    164
  • Rank 223,105 (Top 5 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 2 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The design experiment for import.meta.glob from Vite.

DEPRECATED: This plugin has be ported to back to Vite v3.0.


vite-plugin-glob

NPM version

The design experiment for import.meta.glob from Vite.

Motivations

There are quite some scenarios that import.meta.glob wasn't considered when it's been implemented at the beginning. So we received quite a few PRs to improve it.

However, some design considerations might conflict with each other. For example, #2495 support ignore option for glob import supports the ignore glob as a second argument, while in #6953 import.meta.glob support ?raw we uses the second argument to specify glob query (and later been changed to { as } via #7215 deprecate { assert: { type: raw }} in favor of { as: raw }).

There are several other PRs that touches it's design as well:

With these two TC39 proposals (import-reflection and import-assertions) not settled yet, combining with different needs and design tradeoffs in each PR, making the good API design for import.meta.glob directly in Vite core becoming harder and more and more complex than it could be (with the cautions to not break existing usages).

On top of that, in Vite we are having multiple macros for different options:

  • import.meta.glob
  • import.meta.globEager
  • import.meta.globEagerDefault (undocumented)

That results in a quite large API surface to maintain and make the future extension harder. For example, if we want import.meta.globNamed we might also need to add import.meta.globEagerNamed, making the counts to 5.

Thus I propose to experiment with the import.meta.glob as an external plugin so we could introduce breaking change more easier and ships the implementation much faster (in Vite it takes days for a change to be meraged, and weeks to months for it to be landed in stable release). And when we feel the new design is able to cover most of the use cases, then we could embed it into Vite core as a one-time breaking change in v3.0.

Features

  • Globing for multiple patterns
  • Globing ignore
  • HMR on file modification / addition / deletion
  • Ability to provide custom queries
  • Ability to only import default / named export
  • An unified API for different options
  • Vite alias
  • (Optional) Takeover Vite's import.meta.glob
  • Experiments

Install

npm i -D vite-plugin-glob
// vite.config.ts
import { defineConfig } from 'vite'
import GlobPlugin from 'vite-plugin-glob'

export default defineConfig({
  plugins: [
    GlobPlugin({
      // enable to let this plugin interpret `import.meta.glob`
      // takeover: true,
    }),
  ],
})

Usage

The API is named as import.meta.importGlob to avoid conflict with Vite's import.meta.glob in this implementation.

const modules = import.meta.importGlob('./dir/*.js')

/* {
  './dir/foo.js': () => import('./dir/foo.js'),
  './dir/bar.js': () => import('./dir/bar.js'),
} */

Multiple Globs

const modules = import.meta.importGlob([
  './dir/*.js',
  './another/dir/*.js',
])

/* {
  './dir/foo.js': () => import('./dir/foo.js'),
  './dir/bar.js': () => import('./dir/bar.js'),
  './another/dir/index.js': () => import('./another/dir/index.js'),
} */

Ignore Glob

Globs start with ! will be matched to exclude.

const modules = import.meta.importGlob([
  './dir/*.js',
  '!**/index.js',
])

Eager

Import the modules as static imports.

const modules = import.meta.importGlob('./dir/*.js', { eager: true })

/*
import * as __glob__0_0 from './dir/foo.js'
import * as __glob__0_1 from './dir/bar.js'
const modules = {
  './dir/foo.js': __glob__0_0,
  './dir/bar.js': __glob__0_1
}
*/

Import As

const modules = import.meta.importGlob('./dir/*.js', { as: 'raw' })

/* {
  './dir/foo.js': () => import('./dir/foo.js?raw'),
  './dir/bar.js': () => import('./dir/bar.js?raw'),
} */

Named Imports

It's possible to only import parts of the modules with the import options.

const setups = import.meta.importGlob('./dir/*.js', { import: 'setup' })

/* {
  './dir/foo.js': () => import('./dir/foo.js').then(m => m.setup),
  './dir/bar.js': () => import('./dir/bar.js').then(m => m.setup),
} */

Combining with eager, it's even possible to have tree-shaking enable for those modules.

const setups = import.meta.importGlob('./dir/*.js', { import: 'setup', eager: true })

/*
import { setup as __glob__0_0 } from './dir/foo.js'
import { setup as __glob__0_1 } from './dir/bar.js'
const setups = {
  './dir/foo.js': __glob__0_0,
  './dir/bar.js': __glob__0_1
}
*/

Set export to default to import the default export.

const modules = import.meta.importGlob('./dir/*.js', { import: 'default', eager: true })

/*
import __glob__0_0 from './dir/foo.js'
import __glob__0_1 from './dir/bar.js'
const modules = {
  './dir/foo.js': __glob__0_0,
  './dir/bar.js': __glob__0_1
}
*/

Custom Queries

const setups = import.meta.importGlob('./dir/*.js', { query: { foo: 'bar', bar: true } })

/* {
  './dir/foo.js': () => import('./dir/foo.js?foo=bar&bar=true&lang.js').then(m => m.setup),
  './dir/bar.js': () => import('./dir/bar.js?foo=bar&bar=true&lang.js').then(m => m.setup),
} */

Experiments

The following features are in experiments, feedbacks are greatly welcome!

Restore file extension when query is specified

Append fake &lang.(ext) when queries are specified, to preseve the file extension for following plugins to process.

Learn more at Discussions

This is disabled by default since v0.3.0. To enable it, pass restoreQueryExtension: true to plugin options.

TypeScript

Add to tsconfig.json

{
  "compilerOptions": {
    "types": [
      "vite-plugin-glob/client",
      // with takeover enabled
      // "vite-plugin-glob/takeover"
    ]
  }
}

You can use generic to specify the type of the modules.

interface SomeModule {
  name: string
  default: { /* ... */ }
}

import.meta.importGlob<SomeModule>('./dir/*.js')

Sponsors

License

MIT License Β© 2021 Anthony Fu

More Repositories

1

vitesse

πŸ• Opinionated Vite + Vue Starter Template
TypeScript
8,522
star
2

ni

πŸ’‘ Use the right package manager
TypeScript
4,527
star
3

icones

⚑️ Icon Explorer with Instant searching, powered by Iconify
Vue
4,525
star
4

unplugin-vue-components

πŸ“² On-demand components auto importing for Vue
TypeScript
3,011
star
5

unocss

The instant on-demand atomic CSS engine.
TypeScript
2,990
star
6

unplugin-icons

🀹 Access thousands of icons as components on-demand universally.
TypeScript
2,793
star
7

vitesse-webext

⚑️ WebExtension Vite Starter Template
TypeScript
2,581
star
8

unplugin-auto-import

Auto import APIs on-demand for Vite, Webpack and Rollup
TypeScript
2,307
star
9

vscode-file-nesting-config

Config of File Nesting for VS Code
JavaScript
2,132
star
10

vue-starport

πŸ›° Shared component across routes with animations
TypeScript
1,744
star
11

vscode-settings

My VS Code settings and extensions
1,739
star
12

eslint-config

Anthony's ESLint config presets
JavaScript
1,605
star
13

vitesse-nuxt3

Vitesse for Nuxt 3 πŸ”πŸ’šβš‘οΈ
TypeScript
1,510
star
14

shikiji

A syntax highlighter based on TextMate grammars. ESM rewrite of shiki, with more features and capabilities.
TypeScript
1,483
star
15

reactivue

πŸ™Š Use Vue Composition API in React components
TypeScript
1,352
star
16

taze

πŸ₯¦ A modern cli tool that keeps your deps fresh
TypeScript
1,233
star
17

vite-ssg

Static site generation for Vue 3 on Vite
TypeScript
1,198
star
18

handle

A Chinese Hanzi variation of Wordle - 汉字 Wordle
TypeScript
1,194
star
19

case-police

🚨 Make the case correct, PLEASE!
TypeScript
1,098
star
20

vite-plugin-inspect

Inspect the intermediate state of Vite plugins
Vue
1,009
star
21

qrcode-toolkit

Anthony's QR Code Toolkit for AI generated QR Codes
Vue
1,008
star
22

use

Things I am using
946
star
23

vitesse-lite

⛺️ Lightweight version of Vitesse
TypeScript
901
star
24

drauu

Headless SVG-based drawboard in browser.
TypeScript
817
star
25

broz

A simple, frameless browser for screenshots
JavaScript
745
star
26

iroiro

Beautiful Colors Lookup in CLI
TypeScript
735
star
27

retypewriter

Replay the steps of your changes at ease.
TypeScript
715
star
28

live-draw

A tool allows you to draw on screen real-time.
C#
640
star
29

changelogithub

Generate changelog for GitHub
TypeScript
613
star
30

sd-webui-qrcode-toolkit

Anthony's QR Toolkit for Stable Diffusion WebUI
JavaScript
604
star
31

vscode-smart-clicks

Smart selection with double clicks for VS Code.
TypeScript
601
star
32

vite-plugin-md

Markdown with Vue for Vite
TypeScript
580
star
33

unplugin-vue2-script-setup

πŸ’‘ Bring `<script setup>` to Vue 2.
TypeScript
567
star
34

eslint-flat-config-viewer

A visual tool to help you view and understand your ESLint Flat config.
Vue
557
star
35

sponsorkit

πŸ’– Toolkit for generating sponsors images πŸ˜„
TypeScript
550
star
36

unconfig

A universal solution for loading configurations.
TypeScript
524
star
37

utils

Collection of common JavaScript / TypeScript utils
TypeScript
493
star
38

100

My 100-day project of exploring design, compform, and new things.
Vue
491
star
39

v-lazy-show

Compile-time directive to lazy initialize v-show for Vue
TypeScript
480
star
40

antfu.me

My personal website
Markdown
463
star
41

vue-reuse-template

Define and reuse Vue template inside the component scope.
TypeScript
440
star
42

raycast-multi-translate

A Raycast extension that translates text to multiple languages at once
TypeScript
431
star
43

vite-node

Vite as Node.js runtime
JavaScript
428
star
44

vscode-browse-lite

πŸš€ An embedded browser in VS Code
TypeScript
425
star
45

vscode-vite

One step faster for Vite in VS Code ⚑️
TypeScript
404
star
46

vscode-theme-vitesse

πŸ• Vitesse theme for VS Code
TypeScript
395
star
47

starter-ts

Starter template for TypeScript library
TypeScript
386
star
48

vue-template-promise

Template as Promise in Vue
TypeScript
376
star
49

vue-global-api

Use Vue Composition API globally
TypeScript
331
star
50

vue-router-better-scroller

Enhanced scroll behavior for Vue Router
TypeScript
321
star
51

starter-vscode

Starter template for VS Code Extension
TypeScript
299
star
52

vscode-iconify

πŸ™‚ Iconify IntelliSense for VS Code
TypeScript
294
star
53

1990-script

Make your GitHub history back to 1990
Shell
293
star
54

p

Toolkit for managing multiple promises
TypeScript
284
star
55

qr-scanner-wechat

QR Code scanner in JS with Open CV and WeChat's Algorithm
JavaScript
281
star
56

wenyan-lang-vscode

文言 Wenyan Lang for VS Code
TypeScript
277
star
57

fsxx

File system in zx style
JavaScript
262
star
58

birpc

Message-based two-way remote procedure call.
TypeScript
245
star
59

pnpm-patch-i

A better and interactive pnpm patch
TypeScript
243
star
60

vue-minesweeper

A tiny minesweeper clone in Vue 3
TypeScript
242
star
61

contribute

Contribution guide for my projects
231
star
62

purge-icons

🎐 Bundles icons on demand
TypeScript
229
star
63

vscode-open-in-github-button

Add a button to go to the GitHub on the status bar.
TypeScript
223
star
64

github-doorcat

😼 Supercharges GitHub navbar for fast navigation [WIP]
TypeScript
214
star
65

vscode-goto-alias

Go to Definition following alias redirections.
TypeScript
211
star
66

nuxt-server-fn

Server functions in client for Nuxt 3
TypeScript
194
star
67

refined-github-notifications

UserScript that enhances the GitHub Notifications
JavaScript
194
star
68

prism-theme-vars

A customizable Prism.js theme using CSS variables
CSS
178
star
69

what-time

What time works for you?
Vue
177
star
70

esbuild-node-loader

Transpile TypeScript to ESM with Node.js loader.
JavaScript
176
star
71

vitesse-nuxt-bridge

πŸ• Vitesse experience for Nuxt 2 and Vue 2
Vue
174
star
72

talks

Slides & code for my talks
Vue
170
star
73

userscript-clean-twitter

Bring back the peace on Twitter
JavaScript
170
star
74

fs-spy

Monitoring fs accessing for Node process
TypeScript
166
star
75

magic-string-stack

magic-string with the capability of committing changes.
TypeScript
165
star
76

vscode-icons-carbon

Carbon Visual Studio Code product icon theme
TypeScript
157
star
77

vite-plugin-optimize-persist

Persist dynamically analyzed dependencies optimization
TypeScript
155
star
78

diff-match-patch-es

ESM and TypeScript rewrite of Google's diff-match-patch for JavaScript
TypeScript
150
star
79

v-dollar

jQuery-like Vue Reactivity API
TypeScript
146
star
80

eslint-ts-patch

Support loading eslint.config.mjs and eslint.config.ts as flat config files for ESLint.
JavaScript
146
star
81

eslint-typegen

Generate types from ESLint rule schemas, with auto-completion and type-checking for rule options.
TypeScript
144
star
82

.github

The default community health files for all my repos on GitHub
143
star
83

p5i

p5.js, but with more friendly instance mode APIs
TypeScript
133
star
84

markdown-it-github-alerts

Support GitHub-style alerts for markdown-it
TypeScript
132
star
85

magic-string-extra

Extended magic-string with extra utilities
TypeScript
131
star
86

export-size

Analysis bundle cost for each export of a package
TypeScript
130
star
87

local-pkg

Get information on local packages.
TypeScript
126
star
88

vite-plugin-restart

Custom files/globs to restart Vite server
TypeScript
124
star
89

pkg-exports

Get exports of an local npm package.
TypeScript
116
star
90

install-pkg

Install package programmatically.
TypeScript
114
star
91

deploy-check

WIP, Prevent runtime errors earlier in CI
TypeScript
113
star
92

qr-verify

A CLI to verify scannable QR Code in batch
TypeScript
106
star
93

issue-up

Mirror issues to the upstream repos
TypeScript
104
star
94

unplugin-starter

Starter template for unplugin
TypeScript
100
star
95

windicss-runtime-dom

πŸͺ„ Enables Windi CSS for any site with one-line code without any build tools
TypeScript
100
star
96

rex

πŸ“‘ Transform texts with RegExp like a Pro.
Vue
98
star
97

vscode-auto-npx

Auto resolving local Node.js binaries in VS Code terminal.
TypeScript
96
star
98

vite-plugin-remote-assets

Bundles your assets from remote urls with your app
TypeScript
94
star
99

markdown-it-mdc

MDC (Markdown Components) syntax for markdown-it.
TypeScript
92
star
100

cache-async-fn

TypeScript
92
star