From 649fb8f21eaa21e548cf05c7edccd2988bf169f8 Mon Sep 17 00:00:00 2001 From: Adam Wathan Date: Fri, 2 Aug 2019 08:07:42 -0400 Subject: [PATCH 1/3] Support passing config path via object --- __tests__/customConfig.test.js | 27 +++++++++++++++++++++++++++ src/index.js | 6 +++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/__tests__/customConfig.test.js b/__tests__/customConfig.test.js index 0d9cd1581..27ea05487 100644 --- a/__tests__/customConfig.test.js +++ b/__tests__/customConfig.test.js @@ -68,6 +68,33 @@ test('custom config can be passed as an object', () => { }) }) +test('custom config path can be passed using `config` property in an object', () => { + return postcss([tailwind({ config: path.resolve(`${__dirname}/fixtures/custom-config.js`) })]) + .process( + ` + @responsive { + .foo { + color: blue; + } + } + `, + { from: undefined } + ) + .then(result => { + const expected = ` + .foo { + color: blue; + } + @media (min-width: 400px) { + .mobile\\:foo { + color: blue; + } + } + ` + expect(result.css).toMatchCss(expected) + }) +}) + test('tailwind.config.js is picked up by default', () => { return inTempDirectory(() => { fs.writeFileSync( diff --git a/src/index.js b/src/index.js index 4c37432fb..d80ab8c2b 100644 --- a/src/index.js +++ b/src/index.js @@ -13,10 +13,14 @@ import { defaultConfigFile } from './constants' import defaultConfig from '../stubs/defaultConfig.stub.js' function resolveConfigPath(filePath) { - if (_.isObject(filePath)) { + if (_.isObject(filePath) && !_.has(filePath, 'config')) { return undefined } + if (_.isObject(filePath) && _.has(filePath, 'config')) { + return path.resolve(filePath.config) + } + if (!_.isUndefined(filePath)) { return path.resolve(filePath) } From 5cad3910fd0461fe209c2bee55c19b2753522b76 Mon Sep 17 00:00:00 2001 From: Adam Wathan Date: Fri, 2 Aug 2019 08:14:07 -0400 Subject: [PATCH 2/3] Support passing empty object to signal 'use default config path' --- __tests__/customConfig.test.js | 39 ++++++++++++++++++++++++++++++++++ src/index.js | 4 ++-- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/__tests__/customConfig.test.js b/__tests__/customConfig.test.js index 27ea05487..2d674713d 100644 --- a/__tests__/customConfig.test.js +++ b/__tests__/customConfig.test.js @@ -133,3 +133,42 @@ test('tailwind.config.js is picked up by default', () => { }) }) }) + +test('tailwind.config.js is picked up by default when passing an empty object', () => { + return inTempDirectory(() => { + fs.writeFileSync( + path.resolve(defaultConfigFile), + `module.exports = { + theme: { + screens: { + mobile: '400px', + }, + }, + }` + ) + + return postcss([tailwind({})]) + .process( + ` + @responsive { + .foo { + color: blue; + } + } + `, + { from: undefined } + ) + .then(result => { + expect(result.css).toMatchCss(` + .foo { + color: blue; + } + @media (min-width: 400px) { + .mobile\\:foo { + color: blue; + } + } + `) + }) + }) +}) diff --git a/src/index.js b/src/index.js index d80ab8c2b..95af61861 100644 --- a/src/index.js +++ b/src/index.js @@ -13,7 +13,7 @@ import { defaultConfigFile } from './constants' import defaultConfig from '../stubs/defaultConfig.stub.js' function resolveConfigPath(filePath) { - if (_.isObject(filePath) && !_.has(filePath, 'config')) { + if (_.isObject(filePath) && !_.has(filePath, 'config') && !_.isEmpty(filePath)) { return undefined } @@ -21,7 +21,7 @@ function resolveConfigPath(filePath) { return path.resolve(filePath.config) } - if (!_.isUndefined(filePath)) { + if (_.isString(filePath)) { return path.resolve(filePath) } From 0bf8495b96d072d9c9cc5736010e3e076765deff Mon Sep 17 00:00:00 2001 From: Adam Wathan Date: Fri, 2 Aug 2019 08:31:46 -0400 Subject: [PATCH 3/3] Support passing config object under config key --- __tests__/customConfig.test.js | 38 ++++++++++++++++++++++++++++++++++ src/index.js | 16 ++++++++++++-- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/__tests__/customConfig.test.js b/__tests__/customConfig.test.js index 2d674713d..5c3f3e7d2 100644 --- a/__tests__/customConfig.test.js +++ b/__tests__/customConfig.test.js @@ -95,6 +95,44 @@ test('custom config path can be passed using `config` property in an object', () }) }) +test('custom config can be passed under the `config` property', () => { + return postcss([ + tailwind({ + config: { + theme: { + screens: { + mobile: '400px', + }, + }, + }, + }), + ]) + .process( + ` + @responsive { + .foo { + color: blue; + } + } + `, + { from: undefined } + ) + .then(result => { + const expected = ` + .foo { + color: blue; + } + @media (min-width: 400px) { + .mobile\\:foo { + color: blue; + } + } + ` + + expect(result.css).toMatchCss(expected) + }) +}) + test('tailwind.config.js is picked up by default', () => { return inTempDirectory(() => { fs.writeFileSync( diff --git a/src/index.js b/src/index.js index 95af61861..11f1aba46 100644 --- a/src/index.js +++ b/src/index.js @@ -13,18 +13,27 @@ import { defaultConfigFile } from './constants' import defaultConfig from '../stubs/defaultConfig.stub.js' function resolveConfigPath(filePath) { + // require('tailwindcss')({ theme: ..., variants: ... }) if (_.isObject(filePath) && !_.has(filePath, 'config') && !_.isEmpty(filePath)) { return undefined } - if (_.isObject(filePath) && _.has(filePath, 'config')) { + // require('tailwindcss')({ config: 'custom-config.js' }) + if (_.isObject(filePath) && _.has(filePath, 'config') && _.isString(filePath.config)) { return path.resolve(filePath.config) } + // require('tailwindcss')({ config: { theme: ..., variants: ... } }) + if (_.isObject(filePath) && _.has(filePath, 'config') && _.isObject(filePath.config)) { + undefined + } + + // require('tailwindcss')('custom-config.js') if (_.isString(filePath)) { return path.resolve(filePath) } + // require('tailwindcss') try { const defaultConfigPath = path.resolve(defaultConfigFile) fs.accessSync(defaultConfigPath) @@ -43,7 +52,10 @@ const getConfigFunction = config => () => { delete require.cache[require.resolve(config)] } - return resolveConfig([_.isObject(config) ? config : require(config), defaultConfig]) + return resolveConfig([ + _.isObject(config) ? _.get(config, 'config', config) : require(config), + defaultConfig, + ]) } const plugin = postcss.plugin('tailwind', config => {