Skip to content

Commit d271e59

Browse files
authored
feat: load custom postcss config (#41)
1 parent b5421e7 commit d271e59

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
"magic-string": "^0.25.7",
7373
"minimist": "^1.2.5",
7474
"postcss": "^7.0.27",
75+
"postcss-load-config": "^2.1.0",
7576
"resolve-from": "^5.0.0",
7677
"rollup": "^2.7.2",
7778
"rollup-plugin-terser": "^5.3.0",
@@ -85,6 +86,7 @@
8586
"@types/hash-sum": "^1.0.0",
8687
"@types/lru-cache": "^5.1.0",
8788
"@types/node": "^13.13.1",
89+
"@types/postcss-load-config": "^2.0.1",
8890
"@types/serve-handler": "^6.1.0",
8991
"@types/ws": "^7.2.4",
9092
"conventional-changelog-cli": "^2.0.31",

src/node/serverPluginVue.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { hmrClientId } from './serverPluginHmr'
1313
import resolve from 'resolve-from'
1414
import { cachedRead } from './utils'
1515
import { Context } from 'koa'
16-
16+
import postcssrc from 'postcss-load-config'
1717
const debug = require('debug')('vite:sfc')
1818
const getEtag = require('etag')
1919

@@ -252,8 +252,8 @@ async function compileSFCStyle(
252252
scoped: style.scoped != null,
253253
modules: style.module != null,
254254
preprocessLang: style.lang as any,
255-
preprocessCustomRequire: (id: string) => require(resolve(root, id))
256-
// TODO load postcss config if present
255+
preprocessCustomRequire: (id: string) => require(resolve(root, id)),
256+
...loadPostCssConfig(root)
257257
})
258258

259259
if (result.errors.length) {
@@ -268,3 +268,11 @@ async function compileSFCStyle(
268268
vueCache.set(filePath, cached)
269269
return result
270270
}
271+
272+
function loadPostCssConfig(root: string) {
273+
const config = postcssrc.sync({}, root)
274+
return {
275+
postcssOptions: config.options,
276+
postcssPlugins: config.plugins
277+
}
278+
}

yarn.lock

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,14 @@
688688
resolved "https://registry.yarnpkg.com/@types/parse-json/-/parse-json-4.0.0.tgz#2f8bb441434d163b35fb8ffdccd7138927ffb8c0"
689689
integrity sha512-//oorEZjL6sbPcKUaCdIGlIUeH26mgzimjBB77G6XRgnDl/L5wOnpyBGRe/Mmf5CVW3PwEBE1NjiMZ/ssFh4wA==
690690

691+
"@types/postcss-load-config@^2.0.1":
692+
version "2.0.1"
693+
resolved "https://registry.npm.taobao.org/@types/postcss-load-config/download/@types/postcss-load-config-2.0.1.tgz#76c789032dfc6823eab1900ad7cc95ebadf24d49"
694+
integrity sha1-dseJAy38aCPqsZAK18yV663yTUk=
695+
dependencies:
696+
cosmiconfig "^6.0.0"
697+
postcss "^7.0.0"
698+
691699
"@types/prettier@^1.19.0":
692700
version "1.19.1"
693701
resolved "https://registry.yarnpkg.com/@types/prettier/-/prettier-1.19.1.tgz#33509849f8e679e4add158959fdb086440e9553f"
@@ -2995,6 +3003,13 @@ ieee754@^1.1.4:
29953003
resolved "https://registry.yarnpkg.com/ieee754/-/ieee754-1.1.13.tgz#ec168558e95aa181fd87d37f55c32bbcb6708b84"
29963004
integrity sha512-4vf7I2LYV/HaWerSo3XmlMkp5eZ83i+/CDluXi/IGTs/O1sejBNhTtnxzmRZfvOUqj7lZjqHkeTvpgSFDlWZTg==
29973005

3006+
import-cwd@^2.0.0:
3007+
version "2.1.0"
3008+
resolved "https://registry.npm.taobao.org/import-cwd/download/import-cwd-2.1.0.tgz#aa6cf36e722761285cb371ec6519f53e2435b0a9"
3009+
integrity sha1-qmzzbnInYShcs3HsZRn1PiQ1sKk=
3010+
dependencies:
3011+
import-from "^2.1.0"
3012+
29983013
import-fresh@^2.0.0:
29993014
version "2.0.0"
30003015
resolved "https://registry.yarnpkg.com/import-fresh/-/import-fresh-2.0.0.tgz#d81355c15612d386c61f9ddd3922d4304822a546"
@@ -3011,6 +3026,13 @@ import-fresh@^3.1.0:
30113026
parent-module "^1.0.0"
30123027
resolve-from "^4.0.0"
30133028

3029+
import-from@^2.1.0:
3030+
version "2.1.0"
3031+
resolved "https://registry.npm.taobao.org/import-from/download/import-from-2.1.0.tgz#335db7f2a7affd53aaa471d4b8021dee36b7f3b1"
3032+
integrity sha1-M1238qev/VOqpHHUuAId7ja387E=
3033+
dependencies:
3034+
resolve-from "^3.0.0"
3035+
30143036
import-local@^3.0.2:
30153037
version "3.0.2"
30163038
resolved "https://registry.yarnpkg.com/import-local/-/import-local-3.0.2.tgz#a8cfd0431d1de4a2199703d003e3e62364fa6db6"
@@ -5046,6 +5068,14 @@ postcss-discard-overridden@^4.0.1:
50465068
dependencies:
50475069
postcss "^7.0.0"
50485070

5071+
postcss-load-config@^2.1.0:
5072+
version "2.1.0"
5073+
resolved "https://registry.npm.taobao.org/postcss-load-config/download/postcss-load-config-2.1.0.tgz#c84d692b7bb7b41ddced94ee62e8ab31b417b003"
5074+
integrity sha1-yE1pK3u3tB3c7ZTuYuirMbQXsAM=
5075+
dependencies:
5076+
cosmiconfig "^5.0.0"
5077+
import-cwd "^2.0.0"
5078+
50495079
postcss-merge-longhand@^4.0.11:
50505080
version "4.0.11"
50515081
resolved "https://registry.yarnpkg.com/postcss-merge-longhand/-/postcss-merge-longhand-4.0.11.tgz#62f49a13e4a0ee04e7b98f42bb16062ca2549e24"

0 commit comments

Comments
 (0)