webpack 5.59.0 → 5.71.0

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.

Potentially problematic release.


This version of webpack might be problematic. Click here for more details.

Files changed (209) hide show
  1. package/README.md +1 -7
  2. package/hot/lazy-compilation-node.js +3 -1
  3. package/hot/poll.js +1 -1
  4. package/hot/signal.js +1 -1
  5. package/lib/APIPlugin.js +33 -0
  6. package/lib/BannerPlugin.js +10 -4
  7. package/lib/Cache.js +1 -1
  8. package/lib/CacheFacade.js +4 -11
  9. package/lib/Chunk.js +6 -3
  10. package/lib/ChunkGraph.js +1 -2
  11. package/lib/ChunkGroup.js +1 -1
  12. package/lib/CleanPlugin.js +81 -20
  13. package/lib/Compilation.js +179 -91
  14. package/lib/Compiler.js +86 -17
  15. package/lib/ConstPlugin.js +2 -2
  16. package/lib/ContextModule.js +142 -51
  17. package/lib/ContextModuleFactory.js +65 -25
  18. package/lib/DelegatedModule.js +1 -1
  19. package/lib/DelegatedModuleFactoryPlugin.js +1 -1
  20. package/lib/Dependency.js +17 -0
  21. package/lib/DependencyTemplate.js +9 -0
  22. package/lib/DependencyTemplates.js +1 -1
  23. package/lib/DllModule.js +1 -1
  24. package/lib/EntryOptionPlugin.js +2 -0
  25. package/lib/ErrorHelpers.js +2 -2
  26. package/lib/EvalDevToolModulePlugin.js +16 -1
  27. package/lib/EvalSourceMapDevToolPlugin.js +18 -1
  28. package/lib/ExportsInfo.js +4 -4
  29. package/lib/ExternalModule.js +94 -54
  30. package/lib/ExternalModuleFactoryPlugin.js +5 -5
  31. package/lib/FileSystemInfo.js +89 -44
  32. package/lib/Generator.js +3 -0
  33. package/lib/HookWebpackError.js +1 -1
  34. package/lib/HotModuleReplacementPlugin.js +3 -1
  35. package/lib/LoaderOptionsPlugin.js +1 -1
  36. package/lib/Module.js +27 -4
  37. package/lib/ModuleFilenameHelpers.js +8 -4
  38. package/lib/ModuleHashingError.js +29 -0
  39. package/lib/MultiCompiler.js +1 -1
  40. package/lib/MultiWatching.js +1 -1
  41. package/lib/NodeStuffPlugin.js +10 -0
  42. package/lib/NormalModule.js +41 -26
  43. package/lib/NormalModuleFactory.js +42 -37
  44. package/lib/ProgressPlugin.js +4 -5
  45. package/lib/RawModule.js +1 -1
  46. package/lib/RuntimeGlobals.js +29 -1
  47. package/lib/RuntimeModule.js +1 -1
  48. package/lib/RuntimePlugin.js +77 -1
  49. package/lib/RuntimeTemplate.js +114 -2
  50. package/lib/Template.js +2 -1
  51. package/lib/TemplatedPathPlugin.js +48 -23
  52. package/lib/WatchIgnorePlugin.js +19 -7
  53. package/lib/Watching.js +33 -19
  54. package/lib/WebpackOptionsApply.js +57 -11
  55. package/lib/asset/AssetGenerator.js +193 -63
  56. package/lib/asset/AssetModulesPlugin.js +3 -0
  57. package/lib/asset/RawDataUrlModule.js +148 -0
  58. package/lib/async-modules/AwaitDependenciesInitFragment.js +4 -4
  59. package/lib/buildChunkGraph.js +36 -6
  60. package/lib/cache/PackFileCacheStrategy.js +7 -4
  61. package/lib/cache/ResolverCachePlugin.js +90 -29
  62. package/lib/cli.js +44 -3
  63. package/lib/config/browserslistTargetHandler.js +41 -6
  64. package/lib/config/defaults.js +115 -19
  65. package/lib/config/normalization.js +9 -0
  66. package/lib/config/target.js +10 -0
  67. package/lib/container/ContainerEntryModule.js +8 -5
  68. package/lib/container/FallbackModule.js +4 -4
  69. package/lib/container/RemoteModule.js +4 -2
  70. package/lib/css/CssExportsGenerator.js +139 -0
  71. package/lib/css/CssGenerator.js +109 -0
  72. package/lib/css/CssLoadingRuntimeModule.js +440 -0
  73. package/lib/css/CssModulesPlugin.js +462 -0
  74. package/lib/css/CssParser.js +618 -0
  75. package/lib/css/walkCssTokens.js +659 -0
  76. package/lib/debug/ProfilingPlugin.js +24 -21
  77. package/lib/dependencies/AMDRequireDependency.js +6 -6
  78. package/lib/dependencies/CommonJsExportsParserPlugin.js +1 -2
  79. package/lib/dependencies/CommonJsFullRequireDependency.js +5 -1
  80. package/lib/dependencies/CommonJsImportsParserPlugin.js +5 -3
  81. package/lib/dependencies/CommonJsRequireContextDependency.js +5 -1
  82. package/lib/dependencies/ContextDependency.js +1 -0
  83. package/lib/dependencies/ContextDependencyHelpers.js +3 -3
  84. package/lib/dependencies/ContextDependencyTemplateAsRequireCall.js +4 -1
  85. package/lib/dependencies/ContextElementDependency.js +41 -3
  86. package/lib/dependencies/CreateScriptUrlDependency.js +12 -0
  87. package/lib/dependencies/CssExportDependency.js +85 -0
  88. package/lib/dependencies/CssImportDependency.js +75 -0
  89. package/lib/dependencies/CssLocalIdentifierDependency.js +119 -0
  90. package/lib/dependencies/CssSelfLocalIdentifierDependency.js +101 -0
  91. package/lib/dependencies/CssUrlDependency.js +132 -0
  92. package/lib/dependencies/ExportsInfoDependency.js +6 -0
  93. package/lib/dependencies/HarmonyAcceptImportDependency.js +5 -3
  94. package/lib/dependencies/HarmonyCompatibilityDependency.js +5 -5
  95. package/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js +95 -0
  96. package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +12 -3
  97. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +25 -17
  98. package/lib/dependencies/HarmonyExportInitFragment.js +4 -1
  99. package/lib/dependencies/HarmonyImportDependency.js +23 -0
  100. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +142 -45
  101. package/lib/dependencies/HarmonyImportSpecifierDependency.js +46 -22
  102. package/lib/dependencies/HarmonyModulesPlugin.js +10 -0
  103. package/lib/dependencies/ImportContextDependency.js +0 -2
  104. package/lib/dependencies/ImportMetaContextDependency.js +35 -0
  105. package/lib/dependencies/ImportMetaContextDependencyParserPlugin.js +252 -0
  106. package/lib/dependencies/ImportMetaContextPlugin.js +59 -0
  107. package/lib/dependencies/ImportMetaPlugin.js +22 -3
  108. package/lib/dependencies/LoaderPlugin.js +4 -2
  109. package/lib/dependencies/RequireContextDependency.js +0 -16
  110. package/lib/dependencies/RequireEnsureDependency.js +2 -2
  111. package/lib/dependencies/URLDependency.js +3 -8
  112. package/lib/dependencies/URLPlugin.js +1 -1
  113. package/lib/esm/ModuleChunkFormatPlugin.js +74 -49
  114. package/lib/esm/ModuleChunkLoadingPlugin.js +3 -1
  115. package/lib/esm/ModuleChunkLoadingRuntimeModule.js +25 -9
  116. package/lib/hmr/HotModuleReplacement.runtime.js +29 -14
  117. package/lib/hmr/JavascriptHotModuleReplacement.runtime.js +4 -3
  118. package/lib/hmr/LazyCompilationPlugin.js +54 -26
  119. package/lib/hmr/lazyCompilationBackend.js +51 -12
  120. package/lib/ids/DeterministicModuleIdsPlugin.js +55 -35
  121. package/lib/ids/HashedModuleIdsPlugin.js +11 -14
  122. package/lib/ids/IdHelpers.js +25 -11
  123. package/lib/ids/NamedModuleIdsPlugin.js +6 -9
  124. package/lib/ids/NaturalModuleIdsPlugin.js +10 -13
  125. package/lib/ids/OccurrenceModuleIdsPlugin.js +13 -10
  126. package/lib/ids/SyncModuleIdsPlugin.js +140 -0
  127. package/lib/index.js +13 -0
  128. package/lib/javascript/ArrayPushCallbackChunkFormatPlugin.js +2 -2
  129. package/lib/javascript/BasicEvaluatedExpression.js +5 -2
  130. package/lib/javascript/ChunkHelpers.js +33 -0
  131. package/lib/javascript/JavascriptGenerator.js +1 -0
  132. package/lib/javascript/JavascriptModulesPlugin.js +27 -2
  133. package/lib/javascript/JavascriptParser.js +82 -48
  134. package/lib/javascript/StartupHelpers.js +7 -30
  135. package/lib/library/AssignLibraryPlugin.js +39 -15
  136. package/lib/library/EnableLibraryPlugin.js +11 -0
  137. package/lib/library/UmdLibraryPlugin.js +5 -3
  138. package/lib/node/NodeTargetPlugin.js +3 -0
  139. package/lib/node/NodeWatchFileSystem.js +85 -31
  140. package/lib/node/ReadFileChunkLoadingRuntimeModule.js +23 -8
  141. package/lib/node/RequireChunkLoadingRuntimeModule.js +24 -9
  142. package/lib/optimize/ConcatenatedModule.js +21 -9
  143. package/lib/optimize/ModuleConcatenationPlugin.js +5 -2
  144. package/lib/optimize/SplitChunksPlugin.js +8 -1
  145. package/lib/runtime/AsyncModuleRuntimeModule.js +27 -17
  146. package/lib/runtime/BaseUriRuntimeModule.js +31 -0
  147. package/lib/runtime/CreateScriptRuntimeModule.js +36 -0
  148. package/lib/runtime/CreateScriptUrlRuntimeModule.js +9 -34
  149. package/lib/runtime/GetTrustedTypesPolicyRuntimeModule.js +76 -0
  150. package/lib/schemes/HttpUriPlugin.js +77 -14
  151. package/lib/serialization/FileMiddleware.js +44 -9
  152. package/lib/sharing/ConsumeSharedModule.js +8 -2
  153. package/lib/sharing/ConsumeSharedRuntimeModule.js +26 -5
  154. package/lib/sharing/ProvideSharedModule.js +4 -2
  155. package/lib/sharing/ShareRuntimeModule.js +1 -1
  156. package/lib/sharing/utils.js +1 -1
  157. package/lib/stats/DefaultStatsFactoryPlugin.js +113 -68
  158. package/lib/stats/DefaultStatsPrinterPlugin.js +89 -24
  159. package/lib/util/ArrayHelpers.js +30 -0
  160. package/lib/util/AsyncQueue.js +1 -1
  161. package/lib/util/compileBooleanMatcher.js +1 -1
  162. package/lib/util/create-schema-validation.js +9 -2
  163. package/lib/util/createHash.js +12 -0
  164. package/lib/util/deprecation.js +10 -2
  165. package/lib/util/deterministicGrouping.js +1 -1
  166. package/lib/util/extractUrlAndGlobal.js +3 -0
  167. package/lib/util/fs.js +11 -0
  168. package/lib/util/hash/BatchedHash.js +7 -4
  169. package/lib/util/hash/md4.js +20 -0
  170. package/lib/util/hash/wasm-hash.js +163 -0
  171. package/lib/util/hash/xxhash64.js +5 -139
  172. package/lib/util/identifier.js +65 -44
  173. package/lib/util/internalSerializables.js +15 -0
  174. package/lib/util/nonNumericOnlyHash.js +22 -0
  175. package/lib/util/semver.js +17 -10
  176. package/lib/wasm-async/AsyncWebAssemblyJavascriptGenerator.js +9 -3
  177. package/lib/web/JsonpChunkLoadingRuntimeModule.js +20 -9
  178. package/lib/webpack.js +10 -3
  179. package/lib/webworker/ImportScriptsChunkLoadingPlugin.js +3 -11
  180. package/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +33 -22
  181. package/module.d.ts +215 -0
  182. package/package.json +23 -28
  183. package/schemas/WebpackOptions.check.js +1 -1
  184. package/schemas/WebpackOptions.json +254 -29
  185. package/schemas/plugins/DllReferencePlugin.check.js +1 -1
  186. package/schemas/plugins/HashedModuleIdsPlugin.check.js +1 -1
  187. package/schemas/plugins/ProgressPlugin.check.js +1 -1
  188. package/schemas/plugins/asset/AssetGeneratorOptions.check.js +1 -1
  189. package/schemas/plugins/asset/AssetParserOptions.check.js +1 -1
  190. package/schemas/plugins/asset/AssetResourceGeneratorOptions.check.js +1 -1
  191. package/schemas/plugins/container/ContainerPlugin.check.js +1 -1
  192. package/schemas/plugins/container/ContainerPlugin.json +2 -1
  193. package/schemas/plugins/container/ContainerReferencePlugin.check.js +1 -1
  194. package/schemas/plugins/container/ContainerReferencePlugin.json +1 -0
  195. package/schemas/plugins/container/ExternalsType.check.js +1 -1
  196. package/schemas/plugins/container/ModuleFederationPlugin.check.js +1 -1
  197. package/schemas/plugins/container/ModuleFederationPlugin.json +3 -1
  198. package/schemas/plugins/css/CssGeneratorOptions.check.d.ts +7 -0
  199. package/schemas/plugins/css/CssGeneratorOptions.check.js +6 -0
  200. package/schemas/plugins/css/CssGeneratorOptions.json +3 -0
  201. package/schemas/plugins/css/CssParserOptions.check.d.ts +7 -0
  202. package/schemas/plugins/css/CssParserOptions.check.js +6 -0
  203. package/schemas/plugins/css/CssParserOptions.json +3 -0
  204. package/schemas/plugins/optimize/AggressiveSplittingPlugin.check.js +1 -1
  205. package/schemas/plugins/optimize/LimitChunkCountPlugin.check.js +1 -1
  206. package/schemas/plugins/optimize/MinChunkSizePlugin.check.js +1 -1
  207. package/schemas/plugins/schemes/HttpUriPlugin.check.js +1 -1
  208. package/schemas/plugins/schemes/HttpUriPlugin.json +4 -0
  209. package/types.d.ts +628 -179
