Last active
November 9, 2021 18:55
-
-
Save zthxxx/28e172140d46081c5abe7f0e8b0c55ff to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { existsSync } from 'fs' | |
import { inspect } from 'util' | |
import { Service } from '@umijs/core' | |
const isDev = process.env.NODE_ENV === 'development' | |
const umiService = new Service({ | |
cwd: process.cwd(), | |
presets: ['@umijs/preset-built-in'], | |
}) | |
let isUmiInit = false | |
if (!existsSync(`src/${isDev ? '.umi' : '.umi-production'}/umi.ts`)) { | |
/** | |
* 当没有 .umi 文件时,调用 umi generateFiles 生成所有 umi 需要的临时文件 | |
* https://github.com/umijs/umi/blob/v3.5.10/packages/preset-built-in/src/plugins/commands/generate/generate.ts | |
*/ | |
await umiService.run({ | |
name: 'generate', | |
args: { | |
_: ['tmp'], | |
}, | |
}) | |
isUmiInit = true | |
} | |
/** | |
* 获取 umi 读取各种配置后生成的 webpack config | |
* https://github.com/umijs/umi/blob/v3.5.10/packages/preset-built-in/src/plugins/commands/webpack/webpack.ts | |
*/ | |
const umiWebpackConfig = await umiService[isUmiInit ? 'runCommand' : 'run']({ | |
name: 'webpack', | |
args: { | |
print: false, | |
}, | |
}) | |
console.info( | |
'umiWebpackConfig.module.rules ==>', | |
inspect( | |
umiWebpackConfig.module.rules.map(({ test, use, include, exclude, loader }) => { | |
const simple = { test } | |
if (include) simple.include = include | |
if (exclude) simple.exclude = exclude | |
if (loader) simple.loader = loader | |
if (use?.length) { | |
simple.use = use.map(use => { | |
if (typeof use.loader === 'string') return use.loader | |
else return use | |
}) | |
} | |
return simple | |
}), | |
{ colors: true, compact: false, depth: null }, | |
), | |
) | |
process.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment