rsbuild-plugin-virtual-module 0.1.0 → 0.1.2

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
@@ -1,6 +1,8 @@
1
- # rsbuild-plugin-virtual-module
1
+ # rsbuild-plugin-virtual-module🧙
2
2
 
3
- Example plugin for Rsbuild.
3
+ The simplest and most flexible way to build with a compiling magic 🪄
4
+
5
+ An Rsbuild plugin that allows you to create virtual modules, the pro version of [rspack-plugin-virtual-module](https://github.com/rspack-contrib/rspack-plugin-virtual-module) with loader API.
4
6
 
5
7
  <p>
6
8
  <a href="https://npmjs.com/package/rsbuild-plugin-virtual-module">
@@ -22,29 +24,80 @@ Add plugin to your `rsbuild.config.ts`:
22
24
 
23
25
  ```ts
24
26
  // rsbuild.config.ts
25
- import { pluginVirtualModule } from "rsbuild-plugin-virtual-module";
27
+ import { pluginVirtualModule } from 'rsbuild-plugin-virtual-module';
26
28
 
27
29
  export default {
28
- plugins: [pluginVirtualModule()],
30
+ plugins: [
31
+ pluginVirtualModule({
32
+ virtualModules: {
33
+ 'virtual-foo': async () => {
34
+ return 'export default {}';
35
+ },
36
+ },
37
+ }),
38
+ ],
29
39
  };
30
40
  ```
31
41
 
42
+ ```ts
43
+ import foo from 'virtual-foo';
44
+
45
+ console.log(foo); // {}
46
+ ```
47
+
32
48
  ## Options
33
49
 
34
- ### foo
50
+ ### virtualModules
35
51
 
36
- Some description.
52
+ 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)
37
53
 
38
- - Type: `string`
39
- - Default: `undefined`
54
+ - Type:
55
+
56
+ ```ts
57
+ import type { TransformHandler } from '@rsbuild/core';
58
+
59
+ type VirtualModules = Record<string, TransformHandler>;
60
+ ```
61
+
62
+ - Default: `{}`
40
63
  - Example:
41
64
 
42
65
  ```js
43
66
  pluginVirtualModule({
44
- foo: "bar",
67
+ virtualModules: {
68
+ 'virtual-json-list': async ({ addDependency, addContextDependency }) => {
69
+ const jsonFolderPath = join(__dirname, 'json');
70
+ const ls = await readdir(jsonFolderPath);
71
+ addContextDependency(jsonFolderPath);
72
+
73
+ const res: Record<string, unknown> = {};
74
+ for (const file of ls) {
75
+ if (file.endsWith('.json')) {
76
+ const jsonFilePath = join(jsonFolderPath, file);
77
+ const jsonContent = await readFile(jsonFilePath, 'utf-8');
78
+ addDependency(jsonFilePath);
79
+ res[file] = JSON.parse(jsonContent);
80
+ }
81
+ }
82
+
83
+ return `export default ${JSON.stringify(res)}`;
84
+ },
85
+ },
45
86
  });
46
87
  ```
47
88
 
89
+ ```js
90
+ import jsonList from 'virtual-json-list';
91
+ console.log(jsonList);
92
+ ```
93
+
94
+ ### tempDir
95
+
96
+ The name of the virtual module folder under `node_modules`.
97
+
98
+ - Type: `string`
99
+ - Default: `.rsbuild-virtual-module`
100
+
48
101
  ## License
49
102
 
50
103
  [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
  });
