@originjs/vite-plugin-require-context
TypeScript icon, indicating that this package has built-in type declarations

1.0.9 • Public • Published

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 :
    1. search in relative path : require.context("./dir_name/", true, /\.vue$/), require.context("../dir_name/", false, /\.vue$/)
    2. search in node_modules : require.context("dir_name", true, /\.vue$/)
    3. search in src : require.context("@/dir_name/", true, /\.vue$/)

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 be process.cwd()

  • defaultRegExp: RegExp The default RegExp used in require.context if the third parameter regExp of require.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

Package Sidebar

Install

npm i @originjs/vite-plugin-require-context

Weekly Downloads

9,241

Version

1.0.9

License

MulanPSL2

Unpacked Size

39.4 kB

Total Files

6

Last publish

Collaborators

  • flyfishzy
  • alan_scut
  • yuangongji
  • peteralfredlee
  • jiawulin