Skip to content

Commit 52f6fb2

Browse files
authored
Merge pull request #77 from tailwindcss/watch-config
Register config file with Webpack for watching
2 parents a77d879 + df77207 commit 52f6fb2

14 files changed

+73
-37
lines changed

__tests__/applyAtRule.test.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import postcss from 'postcss'
22
import plugin from '../src/lib/substituteClassApplyAtRules'
33

4-
function run(input, opts) {
4+
function run(input, opts = () => {}) {
55
return postcss([plugin(opts)]).process(input)
66
}
77

88
test("it copies a class's declarations into itself", () => {
99
const output = '.a { color: red; } .b { color: red; }'
1010

11-
return run('.a { color: red; } .b { @apply .a; }', {}).then(result => {
11+
return run('.a { color: red; } .b { @apply .a; }').then(result => {
1212
expect(result.css).toEqual(output)
1313
expect(result.warnings().length).toBe(0)
1414
})
@@ -38,16 +38,15 @@ test("it doesn't copy a media query definition into itself", () => {
3838
3939
.b {
4040
@apply .a;
41-
}`,
42-
{}
43-
).then(result => {
41+
}`)
42+
.then(result => {
4443
expect(result.css).toEqual(output)
4544
expect(result.warnings().length).toBe(0)
4645
})
4746
})
4847

4948
test('it fails if the class does not exist', () => {
50-
run('.b { @apply .a; }', {}).catch(error => {
49+
run('.b { @apply .a; }').catch(error => {
5150
expect(error.reason).toEqual('No .a class found.')
5251
})
5352
})

__tests__/focusableAtRule.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import postcss from 'postcss'
22
import plugin from '../src/lib/substituteFocusableAtRules'
33

4-
function run(input, opts = {}) {
4+
function run(input, opts = () => {}) {
55
return postcss([plugin(opts)]).process(input)
66
}
77

@@ -18,7 +18,7 @@ test("it adds a focusable variant to each nested class definition", () => {
1818
.chocolate, .focus\\:chocolate:focus { color: brown; }
1919
`
2020

21-
return run(input, {}).then(result => {
21+
return run(input).then(result => {
2222
expect(result.css).toEqual(output)
2323
expect(result.warnings().length).toBe(0)
2424
})

__tests__/hoverableAtRule.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import postcss from 'postcss'
22
import plugin from '../src/lib/substituteHoverableAtRules'
33

4-
function run(input, opts = {}) {
4+
function run(input, opts = () => {}) {
55
return postcss([plugin(opts)]).process(input)
66
}
77

@@ -18,7 +18,7 @@ test("it adds a hoverable variant to each nested class definition", () => {
1818
.chocolate, .hover\\:chocolate:hover { color: brown; }
1919
`
2020

21-
return run(input, {}).then(result => {
21+
return run(input).then(result => {
2222
expect(result.css).toEqual(output)
2323
expect(result.warnings().length).toBe(0)
2424
})

docs/tailwind.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var config = require('../defaultConfig')
1+
var config = require('../lib/index').defaultConfig()
22

33
config.colors = {
44
...config.colors,

src/index.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import _ from 'lodash'
55
import postcss from 'postcss'
66
import stylefmt from 'stylefmt'
77

8+
import registerConfigAsDependency from './lib/registerConfigAsDependency'
89
import substitutePreflightAtRule from './lib/substitutePreflightAtRule'
910
import evaluateTailwindFunctions from './lib/evaluateTailwindFunctions'
1011
import generateUtilities from './lib/generateUtilities'
@@ -15,23 +16,30 @@ import substituteScreenAtRules from './lib/substituteScreenAtRules'
1516
import substituteClassApplyAtRules from './lib/substituteClassApplyAtRules'
1617

1718
const plugin = postcss.plugin('tailwind', (config) => {
18-
if (_.isUndefined(config)) {
19-
config = require('../defaultConfig')
19+
const plugins = []
20+
21+
if (! _.isUndefined(config)) {
22+
plugins.push(registerConfigAsDependency(path.resolve(config)))
2023
}
2124

22-
if (_.isString(config)) {
23-
config = require(path.resolve(config))
25+
const lazyConfig = () => {
26+
if (_.isUndefined(config)) {
27+
return require('../defaultConfig')
28+
}
29+
30+
delete require.cache[require.resolve(path.resolve(config))]
31+
return require(path.resolve(config))
2432
}
2533

26-
return postcss([
27-
substitutePreflightAtRule(config),
28-
evaluateTailwindFunctions(config),
29-
generateUtilities(config),
30-
substituteHoverableAtRules(config),
31-
substituteFocusableAtRules(config),
32-
substituteResponsiveAtRules(config),
33-
substituteScreenAtRules(config),
34-
substituteClassApplyAtRules(config),
34+
return postcss(...plugins, ...[
35+
substitutePreflightAtRule(lazyConfig),
36+
evaluateTailwindFunctions(lazyConfig),
37+
generateUtilities(lazyConfig),
38+
substituteHoverableAtRules(lazyConfig),
39+
substituteFocusableAtRules(lazyConfig),
40+
substituteResponsiveAtRules(lazyConfig),
41+
substituteScreenAtRules(lazyConfig),
42+
substituteClassApplyAtRules(lazyConfig),
3543
stylefmt,
3644
])
3745
})

src/lib/evaluateTailwindFunctions.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import _ from 'lodash'
22
import functions from 'postcss-functions'
33

4-
export default function(options) {
4+
export default function(config) {
5+
const options = config()
6+
57
return functions({
68
functions: {
79
config: function (path) {

src/lib/generateUtilities.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ import verticalAlign from '../generators/verticalAlign'
3737
import visibility from '../generators/visibility'
3838
import zIndex from '../generators/zIndex'
3939

40-
export default function(options) {
40+
export default function(config) {
4141
return function(css) {
42+
const options = config()
43+
4244
css.walkAtRules('tailwind', atRule => {
4345
if (atRule.params === 'utilities') {
4446
const utilities = _.flatten([
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import fs from 'fs'
2+
3+
export default function(configFile) {
4+
if (! fs.existsSync(configFile)) {
5+
throw new Error(`Specified Tailwind config file "${configFile}" doesn't exist.`)
6+
}
7+
8+
return function (css, opts) {
9+
opts.messages = [{
10+
type: 'dependency',
11+
file: configFile,
12+
parent: css.source.input.file,
13+
}]
14+
}
15+
}

src/lib/substituteClassApplyAtRules.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ function normalizeClassNames(classNames) {
99
})
1010
}
1111

12-
export default postcss.plugin('tailwind-apply', function(css) {
13-
return function(css) {
12+
export default function(config) {
13+
return function (css) {
14+
const options = config()
1415
css.walkRules(function(rule) {
1516
rule.walkAtRules('apply', atRule => {
1617
const mixins = normalizeClassNames(postcss.list.space(atRule.params))
@@ -42,4 +43,4 @@ export default postcss.plugin('tailwind-apply', function(css) {
4243
})
4344
})
4445
}
45-
})
46+
}

src/lib/substituteFocusableAtRules.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import _ from 'lodash'
22
import postcss from 'postcss'
33
import cloneNodes from '../util/cloneNodes'
44

5-
export default function(options) {
6-
return function(css) {
5+
export default function(config) {
6+
return function (css) {
7+
const options = config()
8+
79
css.walkAtRules('focusable', atRule => {
810

911
atRule.walkRules(rule => {

0 commit comments

Comments
 (0)