Skip to content

Commit 650ca16

Browse files
authored
fix(build): ensure a non-zero exit code on failure (#1038)
Previously, when the following actions failed, the process would exit with an exit code of zero, which would break CI. - transform with Esbuild - transform CSS - minify CSS - load PostCSS config
1 parent fe937c4 commit 650ca16

File tree

4 files changed

+16
-5
lines changed

4 files changed

+16
-5
lines changed

src/node/build/buildPluginCss.ts

+2
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export const createBuildCssPlugin = ({
8585
if (result.errors.length) {
8686
console.error(`[vite] error applying css transforms: `)
8787
result.errors.forEach(console.error)
88+
process.exit(1)
8889
}
8990
css = result.code
9091
modules = result.modules
@@ -228,6 +229,7 @@ function minifyCSS(css: string) {
228229
if (res.errors && res.errors.length) {
229230
console.error(chalk.red(`[vite] error when minifying css:`))
230231
console.error(res.errors)
232+
process.exit(1)
231233
}
232234

233235
if (res.warnings && res.warnings.length) {

src/node/build/buildPluginEsbuild.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ export const createEsbuildPlugin = async (
3939
...jsxConfig,
4040
...(isVueTs ? { loader: 'ts' } : null)
4141
},
42-
jsx
42+
jsx,
43+
true // exitOnFailure
4344
)
4445
}
4546
}

src/node/esbuildService.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ export const transform = async (
6767
src: string,
6868
request: string,
6969
options: TransformOptions = {},
70-
jsxOption?: SharedConfig['jsx']
70+
jsxOption?: SharedConfig['jsx'],
71+
exitOnFailure?: boolean
7172
) => {
7273
const service = await ensureService()
7374
const file = cleanUrl(request)
@@ -114,6 +115,9 @@ export const transform = async (
114115
console.error(e)
115116
}
116117
debug(`options used: `, options)
118+
if (exitOnFailure) {
119+
process.exit(1)
120+
}
117121
return {
118122
code: '',
119123
map: undefined

src/node/utils/cssUtils.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export async function compileCss(
6868
isBuild: boolean = false
6969
): Promise<SFCStyleCompileResults | string> {
7070
const id = hash_sum(publicPath)
71-
const postcssConfig = await loadPostcssConfig(root)
71+
const postcssConfig = await loadPostcssConfig(root, isBuild)
7272
const { compileStyleAsync } = resolveCompiler(root)
7373

7474
if (
@@ -138,7 +138,8 @@ type PostCSSConfigResult = ReturnType<typeof postcssrc> extends Promise<infer T>
138138
let cachedPostcssConfig: PostCSSConfigResult | null | undefined
139139

140140
async function loadPostcssConfig(
141-
root: string
141+
root: string,
142+
exitOnFailure?: boolean
142143
): Promise<PostCSSConfigResult | null> {
143144
if (cachedPostcssConfig !== undefined) {
144145
return cachedPostcssConfig
@@ -150,13 +151,16 @@ async function loadPostcssConfig(
150151
if (!/No PostCSS Config found/.test(e.message)) {
151152
console.error(chalk.red(`[vite] Error loading postcss config:`))
152153
console.error(e)
154+
if (exitOnFailure) {
155+
process.exit(1)
156+
}
153157
}
154158
return (cachedPostcssConfig = null)
155159
}
156160
}
157161

158162
export async function resolvePostcssOptions(root: string, isBuild: boolean) {
159-
const config = await loadPostcssConfig(root)
163+
const config = await loadPostcssConfig(root, isBuild)
160164
const options = config && config.options
161165
const plugins = config && config.plugins ? config.plugins.slice() : []
162166
plugins.unshift(require('postcss-import')())

0 commit comments

Comments
 (0)