Skip to content

Commit

Permalink
chore: fix test and provide toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
ineshbose committed Jul 11, 2024
1 parent f606398 commit 60a4bba
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
10 changes: 7 additions & 3 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand All @@ -92,15 +96,15 @@ 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.',
)
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)`)
}

Expand Down
17 changes: 7 additions & 10 deletions src/runtime/merger.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ const isJSObject = value => typeof value === 'object' && !Array.isArray(value)
*
* @type {(...p: Array<Partial<TWConfig> | Record<string | number | symbol, any> | undefined>) => Partial<TWConfig>}
*/
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') {
Expand All @@ -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))
}
10 changes: 10 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ export interface ModuleOptions {
* @default false // if true, { autocompleteUtil: true }
*/
editorSupport: BoolObj<EditorSupportConfig>
/**
* 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 {
Expand Down
1 change: 0 additions & 1 deletion test/configs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ describe('tailwindcss module configs', async () => {
// set from override-tailwind.config.ts
const { content: { files: contentFiles } } = destr<TWConfigWithStringContent>(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')
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/basic/override-tailwind.config.js
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down

0 comments on commit 60a4bba

Please sign in to comment.