@originjs/vite-plugin-require-context
v1.0.9
Published
A vite plugin that support require.context in vite
Downloads
26,373
Readme
vite-plugin-require-context
Support require.context
in vite
.
Features
- Based on RegExp search and replacement.
- Only
snyc
/eager
mode is supported for now. - The default regexp is
/^.*$/
, and it's different from webpack's default regexp/^\.\/.*$/
. - The implementation of
require.context.id
is different from webpack. - Syntax supported :
- search in relative path :
require.context("./dir_name/", true, /\.vue$/)
,require.context("../dir_name/", false, /\.vue$/)
- search in
node_modules
:require.context("dir_name", true, /\.vue$/)
- search in
src
:require.context("@/dir_name/", true, /\.vue$/)
- search in relative path :
Install
npm install @originjs/vite-plugin-require-context --save-dev
or
yarn add @originjs/vite-plugin-require-context --dev
Usage
import ViteRequireContext from '@originjs/vite-plugin-require-context'
export default {
plugins: [
ViteRequireContext(
/* options */
)
]
}
Options
projectBasePath: string
The base path of your project used in require.context. Default to beprocess.cwd()
defaultRegExp: RegExp
The default RegExp used inrequire.context
if the third parameterregExp
ofrequire.context
is not specified. Default to be/^.*$/
Implementation details
The following require.context
const requireComponents = require.context('./components')
will be replaced by
// generated by @originjs/vite-plugin-require-context
// start of generated code of __require_context_for_vite_function_0, generated by @originjs/vite-plugin-require-context
import * as __require_context_for_vite_0_0 from "/src/components/a.vue";
import * as __require_context_for_vite_0_1 from "/src/components/b.vue";
import * as __require_context_for_vite_0_2 from "/src/components/c.vue";
var __require_context_for_vite_map_0 = {
"./a.vue" : __require_context_for_vite_0_0,
"./b.vue" : __require_context_for_vite_0_1,
"./c.vue" : __require_context_for_vite_0_2
};
function __require_context_for_vite_function_0(req) {
var id = __require_context_for_vite_function_0_resolve(req);
return __require_context_for_vite_map_0[req];
}
function __require_context_for_vite_function_0_resolve(req) {
if (req in __require_context_for_vite_map_0) {
return __require_context_for_vite_map_0[req];
}
var e = new Error("Cannot find module '" + req + "'");
e.code = 'MODULE_NOT_FOUND';
throw e;
}
__require_context_for_vite_function_0.keys = function __require_context_for_vite_function_0_keys() {
return Object.keys(__require_context_for_vite_map_0);
}
__require_context_for_vite_function_0.resolve = __require_context_for_vite_function_0_resolve
__require_context_for_vite_function_0.id = "__require_context_for_vite_function_0"
// end of generated code of __require_context_for_vite_function_0
const requireComponents = __require_context_for_vite_function_0