1
- import { resolve , dirname , basename , extname , isAbsolute , join , relative } from 'path' ;
2
- import { writeFileSync , appendFileSync } from 'fs' ;
3
- import mkdirp from 'mkdirp' ;
1
+ import { resolve , dirname , isAbsolute } from 'path' ;
4
2
5
3
// options resolvers
6
4
import * as requireHooksOptions from './options_resolvers' ;
7
5
8
- const writeCssFile = ( filename , content ) => {
9
- mkdirp . sync ( dirname ( filename ) ) ;
10
- writeFileSync ( filename , content ) ;
11
- } ;
12
- const appendCssFile = ( filename , content ) => {
13
- mkdirp . sync ( dirname ( filename ) ) ;
14
- appendFileSync ( filename , content ) ;
15
- } ;
6
+ // utils.
7
+ import { extractCssFile } from './utils' ;
16
8
17
9
const defaultOptions = {
18
10
generateScopedName : '[name]__[local]___[hash:base64:5]'
@@ -79,45 +71,6 @@ export default function transformCssModules({ types: t }) {
79
71
styles : new Map ( )
80
72
} ;
81
73
82
- const extractCssFile = ( filepath , css ) => {
83
- const { extractCss = null } = state . opts ;
84
- if ( ! extractCss ) return null ;
85
-
86
- // this is the case where a single extractCss is requested
87
- if ( typeof ( extractCss ) === 'string' ) {
88
- // If this is the first file, then we should replace
89
- // old content
90
- if ( state . $$css . styles . size === 1 ) {
91
- return writeCssFile ( extractCss , css ) ;
92
- }
93
- // this should output in a single file.
94
- // Let's append the new file content.
95
- return appendCssFile ( extractCss , css ) ;
96
- }
97
-
98
- // This is the case where each css file is written in
99
- // its own file.
100
- const {
101
- dir = 'dist' ,
102
- filename = '[name].css' ,
103
- relativeRoot = ''
104
- } = extractCss ;
105
-
106
- // Make css file narmpe relative to relativeRoot
107
- const relativePath = relative (
108
- resolve ( process . cwd ( ) , relativeRoot ) ,
109
- filepath
110
- ) ;
111
- const destination = join (
112
- resolve ( process . cwd ( ) , dir ) ,
113
- filename
114
- )
115
- . replace ( / \[ n a m e ] / , basename ( filepath , extname ( filepath ) ) )
116
- . replace ( / \[ p a t h ] / , relativePath ) ;
117
-
118
- writeCssFile ( destination , css ) ;
119
- } ;
120
-
121
74
const pushStylesCreator = ( toWrap ) => ( css , filepath ) => {
122
75
let processed ;
123
76
@@ -129,7 +82,7 @@ export default function transformCssModules({ types: t }) {
129
82
130
83
if ( ! state . $$css . styles . has ( filepath ) ) {
131
84
state . $$css . styles . set ( filepath , processed ) ;
132
- extractCssFile ( filepath , processed ) ;
85
+ extractCssFile ( process . cwd ( ) , filepath , processed , state ) ;
133
86
}
134
87
135
88
return processed ;
0 commit comments