(configUpdatedHook: Record<string, string>, meta: ReturnType<typeof twCtx.use>['meta'])
| 8 | const JSONStringifyWithRegex = (obj: any) => JSON.stringify(obj, (_, v) => v instanceof RegExp ? `__REGEXP ${v.toString()}` : v) |
| 9 | |
| 10 | export const createObjProxy = (configUpdatedHook: Record<string, string>, meta: ReturnType<typeof twCtx.use>['meta']) => { |
| 11 | return (configPath: string, oldConfig: Partial<TWConfig>, newConfig: Partial<TWConfig>) => |
| 12 | diff(oldConfig, newConfig).forEach((change) => { |
| 13 | const path = change.key.split('.').map(k => `[${JSON.stringify(k)}]`).join('') |
| 14 | const newValue = change.newValue?.value |
| 15 | |
| 16 | switch (change.type) { |
| 17 | case 'removed': configUpdatedHook[configPath] += `delete cfg${path};` |
| 18 | break |
| 19 | case 'added': |
| 20 | case 'changed': { |
| 21 | const resultingCode = `cfg${path} = ${JSONStringifyWithRegex(newValue)?.replace(/"__REGEXP (.*)"/g, (_, substr) => substr.replace(/\\"/g, '"')) || `cfg${path}`};` |
| 22 | |
| 23 | if (JSONStringifyWithUnsupportedVals(change.oldValue?.value) === JSONStringifyWithUnsupportedVals(newValue) || configUpdatedHook[configPath].endsWith(resultingCode)) { |
| 24 | return |
| 25 | } |
| 26 | |
| 27 | if (JSONStringifyWithUnsupportedVals(newValue).includes(`"${UNSUPPORTED_VAL_STR}"`) && !meta?.disableHMR) { |
| 28 | logger.warn( |
| 29 | `A hook has injected a non-serializable value in \`config${path}\`, so the Tailwind Config cannot be serialized. Falling back to providing the loaded configuration inlined directly to PostCSS loader..`, |
| 30 | 'Please consider using a configuration file/template instead (specifying in `configPath` of the module options) to enable additional support for IntelliSense and HMR.', |
| 31 | ) |
| 32 | twCtx.set({ meta: { disableHMR: true } }) |
| 33 | } |
| 34 | |
| 35 | if (JSONStringifyWithRegex(newValue).includes('__REGEXP') && !meta?.disableHMR) { |
| 36 | logger.warn(`A hook is injecting RegExp values in your configuration (check \`config${path}\`) which may be unsafely serialized. Consider moving your safelist to a separate configuration file/template instead (specifying in \`configPath\` of the module options)`) |
| 37 | } |
| 38 | |
| 39 | configUpdatedHook[configPath] += resultingCode |
| 40 | } |
| 41 | } |
| 42 | }) |
| 43 | } |
no test coverage detected