Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit 2d0195c

Browse files
committed
feat: re-add the postcss option for Config type
1 parent a232830 commit 2d0195c

File tree

5 files changed

+45
-38
lines changed

5 files changed

+45
-38
lines changed

compiler/css.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import { Plugin, PluginCreator } from 'https://esm.sh/[email protected]'
21
import util from '../shared/util.ts'
2+
import { PostCSSPlugin } from '../types.ts'
33

44
const postcssVersion = '8.2.8'
55
const productionOnlyPostcssPlugins = ['autoprefixer']
66

7-
export type PostCSSPlugin = string | [string, any] | Plugin | PluginCreator<any>
8-
97
export class CSSProcessor {
108
#isProd: boolean
119
#postcssPlugins: PostCSSPlugin[]

server/app.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import type {
4040
} from '../types.ts'
4141
import { VERSION } from '../version.ts'
4242
import { Bundler, bundlerRuntimeCode } from './bundler.ts'
43-
import { defaultConfig, loadConfig, loadImportMap, loadPostCSSConfig } from './config.ts'
43+
import { defaultConfig, loadConfig, loadImportMap } from './config.ts'
4444
import {
4545
computeHash,
4646
formatBytesWithColor,
@@ -108,16 +108,17 @@ export class Application implements ServerApplication {
108108
/** initiate application */
109109
private async init(reload: boolean) {
110110
let t = performance.now()
111-
const [config, importMap, postcssConfig] = await Promise.all([
111+
const [config, importMap,] = await Promise.all([
112112
loadConfig(this.workingDir),
113113
loadImportMap(this.workingDir),
114-
loadPostCSSConfig(this.workingDir),
115114
])
116115

117116
Object.assign(this.config, config)
118117
Object.assign(this.importMap, importMap)
119118
this.#pageRouting.config(this.config)
120-
this.#cssProcesser.config(!this.isDev, postcssConfig.plugins)
119+
this.#cssProcesser.config(!this.isDev, this.config.postcss.plugins)
120+
121+
console.log(this.config.postcss.plugins)
121122

122123
// inject env variables
123124
Deno.env.set('ALEPH_VERSION', VERSION)
@@ -150,7 +151,7 @@ export class Application implements ServerApplication {
150151
const configChecksum = computeHash(JSON.stringify({
151152
...this.sharedCompileOptions,
152153
plugins: this.config.plugins.filter(isLoaderPlugin).map(({ name }) => name),
153-
postcssPlugins: postcssConfig.plugins.map(p => p.toString())
154+
postcssPlugins: this.config.postcss.plugins.map(p => p.toString())
154155
}, (key: string, value: any) => {
155156
if (key === 'inlineStylePreprocess') {
156157
return void 0

server/config.ts

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { join } from 'https://deno.land/[email protected]/path/mod.ts'
2-
import type { PostCSSPlugin } from '../compiler/css.ts'
32
import type { ImportMap } from '../compiler/mod.ts'
43
import { defaultReactVersion } from '../shared/constants.ts'
54
import { existsFileSync, existsDirSync } from '../shared/fs.ts'
65
import log from '../shared/log.ts'
76
import util from '../shared/util.ts'
8-
import type { Config } from '../types.ts'
7+
import type { Config, PostCSSPlugin } from '../types.ts'
98
import { getAlephPkgUri, reLocaleID } from './helper.ts'
109

1110
export 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` */
141126
export 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+
175183
function isFramework(v: any): v is 'react' {
176184
switch (v) {
177185
case 'react':

server/helper.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import { createHash } from 'https://deno.land/[email protected]/hash/mod.ts'
33
import { relative } from 'https://deno.land/[email protected]/path/mod.ts'
44
import { existsDirSync } from '../shared/fs.ts'
55
import util from '../shared/util.ts'
6-
import type { Plugin, LoaderPlugin } from '../types.ts'
6+
import type { ServerPlugin, LoaderPlugin } from '../types.ts'
77
import { VERSION } from '../version.ts'
88

99
export const reLocaleID = /^[a-z]{2}(-[a-zA-Z0-9]+)?$/
1010
export const reFullVersion = /@v?\d+\.\d+\.\d+/i
1111

1212
/** check the plugin whether is a loader plugin. */
13-
export function isLoaderPlugin(plugin: Plugin): plugin is LoaderPlugin {
13+
export function isLoaderPlugin(plugin: LoaderPlugin | ServerPlugin): plugin is LoaderPlugin {
1414
return plugin.type === 'loader'
1515
}
1616

types.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { BufReader, BufWriter } from 'https://deno.land/[email protected]/io/bufio.ts'
22
import type { MultipartFormData } from 'https://deno.land/[email protected]/mime/multipart.ts'
3+
import { Plugin, PluginCreator } from 'https://esm.sh/[email protected]'
34

45
/**
56
* A loader plugin to load source media.
@@ -47,10 +48,7 @@ export type ServerPlugin = {
4748
onInit(app: ServerApplication): Promise<void> | void
4849
}
4950

50-
/**
51-
* A plugin for the aleph server application.
52-
*/
53-
export type Plugin = LoaderPlugin | ServerPlugin
51+
export type PostCSSPlugin = string | [string, any] | Plugin | PluginCreator<any>
5452

5553
/**
5654
* The config for the aleph server application.
@@ -73,7 +71,9 @@ export type Config = {
7371
/** `ssr` specifies the options for **SSR**. */
7472
ssr?: boolean | SSROptions
7573
/** `plugins` specifies some plugins for the appliaction. */
76-
plugins?: Plugin[]
74+
plugins?: (LoaderPlugin | ServerPlugin)[]
75+
/** `postcss` specifies the postcss plugins. */
76+
postcss?: { plugins: PostCSSPlugin[] }
7777
/** `headers` appends custom headers for server requests. */
7878
headers?: Record<string, string>
7979
/** `rewrites` specifies the server rewrite map. */

0 commit comments

Comments
 (0)