npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vite-plugin-import-context

v1.0.0-rc.1

Published

A dynamic introduction plugin based on vite.Support dynamic import

Downloads

339

Readme

vite-plugin-import-context

English | 中文

npm node

A dynamic import plugin based on vite.Support dynamic import

  • 💡 Similar to webpack's require.context
  • ⚡️ Support dynamic loading
  • 📦 Detailed documentation and examples

Install (yarn or npm)

node version: >=12.0.0

vite version: >=2.0.0-beta.12

yarn add vite-plugin-import-context -D or npm i vite-plugin-import-context -D

Run Example


cd ./examples

yarn install

yarn serve

Usage

  • Config plugin in vite.config.ts
import { UserConfigExport } from 'vite';
import vue from '@vitejs/plugin-vue';

import dynamicImport from '../src/index';

export default (): UserConfigExport => {
  return {
    plugins: [vue(), dynamicImport(/*options*/)],
  };
};
  • If you are using the ts development environment. Then add the type to tsconfig.json. The corresponding type definition has been configured
{
  "compilerOptions": {
    "types": ["vite-plugin-import-context/client"]
  }
}

Options Description

| param | type | default | description | | --- | --- | --- | --- | | include | string / RegExp / (string / RegExp)[] / null / undefined | ['**/*.js', '**/*.ts', '**/*.tsx', '**/*.jsx'] | Code directory and file format to be converted | | exclude | string / RegExp / (string / RegExp)[] / null / undefined | 'node_modules/**' | Excluded files/folders |

Usage in Code

In the code, if the keyword function importContext appears, it will be transformed by the plugin.

Please ensure that there are no custom importContext functions or variables in your own code

basicExample

This example is non-dynamic import. And no deep recursion

// xxx.ts

const nextMainModule = importContext({
  dir: './',
  deep: false,
  regexp: /\.ts$/,
  dynamicImport: false,
  ignoreCurrentFile: true,
});

nextMainModule.keys().forEach((key) => {
  console.log('nextMain=>', nextMainModule(key));
});

aliasExample

It can be matched according to the alias of vite

// xxx.ts

const aliasModule = importContext({
  dir: '/@/views',
  deep: true,
  regexp: /\.ts$/,
});

aliasModule.keys().forEach((key) => {
  console.log('aliasModule=>', aliasModule(key));
});

deepImportExample

This example is non-dynamic import. And deep recursion

// xxx.ts

const nextMainModule = importContext({
  dir: './',
  deep: true,
  regexp: /\.ts$/,
  dynamicImport: false,
  ignoreCurrentFile: true,
});

nextMainModule.keys().forEach((key) => {
  console.log('nextMain=>', nextMainModule(key));
});

dynamicImportExample

This example is import dynamically. And deep recursion

const dynamicModule = importContext({
  dir: './',
  deep: true,
  regexp: /\.ts$/,
  dynamicImport: true,
});

dynamicModule.keys().forEach((key: string) => {
  console.log('dynamicModule=>', dynamicModule(key));
  dynamicModule(key)().then((res) => {
    console.log('======================');
    console.log('dynamicModuleRes=>', res);
    console.log('======================');
  });
});

ImportContext Parameter description

deep: false, regexp: /.ts$/, dynamicImport: false, ignoreCurrentFile: true,

| param | type | default | description | | --- | --- | --- | --- | | dir | string | ./ | File path to be imported, support alias | | deep | boolean | false | Whether to introduce deeply | | enabled | boolean | true | Whether to convert the code, an empty function will be returned after closing | | deep | boolean | false | Whether to introduce deeply | | regexp | regexp | /^\.\// | File matching regular | | dynamicImport | boolean | false | Whether to enable dynamic import | | ext | boolean | true | Whether the key value has a suffix | | ignoreCurrentFile | boolean | true | Whether to ignore the current file. If dir='./', It will import itself, this configuration can ignore the import of itself |

NOTE

  • The plugin draws on the idea of rollup-plugin-require-context。Because the plug-in does not support dynamic import and ts files.So i finished it
  • The function must have a variable to accept the return value of the function. Otherwise it will parse error

License

MIT