@@ -35,9 +33,9 @@ const PLUGIN_VIRTUAL_MODULE_NAME = 'rsbuild:virtual-module';
35
33
  const pluginVirtualModule = (pluginOptions = {})=>({
36
34
  name: PLUGIN_VIRTUAL_MODULE_NAME,
37
35
  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)=>{
36
+ const { virtualModules = {}, tempDir: virtualFolderName = '.rsbuild-virtual-module' } = pluginOptions;
37
+ const TEMP_DIR = (0, external_node_path_namespaceObject.join)(api.context.rootPath, 'node_modules', virtualFolderName);
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 [
@@ -45,6 +43,13 @@ const pluginVirtualModule = (pluginOptions = {})=>({
45
43
  absolutePath
46
44
  ];
47
45
  });
46
+ api.modifyRsbuildConfig((config)=>{
47
+ if (!config.source) config.source = {};
48
+ config.source.include = [
49
+ ...config.source.include || [],
50
+ TEMP_DIR
51
+ ];
52
+ });
48
53
  api.onBeforeCreateCompiler(async ()=>{
49
54
  await (0, promises_namespaceObject.mkdir)(TEMP_DIR, {
50
55
  recursive: true
@@ -55,15 +60,19 @@ const pluginVirtualModule = (pluginOptions = {})=>({
55
60
  chain.resolve.alias.merge(Object.fromEntries(virtualFileAbsolutePaths));
56
61
  });
57
62
  for (const [moduleName, absolutePath] of virtualFileAbsolutePaths){
58
- const handler = null == virtualModules ? void 0 : virtualModules[moduleName];
59
- if (!!handler) api.transform({
63
+ const handler = virtualModules[moduleName];
64
+ if (handler) api.transform({
60
65
  test: absolutePath
61
66
  }, handler);
62
67
  }
63
68
  }
64
69
  });
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', {
70
+ exports.PLUGIN_VIRTUAL_MODULE_NAME = __webpack_exports__.PLUGIN_VIRTUAL_MODULE_NAME;
71
+ exports.pluginVirtualModule = __webpack_exports__.pluginVirtualModule;
72
+ for(var __webpack_i__ in __webpack_exports__)if (-1 === [
73
+ "PLUGIN_VIRTUAL_MODULE_NAME",
74
+ "pluginVirtualModule"
75
+ ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
76
+ Object.defineProperty(exports, '__esModule', {
68
77
  value: true
69
78
  });
package/dist/index.d.ts CHANGED
@@ -1,8 +1,17 @@
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
+ /**
5
+ * 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)
6
+ */
7
+ virtualModules?: VirtualModules;
8
+ /**
9
+ * The name of the virtual module folder under `node_modules`
10
+ * @default '.rsbuild-virtual-module'
11
+ */
12
+ tempDir?: string;
4
13
  }
5
14
  declare const PLUGIN_VIRTUAL_MODULE_NAME = "rsbuild:virtual-module";
6
15
  declare const pluginVirtualModule: (pluginOptions?: PluginVirtualModuleOptions) => RsbuildPlugin;
7
16
  export { pluginVirtualModule, PLUGIN_VIRTUAL_MODULE_NAME };
8
- export type { PluginVirtualModuleOptions };
17
+ export type { PluginVirtualModuleOptions, VirtualModules };
package/dist/index.js CHANGED
@@ -4,9 +4,9 @@ const PLUGIN_VIRTUAL_MODULE_NAME = 'rsbuild:virtual-module';
4
4
  const pluginVirtualModule = (pluginOptions = {})=>({
5
5
  name: PLUGIN_VIRTUAL_MODULE_NAME,
6
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)=>{
7
+ const { virtualModules = {}, tempDir: virtualFolderName = '.rsbuild-virtual-module' } = pluginOptions;
8
+ const TEMP_DIR = (0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.join)(api.context.rootPath, 'node_modules', virtualFolderName);
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 [
@@ -14,6 +14,13 @@ const pluginVirtualModule = (pluginOptions = {})=>({
14
14
  absolutePath
15
15
  ];
16
16
  });
17
+ api.modifyRsbuildConfig((config)=>{
18
+ if (!config.source) config.source = {};
19
+ config.source.include = [
20
+ ...config.source.include || [],
21
+ TEMP_DIR
22
+ ];
23
+ });
17
24
  api.onBeforeCreateCompiler(async ()=>{
18
25
  await (0, __WEBPACK_EXTERNAL_MODULE_node_fs_promises_153e37e0__.mkdir)(TEMP_DIR, {
19
26
  recursive: true
@@ -24,8 +31,8 @@ const pluginVirtualModule = (pluginOptions = {})=>({
24
31
  chain.resolve.alias.merge(Object.fromEntries(virtualFileAbsolutePaths));
25
32
  });
26
33
  for (const [moduleName, absolutePath] of virtualFileAbsolutePaths){
27
- const handler = null == virtualModules ? void 0 : virtualModules[moduleName];
28
- if (!!handler) api.transform({
34
+ const handler = virtualModules[moduleName];
35
+ if (handler) api.transform({
29
36
  test: absolutePath
30
37
  }, handler);
31
38
  }
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.2",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/rspack-contrib/rsbuild-plugin-virtual-module"
@@ -17,22 +17,29 @@
17
17
  "main": "./dist/index.js",
18
18
  "module": "./dist/index.mjs",
19
19
  "types": "./dist/index.d.ts",
20
- "files": [
21
- "dist"
22
- ],
20
+ "files": ["dist"],
21
+ "scripts": {
22
+ "build": "rslib build",
23
+ "dev": "rslib build --watch",
24
+ "lint": "biome check .",
25
+ "lint:write": "biome check . --write",
26
+ "prepare": "simple-git-hooks && npm run build",
27
+ "test": "playwright test",
28
+ "bump": "npx bumpp"
29
+ },
23
30
  "simple-git-hooks": {
24
31
  "pre-commit": "npm run lint:write"
25
32
  },
26
33
  "devDependencies": {
27
34
  "@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",
35
+ "@playwright/test": "^1.51.1",
36
+ "@rsbuild/core": "^1.3.13",
37
+ "@rslib/core": "^0.6.1",
38
+ "@types/node": "^22.13.16",
39
+ "playwright": "^1.51.1",
40
+ "simple-git-hooks": "^2.12.1",
34
41
  "typescript": "^5.8.2",
35
- "rsbuild-plugin-virtual-module": "0.1.0"
42
+ "rsbuild-plugin-virtual-module": "workspace:*"
36
43
  },
37
44
  "peerDependencies": {
38
45
  "@rsbuild/core": "1.x"
@@ -42,16 +49,9 @@
42
49
  "optional": true
43
50
  }
44
51
  },
52
+ "packageManager": "pnpm@10.5.2",
45
53
  "publishConfig": {
46
54
  "access": "public",
47
55
  "registry": "https://registry.npmjs.org/"
48
- },
49
- "scripts": {
50
- "build": "rslib build",
51
- "dev": "rslib build --watch",
52
- "lint": "biome check .",
53
- "lint:write": "biome check . --write",
54
- "test": "playwright test",
55
- "bump": "npx bumpp"
56
56
  }
57
- }
57
+ }