rsbuild-plugin-virtual-module 0.0.1 → 0.1.1

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/README.md CHANGED
@@ -22,29 +22,73 @@ Add plugin to your `rsbuild.config.ts`:
22
22
 
23
23
  ```ts
24
24
  // rsbuild.config.ts
25
- import { pluginVirtualModule } from "rsbuild-plugin-virtual-module";
25
+ import { pluginVirtualModule } from 'rsbuild-plugin-virtual-module';
26
26
 
27
27
  export default {
28
- plugins: [pluginVirtualModule()],
28
+ plugins: [
29
+ pluginVirtualModule({
30
+ virtualModules: {
31
+ 'virtual-foo': async () => {
32
+ return 'export default {}';
33
+ },
34
+ },
35
+ }),
36
+ ],
29
37
  };
30
38
  ```
31
39
 
40
+ ```ts
41
+ import foo from 'virtual-foo';
42
+
43
+ console.log(foo); // {}
44
+ ```
45
+
32
46
  ## Options
33
47
 
34
- ### foo
48
+ ### virtualModules
49
+
50
+ Generate virtual modules, where the key is the name of the virtual module and the value is `TransformHandler`. See [Rsbuild - api.transform](https://rsbuild.dev/plugins/dev/core#apitransform)
51
+
52
+ - Type:
53
+
54
+ ```ts
55
+ import type { RsbuildPlugin, TransformHandler } from '@rsbuild/core';
35
56
 
36
- Some description.
57
+ type VirtualModules = Record<string, TransformHandler>;
58
+ ```
37
59
 
38
- - Type: `string`
39
- - Default: `undefined`
60
+ - Default: `{}`
40
61
  - Example:
41
62
 
42
63
  ```js
43
64
  pluginVirtualModule({
44
- foo: "bar",
65
+ virtualModules: {
66
+ 'virtual-json-list': async ({ addDependency, addContextDependency }) => {
67
+ const jsonFolderPath = join(__dirname, 'json');
68
+ const ls = await readdir(jsonFolderPath);
69
+ addContextDependency(jsonFolderPath);
70
+
71
+ const res: Record<string, unknown> = {};
72
+ for (const file of ls) {
73
+ if (file.endsWith('.json')) {
74
+ const jsonFilePath = join(jsonFolderPath, file);
75
+ const jsonContent = await readFile(jsonFilePath, 'utf-8');
76
+ addDependency(jsonFilePath);
77
+ res[file] = JSON.parse(jsonContent);
78
+ }
79
+ }
80
+
81
+ return `export default ${JSON.stringify(res)}`;
82
+ },
83
+ },
45
84
  });
46
85
  ```
47
86
 
87
+ ```js
88
+ import jsonList from 'virtual-json-list';
89
+ console.log(jsonList);
90
+ ```
91
+
48
92
  ## License
49
93
 
50
94
  [MIT](./LICENSE).
package/dist/index.cjs CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  var __webpack_require__ = {};
3
3
  (()=>{
4
- __webpack_require__.d = function(exports1, definition) {
4
+ __webpack_require__.d = (exports1, definition)=>{
5
5
  for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
6
  enumerable: true,
7
7
  get: definition[key]
@@ -9,12 +9,10 @@ var __webpack_require__ = {};
9
9
  };
10
10
  })();
11
11
  (()=>{
12
- __webpack_require__.o = function(obj, prop) {
13
- return Object.prototype.hasOwnProperty.call(obj, prop);
14
- };
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
15
13
  })();
16
14
  (()=>{
17
- __webpack_require__.r = function(exports1) {
15
+ __webpack_require__.r = (exports1)=>{
18
16
  if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
19
17
  value: 'Module'
20
18
  });
@@ -26,16 +24,48 @@ var __webpack_require__ = {};
26
24
  var __webpack_exports__ = {};
27
25
  __webpack_require__.r(__webpack_exports__);
28
26
  __webpack_require__.d(__webpack_exports__, {
27
+ PLUGIN_VIRTUAL_MODULE_NAME: ()=>PLUGIN_VIRTUAL_MODULE_NAME,
29
28
  pluginVirtualModule: ()=>pluginVirtualModule
30
29
  });
31
- const pluginVirtualModule = (options = {})=>({
32
- name: 'plugin-example',
33
- setup () {
34
- console.log('Hello Rsbuild!', options);
30
+ const promises_namespaceObject = require("node:fs/promises");
31
+ const external_node_path_namespaceObject = require("node:path");
32
+ const PLUGIN_VIRTUAL_MODULE_NAME = 'rsbuild:virtual-module';
33
+ const pluginVirtualModule = (pluginOptions = {})=>({
34
+ name: PLUGIN_VIRTUAL_MODULE_NAME,
35
+ async setup (api) {
36
+ const TEMP_DIR = (0, external_node_path_namespaceObject.join)(process.cwd(), 'node_modules/.rsbuild-virtual-module');
37
+ const { virtualModules = {} } = pluginOptions;
38
+ const virtualFileAbsolutePaths = Object.keys(virtualModules).map((i)=>{
39
+ let absolutePath = (0, external_node_path_namespaceObject.join)(TEMP_DIR, i);
40
+ if (!(0, external_node_path_namespaceObject.extname)(absolutePath)) absolutePath = `${absolutePath}.js`;
41
+ return [
42
+ i,
43
+ absolutePath
44
+ ];
45
+ });
46
+ api.onBeforeCreateCompiler(async ()=>{
47
+ await (0, promises_namespaceObject.mkdir)(TEMP_DIR, {
48
+ recursive: true
49
+ });
50
+ await Promise.all(virtualFileAbsolutePaths.map(([_, absolutePath])=>(0, promises_namespaceObject.writeFile)(absolutePath, '', 'utf-8')));
51
+ });
52
+ api.modifyBundlerChain((chain)=>{
53
+ chain.resolve.alias.merge(Object.fromEntries(virtualFileAbsolutePaths));
54
+ });
55
+ for (const [moduleName, absolutePath] of virtualFileAbsolutePaths){
56
+ const handler = virtualModules[moduleName];
57
+ if (handler) api.transform({
58
+ test: absolutePath
59
+ }, handler);
60
+ }
35
61
  }
36
62
  });
37
- var __webpack_export_target__ = exports;
38
- for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
39
- if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
63
+ exports.PLUGIN_VIRTUAL_MODULE_NAME = __webpack_exports__.PLUGIN_VIRTUAL_MODULE_NAME;
64
+ exports.pluginVirtualModule = __webpack_exports__.pluginVirtualModule;
65
+ for(var __webpack_i__ in __webpack_exports__)if (-1 === [
66
+ "PLUGIN_VIRTUAL_MODULE_NAME",
67
+ "pluginVirtualModule"
68
+ ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
69
+ Object.defineProperty(exports, '__esModule', {
40
70
  value: true
41
71
  });
package/dist/index.d.ts CHANGED
@@ -1,6 +1,9 @@
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
+ type VirtualModules = Record<string, TransformHandler>;
3
+ interface PluginVirtualModuleOptions {
4
+ virtualModules?: VirtualModules;
5
+ }
6
+ declare const PLUGIN_VIRTUAL_MODULE_NAME = "rsbuild:virtual-module";
7
+ declare const pluginVirtualModule: (pluginOptions?: PluginVirtualModuleOptions) => RsbuildPlugin;
8
+ export { pluginVirtualModule, PLUGIN_VIRTUAL_MODULE_NAME };
9
+ export type { PluginVirtualModuleOptions, VirtualModules };
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 = 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.1",
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",
@@ -25,13 +25,14 @@
25
25
  },
26
26
  "devDependencies": {
27
27
  "@biomejs/biome": "^1.9.4",
28
- "@playwright/test": "^1.50.1",
29
- "@rsbuild/core": "^1.2.13",
30
- "@rslib/core": "^0.5.2",
31
- "@types/node": "^22.13.7",
32
- "playwright": "^1.50.1",
33
- "simple-git-hooks": "^2.11.1",
34
- "typescript": "^5.8.2"
28
+ "@playwright/test": "^1.51.1",
29
+ "@rsbuild/core": "^1.3.1",
30
+ "@rslib/core": "^0.6.1",
31
+ "@types/node": "^22.13.16",
32
+ "playwright": "^1.51.1",
33
+ "simple-git-hooks": "^2.12.1",
34
+ "typescript": "^5.8.2",
35
+ "rsbuild-plugin-virtual-module": "0.1.1"
35
36
  },
36
37
  "peerDependencies": {
37
38
  "@rsbuild/core": "1.x"