@@ -8,63 +8,23 @@ function fixture(...args) {
88 return path . join ( __dirname , 'fixtures' , ...args ) ;
99}
1010
11- async function write ( { inputs, outDir } ) {
12- const jsCodePath = path . join ( outDir , 'bundle.js' ) ;
13-
14- const bundleFromPlugins = await rollup ( {
15- input : fixture ( inputs [ 0 ] ) ,
16- plugins : [
17- inlinePostCSS ( {
18- plugins : [ require ( 'postcss-csso' ) , require ( 'postcss-rgb-plz' ) ] ,
19- } ) ,
20- ] ,
21- } ) ;
22-
23- const bundleFromPostCSSConfig = await rollup ( {
24- input : fixture ( inputs [ 2 ] ) ,
25- plugins : [
26- inlinePostCSS ( {
27- styleRegex : / (?: f o o ` ) ( ( .| \n ) + ?) (? = ( ` ( \n | ; | , ) ) ) / gi,
28- } ) ,
29- ] ,
30- } ) ;
11+ async function write ( { input, output, plugin, outDir, options } ) {
3112
32- const bundleFromExternalPostCSSConfig = await rollup ( {
33- input : fixture ( inputs [ 0 ] ) ,
34- plugins : [
35- inlinePostCSS ( {
36- configPath : path . join ( __dirname , 'config' ) ,
37- } ) ,
38- ] ,
39- } ) ;
13+ const jsCodePath = path . join ( outDir , 'bundle.js' ) ;
4014
41- const bundleWithMultipleCSS = await rollup ( {
42- input : fixture ( inputs [ 1 ] ) ,
15+ const bundle = await rollup ( {
16+ input : fixture ( input ) ,
4317 plugins : [
44- inlinePostCSS ( { } ) ,
18+ plugin ,
4519 ] ,
4620 } ) ;
4721
48- await bundleFromPlugins . write ( {
22+ await bundle . write ( {
4923 format : 'esm' ,
50- file : path . join ( outDir , 'bundle.js' ) ,
24+ file : path . join ( outDir , output ) ,
5125 sourcemap : true ,
52- sourcemapFile : path . join ( outDir , 'bundle.js.map' )
53- } ) ;
54-
55- await bundleFromPostCSSConfig . write ( {
56- format : 'esm' ,
57- file : path . join ( outDir , 'bundle.custom.js' ) ,
58- } ) ;
59-
60- await bundleFromExternalPostCSSConfig . write ( {
61- format : 'esm' ,
62- file : path . join ( outDir , 'bundle.external.js' ) ,
63- } ) ;
64-
65- await bundleWithMultipleCSS . write ( {
66- format : 'esm' ,
67- file : path . join ( outDir , 'bundle.multiple.js' ) ,
26+ sourcemapFile : path . join ( outDir , `${ output } .map` ) ,
27+ ...options
6828 } ) ;
6929
7030 return {
@@ -81,20 +41,70 @@ async function write({ inputs, outDir }) {
8141 const style = file . match ( / \` ( ( .| \n ) * ) \` / gm) [ 0 ] ;
8242 return / \r | \n / . exec ( style ) == null ? true : false ;
8343 } ,
44+ hasSourceMap ( bundle ) {
45+ const file = fs . readFileSync ( path . join ( outDir , bundle ) , 'utf8' ) ;
46+ return file . includes ( 'sourceMappingURL' ) ;
47+ }
8448 } ;
49+
8550}
8651
87- test ( 'inline css is processed ' , async ( ) => {
52+ test ( 'should process with plugins declared in rollup.config.js ' , async ( ) => {
8853 const res = await write ( {
89- inputs : [ 'component.js' , 'multiple.js' , 'custom.js' ] ,
54+ input : 'component.js' ,
55+ output : 'bundle.js' ,
9056 outDir : 'test/onExtract' ,
91- options : { } ,
57+ plugin : inlinePostCSS ( {
58+ plugins : [ require ( 'postcss-csso' ) , require ( 'postcss-rgb-plz' ) ] ,
59+ } ) ,
60+ options : {
61+ sourcemap : false
62+ } ,
9263 } ) ;
9364 expect ( await res . hasRGBColorValues ( 'bundle.js' ) ) . toBe ( true ) ;
9465 expect ( await res . isMinified ( 'bundle.js' ) ) . toBe ( true ) ;
95- expect ( await res . hasRGBColorValues ( 'bundle.custom.js' ) ) . toBe ( true ) ;
96- expect ( await res . isMinified ( 'bundle.custom.js' ) ) . toBe ( true ) ;
97- expect ( await res . hasRGBColorValues ( 'bundle.external.js' ) ) . toBe ( true ) ;
98- expect ( await res . isMinified ( 'bundle.external.js' ) ) . toBe ( true ) ;
99- expect ( await res . hasRGBColorValues ( 'bundle.multiple.js' ) ) . toBe ( true ) ;
66+ expect ( await res . hasSourceMap ( 'bundle.js' ) ) . toBe ( false ) ;
67+ } ) ;
68+
69+ test ( 'should process file with custom regex' , async ( ) => {
70+ const res = await write ( {
71+ input : 'custom.js' ,
72+ output : 'custom.js' ,
73+ outDir : 'test/onExtract' ,
74+ plugin : inlinePostCSS ( {
75+ styleRegex : / (?: f o o ` ) ( ( .| \n ) + ?) (? = ( ` ( \n | ; | , ) ) ) / gi,
76+ } ) ,
77+ options : { } ,
78+ } ) ;
79+ expect ( await res . hasRGBColorValues ( 'custom.js' ) ) . toBe ( true ) ;
80+ expect ( await res . isMinified ( 'custom.js' ) ) . toBe ( true ) ;
10081} ) ;
82+
83+ test ( 'should reference postcss.config.js' , async ( ) => {
84+ const res = await write ( {
85+ input : 'component.js' ,
86+ output : 'config.js' ,
87+ outDir : 'test/onExtract' ,
88+ plugin : inlinePostCSS ( {
89+ configPath : path . join ( __dirname , 'config' ) ,
90+ } ) ,
91+ options : { } ,
92+ } ) ;
93+ expect ( await res . hasRGBColorValues ( 'config.js' ) ) . toBe ( true ) ;
94+ expect ( await res . isMinified ( 'config.js' ) ) . toBe ( true ) ;
95+ } ) ;
96+
97+ test ( 'should process multiple css declarations' , async ( ) => {
98+ const res = await write ( {
99+ input : 'multiple.js' ,
100+ output : 'multiple.js' ,
101+ outDir : 'test/onExtract' ,
102+ plugin : inlinePostCSS ( ) ,
103+ options : { } ,
104+ } ) ;
105+ expect ( await res . hasRGBColorValues ( 'multiple.js' ) ) . toBe ( true ) ;
106+ expect ( await res . hasSourceMap ( 'multiple.js' ) ) . toBe ( true ) ;
107+ // expect(await res.isMinified('multiple.js')).toBe(true);
108+ } ) ;
109+
110+
0 commit comments