From b507d879dca4f82b50079672e66bf4203035a795 Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Mon, 13 Dec 2021 12:00:33 +0100 Subject: [PATCH 1/3] rewrite invalid globs if we can --- src/util/normalizeConfig.js | 7 +++++++ tests/normalize-config.test.js | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/util/normalizeConfig.js b/src/util/normalizeConfig.js index 02c80d79177b..ca0d302d58cd 100644 --- a/src/util/normalizeConfig.js +++ b/src/util/normalizeConfig.js @@ -245,5 +245,12 @@ export function normalizeConfig(config) { })(), } + // Rewrite globs to prevent bogus globs. + // E.g.: `./src/*.{html}` is invalid, the `{html}` should just be `html` + config.content.files = config.content.files.map((file) => { + if (typeof file !== 'string') return file + return file.replace(/{([^,]*?)}/g, '$1') + }) + return config } diff --git a/tests/normalize-config.test.js b/tests/normalize-config.test.js index 602e10963a6c..69185e86e941 100644 --- a/tests/normalize-config.test.js +++ b/tests/normalize-config.test.js @@ -1,4 +1,6 @@ +import { normalizeConfig } from '../src/util/normalizeConfig' import { run, css } from './util/run' +import resolveConfig from '../src/public/resolve-config' it.each` config @@ -95,3 +97,35 @@ it('should still be possible to use the "old" v2 config', () => { `) }) }) + +it('should keep content files with globs', () => { + let config = { + content: ['./example-folder/**/*.{html,js}'], + } + + expect(normalizeConfig(resolveConfig(config)).content).toEqual({ + files: ['./example-folder/**/*.{html,js}'], + extract: {}, + transform: {}, + }) +}) + +it('should rewrite globs with incorrect bracket expansion', () => { + let config = { + content: [ + './{example-folder}/**/*.{html,js}', + './{example-folder}/**/*.{html}', + './example-folder/**/*.{html}', + ], + } + + expect(normalizeConfig(resolveConfig(config)).content).toEqual({ + files: [ + './example-folder/**/*.{html,js}', + './example-folder/**/*.html', + './example-folder/**/*.html', + ], + extract: {}, + transform: {}, + }) +}) From 921eac26d8d9ee5ad7abd518c90e01c2c6218507 Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Mon, 13 Dec 2021 14:28:06 +0100 Subject: [PATCH 2/3] warn instead of rewrite --- src/util/log.js | 4 ++++ src/util/normalizeConfig.js | 18 ++++++++++++------ tests/normalize-config.test.js | 18 ++++++++++++++---- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/util/log.js b/src/util/log.js index 35e3ae4ef726..8ff3778527a0 100644 --- a/src/util/log.js +++ b/src/util/log.js @@ -12,6 +12,10 @@ function log(chalk, messages, key) { messages.forEach((message) => console.warn(chalk, '-', message)) } +export function dim(input) { + return chalk.dim(input) +} + export default { info(key, messages) { log(chalk.bold.cyan('info'), ...(Array.isArray(key) ? [key] : [messages, key])) diff --git a/src/util/normalizeConfig.js b/src/util/normalizeConfig.js index ca0d302d58cd..31eee90f49cd 100644 --- a/src/util/normalizeConfig.js +++ b/src/util/normalizeConfig.js @@ -1,4 +1,4 @@ -import log from './log' +import log, { dim } from './log' export function normalizeConfig(config) { // Quick structure validation @@ -245,12 +245,18 @@ export function normalizeConfig(config) { })(), } - // Rewrite globs to prevent bogus globs. + // Validate globs to prevent bogus globs. // E.g.: `./src/*.{html}` is invalid, the `{html}` should just be `html` - config.content.files = config.content.files.map((file) => { - if (typeof file !== 'string') return file - return file.replace(/{([^,]*?)}/g, '$1') - }) + for (let file of config.content.files) { + if (typeof file === 'string' && /{([^,]*?)}/g.test(file)) { + log.warn('invalid-glob-braces', [ + `The glob pattern ${dim(file)} in your config is invalid.`, + ` Update it to ${dim(file.replace(/{([^,]*?)}/g, '$1'))} to silence this warning.`, + // TODO: Add https://tw.wtf/invalid-glob-braces + ]) + break + } + } return config } diff --git a/tests/normalize-config.test.js b/tests/normalize-config.test.js index 69185e86e941..71f304ea84ba 100644 --- a/tests/normalize-config.test.js +++ b/tests/normalize-config.test.js @@ -110,7 +110,10 @@ it('should keep content files with globs', () => { }) }) -it('should rewrite globs with incorrect bracket expansion', () => { +it('should warn when we detect invalid globs with incorrect brace expansion', () => { + let log = require('../src/util/log') + let spy = jest.spyOn(log.default, 'warn') + let config = { content: [ './{example-folder}/**/*.{html,js}', @@ -119,13 +122,20 @@ it('should rewrite globs with incorrect bracket expansion', () => { ], } + // No rewrite happens expect(normalizeConfig(resolveConfig(config)).content).toEqual({ files: [ - './example-folder/**/*.{html,js}', - './example-folder/**/*.html', - './example-folder/**/*.html', + './{example-folder}/**/*.{html,js}', + './{example-folder}/**/*.{html}', + './example-folder/**/*.{html}', ], extract: {}, transform: {}, }) + + // But a warning should happen + expect(spy).toHaveBeenCalledTimes(2) + expect(spy.mock.calls.map((x) => x[0])).toEqual(['invalid-glob-braces', 'invalid-glob-braces']) + + spy.mockClear() }) From 7e962a55e7ce2ab34b6e99496ae653d9de3c1a81 Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Mon, 13 Dec 2021 14:35:11 +0100 Subject: [PATCH 3/3] update changelog --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bd0228365392..07d169b08e72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] -- Nothing yet! +### Added + +- Warn about invalid globs in `content` ([#6449](https://github.com/tailwindlabs/tailwindcss/pull/6449)) ## [3.0.2] - 2021-12-13