rsbuild-plugin-virtual-module 0.1.0 → 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
  });
@@ -36,8 +34,8 @@ const pluginVirtualModule = (pluginOptions = {})=>({
36
34
  name: PLUGIN_VIRTUAL_MODULE_NAME,
37
35
  async setup (api) {
38
36
  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)=>{
37
+ const { virtualModules = {} } = pluginOptions;
38
+ const virtualFileAbsolutePaths = Object.keys(virtualModules).map((i)=>{
41
39
  let absolutePath = (0, external_node_path_namespaceObject.join)(TEMP_DIR, i);
42
40
  if (!(0, external_node_path_namespaceObject.extname)(absolutePath)) absolutePath = `${absolutePath}.js`;
43
41
  return [
@@ -55,15 +53,19 @@ const pluginVirtualModule = (pluginOptions = {})=>({
55
53
  chain.resolve.alias.merge(Object.fromEntries(virtualFileAbsolutePaths));
56
54
  });
57
55
  for (const [moduleName, absolutePath] of virtualFileAbsolutePaths){
58
- const handler = null == virtualModules ? void 0 : virtualModules[moduleName];
59
- if (!!handler) api.transform({
56
+ const handler = virtualModules[moduleName];
57
+ if (handler) api.transform({
60
58
  test: absolutePath
61
59
  }, handler);
62
60
  }
63
61
  }
64
62
  });
65
- var __webpack_export_target__ = exports;
66
- for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
67
- 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', {
68
70
  value: true
69
71
  });
package/dist/index.d.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  import type { RsbuildPlugin, TransformHandler } from '@rsbuild/core';
2
+ type VirtualModules = Record<string, TransformHandler>;
2
3
  interface PluginVirtualModuleOptions {
3
- virtualModules?: Record<string, TransformHandler>;
4
+ virtualModules?: VirtualModules;
4
5
  }
5
6
  declare const PLUGIN_VIRTUAL_MODULE_NAME = "rsbuild:virtual-module";
6
7
  declare const pluginVirtualModule: (pluginOptions?: PluginVirtualModuleOptions) => RsbuildPlugin;
7
8
  export { pluginVirtualModule, PLUGIN_VIRTUAL_MODULE_NAME };
8
- export type { PluginVirtualModuleOptions };
9
+ export type { PluginVirtualModuleOptions, VirtualModules };
package/dist/index.js CHANGED
@@ -5,8 +5,8 @@ const pluginVirtualModule = (pluginOptions = {})=>({
5
5
  name: PLUGIN_VIRTUAL_MODULE_NAME,
6
6
  async setup (api) {
7
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)=>{
8
+ const { virtualModules = {} } = pluginOptions;
9
+ const virtualFileAbsolutePaths = Object.keys(virtualModules).map((i)=>{
10
10
  let absolutePath = (0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.join)(TEMP_DIR, i);
11
11
  if (!(0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.extname)(absolutePath)) absolutePath = `${absolutePath}.js`;
12
12
  return [
@@ -24,8 +24,8 @@ const pluginVirtualModule = (pluginOptions = {})=>({
24
24
  chain.resolve.alias.merge(Object.fromEntries(virtualFileAbsolutePaths));
25
25
  });
26
26
  for (const [moduleName, absolutePath] of virtualFileAbsolutePaths){
27
- const handler = null == virtualModules ? void 0 : virtualModules[moduleName];
28
- if (!!handler) api.transform({
27
+ const handler = virtualModules[moduleName];
28
+ if (handler) api.transform({
29
29
  test: absolutePath
30
30
  }, handler);
31
31
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rsbuild-plugin-virtual-module",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/rspack-contrib/rsbuild-plugin-virtual-module"
@@ -25,14 +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",
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
34
  "typescript": "^5.8.2",
35
- "rsbuild-plugin-virtual-module": "0.1.0"
35
+ "rsbuild-plugin-virtual-module": "0.1.1"
36
36
  },
37
37
  "peerDependencies": {
38
38
  "@rsbuild/core": "1.x"