11import { join } from 'https://deno.land/std@0.90.0/path/mod.ts'
2- import type { PostCSSPlugin } from '../compiler/css.ts'
32import type { ImportMap } from '../compiler/mod.ts'
43import { defaultReactVersion } from '../shared/constants.ts'
54import { existsFileSync , existsDirSync } from '../shared/fs.ts'
65import log from '../shared/log.ts'
76import util from '../shared/util.ts'
8- import type { Config } from '../types.ts'
7+ import type { Config , PostCSSPlugin } from '../types.ts'
98import { getAlephPkgUri , reLocaleID } from './helper.ts'
109
1110export const defaultConfig : Readonly < Required < Config > > = {
@@ -19,6 +18,7 @@ export const defaultConfig: Readonly<Required<Config>> = {
1918 rewrites : { } ,
2019 ssr : { } ,
2120 plugins : [ ] ,
21+ postcss : { plugins : [ 'autoprefixer' ] } ,
2222 headers : { } ,
2323 env : { } ,
2424}
@@ -60,6 +60,7 @@ export async function loadConfig(workingDir: string): Promise<Config> {
6060 ssr,
6161 rewrites,
6262 plugins,
63+ postcss,
6364 headers,
6465 env,
6566 } = data
@@ -110,32 +111,16 @@ export async function loadConfig(workingDir: string): Promise<Config> {
110111 if ( util . isNEArray ( plugins ) ) {
111112 config . plugins = plugins
112113 }
114+ if ( isPostcssConfig ( postcss ) ) {
115+ config . postcss = postcss
116+ } else {
117+ config . postcss = await loadPostCSSConfig ( workingDir )
118+ }
113119
114120 return config
115121}
116122
117- export async function loadPostCSSConfig ( workingDir : string ) : Promise < { plugins : PostCSSPlugin [ ] } > {
118- for ( const name of Array . from ( [ 'ts' , 'js' , 'json' ] ) . map ( ext => `postcss.config.${ ext } ` ) ) {
119- const p = join ( workingDir , name )
120- if ( existsFileSync ( p ) ) {
121- let config : any = null
122- if ( name . endsWith ( '.json' ) ) {
123- config = JSON . parse ( await Deno . readTextFile ( p ) )
124- } else {
125- const mod = await import ( 'file://' + p )
126- config = mod . default
127- if ( util . isFunction ( config ) ) {
128- config = await config ( )
129- }
130- }
131- if ( isPostcssConfig ( config ) ) {
132- return config
133- }
134- }
135- }
136123
137- return { plugins : [ 'autoprefixer' ] }
138- }
139124
140125/** load import maps from `import_map.json` */
141126export async function loadImportMap ( workingDir : string ) : Promise < ImportMap > {
@@ -172,6 +157,29 @@ export async function loadImportMap(workingDir: string): Promise<ImportMap> {
172157 return importMap
173158}
174159
160+ async function loadPostCSSConfig ( workingDir : string ) : Promise < { plugins : PostCSSPlugin [ ] } > {
161+ for ( const name of Array . from ( [ 'ts' , 'js' , 'json' ] ) . map ( ext => `postcss.config.${ ext } ` ) ) {
162+ const p = join ( workingDir , name )
163+ if ( existsFileSync ( p ) ) {
164+ let config : any = null
165+ if ( name . endsWith ( '.json' ) ) {
166+ config = JSON . parse ( await Deno . readTextFile ( p ) )
167+ } else {
168+ const mod = await import ( 'file://' + p )
169+ config = mod . default
170+ if ( util . isFunction ( config ) ) {
171+ config = await config ( )
172+ }
173+ }
174+ if ( isPostcssConfig ( config ) ) {
175+ return config
176+ }
177+ }
178+ }
179+
180+ return { plugins : [ 'autoprefixer' ] }
181+ }
182+
175183function isFramework ( v : any ) : v is 'react' {
176184 switch ( v ) {
177185 case 'react' :
0 commit comments