rsbuild-plugin-virtual-module 0.3.1 → 0.4.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
@@ -93,10 +93,24 @@ console.log(jsonList);
93
93
 
94
94
  ### tempDir
95
95
 
96
- The name of the virtual module folder under `node_modules`.
96
+ The name of the virtual module folder based on `api.context.rootPath`
97
97
 
98
98
  - Type: `string`
99
99
  - Default: `.rsbuild-virtual-module`
100
+ - Example:
101
+
102
+ ```js
103
+ pluginVirtualModule({
104
+ tempDir: 'src',
105
+ virtualModules: {
106
+ 'virtual-foo': async () => {
107
+ return 'export default {}';
108
+ },
109
+ },
110
+ });
111
+ ```
112
+
113
+ The actual virtual module is `./src/virtual-foo.js`
100
114
 
101
115
  ## License
102
116
 
package/dist/index.cjs CHANGED
@@ -27,14 +27,13 @@ __webpack_require__.d(__webpack_exports__, {
27
27
  PLUGIN_VIRTUAL_MODULE_NAME: ()=>PLUGIN_VIRTUAL_MODULE_NAME,
28
28
  pluginVirtualModule: ()=>pluginVirtualModule
29
29
  });
30
- const promises_namespaceObject = require("node:fs/promises");
31
30
  const external_node_path_namespaceObject = require("node:path");
32
31
  const PLUGIN_VIRTUAL_MODULE_NAME = 'rsbuild:virtual-module';
