11import { resolve , dirname , basename , extname , isAbsolute , join , relative } from 'path' ;
2-
3- import mkdirp from 'mkdirp' ;
4- // *
52import { writeFileSync , appendFileSync } from 'fs' ;
6- /* /
7- const writeFileSync = (file, content) => {
8- console.log(`Will save ${file}\n${content.replace(/^/gm, ' ')}`);
9- };
10- // */
3+ import mkdirp from 'mkdirp' ;
4+
5+ // options resolvers
6+ import * as requireHooksOptions from './options_resolvers' ;
117
128const writeCssFile = ( filename , content ) => {
139 mkdirp . sync ( dirname ( filename ) ) ;
@@ -18,18 +14,6 @@ const appendCssFile = (filename, content) => {
1814 appendFileSync ( filename , content ) ;
1915} ;
2016
21- const simpleRequires = [
22- 'createImportedName' ,
23- 'generateScopedName' ,
24- 'processCss' ,
25- 'preprocessCss'
26- ] ;
27-
28- const complexRequires = [
29- 'append' ,
30- 'prepend'
31- ] ;
32-
3317const defaultOptions = {
3418 generateScopedName : '[name]__[local]___[hash:base64:5]'
3519} ;
@@ -56,16 +40,23 @@ export default function transformCssModules({ types: t }) {
5640 const from = resolveModulePath ( filepath ) ;
5741 filePathOrModuleName = resolve ( from , filePathOrModuleName ) ;
5842 }
59- return require ( filePathOrModuleName ) ;
43+
44+ // css-modules-require-hooks throws if file is ignored
45+ try {
46+ return require ( filePathOrModuleName ) ;
47+ } catch ( e ) {
48+ return { } ; // return empty object, this simulates result of ignored stylesheet file
49+ }
6050 }
6151
6252 // is css modules require hook initialized?
6353 let initialized = false ;
6454
6555 let matchExtensions = / \. c s s $ / i;
56+
6657 function matcher ( extensions = [ '.css' ] ) {
67- const extensionsPatern = extensions . join ( '|' ) . replace ( / \. / g, '\\\.' ) ;
68- return new RegExp ( `(${ extensionsPatern } )$` , 'i' ) ;
58+ const extensionsPattern = extensions . join ( '|' ) . replace ( / \. / g, '\\\.' ) ;
59+ return new RegExp ( `(${ extensionsPattern } )$` , 'i' ) ;
6960 }
7061
7162 return {
@@ -129,9 +120,11 @@ export default function transformCssModules({ types: t }) {
129120
130121 const pushStylesCreator = ( toWrap ) => ( css , filepath ) => {
131122 let processed ;
123+
132124 if ( typeof toWrap === 'function' ) {
133125 processed = toWrap ( css , filepath ) ;
134126 }
127+
135128 if ( typeof processed !== 'string' ) processed = css ;
136129
137130 if ( ! state . $$css . styles . has ( filepath ) ) {
@@ -142,83 +135,19 @@ export default function transformCssModules({ types: t }) {
142135 return processed ;
143136 } ;
144137
145- // check if there are simple requires and if they are functions
146- simpleRequires . forEach ( key => {
147- if ( typeof currentConfig [ key ] !== 'string' ) {
148- return ;
149- }
150-
151- const modulePath = resolve ( process . cwd ( ) , currentConfig [ key ] ) ;
152-
153- // this one can be require or string
154- if ( key === 'generateScopedName' ) {
155- try {
156- // if it is existing file, require it, otherwise use value
157- currentConfig [ key ] = require ( modulePath ) ;
158- } catch ( e ) {
159- try {
160- currentConfig [ key ] = require ( currentConfig [ key ] ) ;
161- } catch ( _e ) {
162- // do nothing, because it is not a valid path
163- }
164- }
165-
166- if ( typeof currentConfig [ key ] !== 'function' && typeof currentConfig [ key ] !== 'string' ) {
167- throw new Error ( `Configuration '${ key } ' is not a string or function.` ) ;
168- }
169-
138+ // resolve options
139+ Object . keys ( requireHooksOptions ) . forEach ( key => {
140+ // skip undefined options
141+ if ( currentConfig [ key ] === undefined ) {
170142 return ;
171143 }
172144
173- if ( currentConfig . hasOwnProperty ( key ) ) {
174- try {
175- currentConfig [ key ] = require ( modulePath ) ;
176- } catch ( e ) {
177- try {
178- currentConfig [ key ] = require ( currentConfig [ key ] ) ;
179- } catch ( _e ) {
180- // do nothing because it is not a valid path
181- }
182- }
183-
184- if ( typeof currentConfig [ key ] !== 'function' ) {
185- throw new Error ( `Module '${ modulePath } ' does not exist or is not a function.` ) ;
186- }
187- }
145+ currentConfig [ key ] = requireHooksOptions [ key ] ( currentConfig [ key ] , currentConfig ) ;
188146 } ) ;
189147
190148 // wrap or define processCss function that collect generated css
191149 currentConfig . processCss = pushStylesCreator ( currentConfig . processCss ) ;
192150
193- complexRequires . forEach ( key => {
194- if ( ! currentConfig . hasOwnProperty ( key ) ) {
195- return ;
196- }
197-
198- if ( ! Array . isArray ( currentConfig [ key ] ) ) {
199- throw new Error ( `Configuration '${ key } ' has to be an array.` ) ;
200- }
201-
202- currentConfig [ key ] . forEach ( ( plugin , index ) => {
203- // first try to load it using npm
204- try {
205- currentConfig [ key ] [ index ] = require ( plugin ) ;
206- } catch ( e ) {
207- try {
208- currentConfig [ key ] [ index ] = require ( resolve ( process . cwd ( ) , plugin ) ) ;
209- } catch ( _e ) {
210- // do nothing
211- }
212- }
213-
214- if ( typeof currentConfig [ key ] [ index ] !== 'function' ) {
215- throw new Error ( `Configuration '${ key } ' has to be valid path to a module at index ${ index } or it does not export a function.` ) ;
216- }
217-
218- currentConfig [ key ] [ index ] = currentConfig [ key ] [ index ] ( ) ;
219- } ) ;
220- } ) ;
221-
222151 require ( 'css-modules-require-hook' ) ( currentConfig ) ;
223152
224153 initialized = true ;
0 commit comments