Skip to content

Commit 48b7230

Browse files
authored
Merge pull request #640 from MattStypa/build_refactor
CLI build process improvements
2 parents 3bea3a1 + 84a5712 commit 48b7230

File tree

4 files changed

+77
-43
lines changed

4 files changed

+77
-43
lines changed

__tests__/cli.compile.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import path from 'path'
2+
3+
import autoprefixer from 'autoprefixer'
4+
5+
import tailwind from '../src'
6+
import compile from '../src/cli/compile'
7+
8+
describe('cli compile', () => {
9+
const inputFile = path.resolve(__dirname, 'fixtures/tailwind-input.css')
10+
const outputFile = 'output.css'
11+
const plugins = [tailwind(), autoprefixer]
12+
13+
it('compiles CSS file', () => {
14+
return compile({ inputFile, outputFile, plugins }).then(result => {
15+
expect(result.css).toContain('.example')
16+
expect(result.css).toContain('-ms-input-placeholder')
17+
})
18+
})
19+
})

__tests__/cli.utils.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import path from 'path'
2+
23
import * as utils from '../src/cli/utils'
34

45
describe('cli utils', () => {

src/cli/commands/build.js

Lines changed: 18 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import autoprefixer from 'autoprefixer'
22
import bytes from 'bytes'
33
import chalk from 'chalk'
4-
import postcss from 'postcss'
54
import prettyHrtime from 'pretty-hrtime'
65

76
import tailwind from '../..'
87

98
import commands from '.'
9+
import compile from '../compile'
1010
import * as emoji from '../emoji'
1111
import * 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
})

src/cli/compile.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import postcss from 'postcss'
2+
3+
import * as utils from './utils'
4+
5+
/**
6+
* Compiler options
7+
*
8+
* @typedef {Object} CompileOptions
9+
* @property {string} inputFile
10+
* @property {string} outputFile
11+
* @property {array} plugins
12+
*/
13+
14+
const defaultOptions = {
15+
inputFile: null,
16+
outputFile: null,
17+
plugins: [],
18+
}
19+
20+
/**
21+
* Compiles CSS file.
22+
*
23+
* @param {CompileOptions} options
24+
* @return {Promise}
25+
*/
26+
export default function compile(options = {}) {
27+
const config = { ...defaultOptions, ...options }
28+
const css = utils.readFile(config.inputFile)
29+
30+
return new Promise((resolve, reject) => {
31+
postcss(config.plugins)
32+
.process(css, {
33+
from: config.inputFile,
34+
to: config.outputFile,
35+
})
36+
.then(resolve)
37+
.catch(reject)
38+
})
39+
}

0 commit comments

Comments
 (0)