Skip to content

Commit 67ff4fa

Browse files
authored
docs: add VirtualUrlPlugin (#7627)
1 parent a5de00e commit 67ff4fa

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
---
2+
title: VirtualUrlPlugin
3+
group: webpack
4+
contributors:
5+
- xiaoxiaojx
6+
---
7+
8+
Allow creating virtual modules of any type, such as `.ts`, `.json`, `.css`, etc. default is `.js`.
9+
10+
<Badge text="5.100.0+" />
11+
12+
```javascript
13+
const webpack = require('webpack');
14+
15+
new webpack.experiments.schemes.VirtualUrlPlugin({
16+
myModule: `export const msg = "from virtual module"`,
17+
});
18+
```
19+
20+
**src/app.js**
21+
22+
```javascript
23+
import { msg } from 'virtual:myModule';
24+
25+
console.log(msg);
26+
```
27+
28+
## Basic Example
29+
30+
Create a virtual module that generates build information
31+
32+
```javascript
33+
const webpack = require('webpack')
34+
35+
new webpack.experiments.schemes.VirtualUrlPlugin({
36+
buildInfo: {
37+
source: () {
38+
return `export const buildTime = ${+new Date()}`
39+
},
40+
version: true
41+
}
42+
});
43+
```
44+
45+
**src/app.js**
46+
47+
```javascript
48+
import { buildTime } from 'virtual:buildInfo';
49+
50+
console.log('App version: ', buildTime);
51+
```
52+
53+
Use custom schema
54+
55+
```javascript
56+
const webpack = require('webpack');
57+
58+
new webpack.experiments.schemes.VirtualUrlPlugin(
59+
{
60+
myModule: `export const msg = "from virtual module"`,
61+
},
62+
'v'
63+
);
64+
```
65+
66+
**src/app.js**
67+
68+
```javascript
69+
import { msg } from 'v:myModule';
70+
71+
console.log(msg);
72+
```
73+
74+
## Advanced Example
75+
76+
Create multiple virtual modules of different types
77+
78+
```javascript
79+
const webpack = require('webpack');
80+
81+
new webpack.experiments.schemes.VirtualUrlPlugin({
82+
myCssModule: {
83+
type: '.css',
84+
source: 'body{background-color: powderblue;}',
85+
},
86+
myJsonModule: {
87+
type: '.json',
88+
source: `{"name": "virtual-url-plugin"}`,
89+
},
90+
});
91+
```
92+
93+
**src/app.js**
94+
95+
```javascript
96+
import json from 'virtual:myJsonModule';
97+
import 'virtual:myCssModule';
98+
```
99+
100+
Virtualize the routing file
101+
102+
```javascript
103+
const webpack = require('webpack');
104+
const path = require('path');
105+
const watchDir = path.join(__dirname, './src/routes');
106+
107+
new webpack.experiments.schemes.VirtualUrlPlugin({
108+
routes: {
109+
source(loaderContext) {
110+
// Use addContextDependency to monitor the addition or removal of subdirectories in watchDir to trigger the rebuilding of virtual modules.
111+
loaderContext.addContextDependency(watchDir);
112+
113+
const files = fs.readdirSync(watchDir);
114+
return `
115+
export const routes = {
116+
${files.map((key) => `${key.split('.')[0]}: () => import('./src/routes/${key}')`).join(',\n')}
117+
}
118+
`;
119+
},
120+
},
121+
});
122+
```
123+
124+
**src/app.js**
125+
126+
```javascript
127+
import { routes } from 'virtual:routes';
128+
```
129+
130+
## Options
131+
132+
- `module.type` (`string`): Content type of the virtual module.
133+
134+
T> Make sure that these types have a loader set via [module.rules](https://webpack.js.org/configuration/module/#modulerules).
135+
136+
- `module.source` (`string | ((loaderContext: import('webpack').LoaderContext<T>) => Promise<string> | string))`: Factory function for generating the content of virtual module.
137+
138+
- `module.version`(`boolean | string | () => string`): When a invalidate is triggered, the source function is called again if the value of the version is different from the previous one. If set to true it will always trigger.
139+
140+
- `schema` (`string`): Customizable virtual module schema, default is `virtual`.

0 commit comments

Comments
 (0)