Skip to content

Commit

Permalink
style(mini-runner/mini-split-chunks): format file
Browse files Browse the repository at this point in the history
  • Loading branch information
huangcj99 committed Nov 26, 2020
1 parent cbdbd60 commit d773493
Showing 1 changed file with 28 additions and 36 deletions.
64 changes: 28 additions & 36 deletions packages/taro-mini-runner/src/plugins/MiniSplitChunksPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default class MiniSplitChunksPlugin extends SplitChunksPlugin {
subRoots: string[]
subRootRegExps: RegExp[]

constructor() {
constructor () {
super()
this.options = null
this.subCommonDeps = new Map()
Expand All @@ -39,7 +39,7 @@ export default class MiniSplitChunksPlugin extends SplitChunksPlugin {
this.distPath = ''
}

apply(compiler: any) {
apply (compiler: any) {
this.context = compiler.context
this.subPackages = this.getSubpackageConfig(compiler).map((subPackage: SubPackage) => ({
...subPackage,
Expand Down Expand Up @@ -86,7 +86,7 @@ export default class MiniSplitChunksPlugin extends SplitChunksPlugin {
if (!module.resource) {
return
}

if (module.type === CSS_MINI_EXTRACT) {
return
}
Expand All @@ -95,18 +95,16 @@ export default class MiniSplitChunksPlugin extends SplitChunksPlugin {
let depName = ''

if (this.isDevMode) {
/**
* 避免开发模式下,清除sub-common源目录后,触发重新编译时,sub-common目录缺失无变化的chunk导致文件copy失败的问题
/**
* 避免开发模式下,清除sub-common源目录后,触发重新编译时,sub-common目录缺失无变化的chunk导致文件copy失败的问题
*/
depName = md5(depPath + new Date().getTime())
} else {
depName = md5(depPath)
}

if (!this.subCommonDeps.has(depName)) {
const subCommonDepChunks = new Set(
chunks.map((chunk) => chunk.name)
)
const subCommonDepChunks = new Set(chunks.map(chunk => chunk.name))

this.subCommonDeps.set(depName, {
resource: module.resource,
Expand Down Expand Up @@ -152,7 +150,7 @@ export default class MiniSplitChunksPlugin extends SplitChunksPlugin {
const depName = chunkName.replace(new RegExp(`^${SUB_COMMON_DIR}/(.*)`), '$1')

if (this.subCommonDeps.has(depName)) {
existSubCommonDeps.set(depName, (this.subCommonDeps.get(depName) as DepInfo))
existSubCommonDeps.set(depName, this.subCommonDeps.get(depName) as DepInfo)
}
}
})
Expand All @@ -178,7 +176,7 @@ export default class MiniSplitChunksPlugin extends SplitChunksPlugin {
/**
* require该分包下的vendors
*/
if (!!subVendors) {
if (subVendors) {
const subVendorsAbsolutePath = path.resolve(this.distPath, subVendors.name)
const relativePath = this.getRealRelativePath(chunkAbsulutePath, subVendorsAbsolutePath)

Expand All @@ -188,7 +186,7 @@ export default class MiniSplitChunksPlugin extends SplitChunksPlugin {
// require sub-common下的模块
if (subCommon.length > 0) {
subCommon.forEach(moduleName => {
const moduleAbsulutePath = path.resolve(this.distPath, (chunkSubRoot as string), SUB_COMMON_DIR, moduleName)
const moduleAbsulutePath = path.resolve(this.distPath, chunkSubRoot as string, SUB_COMMON_DIR, moduleName)
const relativePath = this.getRealRelativePath(chunkAbsulutePath, moduleAbsulutePath)

source.add(`require(${JSON.stringify(relativePath)});\n`)
Expand Down Expand Up @@ -237,17 +235,11 @@ export default class MiniSplitChunksPlugin extends SplitChunksPlugin {
mkdirp.sync(targetDirPath)

if (fs.pathExistsSync(sourcePath)) {
fs.outputFileSync(
sourceTargetPath,
fs.readFileSync(sourcePath)
)
fs.outputFileSync(sourceTargetPath, fs.readFileSync(sourcePath))
}

if (fs.pathExistsSync(mapSourcePath)) {
fs.outputFileSync(
mapSourceTargetPath,
fs.readFileSync(mapSourcePath)
)
fs.outputFileSync(mapSourceTargetPath, fs.readFileSync(mapSourcePath))
}
})
})
Expand All @@ -263,7 +255,7 @@ export default class MiniSplitChunksPlugin extends SplitChunksPlugin {
/**
* 根据 webpack entry 配置获取入口文件路径
*/
getAppEntry(compiler: webpack.Compiler): string {
getAppEntry (compiler: webpack.Compiler): string {
const originalEntry = compiler.options.entry as webpack.Entry

return path.resolve(this.context, originalEntry.app[0])
Expand All @@ -272,7 +264,7 @@ export default class MiniSplitChunksPlugin extends SplitChunksPlugin {
/**
* 获取分包配置
*/
getSubpackageConfig(compiler: webpack.Compiler): SubPackage[] {
getSubpackageConfig (compiler: webpack.Compiler): SubPackage[] {
const appEntry = this.getAppEntry(compiler)
const appConfigPath = this.getConfigFilePath(appEntry)
const appConfig: AppConfig = readConfig(appConfigPath)
Expand All @@ -283,14 +275,14 @@ export default class MiniSplitChunksPlugin extends SplitChunksPlugin {
/**
* 根据 app、页面、组件的路径获取对应的 config 配置文件的路径
*/
getConfigFilePath(filePath: string): string {
getConfigFilePath (filePath: string): string {
return resolveMainFilePath(`${filePath.replace(path.extname(filePath), '')}.config`)
}

/**
* 去掉尾部的/
*/
formatSubRoot(subRoot: string): string {
formatSubRoot (subRoot: string): string {
const lastApl = subRoot[subRoot.length - 1]

if (lastApl === '/') {
Expand All @@ -299,7 +291,7 @@ export default class MiniSplitChunksPlugin extends SplitChunksPlugin {
return subRoot
}

isSubChunk(chunk: webpack.compilation.Chunk): boolean {
isSubChunk (chunk: webpack.compilation.Chunk): boolean {
const isSubChunk = this.subRootRegExps.find(subRootRegExp => subRootRegExp.test(chunk.name))

return !!isSubChunk
Expand All @@ -308,7 +300,7 @@ export default class MiniSplitChunksPlugin extends SplitChunksPlugin {
/**
* match *\/vendors
*/
matchSubVendors(chunk: webpack.compilation.Chunk): boolean {
matchSubVendors (chunk: webpack.compilation.Chunk): boolean {
const subVendorsRegExps = this.subRoots.map(subRoot => new RegExp(`^${path.join(subRoot, SUB_VENDORS_NAME)}$`))
const isSubVendors = subVendorsRegExps.find(subVendorsRegExp => subVendorsRegExp.test(chunk.name))

Expand All @@ -318,16 +310,16 @@ export default class MiniSplitChunksPlugin extends SplitChunksPlugin {
/**
* match sub-common\/*
*/
matchSubCommon(chunk: webpack.compilation.Chunk): boolean {
matchSubCommon (chunk: webpack.compilation.Chunk): boolean {
return new RegExp(`^${SUB_COMMON_DIR}/`).test(chunk.name)
}

/**
* 判断module有没被主包引用
*/
hasMainChunk(chunks: webpack.compilation.Chunk[]): boolean {
hasMainChunk (chunks: webpack.compilation.Chunk[]): boolean {
const chunkNames: string[] = chunks.map(chunk => chunk.name)
let hasMainChunk: boolean = false
let hasMainChunk = false

/**
* 遍历chunk,如果其中有一个chunk,无法匹配分包root,则视为非分包的chunk
Expand All @@ -345,7 +337,7 @@ export default class MiniSplitChunksPlugin extends SplitChunksPlugin {
/**
* 判断该module有没被多个分包引用
*/
isSubsDep(chunks: webpack.compilation.Chunk[]): boolean {
isSubsDep (chunks: webpack.compilation.Chunk[]): boolean {
const chunkNames: string[] = chunks.map(chunk => chunk.name)
const chunkSubRoots: Set<string> = new Set()

Expand All @@ -362,7 +354,7 @@ export default class MiniSplitChunksPlugin extends SplitChunksPlugin {
/**
* 仅分包有引用的module抽取到分包下的vendors
*/
getSubPackageVendorsCacheGroup() {
getSubPackageVendorsCacheGroup () {
const subPackageVendorsCacheGroup = {}

this.subRoots.forEach(subRoot => {
Expand All @@ -371,8 +363,8 @@ export default class MiniSplitChunksPlugin extends SplitChunksPlugin {
if (module.type === CSS_MINI_EXTRACT) {
return false
}
return chunks.every((chunk) => (new RegExp(`^${subRoot}/`)).test(chunk.name))

return chunks.every(chunk => new RegExp(`^${subRoot}/`).test(chunk.name))
},
name: `${subRoot}/${SUB_VENDORS_NAME}`,
minChunks: 2,
Expand All @@ -385,13 +377,13 @@ export default class MiniSplitChunksPlugin extends SplitChunksPlugin {
/**
* 没有被主包引用, 且被多个分包引用, 提取成单个模块,输出到sub-common下
*/
getSubCommonCacheGroup() {
getSubCommonCacheGroup () {
const subCommonCacheGroup = {}

this.subCommonDeps.forEach((depInfo: DepInfo, depName: string) => {
subCommonCacheGroup[`${SUB_COMMON_DIR}/${depName}`] = {
name: `${SUB_COMMON_DIR}/${depName}`,
test: (module) => {
test: module => {
return module.resource === depInfo.resource
},
priority: 1000
Expand All @@ -400,7 +392,7 @@ export default class MiniSplitChunksPlugin extends SplitChunksPlugin {
return subCommonCacheGroup
}

setChunkSubCommons(subCommonDeps: Map<string, DepInfo>) {
setChunkSubCommons (subCommonDeps: Map<string, DepInfo>) {
const chunkSubCommons: Map<string, Set<string>> = new Map()

subCommonDeps.forEach((depInfo: DepInfo, depName: string) => {
Expand All @@ -423,7 +415,7 @@ export default class MiniSplitChunksPlugin extends SplitChunksPlugin {
/**
* 获取page相对于公共模块的路径
*/
getRealRelativePath(from: string, to: string): string {
getRealRelativePath (from: string, to: string): string {
return promoteRelativePath(path.relative(from, to))
}
}

0 comments on commit d773493

Please sign in to comment.