-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
webpack.ts
37 lines (31 loc) · 1.02 KB
/
webpack.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import path from 'node:path'
import { promoteRelativePath } from '@tarojs/helper'
import { sources } from 'webpack'
import type { Chunk, Compilation } from 'webpack'
const { ConcatSource } = sources
/**
* 在文本头部加入一些 require 语句
*/
export function addRequireToSource (id: string, modules: sources.Source, commonChunks: (Chunk | { name: string })[]) {
const source = new ConcatSource()
commonChunks.forEach(chunkItem => {
source.add(`require(${JSON.stringify(promoteRelativePath(path.relative(id, chunkItem.name!)))});\n`)
})
source.add('\n')
source.add(modules)
source.add(';')
return source
}
export function getChunkEntryModule (compilation: Compilation, chunk: Chunk) {
const chunkGraph = compilation.chunkGraph
const entryModules = Array.from(chunkGraph.getChunkEntryModulesIterable(chunk))
if (entryModules.length) {
return entryModules[0]
}
}
export function getChunkIdOrName (chunk: Chunk) {
if (typeof chunk.id === 'string') {
return chunk.id
}
return chunk.name!
}