|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | | -const fs = require('fs'); |
4 | | -const path = require('path'); |
5 | | -const resolveConfig = require('tailwindcss/resolveConfig'); |
6 | | -let twLoadConfig; |
| 3 | +const { TailwindUtils } = require('tailwind-api-utils'); |
7 | 4 |
|
8 | | -try { |
9 | | - twLoadConfig = require('tailwindcss/lib/lib/load-config'); |
10 | | -} catch (err) { |
11 | | - twLoadConfig = null; |
12 | | -} |
13 | | - |
14 | | -const CHECK_REFRESH_RATE = 1_000; |
15 | | -let previousConfig = null; |
16 | | -let lastCheck = null; |
17 | | -let mergedConfig = null; |
18 | | -let lastModifiedDate = null; |
19 | | - |
20 | | -/** |
21 | | - * @see https://stackoverflow.com/questions/9210542/node-js-require-cache-possible-to-invalidate |
22 | | - * @param {string} module The path to the module |
23 | | - * @returns the module's export |
24 | | - */ |
25 | | -function requireUncached(module) { |
26 | | - delete require.cache[require.resolve(module)]; |
27 | | - if (twLoadConfig === null) { |
28 | | - // Using native loading |
29 | | - return require(module); |
30 | | - } else { |
31 | | - // Using Tailwind CSS's loadConfig utility |
32 | | - return twLoadConfig.loadConfig(module); |
33 | | - } |
34 | | -} |
| 5 | +// for nativewind preset |
| 6 | +process.env.TAILWIND_MODE = 'build'; |
35 | 7 |
|
| 8 | +const CHECK_REFRESH_RATE = 10_000; |
| 9 | +let lastCheck = new Map(); |
36 | 10 | /** |
37 | | - * Load the config from a path string or parsed from an object |
38 | | - * @param {string|Object} config |
39 | | - * @returns `null` when unchanged, `{}` when not found |
| 11 | + * @type {Map<string, TailwindUtils}>} |
40 | 12 | */ |
41 | | -function loadConfig(config) { |
42 | | - let loadedConfig = null; |
43 | | - if (typeof config === 'string') { |
44 | | - const resolvedPath = path.isAbsolute(config) ? config : path.join(path.resolve(), config); |
45 | | - try { |
46 | | - const stats = fs.statSync(resolvedPath); |
47 | | - const mtime = `${stats.mtime || ''}`; |
48 | | - if (stats === null) { |
49 | | - // Default to no config |
50 | | - loadedConfig = {}; |
51 | | - } else if (lastModifiedDate !== mtime) { |
52 | | - // Load the config based on path |
53 | | - lastModifiedDate = mtime; |
54 | | - loadedConfig = requireUncached(resolvedPath); |
55 | | - } else { |
56 | | - // Unchanged config |
57 | | - loadedConfig = null; |
58 | | - } |
59 | | - } catch (err) { |
60 | | - // Default to no config |
61 | | - loadedConfig = {}; |
62 | | - } finally { |
63 | | - return loadedConfig; |
64 | | - } |
65 | | - } else { |
66 | | - if (typeof config === 'object' && config !== null) { |
67 | | - return config; |
68 | | - } |
69 | | - return {}; |
70 | | - } |
71 | | -} |
72 | | - |
73 | | -function convertConfigToString(config) { |
74 | | - switch (typeof config) { |
75 | | - case 'string': |
76 | | - return config; |
77 | | - case 'object': |
78 | | - return JSON.stringify(config); |
79 | | - default: |
80 | | - return config.toString(); |
81 | | - } |
82 | | -} |
| 13 | +let mergedConfig = new Map(); |
83 | 14 |
|
84 | 15 | function resolve(twConfig) { |
85 | | - const newConfig = convertConfigToString(twConfig) !== convertConfigToString(previousConfig); |
| 16 | + const newConfig = mergedConfig.get(twConfig) === undefined; |
86 | 17 | const now = Date.now(); |
87 | | - const expired = now - lastCheck > CHECK_REFRESH_RATE; |
| 18 | + const expired = now - lastCheck.get(twConfig) > CHECK_REFRESH_RATE; |
88 | 19 | if (newConfig || expired) { |
89 | | - previousConfig = twConfig; |
90 | | - lastCheck = now; |
91 | | - const userConfig = loadConfig(twConfig); |
92 | | - // userConfig is null when config file was not modified |
93 | | - if (userConfig !== null) { |
94 | | - mergedConfig = resolveConfig(userConfig); |
95 | | - } |
| 20 | + lastCheck.set(twConfig, now); |
| 21 | + const tailwindUtils = new TailwindUtils(); |
| 22 | + mergedConfig.set(twConfig, tailwindUtils); |
96 | 23 | } |
97 | | - return mergedConfig; |
| 24 | + return mergedConfig.get(twConfig); |
98 | 25 | } |
99 | 26 |
|
100 | 27 | module.exports = { |
|
0 commit comments