Skip to content

Commit 9d56132

Browse files
committed
feat: Support VirtualUrlPlugin
1 parent cecc48f commit 9d56132

File tree

1 file changed

+137
-0
lines changed

1 file changed

+137
-0
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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+
myModule: `export const msg = "from virtual module"`
60+
}, "v");
61+
```
62+
63+
**src/app.js**
64+
65+
```javascript
66+
import { msg } from 'v:myModule';
67+
68+
console.log(msg);
69+
```
70+
71+
## Advanced Example
72+
73+
Create multiple virtual modules of different types
74+
75+
```javascript
76+
const webpack = require("webpack)
77+
78+
new webpack.experiments.schemes.VirtualUrlPlugin({
79+
myCssModule: {
80+
type: ".css",
81+
source: "body{background-color: powderblue;}",
82+
},
83+
myJsonModule: {
84+
type: ".json",
85+
source: `{"name": "virtual-url-plugin"}`,
86+
},
87+
});
88+
```
89+
90+
**src/app.js**
91+
92+
```javascript
93+
import json from 'virtual:myJsonModule';
94+
import 'virtual:myCssModule';
95+
```
96+
97+
Virtualize the routing file
98+
99+
```javascript
100+
const webpack = require("webpack)
101+
const path = require("path)
102+
const watchDir = path.join(__dirname, "./src/routes")
103+
104+
new webpack.experiments.schemes.VirtualUrlPlugin({
105+
routes: {
106+
source(loaderContext) {
107+
// Use addContextDependency to monitor the addition or removal of subdirectories in watchDir to trigger the rebuilding of virtual modules.
108+
loaderContext.addContextDependency(watchDir)
109+
110+
const files = fs.readdirSync(watchDir)
111+
return `
112+
export const routes = {
113+
${files.map(key => `${key.split(".")[0]}: () => import('./src/routes/${key}')`).join(",\n")}
114+
}
115+
`
116+
},
117+
}
118+
});
119+
```
120+
121+
**src/app.js**
122+
123+
```javascript
124+
import { routes } from 'virtual:routes';
125+
```
126+
127+
## Options
128+
129+
- `module.type` (`string`): Content type of the virtual module.
130+
131+
T> Make sure that these types have a loader set via [module.rules](https://webpack.js.org/configuration/module/#modulerules).
132+
133+
- `module.source` (`string | ((loaderContext: import('webpack').LoaderContext<T>) => Promise<string> | string))`: Factory function for generating the content of virtual module.
134+
135+
- `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.
136+
137+
- `schema` (`string`): Customizable virtual module schema, default is `virtual`.

0 commit comments

Comments
 (0)