rsbuild-plugin-virtual-module 0.3.0 → 0.4.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/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,11 @@ 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
+ chain.plugin('RSBUILD_VIRTUAL_MODULE_PLUGIN').use(new rspack.experiments.VirtualModulesPlugin(Object.fromEntries(virtualFileAbsolutePaths.map((i)=>[
47
+ i[1],
48
+ ''
49
+ ]))));
63
50
  chain.resolve.alias.merge(Object.fromEntries(virtualFileAbsolutePaths));
64
51
  });
65
52
  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,11 +1,10 @@
1
- import * as __WEBPACK_EXTERNAL_MODULE_node_fs_promises_153e37e0__ from "node:fs/promises";
2
1
  import * as __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__ 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 = (0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.join)(api.context.rootPath, virtualFolderName);
9
8
  const virtualFileAbsolutePaths = Object.keys(virtualModules).map((i)=>{
10
9
  let absolutePath = (0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.join)(TEMP_DIR, i);
11
10
  if (!(0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.extname)(absolutePath)) absolutePath = `${absolutePath}.js`;
@@ -14,23 +13,11 @@ const pluginVirtualModule = (pluginOptions = {})=>({
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
+ chain.plugin('RSBUILD_VIRTUAL_MODULE_PLUGIN').use(new rspack.experiments.VirtualModulesPlugin(Object.fromEntries(virtualFileAbsolutePaths.map((i)=>[
18
+ i[1],
19
+ ''
20
+ ]))));
34
21
  chain.resolve.alias.merge(Object.fromEntries(virtualFileAbsolutePaths));
35
22
  });
36
23
  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.0",
3
+ "version": "0.4.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/rspack-contrib/rsbuild-plugin-virtual-module"
@@ -33,7 +33,7 @@
33
33
  "devDependencies": {
34
34
  "@biomejs/biome": "^1.9.4",
35
35
  "@playwright/test": "^1.51.1",
36
- "@rsbuild/core": "^1.3.13",
36
+ "@rsbuild/core": "^1.5.3",
37
37
  "@rslib/core": "^0.6.1",
38
38
  "@types/node": "^22.13.16",
39
39
  "playwright": "^1.51.1",