1- import CleanCSS from 'https://esm.sh/clean-css@5.1.1?no-check'
2- import PostCSS , { AcceptedPlugin } from 'https://esm.sh/postcss@8.2.7'
1+ import type { Plugin , PluginCreator } from 'https://esm.sh/postcss@8.2.8'
32import { join } from 'https://deno.land/std@0.90.0/path/mod.ts'
43import { existsFileSync } from '../shared/fs.ts'
54import util from '../shared/util.ts'
65import type { LoaderPlugin } from '../types.ts'
76
8- const cleanCSS = new CleanCSS ( { compatibility : '*' /* Internet Explorer 10+ */ } )
7+ const postcssVersion = '8.2.8'
98const productionOnlyPostcssPlugins = [ 'autoprefixer' ]
9+ const decoder = new TextDecoder ( )
1010
11- type Options = {
11+ export type AcceptedPlugin = string | [ string , any ] | Plugin | PluginCreator < any >
12+
13+ export type Options = {
1214 postcss ?: {
13- plugins : ( string | [ string , any ] | AcceptedPlugin ) [ ]
15+ plugins : AcceptedPlugin [ ]
1416 }
1517}
1618
1719export default ( options ?: Options ) : LoaderPlugin => {
18- const decoder = new TextDecoder ( )
1920 let pcssProcessor : any = null
21+ let cleanCSS : any = null
22+ let isProd : any = null
2023
2124 return {
2225 name : 'css-loader' ,
2326 type : 'loader' ,
2427 test : / \. p ? c s s $ / i,
2528 acceptHMR : true ,
2629 async transform ( { url, content } ) {
30+ if ( isProd === null ) {
31+ isProd = Deno . env . get ( 'BUILD_MODE' ) === 'production'
32+ }
33+ if ( isProd ) {
34+ const { default : CleanCSS } = await import ( 'https://esm.sh/clean-css@5.1.2?no-check' )
35+ cleanCSS = new CleanCSS ( { compatibility : '*' /* Internet Explorer 10+ */ } )
36+ }
2737 if ( pcssProcessor == null ) {
2838 pcssProcessor = await initPostCSSProcessor ( options )
2939 }
3040 const { content : pcss } = await pcssProcessor . process ( decoder . decode ( content ) ) . async ( )
31- const css = Deno . env . get ( 'BUILD_MODE' ) === 'production' ? cleanCSS . minify ( pcss ) . styles : pcss
41+ const css = isProd ? cleanCSS . minify ( pcss ) . styles : pcss
3242 if ( url . startsWith ( '#inline-style-' ) ) {
3343 return {
3444 code : css ,
@@ -48,6 +58,8 @@ export default (options?: Options): LoaderPlugin => {
4858}
4959
5060async function initPostCSSProcessor ( options ?: Options ) {
61+ const { default : PostCSS } = await import ( `https://esm.sh/postcss@${ postcssVersion } ` )
62+
5163 if ( options ?. postcss ) {
5264 return PostCSS ( await loadPostcssPlugins ( options . postcss . plugins ) )
5365 }
@@ -74,7 +86,7 @@ async function initPostCSSProcessor(options?: Options) {
7486 return PostCSS ( await loadPostcssPlugins ( [ 'autoprefixer' ] ) )
7587}
7688
77- async function loadPostcssPlugins ( plugins : ( string | [ string , any ] | AcceptedPlugin ) [ ] ) {
89+ async function loadPostcssPlugins ( plugins : AcceptedPlugin [ ] ) {
7890 const isDev = Deno . env . get ( 'BUILD_MODE' ) === 'development'
7991 return await Promise . all ( plugins . filter ( p => {
8092 if ( isDev ) {
@@ -98,10 +110,10 @@ async function loadPostcssPlugins(plugins: (string | [string, any] | AcceptedPlu
98110}
99111
100112async function importPostcssPluginByName ( name : string ) {
101- const { default : Plugin } = await import ( `https://esm.sh/${ name } ?external=postcss@8.2.4 &no-check` )
113+ const { default : Plugin } = await import ( `https://esm.sh/${ name } ?external=postcss@${ postcssVersion } &no-check` )
102114 return Plugin
103115}
104116
105- function isPostcssConfig ( v : any ) : v is { plugins : ( string | [ string , any ] | AcceptedPlugin ) [ ] } {
117+ function isPostcssConfig ( v : any ) : v is { plugins : AcceptedPlugin [ ] } {
106118 return util . isPlainObject ( v ) && util . isArray ( v . plugins )
107119}
0 commit comments