rsbuild-plugin-virtual-module 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -26,12 +26,40 @@ var __webpack_require__ = {};
26
26
  var __webpack_exports__ = {};
27
27
  __webpack_require__.r(__webpack_exports__);
28
28
  __webpack_require__.d(__webpack_exports__, {
29
+ PLUGIN_VIRTUAL_MODULE_NAME: ()=>PLUGIN_VIRTUAL_MODULE_NAME,
29
30
  pluginVirtualModule: ()=>pluginVirtualModule
30
31
  });
31
- const pluginVirtualModule = (options = {})=>({
32
- name: 'plugin-example',
33
- setup () {
34
- console.log('Hello Rsbuild!', options);
32
+ const promises_namespaceObject = require("node:fs/promises");
33
+ const external_node_path_namespaceObject = require("node:path");
34
+ const PLUGIN_VIRTUAL_MODULE_NAME = 'rsbuild:virtual-module';
35
+ const pluginVirtualModule = (pluginOptions = {})=>({
36
+ name: PLUGIN_VIRTUAL_MODULE_NAME,
37
+ async setup (api) {
38
+ const TEMP_DIR = (0, external_node_path_namespaceObject.join)(process.cwd(), 'node_modules/.rsbuild-virtual-module');
39
+ const { virtualModules } = pluginOptions;
40
+ const virtualFileAbsolutePaths = Object.keys(virtualModules ?? {}).map((i)=>{
41
+ let absolutePath = (0, external_node_path_namespaceObject.join)(TEMP_DIR, i);
42
+ if (!(0, external_node_path_namespaceObject.extname)(absolutePath)) absolutePath = `${absolutePath}.js`;
43
+ return [
44
+ i,
45
+ absolutePath
46
+ ];
47
+ });
48
+ api.onBeforeCreateCompiler(async ()=>{
49
+ await (0, promises_namespaceObject.mkdir)(TEMP_DIR, {
50
+ recursive: true
51
+ });
52
+ await Promise.all(virtualFileAbsolutePaths.map(([_, absolutePath])=>(0, promises_namespaceObject.writeFile)(absolutePath, '', 'utf-8')));
53
+ });
54
+ api.modifyBundlerChain((chain)=>{
55
+ chain.resolve.alias.merge(Object.fromEntries(virtualFileAbsolutePaths));
56
+ });
57
+ for (const [moduleName, absolutePath] of virtualFileAbsolutePaths){
58
+ const handler = null == virtualModules ? void 0 : virtualModules[moduleName];
59
+ if (!!handler) api.transform({
60
+ test: absolutePath
61
+ }, handler);
62
+ }
35
63
  }
36
64
  });
37
65
  var __webpack_export_target__ = exports;
package/dist/index.d.ts CHANGED
@@ -1,6 +1,8 @@
1
- import type { RsbuildPlugin } from '@rsbuild/core';
2
- export type pluginVirtualModuleOptions = {
3
- foo?: string;
4
- bar?: boolean;
5
- };
6
- export declare const pluginVirtualModule: (options?: pluginVirtualModuleOptions) => RsbuildPlugin;
1
+ import type { RsbuildPlugin, TransformHandler } from '@rsbuild/core';
2
+ interface PluginVirtualModuleOptions {
3
+ virtualModules?: Record<string, TransformHandler>;
4
+ }
5
+ declare const PLUGIN_VIRTUAL_MODULE_NAME = "rsbuild:virtual-module";
6
+ declare const pluginVirtualModule: (pluginOptions?: PluginVirtualModuleOptions) => RsbuildPlugin;
7
+ export { pluginVirtualModule, PLUGIN_VIRTUAL_MODULE_NAME };
8
+ export type { PluginVirtualModuleOptions };
package/dist/index.js CHANGED
@@ -1,7 +1,34 @@
1
- const pluginVirtualModule = (options = {})=>({
2
- name: 'plugin-example',
3
- setup () {
4
- console.log('Hello Rsbuild!', options);
1
+ import * as __WEBPACK_EXTERNAL_MODULE_node_fs_promises_153e37e0__ from "node:fs/promises";
2
+ import * as __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__ from "node:path";
3
+ const PLUGIN_VIRTUAL_MODULE_NAME = 'rsbuild:virtual-module';
4
+ const pluginVirtualModule = (pluginOptions = {})=>({
5
+ name: PLUGIN_VIRTUAL_MODULE_NAME,
6
+ async setup (api) {
7
+ const TEMP_DIR = (0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.join)(process.cwd(), 'node_modules/.rsbuild-virtual-module');
8
+ const { virtualModules } = pluginOptions;
9
+ const virtualFileAbsolutePaths = Object.keys(virtualModules ?? {}).map((i)=>{
10
+ let absolutePath = (0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.join)(TEMP_DIR, i);
11
+ if (!(0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.extname)(absolutePath)) absolutePath = `${absolutePath}.js`;
12
+ return [
13
+ i,
14
+ absolutePath
15
+ ];
16
+ });
17
+ api.onBeforeCreateCompiler(async ()=>{
18
+ await (0, __WEBPACK_EXTERNAL_MODULE_node_fs_promises_153e37e0__.mkdir)(TEMP_DIR, {
19
+ recursive: true
20
+ });
21
+ await Promise.all(virtualFileAbsolutePaths.map(([_, absolutePath])=>(0, __WEBPACK_EXTERNAL_MODULE_node_fs_promises_153e37e0__.writeFile)(absolutePath, '', 'utf-8')));
22
+ });
23
+ api.modifyBundlerChain((chain)=>{
24
+ chain.resolve.alias.merge(Object.fromEntries(virtualFileAbsolutePaths));
25
+ });
26
+ for (const [moduleName, absolutePath] of virtualFileAbsolutePaths){
27
+ const handler = null == virtualModules ? void 0 : virtualModules[moduleName];
28
+ if (!!handler) api.transform({
29
+ test: absolutePath
30
+ }, handler);
31
+ }
5
32
  }
6
33
  });
7
- export { pluginVirtualModule };
34
+ export { PLUGIN_VIRTUAL_MODULE_NAME, pluginVirtualModule };
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "rsbuild-plugin-virtual-module",
3
- "version": "0.0.1",
3
+ "version": "0.1.0",
4
4
  "repository": {
5
5
  "type": "git",
6
- "url": "git+https://github.com/rspack-contrib/rsbuild-plugin-template.git"
6
+ "url": "https://github.com/rspack-contrib/rsbuild-plugin-virtual-module"
7
7
  },
8
8
  "license": "MIT",
9
9
  "type": "module",
@@ -31,7 +31,8 @@
31
31
  "@types/node": "^22.13.7",
32
32
  "playwright": "^1.50.1",
33
33
  "simple-git-hooks": "^2.11.1",
34
- "typescript": "^5.8.2"
34
+ "typescript": "^5.8.2",
35
+ "rsbuild-plugin-virtual-module": "0.1.0"
35
36
  },
36
37
  "peerDependencies": {
37
38
  "@rsbuild/core": "1.x"