Skip to content

Commit

Permalink
fix(build): ensure a non-zero exit code on failure (vitejs#1038)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
aleclarson authored Dec 4, 2020
1 parent fe937c4 commit 650ca16
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/node/build/buildPluginCss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export const createBuildCssPlugin = ({
if (result.errors.length) {
console.error(`[vite] error applying css transforms: `)
result.errors.forEach(console.error)
process.exit(1)
}
css = result.code
modules = result.modules
Expand Down Expand Up @@ -228,6 +229,7 @@ function minifyCSS(css: string) {
if (res.errors && res.errors.length) {
console.error(chalk.red(`[vite] error when minifying css:`))
console.error(res.errors)
process.exit(1)
}

if (res.warnings && res.warnings.length) {
Expand Down
3 changes: 2 additions & 1 deletion src/node/build/buildPluginEsbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ export const createEsbuildPlugin = async (
...jsxConfig,
...(isVueTs ? { loader: 'ts' } : null)
},
jsx
jsx,
true // exitOnFailure
)
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/node/esbuildService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ export const transform = async (
src: string,
request: string,
options: TransformOptions = {},
jsxOption?: SharedConfig['jsx']
jsxOption?: SharedConfig['jsx'],
exitOnFailure?: boolean
) => {
const service = await ensureService()
const file = cleanUrl(request)
Expand Down Expand Up @@ -114,6 +115,9 @@ export const transform = async (
console.error(e)
}
debug(`options used: `, options)
if (exitOnFailure) {
process.exit(1)
}
return {
code: '',
map: undefined
Expand Down
10 changes: 7 additions & 3 deletions src/node/utils/cssUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export async function compileCss(
isBuild: boolean = false
): Promise<SFCStyleCompileResults | string> {
const id = hash_sum(publicPath)
const postcssConfig = await loadPostcssConfig(root)
const postcssConfig = await loadPostcssConfig(root, isBuild)
const { compileStyleAsync } = resolveCompiler(root)

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

async function loadPostcssConfig(
root: string
root: string,
exitOnFailure?: boolean
): Promise<PostCSSConfigResult | null> {
if (cachedPostcssConfig !== undefined) {
return cachedPostcssConfig
Expand All @@ -150,13 +151,16 @@ async function loadPostcssConfig(
if (!/No PostCSS Config found/.test(e.message)) {
console.error(chalk.red(`[vite] Error loading postcss config:`))
console.error(e)
if (exitOnFailure) {
process.exit(1)
}
}
return (cachedPostcssConfig = null)
}
}

export async function resolvePostcssOptions(root: string, isBuild: boolean) {
const config = await loadPostcssConfig(root)
const config = await loadPostcssConfig(root, isBuild)
const options = config && config.options
const plugins = config && config.plugins ? config.plugins.slice() : []
plugins.unshift(require('postcss-import')())
Expand Down

0 comments on commit 650ca16

Please sign in to comment.