
Company News
Socket Named Top Sales Organization by RepVue
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.
vite-plugin-import-context
Advanced tools
A dynamic introduction plugin based on vite.Support dynamic import
English | 中文
A dynamic import plugin based on vite.Support dynamic import
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
import { UserConfigExport } from 'vite';
import vue from '@vitejs/plugin-vue';
import dynamicImport from '../src/index';
export default (): UserConfigExport => {
return {
plugins: [vue(), dynamicImport(/*options*/)],
};
};
ts development environment. Then add the type to tsconfig.json. The corresponding type definition has been configured{
"compilerOptions": {
"types": ["vite-plugin-import-context/client"]
}
}
| 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 |
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
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));
});
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));
});
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));
});
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('======================');
});
});
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 |
MIT
FAQs
A dynamic introduction plugin based on vite.Support dynamic import
We found that vite-plugin-import-context demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.

Security News
NIST will stop enriching most CVEs under a new risk-based model, narrowing the NVD's scope as vulnerability submissions continue to surge.

Company News
/Security News
Socket is an initial recipient of OpenAI's Cybersecurity Grant Program, which commits $10M in API credits to defenders securing open source software.