diff --git a/src/lib/setupContext.js b/src/lib/setupContext.js index 65538d0..f221320 100644 --- a/src/lib/setupContext.js +++ b/src/lib/setupContext.js @@ -724,6 +724,12 @@ function setupContext(configOrPath) { let contextDependenciesChanged = trackModified([...contextDependencies]) + let oldConfigHash = sharedState.sourceConfigHashMap.get(sourcePath) + if (tailwindConfigHash !== oldConfigHash) { + contextDependenciesChanged = contextDependenciesChanged || true + sharedState.sourceConfigHashMap.set(sourcePath, tailwindConfigHash) + } + process.env.DEBUG && console.log('Source path:', sourcePath) if (!contextDependenciesChanged) { diff --git a/src/lib/sharedState.js b/src/lib/sharedState.js index ecec1cd..3c21987 100644 --- a/src/lib/sharedState.js +++ b/src/lib/sharedState.js @@ -11,5 +11,6 @@ module.exports = { contextMap: new Map(), configContextMap: new Map(), contextSourcesMap: new Map(), + sourceConfigHashMap: new Map(), contentMatchCache: new LRU({ maxSize: 25000 }), } diff --git a/tests/rebuild.worker.js b/tests/rebuild.worker.js new file mode 100644 index 0000000..4dcb720 --- /dev/null +++ b/tests/rebuild.worker.js @@ -0,0 +1,71 @@ +const fs = require("fs") +const path = require("path") +const postcss = require("postcss") +const tailwind = require("../src/index.js") +const configPath = path.resolve(__dirname, './rebuilds.tailwind.config.js') + +function run(input, config = {}, from = null) { + from = from || path.resolve(__filename) + + return postcss([tailwind(config)]).process(input, { from }) +} + +async function runTest() { + let messages = [] + let config1 = { + darkMode: 'class', + purge: [path.resolve(__dirname, './rebuilds.test.html')], + corePlugins: { preflight: false }, + theme: { + colors: { plorange: 'orange' }, + }, + plugins: [], + } + + let config2 = { + ...config1, + theme: { + colors: { plorange: 'purple' }, + }, + } + + let from = path.resolve(__filename) + let css = ` + @tailwind utilities; + body { @apply bg-plorange; } + ` + + fs.writeFileSync(configPath, `module.exports = ${JSON.stringify(config1)};`) + + let results = [ + await run(css, configPath, `${from}?id=1`), + await run(css, configPath, `${from}?id=2`), + ] + + results.forEach(result => { + messages.push({ + css: result.css, + }) + }) + + await new Promise(resolve => setTimeout(resolve, 100)) + + fs.writeFileSync(configPath, `module.exports = ${JSON.stringify(config2)};`) + + results = [ + await run(css, configPath, `${from}?id=1`), + await run(css, configPath, `${from}?id=2`), + ] + + results.forEach(result => { + messages.push({ + css: result.css, + }) + }) + + process.stdout.write(JSON.stringify(messages)) +} + +runTest().finally(() => { + fs.unlinkSync(configPath) +}) diff --git a/tests/rebuilds.test.html b/tests/rebuilds.test.html new file mode 100644 index 0000000..0eb1922 --- /dev/null +++ b/tests/rebuilds.test.html @@ -0,0 +1,11 @@ + + + + + + + Title + + + + diff --git a/tests/rebuilds.test.js b/tests/rebuilds.test.js new file mode 100644 index 0000000..639e44d --- /dev/null +++ b/tests/rebuilds.test.js @@ -0,0 +1,36 @@ +const path = require('path') +const process = require('child_process') +const { promisify } = require('util') +const exec = promisify(process.exec) + +test('builds with multiple sources handle config file changes', async () => { + const filepath = path.resolve(__dirname, './rebuild.worker.js') + + const { stdout } = await exec(`node ${filepath}`) + + const results = JSON.parse(stdout.trim()) + expect(results[0].css).toMatchCss(` + body { + --tw-bg-opacity: 1; + background-color: rgba(255, 165, 0, var(--tw-bg-opacity)); + } + `) + expect(results[1].css).toMatchCss(` + body { + --tw-bg-opacity: 1; + background-color: rgba(255, 165, 0, var(--tw-bg-opacity)); + } + `) + expect(results[2].css).toMatchCss(` + body { + --tw-bg-opacity: 1; + background-color: rgba(128, 0, 128, var(--tw-bg-opacity)); + } + `) + expect(results[3].css).toMatchCss(` + body { + --tw-bg-opacity: 1; + background-color: rgba(128, 0, 128, var(--tw-bg-opacity)); + } + `) +})