From 0187d3f525fd76fa9855284b23836f4c3b68952a Mon Sep 17 00:00:00 2001 From: Evan You Date: Sun, 3 May 2020 15:57:52 -0400 Subject: [PATCH] feat: support postcss config in js css imports as well --- src/node/config.ts | 16 ++++++++++++ src/node/serverPluginCss.ts | 51 ++++++++++++++++++++++++++----------- src/node/serverPluginVue.ts | 20 +++++++-------- 3 files changed, 62 insertions(+), 25 deletions(-) create mode 100644 src/node/config.ts diff --git a/src/node/config.ts b/src/node/config.ts new file mode 100644 index 00000000000000..3eebc49a8e9aee --- /dev/null +++ b/src/node/config.ts @@ -0,0 +1,16 @@ +import postcssrc from 'postcss-load-config' + +// postcss-load-config doesn't expose Result type +type Result = ReturnType extends Promise ? T : never + +let cachedPostcssConfig: Result | null | undefined + +export async function loadPostcssConfig(root: string): Promise { + try { + return ( + cachedPostcssConfig || (cachedPostcssConfig = await postcssrc({}, root)) + ) + } catch (e) { + return (cachedPostcssConfig = null) + } +} diff --git a/src/node/serverPluginCss.ts b/src/node/serverPluginCss.ts index 9b5f186fb2a0f7..45bfe4cd99d470 100644 --- a/src/node/serverPluginCss.ts +++ b/src/node/serverPluginCss.ts @@ -1,29 +1,50 @@ import { Plugin } from './server' -import { isImportRequest } from './utils' +import { isImportRequest, readBody } from './utils' import { hmrClientId } from './serverPluginHmr' import hash_sum from 'hash-sum' +import { loadPostcssConfig } from './config' -export const cssPlugin: Plugin = ({ app, watcher, resolver }) => { +export const cssPlugin: Plugin = ({ root, app, watcher, resolver }) => { app.use(async (ctx, next) => { await next() // handle .css imports - // we rewrite it to JS that injects a