1- const _ = require ( ' lodash' ) ;
2- const fs = require ( 'fs' ) ;
3- const path = require ( ' path' ) ;
4- const { green } = require ( ' kleur' ) ;
5- const postcss = require ( ' postcss' ) ;
6- const SubsequentPlugins = require ( ' ./subsequent-plugins' ) ;
1+ const _ = require ( " lodash" ) ;
2+ const fs = require ( "fs" ) ;
3+ const path = require ( " path" ) ;
4+ const { green } = require ( " kleur" ) ;
5+ const postcss = require ( " postcss" ) ;
6+ const SubsequentPlugins = require ( " ./subsequent-plugins" ) ;
77
88const plugins = new SubsequentPlugins ( ) ;
99
1010module . exports = ( opts ) => {
1111 opts = _ . merge (
1212 {
1313 output : {
14- path : path . join ( __dirname , '..' ) ,
15- name : ' [name]-[query].[ext]' ,
14+ path : path . join ( __dirname , ".." ) ,
15+ name : " [name]-[query].[ext]" ,
1616 } ,
1717 queries : { } ,
1818 extractAll : true ,
1919 stats : true ,
2020 entry : null ,
2121 } ,
22- opts
22+ opts ,
2323 ) ;
2424
2525 if ( opts . config ) {
@@ -28,24 +28,31 @@ module.exports = (opts) => {
2828
2929 const media = { } ;
3030
31- function addMedia ( key , css , query ) {
32- if ( ! Array . isArray ( media [ key ] ) ) {
33- media [ key ] = [ ] ;
31+ function addMedia ( name , key , css , query ) {
32+ if ( ! Array . isArray ( media ?. [ name ] ?. [ key ] ) ) {
33+ media [ name ] = {
34+ ...media [ name ] ,
35+ [ key ] : new Array ( ) ,
36+ } ;
3437 }
35- media [ key ] . push ( { css, query } ) ;
38+ media [ name ] [ key ] . push ( { css, query } ) ;
3639 }
3740
38- function getMedia ( key ) {
39- const css = media [ key ] . map ( ( data ) => data . css ) . join ( '\n' ) ;
40- const query = media [ key ] [ 0 ] . query ;
41+ function getMedia ( name , key ) {
42+ if ( media ?. [ name ] ?. [ key ] ?. length ) {
43+ const css = media [ name ] [ key ] . map ( ( data ) => data . css ) . join ( "\n" ) ;
44+ const query = media [ name ] [ key ] [ 0 ] . query ;
4145
42- return { css, query } ;
46+ return { css, query } ;
47+ } else {
48+ return { } ;
49+ }
4350 }
4451
4552 return {
46- postcssPlugin : ' postcss-extract-media-query' ,
53+ postcssPlugin : " postcss-extract-media-query" ,
4754 Once ( root , { result } ) {
48- let from = ' undefined.css' ;
55+ let from = " undefined.css" ;
4956
5057 if ( opts . entry ) {
5158 from = opts . entry ;
@@ -58,15 +65,14 @@ module.exports = (opts) => {
5865 const ext = file [ 2 ] ;
5966
6067 if ( opts . output . path ) {
61- root . walkAtRules ( ' media' , ( atRule ) => {
68+ root . walkAtRules ( " media" , ( atRule ) => {
6269 const query = atRule . params ;
6370 const queryname =
6471 opts . queries [ query ] || ( opts . extractAll && _ . kebabCase ( query ) ) ;
6572
6673 if ( queryname ) {
6774 const css = postcss . root ( ) . append ( atRule ) . toString ( ) ;
68-
69- addMedia ( queryname , css , query ) ;
75+ addMedia ( name , queryname , css , query ) ;
7076 atRule . remove ( ) ;
7177 }
7278 } ) ;
@@ -77,31 +83,32 @@ module.exports = (opts) => {
7783 // gather promises only if output.path specified because otherwise
7884 // nothing has been extracted
7985 if ( opts . output . path ) {
80- Object . keys ( media ) . forEach ( ( queryname ) => {
81- promises . push (
82- new Promise ( ( resolve ) => {
83- let { css } = getMedia ( queryname ) ;
84- const newFile = opts . output . name
85- . replace ( / \[ n a m e \] / g, name )
86- . replace ( / \[ q u e r y \] / g, queryname )
87- . replace ( / \[ e x t \] / g, ext ) ;
88- const newFilePath = path . join ( opts . output . path , newFile ) ;
89- const newFileDir = path . dirname ( newFilePath ) ;
90-
91- plugins . applyPlugins ( css , newFilePath ) . then ( ( css ) => {
92- if ( ! fs . existsSync ( path . dirname ( newFilePath ) ) ) {
93- // make sure we can write
94- fs . mkdirSync ( newFileDir , { recursive : true } ) ;
95- }
96- fs . writeFileSync ( newFilePath , css ) ;
97-
98- if ( opts . stats === true ) {
99- console . log ( green ( '[extracted media query]' ) , newFile ) ;
100- }
101- resolve ( ) ;
102- } ) ;
103- } )
104- ) ;
86+ Object . entries ( media ) . forEach ( ( [ name , value ] ) => {
87+ Object . keys ( value ) . forEach ( ( queryname ) => {
88+ promises . push (
89+ new Promise ( ( resolve ) => {
90+ let { css } = getMedia ( name , queryname ) ;
91+ const newFile = opts . output . name
92+ . replace ( / \[ n a m e \] / g, name )
93+ . replace ( / \[ q u e r y \] / g, queryname )
94+ . replace ( / \[ e x t \] / g, ext ) ;
95+ const newFilePath = path . join ( opts . output . path , newFile ) ;
96+ const newFileDir = path . dirname ( newFilePath ) ;
97+ plugins . applyPlugins ( css , newFilePath ) . then ( ( css ) => {
98+ if ( ! fs . existsSync ( path . dirname ( newFilePath ) ) ) {
99+ // make sure we can write
100+ fs . mkdirSync ( newFileDir , { recursive : true } ) ;
101+ }
102+ fs . writeFileSync ( newFilePath , css ) ;
103+
104+ if ( opts . stats === true ) {
105+ console . log ( green ( "[extracted media query]" ) , newFile ) ;
106+ }
107+ resolve ( ) ;
108+ } ) ;
109+ } ) ,
110+ ) ;
111+ } ) ;
105112 } ) ;
106113 }
107114
0 commit comments