11import autoprefixer from 'autoprefixer'
22import bytes from 'bytes'
33import chalk from 'chalk'
4- import postcss from 'postcss'
54import prettyHrtime from 'pretty-hrtime'
65
76import tailwind from '../..'
87
98import commands from '.'
9+ import compile from '../compile'
1010import * as emoji from '../emoji'
1111import * as utils from '../utils'
1212
@@ -57,68 +57,37 @@ function stopWithHelp(...msgs) {
5757 utils . die ( )
5858}
5959
60- /**
61- * Compiles CSS file.
62- *
63- * @param {string } inputFile
64- * @param {string } configFile
65- * @param {string } outputFile
66- * @param {boolean } autoprefix
67- * @return {Promise }
68- */
69- function build ( inputFile , configFile , outputFile , autoprefix ) {
70- const css = utils . readFile ( inputFile )
71-
72- return new Promise ( ( resolve , reject ) => {
73- postcss ( [ tailwind ( configFile ) ] . concat ( autoprefix ? [ autoprefixer ] : [ ] ) )
74- . process ( css , {
75- from : inputFile ,
76- to : outputFile ,
77- } )
78- . then ( resolve )
79- . catch ( reject )
80- } )
81- }
82-
8360/**
8461 * Compiles CSS file and writes it to stdout.
8562 *
86- * @param {string } inputFile
87- * @param {string } configFile
88- * @param {string } outputFile
89- * @param {boolean } autoprefix
63+ * @param {CompileOptions } compileOptions
9064 * @return {Promise }
9165 */
92- function buildToStdout ( inputFile , configFile , outputFile , autoprefix ) {
93- return build ( inputFile , configFile , outputFile , autoprefix ) . then ( result =>
94- process . stdout . write ( result . css )
95- )
66+ function buildToStdout ( compileOptions ) {
67+ return compile ( compileOptions ) . then ( result => process . stdout . write ( result . css ) )
9668}
9769
9870/**
9971 * Compiles CSS file and writes it to a file.
10072 *
101- * @param {string } inputFile
102- * @param {string } configFile
103- * @param {string } outputFile
104- * @param {boolean } autoprefix
73+ * @param {CompileOptions } compileOptions
10574 * @param {int[] } startTime
10675 * @return {Promise }
10776 */
108- function buildToFile ( inputFile , configFile , outputFile , autoprefix , startTime ) {
77+ function buildToFile ( compileOptions , startTime ) {
10978 utils . header ( )
11079 utils . log ( )
111- utils . log ( emoji . go , 'Building...' , chalk . bold . cyan ( inputFile ) )
80+ utils . log ( emoji . go , 'Building...' , chalk . bold . cyan ( compileOptions . inputFile ) )
11281
113- return build ( inputFile , configFile , outputFile , autoprefix ) . then ( result => {
114- utils . writeFile ( outputFile , result . css )
82+ return compile ( compileOptions ) . then ( result => {
83+ utils . writeFile ( compileOptions . outputFile , result . css )
11584
11685 const prettyTime = prettyHrtime ( process . hrtime ( startTime ) )
11786
11887 utils . log ( )
11988 utils . log ( emoji . yes , 'Finished in' , chalk . bold . magenta ( prettyTime ) )
12089 utils . log ( emoji . pack , 'Size:' , chalk . bold . magenta ( bytes ( result . css . length ) ) )
121- utils . log ( emoji . disk , 'Saved to' , chalk . bold . cyan ( outputFile ) )
90+ utils . log ( emoji . disk , 'Saved to' , chalk . bold . cyan ( compileOptions . outputFile ) )
12291 utils . footer ( )
12392 } )
12493}
@@ -145,9 +114,15 @@ export function run(cliParams, cliOptions) {
145114 ! utils . exists ( configFile ) &&
146115 stop ( chalk . bold . magenta ( configFile ) , 'does not exist.' )
147116
117+ const compileOptions = {
118+ inputFile,
119+ outputFile,
120+ plugins : [ tailwind ( configFile ) ] . concat ( autoprefix ? [ autoprefixer ] : [ ] ) ,
121+ }
122+
148123 const buildPromise = outputFile
149- ? buildToFile ( inputFile , configFile , outputFile , autoprefix , startTime )
150- : buildToStdout ( inputFile , configFile , outputFile , autoprefix )
124+ ? buildToFile ( compileOptions , startTime )
125+ : buildToStdout ( compileOptions )
151126
152127 buildPromise . then ( resolve ) . catch ( reject )
153128 } )
0 commit comments