@@ -0,0 +1,659 @@
1
+ /*
2
+ MIT License http://www.opensource.org/licenses/mit-license.php
3
+ Author Tobias Koppers @sokra
4
+ */
5
+
6
+ "use strict";
7
+
8
+ /**
9
+ * @typedef {Object} CssTokenCallbacks
10
+ * @property {function(string, number): boolean} isSelector
11
+ * @property {function(string, number, number, number, number): number=} url
12
+ * @property {function(string, number, number): number=} string
13
+ * @property {function(string, number, number): number=} leftParenthesis
14
+ * @property {function(string, number, number): number=} rightParenthesis
15
+ * @property {function(string, number, number): number=} pseudoFunction
16
+ * @property {function(string, number, number): number=} function
17
+ * @property {function(string, number, number): number=} pseudoClass
18
+ * @property {function(string, number, number): number=} atKeyword
19
+ * @property {function(string, number, number): number=} class
20
+ * @property {function(string, number, number): number=} identifier
21
+ * @property {function(string, number, number): number=} id
22
+ * @property {function(string, number, number): number=} leftCurlyBracket
23
+ * @property {function(string, number, number): number=} rightCurlyBracket
24
+ * @property {function(string, number, number): number=} semicolon
25
+ * @property {function(string, number, number): number=} comma
26
+ */
27
+
28
+ /** @typedef {function(string, number, CssTokenCallbacks): number} CharHandler */
29
+
30
+ // spec: https://drafts.csswg.org/css-syntax/
31
+
32
+ const CC_LINE_FEED = "\n".charCodeAt(0);
33
+ const CC_CARRIAGE_RETURN = "\r".charCodeAt(0);
34
+ const CC_FORM_FEED = "\f".charCodeAt(0);
35
+
36
+ const CC_TAB = "\t".charCodeAt(0);
37
+ const CC_SPACE = " ".charCodeAt(0);
38
+
39
+ const CC_SLASH = "/".charCodeAt(0);
40
+ const CC_BACK_SLASH = "\\".charCodeAt(0);
41
+ const CC_ASTERISK = "*".charCodeAt(0);
42
+
43
+ const CC_LEFT_PARENTHESIS = "(".charCodeAt(0);
44
+ const CC_RIGHT_PARENTHESIS = ")".charCodeAt(0);
45
+ const CC_LEFT_CURLY = "{".charCodeAt(0);
46
+ const CC_RIGHT_CURLY = "}".charCodeAt(0);
47
+
48
+ const CC_QUOTATION_MARK = '"'.charCodeAt(0);
49
+ const CC_APOSTROPHE = "'".charCodeAt(0);
50
+
51
+ const CC_FULL_STOP = ".".charCodeAt(0);
52
+ const CC_COLON = ":".charCodeAt(0);
53
+ const CC_SEMICOLON = ";".charCodeAt(0);
54
+ const CC_COMMA = ",".charCodeAt(0);
55
+ const CC_PERCENTAGE = "%".charCodeAt(0);
56
+ const CC_AT_SIGN = "@".charCodeAt(0);
57
+
58
+ const CC_LOW_LINE = "_".charCodeAt(0);
59
+ const CC_LOWER_A = "a".charCodeAt(0);
60
+ const CC_LOWER_U = "u".charCodeAt(0);
61
+ const CC_LOWER_E = "e".charCodeAt(0);
62
+ const CC_LOWER_Z = "z".charCodeAt(0);
63
+ const CC_UPPER_A = "A".charCodeAt(0);
64
+ const CC_UPPER_E = "E".charCodeAt(0);
65
+ const CC_UPPER_Z = "Z".charCodeAt(0);
66
+ const CC_0 = "0".charCodeAt(0);
67
+ const CC_9 = "9".charCodeAt(0);
68
+
69
+ const CC_NUMBER_SIGN = "#".charCodeAt(0);
70
+ const CC_PLUS_SIGN = "+".charCodeAt(0);
71
+ const CC_HYPHEN_MINUS = "-".charCodeAt(0);
72
+
73
+ const CC_LESS_THAN_SIGN = "<".charCodeAt(0);
74
+ const CC_GREATER_THAN_SIGN = ">".charCodeAt(0);
75
+
76
+ const _isNewLine = cc => {
77
+ return (
78
+ cc === CC_LINE_FEED || cc === CC_CARRIAGE_RETURN || cc === CC_FORM_FEED
79
+ );
80
+ };
81
+
82
+ /** @type {CharHandler} */
83
+ const consumeSpace = (input, pos, callbacks) => {
84
+ let cc;
85
+ do {
86
+ pos++;
87
+ cc = input.charCodeAt(pos);
88
+ } while (_isWhiteSpace(cc));
89
+ return pos;
90
+ };
91
+
92
+ const _isWhiteSpace = cc => {
93
+ return (
94
+ cc === CC_LINE_FEED ||
95
+ cc === CC_CARRIAGE_RETURN ||
96
+ cc === CC_FORM_FEED ||
97
+ cc === CC_TAB ||
98
+ cc === CC_SPACE
99
+ );
100
+ };
101
+
102
+ /** @type {CharHandler} */
103
+ const consumeSingleCharToken = (input, pos, callbacks) => {
104
+ return pos + 1;
105
+ };
106
+
107
+ /** @type {CharHandler} */
108
+ const consumePotentialComment = (input, pos, callbacks) => {
109
+ pos++;
110
+ if (pos === input.length) return pos;
111
+ let cc = input.charCodeAt(pos);
112
+ if (cc !== CC_ASTERISK) return pos;
113
+ for (;;) {
114
+ pos++;
115
+ if (pos === input.length) return pos;
116
+ cc = input.charCodeAt(pos);
117
+ while (cc === CC_ASTERISK) {
118
+ pos++;
119
+ if (pos === input.length) return pos;
120
+ cc = input.charCodeAt(pos);
121
+ if (cc === CC_SLASH) return pos + 1;
122
+ }
123
+ }
124
+ };
125
+
126
+ /** @type {function(number): CharHandler} */
127
+ const consumeString = end => (input, pos, callbacks) => {
128
+ const start = pos;
129
+ pos = _consumeString(input, pos, end);
130
+ if (callbacks.string !== undefined) {
131
+ pos = callbacks.string(input, start, pos);
132
+ }
133
+ return pos;
134
+ };
135
+
136
+ const _consumeString = (input, pos, end) => {
137
+ pos++;
138
+ for (;;) {
139
+ if (pos === input.length) return pos;
140
+ const cc = input.charCodeAt(pos);
141
+ if (cc === end) return pos + 1;
142
+ if (_isNewLine(cc)) {
143
+ // bad string
144
+ return pos;
145
+ }
146
+ if (cc === CC_BACK_SLASH) {
147
+ // we don't need to fully parse the escaped code point
148
+ // just skip over a potential new line
149
+ pos++;
150
+ if (pos === input.length) return pos;
151
+ pos++;
152
+ } else {
153
+ pos++;
154
+ }
155
+ }
156
+ };
157
+
158
+ const _isIdentifierStartCode = cc => {
159
+ return (
160
+ cc === CC_LOW_LINE ||
161
+ (cc >= CC_LOWER_A && cc <= CC_LOWER_Z) ||
162
+ (cc >= CC_UPPER_A && cc <= CC_UPPER_Z) ||
163
+ cc > 0x80
164
+ );
165
+ };
166
+
167
+ const _isDigit = cc => {
168
+ return cc >= CC_0 && cc <= CC_9;
169
+ };
170
+
171
+ const _startsIdentifier = (input, pos) => {
172
+ const cc = input.charCodeAt(pos);
173
+ if (cc === CC_HYPHEN_MINUS) {
174
+ if (pos === input.length) return false;
175
+ const cc = input.charCodeAt(pos + 1);
176
+ if (cc === CC_HYPHEN_MINUS) return true;
177
+ if (cc === CC_BACK_SLASH) {
178
+ const cc = input.charCodeAt(pos + 2);
179
+ return !_isNewLine(cc);
180
+ }
181
+ return _isIdentifierStartCode(cc);
182
+ }
183
+ if (cc === CC_BACK_SLASH) {
184
+ const cc = input.charCodeAt(pos + 1);
185
+ return !_isNewLine(cc);
186
+ }
187
+ return _isIdentifierStartCode(cc);
188
+ };
189
+
190
+ /** @type {CharHandler} */
191
+ const consumeNumberSign = (input, pos, callbacks) => {
192
+ const start = pos;
193
+ pos++;
194
+ if (pos === input.length) return pos;
195
+ if (callbacks.isSelector(input, pos) && _startsIdentifier(input, pos)) {
196
+ pos = _consumeIdentifier(input, pos);
197
+ if (callbacks.id !== undefined) {
198
+ return callbacks.id(input, start, pos);
199
+ }
200
+ }
201
+ return pos;
202
+ };
203
+
204
+ /** @type {CharHandler} */
205
+ const consumeMinus = (input, pos, callbacks) => {
206
+ const start = pos;
207
+ pos++;
208
+ if (pos === input.length) return pos;
209
+ const cc = input.charCodeAt(pos);
210
+ if (cc === CC_FULL_STOP || _isDigit(cc)) {
211
+ return consumeNumericToken(input, pos, callbacks);
212
+ } else if (cc === CC_HYPHEN_MINUS) {
213
+ pos++;
214
+ if (pos === input.length) return pos;
215
+ const cc = input.charCodeAt(pos);
216
+ if (cc === CC_GREATER_THAN_SIGN) {
217
+ return pos + 1;
218
+ } else {
219
+ pos = _consumeIdentifier(input, pos);
220
+ if (callbacks.identifier !== undefined) {
221
+ return callbacks.identifier(input, start, pos);
222
+ }
223
+ }
224
+ } else if (cc === CC_BACK_SLASH) {
225
+ if (pos + 1 === input.length) return pos;
226
+ const cc = input.charCodeAt(pos + 1);
227
+ if (_isNewLine(cc)) return pos;
228
+ pos = _consumeIdentifier(input, pos);
229
+ if (callbacks.identifier !== undefined) {
230
+ return callbacks.identifier(input, start, pos);
231
+ }
232
+ } else if (_isIdentifierStartCode(cc)) {
233
+ pos++;
234
+ pos = _consumeIdentifier(input, pos);
235
+ if (callbacks.identifier !== undefined) {
236
+ return callbacks.identifier(input, start, pos);
237
+ }
238
+ }
239
+ return pos;
240
+ };
241
+
242
+ /** @type {CharHandler} */
243
+ const consumeDot = (input, pos, callbacks) => {
244
+ const start = pos;
245
+ pos++;
246
+ if (pos === input.length) return pos;
247
+ const cc = input.charCodeAt(pos);
248
+ if (_isDigit(cc)) return consumeNumericToken(input, pos - 2, callbacks);
249
+ if (!callbacks.isSelector(input, pos) || !_startsIdentifier(input, pos))
250
+ return pos;
251
+ pos = _consumeIdentifier(input, pos);
252
+ if (callbacks.class !== undefined) return callbacks.class(input, start, pos);
253
+ return pos;
254
+ };
255
+
256
+ /** @type {CharHandler} */
257
+ const consumeNumericToken = (input, pos, callbacks) => {
258
+ pos = _consumeNumber(input, pos);
259
+ if (pos === input.length) return pos;
260
+ if (_startsIdentifier(input, pos)) return _consumeIdentifier(input, pos);
261
+ const cc = input.charCodeAt(pos);
262
+ if (cc === CC_PERCENTAGE) return pos + 1;
263
+ return pos;
264
+ };
265
+
266
+ /** @type {CharHandler} */
267
+ const consumeOtherIdentifier = (input, pos, callbacks) => {
268
+ const start = pos;
269
+ pos = _consumeIdentifier(input, pos);
270
+ if (
271
+ pos !== input.length &&
272
+ !callbacks.isSelector(input, pos) &&
273
+ input.charCodeAt(pos) === CC_LEFT_PARENTHESIS
274
+ ) {
275
+ pos++;
276
+ if (callbacks.function !== undefined) {
277
+ return callbacks.function(input, start, pos);
278
+ }
279
+ } else {
280
+ if (callbacks.identifier !== undefined) {
281
+ return callbacks.identifier(input, start, pos);
282
+ }
283
+ }
284
+ return pos;
285
+ };
286
+
287
+ /** @type {CharHandler} */
288
+ const consumePotentialUrl = (input, pos, callbacks) => {
289
+ const start = pos;
290
+ pos = _consumeIdentifier(input, pos);
291
+ if (pos === start + 3 && input.slice(start, pos + 1) === "url(") {
292
+ pos++;
293
+ let cc = input.charCodeAt(pos);
294
+ while (_isWhiteSpace(cc)) {
295
+ pos++;
296
+ if (pos === input.length) return pos;
297
+ cc = input.charCodeAt(pos);
298
+ }
299
+ if (cc === CC_QUOTATION_MARK || cc === CC_APOSTROPHE) {
300
+ pos++;
301
+ const contentStart = pos;
302
+ pos = _consumeString(input, pos, cc);
303
+ const contentEnd = pos - 1;
304
+ cc = input.charCodeAt(pos);
305
+ while (_isWhiteSpace(cc)) {
306
+ pos++;
307
+ if (pos === input.length) return pos;
308
+ cc = input.charCodeAt(pos);
309
+ }
310
+ if (cc !== CC_RIGHT_PARENTHESIS) return pos;
311
+ pos++;
312
+ if (callbacks.url !== undefined)
313
+ return callbacks.url(input, start, pos, contentStart, contentEnd);
314
+ return pos;
315
+ } else {
316
+ const contentStart = pos;
317
+ let contentEnd;
318
+ for (;;) {
319
+ if (cc === CC_BACK_SLASH) {
320
+ pos++;
321
+ if (pos === input.length) return pos;
322
+ pos++;
323
+ } else if (_isWhiteSpace(cc)) {
324
+ contentEnd = pos;
325
+ do {
326
+ pos++;
327
+ if (pos === input.length) return pos;
328
+ cc = input.charCodeAt(pos);
329
+ } while (_isWhiteSpace(cc));
330
+ if (cc !== CC_RIGHT_PARENTHESIS) return pos;
331
+ pos++;
332
+ if (callbacks.url !== undefined) {
333
+ return callbacks.url(input, start, pos, contentStart, contentEnd);
334
+ }
335
+ return pos;
336
+ } else if (cc === CC_RIGHT_PARENTHESIS) {
337
+ contentEnd = pos;
338
+ pos++;
339
+ if (callbacks.url !== undefined) {
340
+ return callbacks.url(input, start, pos, contentStart, contentEnd);
341
+ }
342
+ return pos;
343
+ } else if (cc === CC_LEFT_PARENTHESIS) {
344
+ return pos;
345
+ } else {
346
+ pos++;
347
+ }
348
+ if (pos === input.length) return pos;
349
+ cc = input.charCodeAt(pos);
350
+ }
351
+ }
352
+ } else {
353
+ if (callbacks.identifier !== undefined) {
354
+ return callbacks.identifier(input, start, pos);
355
+ }
356
+ return pos;
357
+ }
358
+ };
359
+
360
+ /** @type {CharHandler} */
361
+ const consumePotentialPseudo = (input, pos, callbacks) => {
362
+ const start = pos;
363
+ pos++;
364
+ if (!callbacks.isSelector(input, pos) || !_startsIdentifier(input, pos))
365
+ return pos;
366
+ pos = _consumeIdentifier(input, pos);
367
+ let cc = input.charCodeAt(pos);
368
+ if (cc === CC_LEFT_PARENTHESIS) {
369
+ pos++;
370
+ if (callbacks.pseudoFunction !== undefined) {
371
+ return callbacks.pseudoFunction(input, start, pos);
372
+ }
373
+ return pos;
374
+ }
375
+ if (callbacks.pseudoClass !== undefined) {
376
+ return callbacks.pseudoClass(input, start, pos);
377
+ }
378
+ return pos;
379
+ };
380
+
381
+ /** @type {CharHandler} */
382
+ const consumeLeftParenthesis = (input, pos, callbacks) => {
383
+ pos++;
384
+ if (callbacks.leftParenthesis !== undefined) {
385
+ return callbacks.leftParenthesis(input, pos - 1, pos);
386
+ }
387
+ return pos;
388
+ };
389
+
390
+ /** @type {CharHandler} */
391
+ const consumeRightParenthesis = (input, pos, callbacks) => {
392
+ pos++;
393
+ if (callbacks.rightParenthesis !== undefined) {
394
+ return callbacks.rightParenthesis(input, pos - 1, pos);
395
+ }
396
+ return pos;
397
+ };
398
+
399
+ /** @type {CharHandler} */
400
+ const consumeLeftCurlyBracket = (input, pos, callbacks) => {
401
+ pos++;
402
+ if (callbacks.leftCurlyBracket !== undefined) {
403
+ return callbacks.leftCurlyBracket(input, pos - 1, pos);
404
+ }
405
+ return pos;
406
+ };
407
+
408
+ /** @type {CharHandler} */
409
+ const consumeRightCurlyBracket = (input, pos, callbacks) => {
410
+ pos++;
411
+ if (callbacks.rightCurlyBracket !== undefined) {
412
+ return callbacks.rightCurlyBracket(input, pos - 1, pos);
413
+ }
414
+ return pos;
415
+ };
416
+
417
+ /** @type {CharHandler} */
418
+ const consumeSemicolon = (input, pos, callbacks) => {
419
+ pos++;
420
+ if (callbacks.semicolon !== undefined) {
421
+ return callbacks.semicolon(input, pos - 1, pos);
422
+ }
423
+ return pos;
424
+ };
425
+
426
+ /** @type {CharHandler} */
427
+ const consumeComma = (input, pos, callbacks) => {
428
+ pos++;
429
+ if (callbacks.comma !== undefined) {
430
+ return callbacks.comma(input, pos - 1, pos);
431
+ }
432
+ return pos;
433
+ };
434
+
435
+ const _consumeIdentifier = (input, pos) => {
436
+ for (;;) {
437
+ const cc = input.charCodeAt(pos);
438
+ if (cc === CC_BACK_SLASH) {
439
+ pos++;
440
+ if (pos === input.length) return pos;
441
+ pos++;
442
+ } else if (
443
+ _isIdentifierStartCode(cc) ||
444
+ _isDigit(cc) ||
445
+ cc === CC_HYPHEN_MINUS
446
+ ) {
447
+ pos++;
448
+ } else {
449
+ return pos;
450
+ }
451
+ }
452
+ };
453
+
454
+ const _consumeNumber = (input, pos) => {
455
+ pos++;
456
+ if (pos === input.length) return pos;
457
+ let cc = input.charCodeAt(pos);
458
+ while (_isDigit(cc)) {
459
+ pos++;
460
+ if (pos === input.length) return pos;
461
+ cc = input.charCodeAt(pos);
462
+ }
463
+ if (cc === CC_FULL_STOP && pos + 1 !== input.length) {
464
+ const next = input.charCodeAt(pos + 1);
465
+ if (_isDigit(next)) {
466
+ pos += 2;
467
+ cc = input.charCodeAt(pos);
468
+ while (_isDigit(cc)) {
469
+ pos++;
470
+ if (pos === input.length) return pos;
471
+ cc = input.charCodeAt(pos);
472
+ }
473
+ }
474
+ }
475
+ if (cc === CC_LOWER_E || cc === CC_UPPER_E) {
476
+ if (pos + 1 !== input.length) {
477
+ const next = input.charCodeAt(pos + 2);
478
+ if (_isDigit(next)) {
479
+ pos += 2;
480
+ } else if (
481
+ (next === CC_HYPHEN_MINUS || next === CC_PLUS_SIGN) &&
482
+ pos + 2 !== input.length
483
+ ) {
484
+ const next = input.charCodeAt(pos + 2);
485
+ if (_isDigit(next)) {
486
+ pos += 3;
487
+ } else {
488
+ return pos;
489
+ }
490
+ } else {
491
+ return pos;
492
+ }
493
+ }
494
+ } else {
495
+ return pos;
496
+ }
497
+ cc = input.charCodeAt(pos);
498
+ while (_isDigit(cc)) {
499
+ pos++;
500
+ if (pos === input.length) return pos;
501
+ cc = input.charCodeAt(pos);
502
+ }
503
+ return pos;
504
+ };
505
+
506
+ /** @type {CharHandler} */
507
+ const consumeLessThan = (input, pos, callbacks) => {
508
+ if (input.slice(pos + 1, pos + 4) === "!--") return pos + 4;
509
+ return pos + 1;
510
+ };
511
+
512
+ /** @type {CharHandler} */
513
+ const consumeAt = (input, pos, callbacks) => {
514
+ const start = pos;
515
+ pos++;
516
+ if (pos === input.length) return pos;
517
+ if (_startsIdentifier(input, pos)) {
518
+ pos = _consumeIdentifier(input, pos);
519
+ if (callbacks.atKeyword !== undefined) {
520
+ pos = callbacks.atKeyword(input, start, pos);
521
+ }
522
+ }
523
+ return pos;
524
+ };
525
+
526
+ const CHAR_MAP = Array.from({ length: 0x80 }, (_, cc) => {
527
+ // https://drafts.csswg.org/css-syntax/#consume-token
528
+ switch (cc) {
529
+ case CC_LINE_FEED:
530
+ case CC_CARRIAGE_RETURN:
531
+ case CC_FORM_FEED:
532
+ case CC_TAB:
533
+ case CC_SPACE:
534
+ return consumeSpace;
535
+ case CC_QUOTATION_MARK:
536
+ case CC_APOSTROPHE:
537
+ return consumeString(cc);
538
+ case CC_NUMBER_SIGN:
539
+ return consumeNumberSign;
540
+ case CC_SLASH:
541
+ return consumePotentialComment;
542
+ // case CC_LEFT_SQUARE:
543
+ // case CC_RIGHT_SQUARE:
544
+ // case CC_COMMA:
545
+ // case CC_COLON:
546
+ // return consumeSingleCharToken;
547
+ case CC_COMMA:
548
+ return consumeComma;
549
+ case CC_SEMICOLON:
550
+ return consumeSemicolon;
551
+ case CC_LEFT_PARENTHESIS:
552
+ return consumeLeftParenthesis;
553
+ case CC_RIGHT_PARENTHESIS:
554
+ return consumeRightParenthesis;
555
+ case CC_LEFT_CURLY:
556
+ return consumeLeftCurlyBracket;
557
+ case CC_RIGHT_CURLY:
558
+ return consumeRightCurlyBracket;
559
+ case CC_COLON:
560
+ return consumePotentialPseudo;
561
+ case CC_PLUS_SIGN:
562
+ return consumeNumericToken;
563
+ case CC_FULL_STOP:
564
+ return consumeDot;
565
+ case CC_HYPHEN_MINUS:
566
+ return consumeMinus;
567
+ case CC_LESS_THAN_SIGN:
568
+ return consumeLessThan;
569
+ case CC_AT_SIGN:
570
+ return consumeAt;
571
+ case CC_LOWER_U:
572
+ return consumePotentialUrl;
573
+ case CC_LOW_LINE:
574
+ return consumeOtherIdentifier;
575
+ default:
576
+ if (_isDigit(cc)) return consumeNumericToken;
577
+ if (
578
+ (cc >= CC_LOWER_A && cc <= CC_LOWER_Z) ||
579
+ (cc >= CC_UPPER_A && cc <= CC_UPPER_Z)
580
+ ) {
581
+ return consumeOtherIdentifier;
582
+ }
583
+ return consumeSingleCharToken;
584
+ }
585
+ });
586
+
587
+ /**
588
+ * @param {string} input input css
589
+ * @param {CssTokenCallbacks} callbacks callbacks
590
+ * @returns {void}
591
+ */
592
+ module.exports = (input, callbacks) => {
593
+ let pos = 0;
594
+ while (pos < input.length) {
595
+ const cc = input.charCodeAt(pos);
596
+ if (cc < 0x80) {
597
+ pos = CHAR_MAP[cc](input, pos, callbacks);
598
+ } else {
599
+ pos++;
600
+ }
601
+ }
602
+ };
603
+
604
+ module.exports.eatComments = (input, pos) => {
605
+ loop: for (;;) {
606
+ const cc = input.charCodeAt(pos);
607
+ if (cc === CC_SLASH) {
608
+ if (pos === input.length) return pos;
609
+ let cc = input.charCodeAt(pos + 1);
610
+ if (cc !== CC_ASTERISK) return pos;
611
+ pos++;
612
+ for (;;) {
613
+ pos++;
614
+ if (pos === input.length) return pos;
615
+ cc = input.charCodeAt(pos);
616
+ while (cc === CC_ASTERISK) {
617
+ pos++;
618
+ if (pos === input.length) return pos;
619
+ cc = input.charCodeAt(pos);
620
+ if (cc === CC_SLASH) {
621
+ pos++;
622
+ continue loop;
623
+ }
624
+ }
625
+ }
626
+ }
627
+ return pos;
628
+ }
629
+ };
630
+
631
+ module.exports.eatWhitespaceAndComments = (input, pos) => {
632
+ loop: for (;;) {
633
+ const cc = input.charCodeAt(pos);
634
+ if (cc === CC_SLASH) {
635
+ if (pos === input.length) return pos;
636
+ let cc = input.charCodeAt(pos + 1);
637
+ if (cc !== CC_ASTERISK) return pos;
638
+ pos++;
639
+ for (;;) {
640
+ pos++;
641
+ if (pos === input.length) return pos;
642
+ cc = input.charCodeAt(pos);
643
+ while (cc === CC_ASTERISK) {
644
+ pos++;
645
+ if (pos === input.length) return pos;
646
+ cc = input.charCodeAt(pos);
647
+ if (cc === CC_SLASH) {
648
+ pos++;
649
+ continue loop;
650
+ }
651
+ }
652
+ }
653
+ } else if (_isWhiteSpace(cc)) {
654
+ pos++;
655
+ continue;
656
+ }
657
+ return pos;
658
+ }
659
+ };