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

1.0.10 • 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$/)
  • Cross-project scenarios support : add fifth param in require.contenxt and you should pass the relative path of other project to require.context('./', true, /\.(vue|js)$/, 'sync', '/project-name/components')

Install

npm install vite-plugin-require-context-lerna --save-dev

or

yarn add vite-plugin-require-context-lerna --dev

Usage

import ViteRequireContext from 'vite-plugin-require-context-lerna'

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 vite-plugin-require-context-lerna

Weekly Downloads

2

Version

1.0.10

License

MIT license

Unpacked Size

40.3 kB

Total Files

6

Last publish

Collaborators

  • wangning6