tailwindcss 0.0.0-insiders.fe08e91 → 0.0.0-oxide.6bf5e56

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (176) hide show
  1. package/CHANGELOG.md +379 -3
  2. package/LICENSE +1 -2
  3. package/README.md +12 -8
  4. package/colors.d.ts +3 -0
  5. package/defaultConfig.d.ts +3 -0
  6. package/defaultTheme.d.ts +4 -0
  7. package/lib/cli/build/deps.js +54 -0
  8. package/lib/cli/build/index.js +48 -0
  9. package/lib/cli/build/plugin.js +367 -0
  10. package/lib/cli/build/utils.js +78 -0
  11. package/lib/cli/build/watching.js +178 -0
  12. package/lib/cli/help/index.js +71 -0
  13. package/lib/cli/index.js +18 -0
  14. package/lib/cli/init/index.js +46 -0
  15. package/lib/cli/shared.js +13 -0
  16. package/lib/cli-peer-dependencies.js +20 -7
  17. package/lib/cli.js +107 -611
  18. package/lib/constants.js +27 -20
  19. package/lib/corePluginList.js +6 -3
  20. package/lib/corePlugins.js +2064 -1811
  21. package/lib/css/preflight.css +5 -5
  22. package/lib/featureFlags.js +31 -22
  23. package/lib/index.js +28 -10
  24. package/lib/lib/cacheInvalidation.js +90 -0
  25. package/lib/lib/collapseAdjacentRules.js +27 -9
  26. package/lib/lib/collapseDuplicateDeclarations.js +12 -9
  27. package/lib/lib/content.js +176 -0
  28. package/lib/lib/defaultExtractor.js +225 -31
  29. package/lib/lib/detectNesting.js +13 -10
  30. package/lib/lib/evaluateTailwindFunctions.js +118 -55
  31. package/lib/lib/expandApplyAtRules.js +439 -190
  32. package/lib/lib/expandTailwindAtRules.js +151 -134
  33. package/lib/lib/findAtConfigPath.js +44 -0
  34. package/lib/lib/generateRules.js +454 -187
  35. package/lib/lib/getModuleDependencies.js +11 -8
  36. package/lib/lib/normalizeTailwindDirectives.js +36 -32
  37. package/lib/lib/offsets.js +217 -0
  38. package/lib/lib/partitionApplyAtRules.js +56 -0
  39. package/lib/lib/regex.js +60 -0
  40. package/lib/lib/resolveDefaultsAtRules.js +89 -67
  41. package/lib/lib/setupContextUtils.js +667 -376
  42. package/lib/lib/setupTrackingContext.js +38 -67
  43. package/lib/lib/sharedState.js +27 -14
  44. package/lib/lib/substituteScreenAtRules.js +11 -9
  45. package/{nesting → lib/postcss-plugins/nesting}/README.md +2 -2
  46. package/lib/postcss-plugins/nesting/index.js +19 -0
  47. package/lib/postcss-plugins/nesting/plugin.js +87 -0
  48. package/lib/processTailwindFeatures.js +35 -25
  49. package/lib/public/colors.js +247 -245
  50. package/lib/public/create-plugin.js +6 -4
  51. package/lib/public/default-config.js +7 -5
  52. package/lib/public/default-theme.js +7 -5
  53. package/lib/public/resolve-config.js +8 -5
  54. package/lib/util/bigSign.js +4 -1
  55. package/lib/util/buildMediaQuery.js +11 -6
  56. package/lib/util/cloneDeep.js +7 -6
  57. package/lib/util/cloneNodes.js +21 -3
  58. package/lib/util/color.js +53 -54
  59. package/lib/util/configurePlugins.js +5 -2
  60. package/lib/util/createPlugin.js +6 -6
  61. package/lib/util/createUtilityPlugin.js +12 -14
  62. package/lib/util/dataTypes.js +119 -110
  63. package/lib/util/defaults.js +4 -1
  64. package/lib/util/escapeClassName.js +7 -4
  65. package/lib/util/escapeCommas.js +5 -2
  66. package/lib/util/flattenColorPalette.js +9 -12
  67. package/lib/util/formatVariantSelector.js +184 -85
  68. package/lib/util/getAllConfigs.js +27 -8
  69. package/lib/util/hashConfig.js +6 -3
  70. package/lib/util/isKeyframeRule.js +5 -2
  71. package/lib/util/isPlainObject.js +5 -2
  72. package/lib/util/{isValidArbitraryValue.js → isSyntacticallyValidPropertyValue.js} +23 -15
  73. package/lib/util/log.js +20 -14
  74. package/lib/util/nameClass.js +20 -9
  75. package/lib/util/negateValue.js +23 -8
  76. package/lib/util/normalizeConfig.js +116 -72
  77. package/lib/util/normalizeScreens.js +120 -11
  78. package/lib/util/parseAnimationValue.js +42 -40
  79. package/lib/util/parseBoxShadowValue.js +30 -23
  80. package/lib/util/parseDependency.js +38 -56
  81. package/lib/util/parseGlob.js +34 -0
  82. package/lib/util/parseObjectStyles.js +11 -8
  83. package/lib/util/pluginUtils.js +147 -50
  84. package/lib/util/prefixSelector.js +10 -8
  85. package/lib/util/removeAlphaVariables.js +29 -0
  86. package/lib/util/resolveConfig.js +97 -85
  87. package/lib/util/resolveConfigPath.js +11 -9
  88. package/lib/util/responsive.js +8 -5
  89. package/lib/util/splitAtTopLevelOnly.js +43 -0
  90. package/lib/util/tap.js +4 -1
  91. package/lib/util/toColorValue.js +5 -3
  92. package/lib/util/toPath.js +20 -4
  93. package/lib/util/transformThemeValue.js +37 -29
  94. package/lib/util/validateConfig.js +24 -0
  95. package/lib/util/validateFormalSyntax.js +24 -0
  96. package/lib/util/withAlphaVariable.js +23 -15
  97. package/nesting/index.js +2 -12
  98. package/package.json +47 -42
  99. package/peers/index.js +11381 -7950
  100. package/plugin.d.ts +11 -0
  101. package/resolveConfig.d.ts +12 -0
  102. package/scripts/generate-types.js +105 -0
  103. package/scripts/release-channel.js +18 -0
  104. package/scripts/release-notes.js +21 -0
  105. package/scripts/type-utils.js +27 -0
  106. package/src/cli/build/deps.js +56 -0
  107. package/src/cli/build/index.js +49 -0
  108. package/src/cli/build/plugin.js +439 -0
  109. package/src/cli/build/utils.js +76 -0
  110. package/src/cli/build/watching.js +227 -0
  111. package/src/cli/help/index.js +70 -0
  112. package/src/cli/index.js +3 -0
  113. package/src/cli/init/index.js +50 -0
  114. package/src/cli/shared.js +6 -0
  115. package/src/cli-peer-dependencies.js +7 -1
  116. package/src/cli.js +50 -629
  117. package/src/corePluginList.js +1 -1
  118. package/src/corePlugins.js +532 -217
  119. package/src/css/preflight.css +5 -5
  120. package/src/featureFlags.js +15 -9
  121. package/src/index.js +20 -1
  122. package/src/lib/cacheInvalidation.js +52 -0
  123. package/src/lib/collapseAdjacentRules.js +21 -2
  124. package/src/lib/content.js +212 -0
  125. package/src/lib/defaultExtractor.js +196 -33
  126. package/src/lib/evaluateTailwindFunctions.js +78 -7
  127. package/src/lib/expandApplyAtRules.js +482 -183
  128. package/src/lib/expandTailwindAtRules.js +106 -85
  129. package/src/lib/findAtConfigPath.js +48 -0
  130. package/src/lib/generateRules.js +418 -129
  131. package/src/lib/normalizeTailwindDirectives.js +1 -0
  132. package/src/lib/offsets.js +270 -0
  133. package/src/lib/partitionApplyAtRules.js +52 -0
  134. package/src/lib/regex.js +74 -0
  135. package/src/lib/resolveDefaultsAtRules.js +51 -30
  136. package/src/lib/setupContextUtils.js +556 -208
  137. package/src/lib/setupTrackingContext.js +11 -48
  138. package/src/lib/sharedState.js +5 -0
  139. package/src/postcss-plugins/nesting/README.md +42 -0
  140. package/src/postcss-plugins/nesting/index.js +13 -0
  141. package/src/postcss-plugins/nesting/plugin.js +80 -0
  142. package/src/processTailwindFeatures.js +8 -0
  143. package/src/util/buildMediaQuery.js +5 -3
  144. package/src/util/cloneNodes.js +19 -2
  145. package/src/util/color.js +25 -21
  146. package/src/util/dataTypes.js +29 -21
  147. package/src/util/formatVariantSelector.js +184 -61
  148. package/src/util/getAllConfigs.js +19 -0
  149. package/src/util/{isValidArbitraryValue.js → isSyntacticallyValidPropertyValue.js} +1 -1
  150. package/src/util/log.js +8 -8
  151. package/src/util/nameClass.js +4 -0
  152. package/src/util/negateValue.js +11 -3
  153. package/src/util/normalizeConfig.js +44 -6
  154. package/src/util/normalizeScreens.js +99 -4
  155. package/src/util/parseBoxShadowValue.js +4 -3
  156. package/src/util/parseDependency.js +37 -42
  157. package/src/util/parseGlob.js +24 -0
  158. package/src/util/pluginUtils.js +132 -10
  159. package/src/util/prefixSelector.js +7 -5
  160. package/src/util/removeAlphaVariables.js +24 -0
  161. package/src/util/resolveConfig.js +70 -32
  162. package/src/util/splitAtTopLevelOnly.js +45 -0
  163. package/src/util/toPath.js +1 -1
  164. package/src/util/transformThemeValue.js +13 -3
  165. package/src/util/validateConfig.js +13 -0
  166. package/src/util/validateFormalSyntax.js +34 -0
  167. package/src/util/withAlphaVariable.js +1 -1
  168. package/stubs/defaultConfig.stub.js +23 -20
  169. package/stubs/simpleConfig.stub.js +1 -0
  170. package/types/config.d.ts +362 -0
  171. package/types/generated/.gitkeep +0 -0
  172. package/types/generated/colors.d.ts +276 -0
  173. package/types/generated/corePluginList.d.ts +1 -0
  174. package/types/generated/default-theme.d.ts +342 -0
  175. package/types/index.d.ts +7 -0
  176. package/nesting/plugin.js +0 -41
