Skip to content

Commit 374e659

Browse files
committed
feat: 模块解析
1 parent 59843c6 commit 374e659

8 files changed

+747
-65
lines changed

index.html

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
<title>Vite App</title>
88
</head>
99
<body>
10+
<div>
11+
hello vite
12+
</div>
1013
<div id="app"></div>
1114
<script type="module" src="/src/main.js"></script>
1215
</body>

lib/index.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const Koa = require('koa')
2-
const serverStaticPlugin = require('./serverPluginService');
2+
const serverStaticPlugin = require('./serverPluginServeStatic');
33
const moduleReWritePlugin = require('./moduleRewritePlugin');
4+
const { moduleResolvePlugin } = require('./serverPluginModuleResolve')
5+
46

57
function createServer(){
68
let app = new Koa();
@@ -12,7 +14,8 @@ function createServer(){
1214
}
1315

1416
const resolvePlugin = [
15-
moduleReWritePlugin,
17+
moduleReWritePlugin, // 重写请求路径
18+
moduleResolvePlugin,
1619
serverStaticPlugin,
1720
]
1821

lib/moduleRewritePlugin.js

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,33 @@
1-
const { readBody} = require('./util')
1+
const { readBody } = require('./util')
2+
const { parse } = require('es-module-lexer')
3+
const MagicString = require('magic-string')
4+
5+
function rewriteImports(source){
6+
const imports = parse(source)[0]
7+
const ms = new MagicString(source)
8+
if(imports.length){
9+
for(let i = 0; i < imports.length; i++){
10+
const { s, e } = imports[i]
11+
let id = source.slice(s, e) // 引用的模块名称
12+
if(/^[^\/\.]/.test(id)){
13+
id = `/@modules/${id}`
14+
ms.overwrite(s, e, id)
15+
}
16+
}
17+
}
18+
return ms.toString()
19+
}
20+
221
function moduleRewritePlugin({ app, root }){
322
app.use(async (ctx, next) => {
423
await next()
524
// 默认会先执行静态服务中间件,会将结果放到ctx.body
625

726
// 需要将流转换成字符串,只需要处理js中的引用问题
827
if(ctx.body && ctx.response.is('js')){
9-
console.log(ctx.body)
28+
const r = await readBody(ctx.body)
29+
const result = rewriteImports(r)
30+
ctx.body = result
1031
}
1132
})
1233
}

lib/serverPluginModuleResolve.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const path = require('path')
2+
const fs = require('fs').promises
3+
4+
const reg = /^\/@modules\//
5+
6+
function moduleResolvePlugin({ app, root }){
7+
app.use(async (ctx, next) => {
8+
9+
// 如果没有匹配到/@modules 就往下执行即可
10+
if(!reg.test(ctx.path)){
11+
return await next()
12+
}
13+
14+
const id = ctx.path.replace(reg, '')
15+
16+
const mapping = {
17+
vue: path.resolve(root, 'node_modules', '@vue/runtime-dom/dist/runtime-dom.esm-browser.js')
18+
}
19+
20+
21+
const result = await fs.readFile(mapping[id], 'utf8')
22+
23+
ctx.type = 'js'
24+
25+
ctx.body = result
26+
})
27+
}
28+
29+
30+
31+
exports.moduleResolvePlugin = moduleResolvePlugin

lib/serverPluginService.js lib/serverPluginServeStatic.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const path = require('path')
33

44
function serverStaticPlugin({ app, root }) {
55
app.use(static(root))
6-
app.use(static(path.resolve(root, 'demo')))
6+
app.use(static(path.resolve(root, 'public')))
77
}
88

99
module.exports = serverStaticPlugin;

lib/util.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
1-
const readBody = async (steam) => {
1+
const { Readable, Stream } = require('stream')
22

3+
const readBody = async (stream) => {
4+
if(stream instanceof Readable){
5+
return new Promise((resolve, reject) => {
6+
let res = ''
7+
stream.on('data', function(chunk){
8+
res += chunk
9+
})
10+
stream.on('end', function(){
11+
resolve(res)
12+
})
13+
})
14+
} else {
15+
return stream
16+
}
317
}
418

519
exports.readBody = readBody

0 commit comments

Comments
 (0)