33
32
  const pluginVirtualModule = (pluginOptions = {})=>({
34
33
  name: PLUGIN_VIRTUAL_MODULE_NAME,
35
34
  async setup (api) {
36
35
  const { virtualModules = {}, tempDir: virtualFolderName = '.rsbuild-virtual-module' } = pluginOptions;
37
- const TEMP_DIR = (0, external_node_path_namespaceObject.join)(api.context.rootPath, 'node_modules', virtualFolderName);
36
+ const TEMP_DIR = (0, external_node_path_namespaceObject.join)(api.context.rootPath, virtualFolderName);
38
37
  const virtualFileAbsolutePaths = Object.keys(virtualModules).map((i)=>{
39
38
  let absolutePath = (0, external_node_path_namespaceObject.join)(TEMP_DIR, i);
40
39
  if (!(0, external_node_path_namespaceObject.extname)(absolutePath)) absolutePath = `${absolutePath}.js`;
@@ -43,23 +42,26 @@ const pluginVirtualModule = (pluginOptions = {})=>({
43
42
  absolutePath
44
43
  ];
45
44
  });
46
- api.modifyRsbuildConfig((config)=>{
47
- if (!config.source) config.source = {};
48
- config.source.include = [
49
- ...config.source.include || [],
50
- TEMP_DIR
51
- ];
52
- });
53
- api.onBeforeCreateCompiler(async ()=>{
54
- await Promise.all(virtualFileAbsolutePaths.map(async ([_, absolutePath])=>{
55
- const dir = (0, external_node_path_namespaceObject.dirname)(absolutePath);
56
- await (0, promises_namespaceObject.mkdir)(dir, {
57
- recursive: true
58
- });
59
- return (0, promises_namespaceObject.writeFile)(absolutePath, '', 'utf-8');
60
- }));
61
- });
62
- api.modifyBundlerChain((chain)=>{
45
+ api.modifyBundlerChain((chain, { rspack })=>{
46
+ const pluginId = 'RSBUILD_VIRTUAL_MODULE_PLUGIN';
47
+ const virtualModules = Object.fromEntries(virtualFileAbsolutePaths.map((i)=>[
48
+ i[1],
49
+ ''
50
+ ]));
51
+ if (chain.plugins.has(pluginId)) chain.plugin(pluginId).tap((options)=>{
52
+ const existingPaths = new Set(Object.keys(options[0] ?? {}));
53
+ const conflictPath = virtualFileAbsolutePaths.find(([, path])=>existingPaths.has(path));
54
+ if (conflictPath) throw new Error(`Virtual module name conflicted: ${conflictPath[0]}`);
55
+ return [
56
+ {
57
+ ...options[0],
58
+ ...virtualModules
59
+ }
60
+ ];
61
+ });
62
+ else chain.plugin(pluginId).use(rspack.experiments.VirtualModulesPlugin, [
63
+ virtualModules
64
+ ]);
63
65
  chain.resolve.alias.merge(Object.fromEntries(virtualFileAbsolutePaths));
64
66
  });
65
67
  for (const [moduleName, absolutePath] of virtualFileAbsolutePaths){
package/dist/index.d.ts CHANGED
@@ -6,12 +6,12 @@ interface PluginVirtualModuleOptions {
6
6
  */
7
7
  virtualModules?: VirtualModules;
8
8
  /**
9
- * The name of the virtual module folder under `node_modules`
9
+ * The name of the virtual module folder based on `api.context.rootPath`
10
10
  * @default '.rsbuild-virtual-module'
11
11
  */
12
12
  tempDir?: string;
13
13
  }
14
14
  declare const PLUGIN_VIRTUAL_MODULE_NAME = "rsbuild:virtual-module";
15
15
  declare const pluginVirtualModule: (pluginOptions?: PluginVirtualModuleOptions) => RsbuildPlugin;
16
- export { pluginVirtualModule, PLUGIN_VIRTUAL_MODULE_NAME };
16
+ export { PLUGIN_VIRTUAL_MODULE_NAME, pluginVirtualModule };
17
17
  export type { PluginVirtualModuleOptions, VirtualModules };
package/dist/index.js CHANGED
@@ -1,36 +1,38 @@
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";
1
+ import { extname, join } from "node:path";
3
2
  const PLUGIN_VIRTUAL_MODULE_NAME = 'rsbuild:virtual-module';
4
3
  const pluginVirtualModule = (pluginOptions = {})=>({
5
4
  name: PLUGIN_VIRTUAL_MODULE_NAME,
6
5
  async setup (api) {
7
6
  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);
7
+ const TEMP_DIR = join(api.context.rootPath, virtualFolderName);
9
8
  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`;
9
+ let absolutePath = join(TEMP_DIR, i);
10
+ if (!extname(absolutePath)) absolutePath = `${absolutePath}.js`;
12
11
  return [
13
12
  i,
14
13
  absolutePath
15
14
  ];
16
15
  });
17
- api.modifyRsbuildConfig((config)=>{
18
- if (!config.source) config.source = {};
19
- config.source.include = [
20
- ...config.source.include || [],
21
- TEMP_DIR
22
- ];
23
- });
24
- api.onBeforeCreateCompiler(async ()=>{
25
- await Promise.all(virtualFileAbsolutePaths.map(async ([_, absolutePath])=>{
26
- const dir = (0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.dirname)(absolutePath);
27
- await (0, __WEBPACK_EXTERNAL_MODULE_node_fs_promises_153e37e0__.mkdir)(dir, {
28
- recursive: true
29
- });
30
- return (0, __WEBPACK_EXTERNAL_MODULE_node_fs_promises_153e37e0__.writeFile)(absolutePath, '', 'utf-8');
31
- }));
32
- });
33
- api.modifyBundlerChain((chain)=>{
16
+ api.modifyBundlerChain((chain, { rspack })=>{
17
+ const pluginId = 'RSBUILD_VIRTUAL_MODULE_PLUGIN';
18
+ const virtualModules = Object.fromEntries(virtualFileAbsolutePaths.map((i)=>[
19
+ i[1],
20
+ ''
21
+ ]));
22
+ if (chain.plugins.has(pluginId)) chain.plugin(pluginId).tap((options)=>{
23
+ const existingPaths = new Set(Object.keys(options[0] ?? {}));
24
+ const conflictPath = virtualFileAbsolutePaths.find(([, path])=>existingPaths.has(path));
25
+ if (conflictPath) throw new Error(`Virtual module name conflicted: ${conflictPath[0]}`);
26
+ return [
27
+ {
28
+ ...options[0],
29
+ ...virtualModules
30
+ }
31
+ ];
32
+ });
33
+ else chain.plugin(pluginId).use(rspack.experiments.VirtualModulesPlugin, [
34
+ virtualModules
35
+ ]);
34
36
  chain.resolve.alias.merge(Object.fromEntries(virtualFileAbsolutePaths));
35
37
  });
36
38
  for (const [moduleName, absolutePath] of virtualFileAbsolutePaths){
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rsbuild-plugin-virtual-module",
3
- "version": "0.3.1",
3
+ "version": "0.4.1",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/rspack-contrib/rsbuild-plugin-virtual-module"
@@ -17,7 +17,9 @@
17
17
  "main": "./dist/index.js",
18
18
  "module": "./dist/index.mjs",
19
19
  "types": "./dist/index.d.ts",
20
- "files": ["dist"],
20
+ "files": [
21
+ "dist"
22
+ ],
21
23
  "scripts": {
22
24
  "build": "rslib build",
23
25
  "dev": "rslib build --watch",
@@ -31,14 +33,14 @@
31
33
  "pre-commit": "npm run lint:write"
32
34
  },
33
35
  "devDependencies": {
34
- "@biomejs/biome": "^1.9.4",
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",
41
- "typescript": "^5.8.2",
36
+ "@biomejs/biome": "^2.2.4",
37
+ "@playwright/test": "^1.55.0",
38
+ "@rsbuild/core": "^1.5.7",
39
+ "@rslib/core": "^0.13.3",
40
+ "@types/node": "^24.5.1",
41
+ "playwright": "^1.55.0",
42
+ "simple-git-hooks": "^2.13.1",
43
+ "typescript": "^5.9.2",
42
44
  "rsbuild-plugin-virtual-module": "workspace:*"
43
45
  },
44
46
  "peerDependencies": {
@@ -49,7 +51,7 @@
49
51
  "optional": true
50
52
  }
51
53
  },
52
- "packageManager": "pnpm@10.5.2",
54
+ "packageManager": "pnpm@10.16.1",
53
55
  "publishConfig": {
54
56
  "access": "public",
55
57
  "registry": "https://registry.npmjs.org/"