@@ -0,0 +1,367 @@
1
+ // @ts-check
2
+ "use strict";
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "createProcessor", {
7
+ enumerable: true,
8
+ get: ()=>createProcessor
9
+ });
10
+ const _path = /*#__PURE__*/ _interopRequireDefault(require("path"));
11
+ const _fs = /*#__PURE__*/ _interopRequireDefault(require("fs"));
12
+ const _postcssLoadConfig = /*#__PURE__*/ _interopRequireDefault(require("postcss-load-config"));
13
+ const _lilconfig = require("lilconfig");
14
+ const _plugins = /*#__PURE__*/ _interopRequireDefault(require("postcss-load-config/src/plugins" // Little bit scary, looking at private/internal API
15
+ ));
16
+ const _options = /*#__PURE__*/ _interopRequireDefault(require("postcss-load-config/src/options" // Little bit scary, looking at private/internal API
17
+ ));
18
+ const _processTailwindFeatures = /*#__PURE__*/ _interopRequireDefault(require("../../processTailwindFeatures"));
19
+ const _deps = require("./deps");
20
+ const _utils = require("./utils");
21
+ const _shared = require("../shared");
22
+ const _resolveConfigJs = /*#__PURE__*/ _interopRequireDefault(require("../../../resolveConfig.js"));
23
+ const _getModuleDependenciesJs = /*#__PURE__*/ _interopRequireDefault(require("../../lib/getModuleDependencies.js"));
24
+ const _contentJs = require("../../lib/content.js");
25
+ const _watchingJs = require("./watching.js");
26
+ const _fastGlob = /*#__PURE__*/ _interopRequireDefault(require("fast-glob"));
27
+ const _findAtConfigPathJs = require("../../lib/findAtConfigPath.js");
28
+ const _log = /*#__PURE__*/ _interopRequireDefault(require("../../util/log"));
29
+ function _interopRequireDefault(obj) {
30
+ return obj && obj.__esModule ? obj : {
31
+ default: obj
32
+ };
33
+ }
34
+ /**
35
+ *
36
+ * @param {string} [customPostCssPath ]
37
+ * @returns
38
+ */ async function loadPostCssPlugins(customPostCssPath) {
39
+ let config = customPostCssPath ? await (async ()=>{
40
+ let file = _path.default.resolve(customPostCssPath);
41
+ // Implementation, see: https://unpkg.com/browse/postcss-load-config@3.1.0/src/index.js
42
+ // @ts-ignore
43
+ let { config ={} } = await (0, _lilconfig.lilconfig)("postcss").load(file);
44
+ if (typeof config === "function") {
45
+ config = config();
46
+ } else {
47
+ config = Object.assign({}, config);
48
+ }
49
+ if (!config.plugins) {
50
+ config.plugins = [];
51
+ }
52
+ return {
53
+ file,
54
+ plugins: (0, _plugins.default)(config, file),
55
+ options: (0, _options.default)(config, file)
56
+ };
57
+ })() : await (0, _postcssLoadConfig.default)();
58
+ let configPlugins = config.plugins;
59
+ let configPluginTailwindIdx = configPlugins.findIndex((plugin)=>{
60
+ if (typeof plugin === "function" && plugin.name === "tailwindcss") {
61
+ return true;
62
+ }
63
+ if (typeof plugin === "object" && plugin !== null && plugin.postcssPlugin === "tailwindcss") {
64
+ return true;
65
+ }
66
+ return false;
67
+ });
68
+ let beforePlugins = configPluginTailwindIdx === -1 ? [] : configPlugins.slice(0, configPluginTailwindIdx);
69
+ let afterPlugins = configPluginTailwindIdx === -1 ? configPlugins : configPlugins.slice(configPluginTailwindIdx + 1);
70
+ return [
71
+ beforePlugins,
72
+ afterPlugins,
73
+ config.options
74
+ ];
75
+ }
76
+ function loadBuiltinPostcssPlugins() {
77
+ let postcss = (0, _deps.loadPostcss)();
78
+ let IMPORT_COMMENT = "__TAILWIND_RESTORE_IMPORT__: ";
79
+ return [
80
+ [
81
+ (root)=>{
82
+ root.walkAtRules("import", (rule)=>{
83
+ if (rule.params.slice(1).startsWith("tailwindcss/")) {
84
+ rule.after(postcss.comment({
85
+ text: IMPORT_COMMENT + rule.params
86
+ }));
87
+ rule.remove();
88
+ }
89
+ });
90
+ },
91
+ (0, _deps.loadPostcssImport)(),
92
+ (root)=>{
93
+ root.walkComments((rule)=>{
94
+ if (rule.text.startsWith(IMPORT_COMMENT)) {
95
+ rule.after(postcss.atRule({
96
+ name: "import",
97
+ params: rule.text.replace(IMPORT_COMMENT, "")
98
+ }));
99
+ rule.remove();
100
+ }
101
+ });
102
+ }
103
+ ],
104
+ [],
105
+ {}
106
+ ];
107
+ }
108
+ let state = {
109
+ /** @type {any} */ context: null,
110
+ /** @type {ReturnType<typeof createWatcher> | null} */ watcher: null,
111
+ /** @type {{content: string, extension: string}[]} */ changedContent: [],
112
+ configDependencies: new Set(),
113
+ contextDependencies: new Set(),
114
+ /** @type {import('../../lib/content.js').ContentPath[]} */ contentPaths: [],
115
+ refreshContentPaths () {
116
+ var ref;
117
+ this.contentPaths = (0, _contentJs.parseCandidateFiles)(this.context, (ref = this.context) === null || ref === void 0 ? void 0 : ref.tailwindConfig);
118
+ },
119
+ get config () {
120
+ return this.context.tailwindConfig;
121
+ },
122
+ get contentPatterns () {
123
+ return {
124
+ all: this.contentPaths.map((contentPath)=>contentPath.pattern),
125
+ dynamic: this.contentPaths.filter((contentPath)=>contentPath.glob !== undefined).map((contentPath)=>contentPath.pattern)
126
+ };
127
+ },
128
+ loadConfig (configPath, content) {
129
+ if (this.watcher && configPath) {
130
+ this.refreshConfigDependencies(configPath);
131
+ }
132
+ let config = configPath ? require(configPath) : {};
133
+ // @ts-ignore
134
+ config = (0, _resolveConfigJs.default)(config, {
135
+ content: {
136
+ files: []
137
+ }
138
+ });
139
+ // Override content files if `--content` has been passed explicitly
140
+ if ((content === null || content === void 0 ? void 0 : content.length) > 0) {
141
+ config.content.files = content;
142
+ }
143
+ return config;
144
+ },
145
+ refreshConfigDependencies (configPath) {
146
+ _shared.env.DEBUG && console.time("Module dependencies");
147
+ for (let file of this.configDependencies){
148
+ delete require.cache[require.resolve(file)];
149
+ }
150
+ if (configPath) {
151
+ let deps = (0, _getModuleDependenciesJs.default)(configPath).map(({ file })=>file);
152
+ for (let dependency of deps){
153
+ this.configDependencies.add(dependency);
154
+ }
155
+ }
156
+ _shared.env.DEBUG && console.timeEnd("Module dependencies");
157
+ },
158
+ readContentPaths () {
159
+ let content = [];
160
+ // Resolve globs from the content config
161
+ // TODO: When we make the postcss plugin async-capable this can become async
162
+ let files = _fastGlob.default.sync(this.contentPatterns.all);
163
+ for (let file of files){
164
+ if (_shared.env.OXIDE) {
165
+ content.push({
166
+ file,
167
+ extension: _path.default.extname(file).slice(1)
168
+ });
169
+ } else {
170
+ content.push({
171
+ content: _fs.default.readFileSync(_path.default.resolve(file), "utf8"),
172
+ extension: _path.default.extname(file).slice(1)
173
+ });
174
+ }
175
+ }
176
+ // Resolve raw content in the tailwind config
177
+ let rawContent = this.config.content.files.filter((file)=>{
178
+ return file !== null && typeof file === "object";
179
+ });
180
+ for (let { raw: htmlContent , extension ="html" } of rawContent){
181
+ content.push({
182
+ content: htmlContent,
183
+ extension
184
+ });
185
+ }
186
+ return content;
187
+ },
188
+ getContext ({ createContext , cliConfigPath , root , result , content }) {
189
+ if (this.context) {
190
+ this.context.changedContent = this.changedContent.splice(0);
191
+ return this.context;
192
+ }
193
+ _shared.env.DEBUG && console.time("Searching for config");
194
+ var ref;
195
+ let configPath = (ref = (0, _findAtConfigPathJs.findAtConfigPath)(root, result)) !== null && ref !== void 0 ? ref : cliConfigPath;
196
+ _shared.env.DEBUG && console.timeEnd("Searching for config");
197
+ _shared.env.DEBUG && console.time("Loading config");
198
+ let config = this.loadConfig(configPath, content);
199
+ _shared.env.DEBUG && console.timeEnd("Loading config");
200
+ _shared.env.DEBUG && console.time("Creating context");
201
+ this.context = createContext(config, []);
202
+ Object.assign(this.context, {
203
+ userConfigPath: configPath
204
+ });
205
+ _shared.env.DEBUG && console.timeEnd("Creating context");
206
+ _shared.env.DEBUG && console.time("Resolving content paths");
207
+ this.refreshContentPaths();
208
+ _shared.env.DEBUG && console.timeEnd("Resolving content paths");
209
+ if (this.watcher) {
210
+ _shared.env.DEBUG && console.time("Watch new files");
211
+ this.watcher.refreshWatchedFiles();
212
+ _shared.env.DEBUG && console.timeEnd("Watch new files");
213
+ }
214
+ for (let file of this.readContentPaths()){
215
+ this.context.changedContent.push(file);
216
+ }
217
+ return this.context;
218
+ }
219
+ };
220
+ async function createProcessor(args, cliConfigPath) {
221
+ var ref;
222
+ let postcss = (0, _deps.loadPostcss)();
223
+ let input = args["--input"];
224
+ let output = args["--output"];
225
+ let includePostCss = args["--postcss"];
226
+ let customPostCssPath = typeof args["--postcss"] === "string" ? args["--postcss"] : undefined;
227
+ let [beforePlugins, afterPlugins, postcssOptions] = includePostCss ? await loadPostCssPlugins(customPostCssPath) : loadBuiltinPostcssPlugins();
228
+ if (args["--purge"]) {
229
+ _log.default.warn("purge-flag-deprecated", [
230
+ "The `--purge` flag has been deprecated.",
231
+ "Please use `--content` instead."
232
+ ]);
233
+ if (!args["--content"]) {
234
+ args["--content"] = args["--purge"];
235
+ }
236
+ }
237
+ var ref1;
238
+ let content = (ref1 = (ref = args["--content"]) === null || ref === void 0 ? void 0 : ref.split(/(?<!{[^}]+),/)) !== null && ref1 !== void 0 ? ref1 : [];
239
+ let tailwindPlugin = ()=>{
240
+ return {
241
+ postcssPlugin: "tailwindcss",
242
+ Once (root, { result }) {
243
+ _shared.env.DEBUG && console.time("Compiling CSS");
244
+ (0, _processTailwindFeatures.default)(({ createContext })=>{
245
+ console.error();
246
+ console.error("Rebuilding...");
247
+ return ()=>{
248
+ return state.getContext({
249
+ createContext,
250
+ cliConfigPath,
251
+ root,
252
+ result,
253
+ content
254
+ });
255
+ };
256
+ })(root, result);
257
+ _shared.env.DEBUG && console.timeEnd("Compiling CSS");
258
+ }
259
+ };
260
+ };
261
+ tailwindPlugin.postcss = true;
262
+ let plugins = [
263
+ ...beforePlugins,
264
+ tailwindPlugin,
265
+ !args["--minify"] && _utils.formatNodes,
266
+ ...afterPlugins,
267
+ !args["--no-autoprefixer"] && (0, _deps.loadAutoprefixer)(),
268
+ args["--minify"] && (0, _deps.loadCssNano)()
269
+ ].filter(Boolean);
270
+ /** @type {import('postcss').Processor} */ // @ts-ignore
271
+ let processor = postcss(plugins);
272
+ async function readInput() {
273
+ // Piping in data, let's drain the stdin
274
+ if (input === "-") {
275
+ return (0, _utils.drainStdin)();
276
+ }
277
+ // Input file has been provided
278
+ if (input) {
279
+ return _fs.default.promises.readFile(_path.default.resolve(input), "utf8");
280
+ }
281
+ // No input file provided, fallback to default atrules
282
+ return "@tailwind base; @tailwind components; @tailwind utilities";
283
+ }
284
+ async function build() {
285
+ let start = process.hrtime.bigint();
286
+ return readInput().then((css)=>processor.process(css, {
287
+ ...postcssOptions,
288
+ from: input,
289
+ to: output
290
+ })).then((result)=>{
291
+ if (!state.watcher) {
292
+ return result;
293
+ }
294
+ _shared.env.DEBUG && console.time("Recording PostCSS dependencies");
295
+ for (let message of result.messages){
296
+ if (message.type === "dependency") {
297
+ state.contextDependencies.add(message.file);
298
+ }
299
+ }
300
+ _shared.env.DEBUG && console.timeEnd("Recording PostCSS dependencies");
301
+ // TODO: This needs to be in a different spot
302
+ _shared.env.DEBUG && console.time("Watch new files");
303
+ state.watcher.refreshWatchedFiles();
304
+ _shared.env.DEBUG && console.timeEnd("Watch new files");
305
+ return result;
306
+ }).then((result)=>{
307
+ if (!output) {
308
+ process.stdout.write(result.css);
309
+ return;
310
+ }
311
+ return Promise.all([
312
+ (0, _utils.outputFile)(result.opts.to, result.css),
313
+ result.map && (0, _utils.outputFile)(result.opts.to + ".map", result.map.toString())
314
+ ]);
315
+ }).then(()=>{
316
+ let end = process.hrtime.bigint();
317
+ console.error();
318
+ console.error("Done in", (end - start) / BigInt(1e6) + "ms.");
319
+ }).then(()=>{}, (err)=>{
320
+ // TODO: If an initial build fails we can't easily pick up any PostCSS dependencies
321
+ // that were collected before the error occurred
322
+ // The result is not stored on the error so we have to store it externally
323
+ // and pull the messages off of it here somehow
324
+ // This results in a less than ideal DX because the watcher will not pick up
325
+ // changes to imported CSS if one of them caused an error during the initial build
326
+ // If you fix it and then save the main CSS file so there's no error
327
+ // The watcher will start watching the imported CSS files and will be
328
+ // resilient to future errors.
329
+ console.error(err);
330
+ });
331
+ }
332
+ /**
333
+ * @param {{file: string, content(): Promise<string>, extension: string}[]} changes
334
+ */ async function parseChanges(changes) {
335
+ return Promise.all(changes.map(async (change)=>({
336
+ content: await change.content(),
337
+ extension: change.extension
338
+ })));
339
+ }
340
+ if (input !== undefined && input !== "-") {
341
+ state.contextDependencies.add(_path.default.resolve(input));
342
+ }
343
+ return {
344
+ build,
345
+ watch: async ()=>{
346
+ state.watcher = (0, _watchingJs.createWatcher)(args, {
347
+ state,
348
+ /**
349
+ * @param {{file: string, content(): Promise<string>, extension: string}[]} changes
350
+ */ async rebuild (changes) {
351
+ let needsNewContext = changes.some((change)=>{
352
+ return state.configDependencies.has(change.file) || state.contextDependencies.has(change.file);
353
+ });
354
+ if (needsNewContext) {
355
+ state.context = null;
356
+ } else {
357
+ for (let change of (await parseChanges(changes))){
358
+ state.changedContent.push(change);
359
+ }
360
+ }
361
+ return build();
362
+ }
363
+ });
364
+ await build();
365
+ }
366
+ };
367
+ }
@@ -0,0 +1,78 @@
1
+ // @ts-check
2
+ "use strict";
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ function _export(target, all) {
7
+ for(var name in all)Object.defineProperty(target, name, {
8
+ enumerable: true,
9
+ get: all[name]
10
+ });
11
+ }
12
+ _export(exports, {
13
+ indentRecursive: ()=>indentRecursive,
14
+ formatNodes: ()=>formatNodes,
15
+ readFileWithRetries: ()=>readFileWithRetries,
16
+ drainStdin: ()=>drainStdin,
17
+ outputFile: ()=>outputFile
18
+ });
19
+ const _fs = /*#__PURE__*/ _interopRequireDefault(require("fs"));
20
+ const _path = /*#__PURE__*/ _interopRequireDefault(require("path"));
21
+ function _interopRequireDefault(obj) {
22
+ return obj && obj.__esModule ? obj : {
23
+ default: obj
24
+ };
25
+ }
26
+ function indentRecursive(node, indent = 0) {
27
+ node.each && node.each((child, i)=>{
28
+ if (!child.raws.before || !child.raws.before.trim() || child.raws.before.includes("\n")) {
29
+ child.raws.before = `\n${node.type !== "rule" && i > 0 ? "\n" : ""}${" ".repeat(indent)}`;
30
+ }
31
+ child.raws.after = `\n${" ".repeat(indent)}`;
32
+ indentRecursive(child, indent + 1);
33
+ });
34
+ }
35
+ function formatNodes(root) {
36
+ indentRecursive(root);
37
+ if (root.first) {
38
+ root.first.raws.before = "";
39
+ }
40
+ }
41
+ async function readFileWithRetries(path, tries = 5) {
42
+ for(let n = 0; n <= tries; n++){
43
+ try {
44
+ return await _fs.default.promises.readFile(path, "utf8");
45
+ } catch (err) {
46
+ if (n !== tries) {
47
+ if (err.code === "ENOENT" || err.code === "EBUSY") {
48
+ await new Promise((resolve)=>setTimeout(resolve, 10));
49
+ continue;
50
+ }
51
+ }
52
+ throw err;
53
+ }
54
+ }
55
+ }
56
+ function drainStdin() {
57
+ return new Promise((resolve, reject)=>{
58
+ let result = "";
59
+ process.stdin.on("data", (chunk)=>{
60
+ result += chunk;
61
+ });
62
+ process.stdin.on("end", ()=>resolve(result));
63
+ process.stdin.on("error", (err)=>reject(err));
64
+ });
65
+ }
66
+ async function outputFile(file, newContents) {
67
+ try {
68
+ let currentContents = await _fs.default.promises.readFile(file, "utf8");
69
+ if (currentContents === newContents) {
70
+ return; // Skip writing the file
71
+ }
72
+ } catch {}
73
+ // Write the file
74
+ await _fs.default.promises.mkdir(_path.default.dirname(file), {
75
+ recursive: true
76
+ });
77
+ await _fs.default.promises.writeFile(file, newContents, "utf8");
78
+ }
@@ -0,0 +1,178 @@
1
+ // @ts-check
2
+ "use strict";
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "createWatcher", {
7
+ enumerable: true,
8
+ get: ()=>createWatcher
9
+ });
10
+ const _chokidar = /*#__PURE__*/ _interopRequireDefault(require("chokidar"));
11
+ const _fs = /*#__PURE__*/ _interopRequireDefault(require("fs"));
12
+ const _micromatch = /*#__PURE__*/ _interopRequireDefault(require("micromatch"));
13
+ const _normalizePath = /*#__PURE__*/ _interopRequireDefault(require("normalize-path"));
14
+ const _path = /*#__PURE__*/ _interopRequireDefault(require("path"));
15
+ const _utilsJs = require("./utils.js");
16
+ function _interopRequireDefault(obj) {
17
+ return obj && obj.__esModule ? obj : {
18
+ default: obj
19
+ };
20
+ }
21
+ function createWatcher(args, { state , rebuild }) {
22
+ let shouldPoll = args["--poll"];
23
+ let shouldCoalesceWriteEvents = shouldPoll || process.platform === "win32";
24
+ // Polling interval in milliseconds
25
+ // Used only when polling or coalescing add/change events on Windows
26
+ let pollInterval = 10;
27
+ let watcher = _chokidar.default.watch([], {
28
+ // Force checking for atomic writes in all situations
29
+ // This causes chokidar to wait up to 100ms for a file to re-added after it's been unlinked
30
+ // This only works when watching directories though
31
+ atomic: true,
32
+ usePolling: shouldPoll,
33
+ interval: shouldPoll ? pollInterval : undefined,
34
+ ignoreInitial: true,
35
+ awaitWriteFinish: shouldCoalesceWriteEvents ? {
36
+ stabilityThreshold: 50,
37
+ pollInterval: pollInterval
38
+ } : false
39
+ });
40
+ // A queue of rebuilds, file reads, etc… to run
41
+ let chain = Promise.resolve();
42
+ /**
43
+ * A list of files that have been changed since the last rebuild
44
+ *
45
+ * @type {{file: string, content: () => Promise<string>, extension: string}[]}
46
+ */ let changedContent = [];
47
+ /**
48
+ * A list of files for which a rebuild has already been queued.
49
+ * This is used to prevent duplicate rebuilds when multiple events are fired for the same file.
50
+ * The rebuilt file is cleared from this list when it's associated rebuild has _started_
51
+ * This is because if the file is changed during a rebuild it won't trigger a new rebuild which it should
52
+ **/ let pendingRebuilds = new Set();
53
+ let _timer;
54
+ let _reject;
55
+ /**
56
+ * Rebuilds the changed files and resolves when the rebuild is
57
+ * complete regardless of whether it was successful or not
58
+ */ async function rebuildAndContinue() {
59
+ let changes = changedContent.splice(0);
60
+ // There are no changes to rebuild so we can just do nothing
61
+ if (changes.length === 0) {
62
+ return Promise.resolve();
63
+ }
64
+ // Clear all pending rebuilds for the about-to-be-built files
65
+ changes.forEach((change)=>pendingRebuilds.delete(change.file));
66
+ // Resolve the promise even when the rebuild fails
67
+ return rebuild(changes).then(()=>{}, ()=>{});
68
+ }
69
+ /**
70
+ *
71
+ * @param {*} file
72
+ * @param {(() => Promise<string>) | null} content
73
+ * @param {boolean} skipPendingCheck
74
+ * @returns {Promise<void>}
75
+ */ function recordChangedFile(file, content = null, skipPendingCheck = false) {
76
+ file = _path.default.resolve(file);
77
+ // Applications like Vim/Neovim fire both rename and change events in succession for atomic writes
78
+ // In that case rebuild has already been queued by rename, so can be skipped in change
79
+ if (pendingRebuilds.has(file) && !skipPendingCheck) {
80
+ return Promise.resolve();
81
+ }
82
+ // Mark that a rebuild of this file is going to happen
83
+ // It MUST happen synchronously before the rebuild is queued for this to be effective
84
+ pendingRebuilds.add(file);
85
+ changedContent.push({
86
+ file,
87
+ content: content !== null && content !== void 0 ? content : ()=>_fs.default.promises.readFile(file, "utf8"),
88
+ extension: _path.default.extname(file).slice(1)
89
+ });
90
+ if (_timer) {
91
+ clearTimeout(_timer);
92
+ _reject();
93
+ }
94
+ // If a rebuild is already in progress we don't want to start another one until the 10ms timer has expired
95
+ chain = chain.then(()=>new Promise((resolve, reject)=>{
96
+ _timer = setTimeout(resolve, 10);
97
+ _reject = reject;
98
+ }));
99
+ // Resolves once this file has been rebuilt (or the rebuild for this file has failed)
100
+ // This queues as many rebuilds as there are changed files
101
+ // But those rebuilds happen after some delay
102
+ // And will immediately resolve if there are no changes
103
+ chain = chain.then(rebuildAndContinue, rebuildAndContinue);
104
+ return chain;
105
+ }
106
+ watcher.on("change", (file)=>recordChangedFile(file));
107
+ watcher.on("add", (file)=>recordChangedFile(file));
108
+ // Restore watching any files that are "removed"
109
+ // This can happen when a file is pseudo-atomically replaced (a copy is created, overwritten, the old one is unlinked, and the new one is renamed)
110
+ // TODO: An an optimization we should allow removal when the config changes
111
+ watcher.on("unlink", (file)=>{
112
+ file = (0, _normalizePath.default)(file);
113
+ // Only re-add the file if it's not covered by a dynamic pattern
114
+ if (!_micromatch.default.some([
115
+ file
116
+ ], state.contentPatterns.dynamic)) {
117
+ watcher.add(file);
118
+ }
119
+ });
120
+ // Some applications such as Visual Studio (but not VS Code)
121
+ // will only fire a rename event for atomic writes and not a change event
122
+ // This is very likely a chokidar bug but it's one we need to work around
123
+ // We treat this as a change event and rebuild the CSS
124
+ watcher.on("raw", (evt, filePath, meta)=>{
125
+ if (evt !== "rename") {
126
+ return;
127
+ }
128
+ let watchedPath = meta.watchedPath;
129
+ // Watched path might be the file itself
130
+ // Or the directory it is in
131
+ filePath = watchedPath.endsWith(filePath) ? watchedPath : _path.default.join(watchedPath, filePath);
132
+ // Skip this event since the files it is for does not match any of the registered content globs
133
+ if (!_micromatch.default.some([
134
+ filePath
135
+ ], state.contentPatterns.all)) {
136
+ return;
137
+ }
138
+ // Skip since we've already queued a rebuild for this file that hasn't happened yet
139
+ if (pendingRebuilds.has(filePath)) {
140
+ return;
141
+ }
142
+ // We'll go ahead and add the file to the pending rebuilds list here
143
+ // It'll be removed when the rebuild starts unless the read fails
144
+ // which will be taken care of as well
145
+ pendingRebuilds.add(filePath);
146
+ async function enqueue() {
147
+ try {
148
+ // We need to read the file as early as possible outside of the chain
149
+ // because it may be gone by the time we get to it. doing the read
150
+ // immediately increases the chance that the file is still there
151
+ let content = await (0, _utilsJs.readFileWithRetries)(_path.default.resolve(filePath));
152
+ if (content === undefined) {
153
+ return;
154
+ }
155
+ // This will push the rebuild onto the chain
156
+ // We MUST skip the rebuild check here otherwise the rebuild will never happen on Linux
157
+ // This is because the order of events and timing is different on Linux
158
+ // @ts-ignore: TypeScript isn't picking up that content is a string here
159
+ await recordChangedFile(filePath, ()=>content, true);
160
+ } catch {
161
+ // If reading the file fails, it's was probably a deleted temporary file
162
+ // So we can ignore it and no rebuild is needed
163
+ }
164
+ }
165
+ enqueue().then(()=>{
166
+ // If the file read fails we still need to make sure the file isn't stuck in the pending rebuilds list
167
+ pendingRebuilds.delete(filePath);
168
+ });
169
+ });
170
+ return {
171
+ fswatcher: watcher,
172
+ refreshWatchedFiles () {
173
+ watcher.add(Array.from(state.contextDependencies));
174
+ watcher.add(Array.from(state.configDependencies));
175
+ watcher.add(state.contentPatterns.all);
176
+ }
177
+ };
178
+ }