Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions lib/util/customConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,22 @@ function requireUncached(module) {
/**
* Load the config from a path string or parsed from an object
* @param {string|Object} config
* @param {boolean} newConfig
* @returns `null` when unchanged, `{}` when not found
*/
function loadConfig(config) {
function loadConfig(config, newConfig) {
let loadedConfig = null;
if (typeof config === 'string') {
const resolvedPath = path.isAbsolute(config) ? config : path.join(path.resolve(), config);
try {
const stats = fs.statSync(resolvedPath);
const stats = fs.statSync(config);
const mtime = `${stats.mtime || ''}`;
if (stats === null) {
// Default to no config
loadedConfig = {};
} else if (lastModifiedDate !== mtime) {
} else if (newConfig || lastModifiedDate !== mtime) {
// Load the config based on path
lastModifiedDate = mtime;
loadedConfig = requireUncached(resolvedPath);
loadedConfig = requireUncached(config);
} else {
// Unchanged config
loadedConfig = null;
Expand Down Expand Up @@ -81,14 +81,24 @@ function convertConfigToString(config) {
}
}

function getResolvedConfig(config) {
switch (typeof config) {
case 'string':
return path.isAbsolute(config) ? config : path.join(path.resolve(), config);
default:
return config;
}
}

function resolve(twConfig) {
const newConfig = convertConfigToString(twConfig) !== convertConfigToString(previousConfig);
const resolvedConfig = getResolvedConfig(twConfig);
const newConfig = convertConfigToString(resolvedConfig) !== convertConfigToString(previousConfig);
const now = Date.now();
const expired = now - lastCheck > CHECK_REFRESH_RATE;
if (newConfig || expired) {
previousConfig = twConfig;
previousConfig = resolvedConfig;
lastCheck = now;
const userConfig = loadConfig(twConfig);
const userConfig = loadConfig(resolvedConfig, newConfig);
// userConfig is null when config file was not modified
if (userConfig !== null) {
mergedConfig = resolveConfig(userConfig);
Expand Down
37 changes: 37 additions & 0 deletions tests/lib/util/customConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @fileoverview Test customConfig utilities
* @author David Hart
*/
"use strict";

var assert = require("assert");
var fs = require("fs");
var path = require("path");
var resolveConfig = require("tailwindcss/resolveConfig");
var customConfig = require("../../../lib/util/customConfig");

describe("resolve", function () {
it("should work with an object", function () {
const result = customConfig.resolve({ a: 1, b: 2 });
assert.deepEqual(result, resolveConfig({ a: 1, b: 2 }));
});

describe("with two config files at different paths with the same modification time", function () {
before(() => {
fs.writeFileSync("config1.js", "module.exports = { a: 1 }");
fs.writeFileSync("config2.js", "module.exports = { a: 2 }");
});

after(() => {
fs.unlinkSync("config1.js");
fs.unlinkSync("config2.js");
});

it("should distinguish between the two files", function () {
const result1 = customConfig.resolve("config1.js");
assert.deepEqual(result1, resolveConfig({ a: 1 }));
const result2 = customConfig.resolve("config2.js");
assert.deepEqual(result2, resolveConfig({ a: 2 }));
});
});
});