From 60a4bba6b7cf8ee8c6491f620b4e88780f117b99 Mon Sep 17 00:00:00 2001 From: Inesh Bose Date: Thu, 11 Jul 2024 17:58:00 +0530 Subject: [PATCH] chore: fix test and provide toggle --- src/context.ts | 10 +++++++--- src/runtime/merger.js | 17 +++++++---------- src/types.ts | 10 ++++++++++ test/configs.test.ts | 1 - test/fixtures/basic/override-tailwind.config.js | 4 ++-- 5 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/context.ts b/src/context.ts index 9a013021..356fe05f 100644 --- a/src/context.ts +++ b/src/context.ts @@ -67,8 +67,12 @@ const createInternalContext = async (moduleOptions: ModuleOptions, nuxt = useNux const configResolvedPath = join(nuxt.options.buildDir, CONFIG_TEMPLATE_NAME) let enableHMR = true + if (moduleOptions.disableHMR) { + enableHMR = false + } + const unsafeProperty = unsafeInlineConfig(moduleOptions.config) - if (unsafeProperty) { + if (unsafeProperty && enableHMR) { logger.warn( `The provided Tailwind configuration in your \`nuxt.config\` is non-serializable. Check \`${unsafeProperty}\`. Falling back to providing the loaded configuration inlined directly to PostCSS loader..`, 'Please consider using `tailwind.config` or a separate file (specifying in `configPath` of the module options) to enable it with additional support for IntelliSense and HMR. Suppress this warning with `quiet: true` in the module options.', @@ -92,7 +96,7 @@ const createInternalContext = async (moduleOptions: ModuleOptions, nuxt = useNux return Reflect.set(target, key, value) } - if (functionalStringify(value).includes(`"${CONFIG_TEMPLATE_NAME + 'ns'}"`)) { + if (functionalStringify(value).includes(`"${CONFIG_TEMPLATE_NAME + 'ns'}"`) && enableHMR) { logger.warn( `A hook has injected a non-serializable value in \`config${cfgKey}\`, so the Tailwind Config cannot be serialized. Falling back to providing the loaded configuration inlined directly to PostCSS loader..`, 'Please consider using a configuration file/template instead (specifying in `configPath` of the module options) to enable additional support for IntelliSense and HMR.', @@ -100,7 +104,7 @@ const createInternalContext = async (moduleOptions: ModuleOptions, nuxt = useNux enableHMR = false } - if (JSONStringifyWithRegex(value).includes('__REGEXP')) { + if (JSONStringifyWithRegex(value).includes('__REGEXP') && enableHMR) { logger.warn(`A hook is injecting RegExp values in your configuration (check \`config${cfgKey}\`) which may be unsafely serialized. Consider moving your safelist to a separate configuration file/template instead (specifying in \`configPath\` of the module options)`) } diff --git a/src/runtime/merger.js b/src/runtime/merger.js index 68da5b0a..45b3439a 100644 --- a/src/runtime/merger.js +++ b/src/runtime/merger.js @@ -12,8 +12,10 @@ const isJSObject = value => typeof value === 'object' && !Array.isArray(value) * * @type {(...p: Array | Record | undefined>) => Partial} */ -export default (_base, ...defaults) => { - const base = _base ?? klona(defaults[0]) +export default (base, ...defaults) => { + if (!base) { + return klona(defaults[0]) + } return createDefu((obj, key, value) => { if (key === 'content') { @@ -28,14 +30,9 @@ export default (_base, ...defaults) => { } // keeping arrayFn - if (Array.isArray(obj[key]) && typeof value === 'function') { - obj[key] = value(obj[key]) + if (obj[key] && typeof value === 'function') { + obj[key] = value(Array.isArray(obj[key]) ? obj[key] : obj[key]['files']) return true } - - // create copy of object - if (!obj[key] && isJSObject(value)) { - obj[key] = Object.assign({}, value) - } - })(base, ...defaults) + })(klona(base), ...defaults.map(klona)) } diff --git a/src/types.ts b/src/types.ts index f8bec6ce..8dc9dc29 100644 --- a/src/types.ts +++ b/src/types.ts @@ -134,6 +134,16 @@ export interface ModuleOptions { * @default false // if true, { autocompleteUtil: true } */ editorSupport: BoolObj + /** + * This option falls back to the Tailwind configuration inlined to the PostCSS + * loader, so any configuration changes while the dev server is running will + * not reflect. This is similar to the functionality prior to v6.12.0. + * + * Note: this is only provided for temporary broken builds that may require + * migration. Usage is discouraged. If any issues occur without this, please open + * an issue on https://github.com/nuxt-modules/tailwindcss/issues. + */ + disableHMR?: boolean } export interface ModuleHooks { diff --git a/test/configs.test.ts b/test/configs.test.ts index 6206f05a..b7d3066d 100644 --- a/test/configs.test.ts +++ b/test/configs.test.ts @@ -86,7 +86,6 @@ describe('tailwindcss module configs', async () => { // set from override-tailwind.config.ts const { content: { files: contentFiles } } = destr(getVfsFile('test-tailwind.config.mjs')!.replace(/^export default /, '')) - expect(contentFiles[0]).toBe('ts-content/**/*.md') expect(contentFiles[1]).toBe('./custom-theme/**/*.vue') expect(contentFiles.filter(c => /\{[AE],[ae]\}/.test(c)).length).toBe(0) expect([...contentFiles].pop()).toBe('my-custom-content') diff --git a/test/fixtures/basic/override-tailwind.config.js b/test/fixtures/basic/override-tailwind.config.js index 4783beab..c01797ae 100644 --- a/test/fixtures/basic/override-tailwind.config.js +++ b/test/fixtures/basic/override-tailwind.config.js @@ -1,8 +1,8 @@ export default { content: contentDefaults => [ - contentDefaults[0], + contentDefaults?.[0], './custom-theme/**/*.vue', - ...contentDefaults.filter(c => !/\{[AE],[ae]\}/.test(c)), + ...(contentDefaults || []).filter(c => !/\{[AE],[ae]\}/.test(c)), ], theme: { extend: {