1
1
import debug from 'debug' ;
2
+ import genericNames from 'generic-names' ;
2
3
import hook from './hook' ;
3
4
import { readFileSync } from 'fs' ;
4
5
import { dirname , sep , relative , resolve } from 'path' ;
@@ -25,8 +26,10 @@ let preProcess = identity;
25
26
let postProcess ;
26
27
// defaults
27
28
let lazyResultOpts = { } ;
28
- let plugins = [ LocalByDefault , ExtractImports , Scope ] ;
29
+ let plugins = [ Values , LocalByDefault , ExtractImports ] ;
30
+ let terminalPlugins = [ ] ;
29
31
let rootDir = process . cwd ( ) ;
32
+ let generateScopedName = genericNames ( '[name]__[local]___[hash:base64:5]' , { context : rootDir } ) ;
30
33
31
34
/**
32
35
* @param {object } opts
@@ -64,11 +67,12 @@ export default function setup(opts = {}) {
64
67
return void ( plugins = customPlugins ) ;
65
68
}
66
69
70
+ terminalPlugins = get ( 'append' , null , 'array' , opts ) || [ ] ;
71
+ generateScopedName = get ( 'generateScopedName' , null , 'function' , opts )
72
+ || genericNames ( '[name]__[local]___[hash:base64:5]' , { context : rootDir } ) ;
67
73
const prepend = get ( 'prepend' , null , 'array' , opts ) || [ ] ;
68
- const append = get ( 'append' , null , 'array' , opts ) || [ ] ;
69
74
const mode = get ( 'mode' , null , 'string' , opts ) ;
70
75
const createImportedName = get ( 'createImportedName' , null , 'function' , opts ) ;
71
- const generateScopedName = get ( 'generateScopedName' , null , 'function' , opts ) ;
72
76
73
77
plugins = [
74
78
...prepend ,
@@ -79,10 +83,6 @@ export default function setup(opts = {}) {
79
83
createImportedName
80
84
? new ExtractImports ( { createImportedName : opts . createImportedName } )
81
85
: ExtractImports ,
82
- generateScopedName
83
- ? new Scope ( { generateScopedName : opts . generateScopedName } )
84
- : Scope ,
85
- ...append ,
86
86
] ;
87
87
}
88
88
@@ -111,8 +111,11 @@ function fetch(_to, _from, _trace) {
111
111
const rootRelativePath = sep + relative ( rootDir , filename ) ;
112
112
const CSSSource = preProcess ( readFileSync ( filename , 'utf8' ) , filename ) ;
113
113
114
- const lazyResult = postcss ( plugins . concat ( new Parser ( { fetch, filename, trace } ) ) )
115
- . process ( CSSSource , assign ( lazyResultOpts , { from : rootRelativePath } ) ) ;
114
+ const lazyResult = postcss ( plugins . concat (
115
+ new Scope ( { generateScopedName : ( name , _ , css ) => generateScopedName ( name , filename , css ) } ) ,
116
+ terminalPlugins ,
117
+ new Parser ( { fetch, filename, trace } ) )
118
+ ) . process ( CSSSource , assign ( lazyResultOpts , { from : rootRelativePath } ) ) ;
116
119
117
120
lazyResult . warnings ( ) . forEach ( message => console . warn ( message . text ) ) ;
118
121
0 commit comments