@tbela99/css-parser 1.4.0 → 1.4.2
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.
- package/CHANGELOG.md +4 -0
- package/README.md +39 -0
- package/dist/index-umd-web.js +17 -11
- package/dist/index.cjs +19 -14
- package/dist/index.d.ts +12 -9
- package/dist/lib/ast/minify.js +5 -1
- package/dist/lib/parser/parse.js +0 -2
- package/dist/node.js +16 -13
- package/dist/web.js +13 -9
- package/package.json +4 -2
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -365,6 +365,45 @@ console.debug(render(result.ast.chi[0].chi[1].chi[1], {withParents: true}));
|
|
|
365
365
|
|
|
366
366
|
```
|
|
367
367
|
|
|
368
|
+
### CSS Modules
|
|
369
|
+
|
|
370
|
+
CSS modules features are fully supported. refer to the [CSS modules](https://tbela99.github.io/css-parser/docs/documents/Guide.CSS_modules.html) documentation for more information.
|
|
371
|
+
|
|
372
|
+
```javascript
|
|
373
|
+
import {transform} from '@tbela99/css-parser';
|
|
374
|
+
|
|
375
|
+
const css = `
|
|
376
|
+
.table {
|
|
377
|
+
border-collapse: collapse;
|
|
378
|
+
width: 100%;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
.table td, .table th {
|
|
382
|
+
border: 1px solid #ddd;
|
|
383
|
+
padding: 8px;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
.table tr:nth-child(even){background-color: #f2f2f2;}
|
|
387
|
+
|
|
388
|
+
.table tr:hover {background-color: #ddd;}
|
|
389
|
+
|
|
390
|
+
.table th {
|
|
391
|
+
padding-top: 12px;
|
|
392
|
+
padding-bottom: 12px;
|
|
393
|
+
text-align: left;
|
|
394
|
+
background-color: #4CAF50;
|
|
395
|
+
color: white;
|
|
396
|
+
}
|
|
397
|
+
`;
|
|
398
|
+
|
|
399
|
+
const result = await transform(css, {module: true});
|
|
400
|
+
|
|
401
|
+
// css code
|
|
402
|
+
console.log(result.code);
|
|
403
|
+
// css mapping
|
|
404
|
+
console.log(result.mapping);
|
|
405
|
+
```
|
|
406
|
+
|
|
368
407
|
### Convert colors
|
|
369
408
|
|
|
370
409
|
```javascript
|
package/dist/index-umd-web.js
CHANGED
|
@@ -19291,8 +19291,6 @@
|
|
|
19291
19291
|
val: tokens.slice(offset)
|
|
19292
19292
|
});
|
|
19293
19293
|
delete node.tokens;
|
|
19294
|
-
// @ts-ignore
|
|
19295
|
-
delete node.raw;
|
|
19296
19294
|
context.chi.push(node);
|
|
19297
19295
|
return null;
|
|
19298
19296
|
}
|
|
@@ -23207,6 +23205,7 @@
|
|
|
23207
23205
|
const combinators = ['+', '>', '~', '||', '|'];
|
|
23208
23206
|
const definedPropertySettings = { configurable: true, enumerable: false, writable: true };
|
|
23209
23207
|
const notEndingWith = ['(', '['].concat(combinators);
|
|
23208
|
+
const rules = [exports.EnumToken.AtRuleNodeType, exports.EnumToken.RuleNodeType, exports.EnumToken.AtRuleTokenType, exports.EnumToken.KeyFramesRuleNodeType];
|
|
23210
23209
|
// @ts-ignore
|
|
23211
23210
|
const features = Object.values(allFeatures).sort((a, b) => a.ordering - b.ordering);
|
|
23212
23211
|
/**
|
|
@@ -23258,6 +23257,9 @@
|
|
|
23258
23257
|
if ((feature.processMode & exports.FeatureWalkMode.Pre) === 0 || (feature.accept != null && !feature.accept.has(parent.typ))) {
|
|
23259
23258
|
continue;
|
|
23260
23259
|
}
|
|
23260
|
+
if (rules.includes(replacement.typ) && !Array.isArray(replacement.tokens)) {
|
|
23261
|
+
Object.defineProperty(replacement, 'tokens', { ...definedPropertySettings, value: parseString(replacement.typ == exports.EnumToken.RuleNodeType || replacement.typ == exports.EnumToken.KeyFramesRuleNodeType ? replacement.sel : replacement.val) });
|
|
23262
|
+
}
|
|
23261
23263
|
const result = feature.run(replacement, options, parent.parent ?? ast, context, exports.FeatureWalkMode.Pre);
|
|
23262
23264
|
if (result != null) {
|
|
23263
23265
|
replacement = result;
|
|
@@ -24638,14 +24640,18 @@
|
|
|
24638
24640
|
})(exports.ResponseType || (exports.ResponseType = {}));
|
|
24639
24641
|
|
|
24640
24642
|
/**
|
|
24641
|
-
*
|
|
24643
|
+
* load file or url
|
|
24642
24644
|
* @param url
|
|
24643
|
-
* @param
|
|
24644
|
-
*
|
|
24645
|
+
* @param currentDirectory
|
|
24645
24646
|
* @param responseType
|
|
24646
|
-
* @
|
|
24647
|
+
* @throws Error file not found
|
|
24648
|
+
*
|
|
24649
|
+
* ```ts
|
|
24650
|
+
* import {load, ResponseType} from '@tbela99/css-parser';
|
|
24651
|
+
* const result = await load(file, '.', ResponseType.ArrayBuffer) as ArrayBuffer;
|
|
24652
|
+
* ```
|
|
24647
24653
|
*/
|
|
24648
|
-
async function load(url,
|
|
24654
|
+
async function load(url, currentDirectory = '.', responseType = false) {
|
|
24649
24655
|
if (typeof responseType == 'boolean') {
|
|
24650
24656
|
responseType = responseType ? exports.ResponseType.ReadableStream : exports.ResponseType.Text;
|
|
24651
24657
|
}
|
|
@@ -24653,11 +24659,11 @@
|
|
|
24653
24659
|
if (matchUrl.test(url)) {
|
|
24654
24660
|
t = new URL(url);
|
|
24655
24661
|
}
|
|
24656
|
-
else if (
|
|
24657
|
-
t = new URL(url,
|
|
24662
|
+
else if (currentDirectory != null && matchUrl.test(currentDirectory)) {
|
|
24663
|
+
t = new URL(url, currentDirectory);
|
|
24658
24664
|
}
|
|
24659
24665
|
else {
|
|
24660
|
-
const path = resolve(url,
|
|
24666
|
+
const path = resolve(url, currentDirectory).absolute;
|
|
24661
24667
|
t = new URL(path, self.origin);
|
|
24662
24668
|
}
|
|
24663
24669
|
return fetch(t, t.origin != self.origin ? { mode: 'cors' } : {}).then(async (response) => {
|
|
@@ -24667,7 +24673,7 @@
|
|
|
24667
24673
|
if (responseType == exports.ResponseType.ArrayBuffer) {
|
|
24668
24674
|
return response.arrayBuffer();
|
|
24669
24675
|
}
|
|
24670
|
-
return responseType == exports.ResponseType.ReadableStream ? response.body :
|
|
24676
|
+
return responseType == exports.ResponseType.ReadableStream ? response.body : response.text();
|
|
24671
24677
|
});
|
|
24672
24678
|
}
|
|
24673
24679
|
/**
|
package/dist/index.cjs
CHANGED
|
@@ -19494,8 +19494,6 @@ function parseNode(results, context, options, errors, src, map, rawTokens, stats
|
|
|
19494
19494
|
val: tokens.slice(offset)
|
|
19495
19495
|
});
|
|
19496
19496
|
delete node.tokens;
|
|
19497
|
-
// @ts-ignore
|
|
19498
|
-
delete node.raw;
|
|
19499
19497
|
context.chi.push(node);
|
|
19500
19498
|
return null;
|
|
19501
19499
|
}
|
|
@@ -23393,6 +23391,7 @@ var allFeatures = /*#__PURE__*/Object.freeze({
|
|
|
23393
23391
|
const combinators = ['+', '>', '~', '||', '|'];
|
|
23394
23392
|
const definedPropertySettings = { configurable: true, enumerable: false, writable: true };
|
|
23395
23393
|
const notEndingWith = ['(', '['].concat(combinators);
|
|
23394
|
+
const rules = [exports.EnumToken.AtRuleNodeType, exports.EnumToken.RuleNodeType, exports.EnumToken.AtRuleTokenType, exports.EnumToken.KeyFramesRuleNodeType];
|
|
23396
23395
|
// @ts-ignore
|
|
23397
23396
|
const features = Object.values(allFeatures).sort((a, b) => a.ordering - b.ordering);
|
|
23398
23397
|
/**
|
|
@@ -23444,6 +23443,9 @@ function minify(ast, options = {}, recursive = false, errors, nestingContent, co
|
|
|
23444
23443
|
if ((feature.processMode & exports.FeatureWalkMode.Pre) === 0 || (feature.accept != null && !feature.accept.has(parent.typ))) {
|
|
23445
23444
|
continue;
|
|
23446
23445
|
}
|
|
23446
|
+
if (rules.includes(replacement.typ) && !Array.isArray(replacement.tokens)) {
|
|
23447
|
+
Object.defineProperty(replacement, 'tokens', { ...definedPropertySettings, value: parseString(replacement.typ == exports.EnumToken.RuleNodeType || replacement.typ == exports.EnumToken.KeyFramesRuleNodeType ? replacement.sel : replacement.val) });
|
|
23448
|
+
}
|
|
23447
23449
|
const result = feature.run(replacement, options, parent.parent ?? ast, context, exports.FeatureWalkMode.Pre);
|
|
23448
23450
|
if (result != null) {
|
|
23449
23451
|
replacement = result;
|
|
@@ -24639,16 +24641,19 @@ function replaceCompoundLiteral(selector, replace) {
|
|
|
24639
24641
|
}
|
|
24640
24642
|
|
|
24641
24643
|
/**
|
|
24642
|
-
* load file or url
|
|
24644
|
+
* load file or url
|
|
24643
24645
|
* @param url
|
|
24644
|
-
* @param
|
|
24646
|
+
* @param currentDirectory
|
|
24645
24647
|
* @param responseType
|
|
24646
24648
|
* @throws Error file not found
|
|
24647
24649
|
*
|
|
24648
|
-
*
|
|
24650
|
+
* ```ts
|
|
24651
|
+
* import {load, ResponseType} from '@tbela99/css-parser';
|
|
24652
|
+
* const result = await load(file, '.', ResponseType.ArrayBuffer) as ArrayBuffer;
|
|
24653
|
+
* ```
|
|
24649
24654
|
*/
|
|
24650
|
-
async function load(url,
|
|
24651
|
-
const resolved = resolve(url,
|
|
24655
|
+
async function load(url, currentDirectory = '.', responseType = false) {
|
|
24656
|
+
const resolved = resolve(url, currentDirectory);
|
|
24652
24657
|
if (typeof responseType == 'boolean') {
|
|
24653
24658
|
responseType = responseType ? exports.ResponseType.ReadableStream : exports.ResponseType.Text;
|
|
24654
24659
|
}
|
|
@@ -24660,18 +24665,18 @@ async function load(url, currentFile = '.', responseType = false) {
|
|
|
24660
24665
|
if (responseType == exports.ResponseType.ArrayBuffer) {
|
|
24661
24666
|
return response.arrayBuffer();
|
|
24662
24667
|
}
|
|
24663
|
-
return responseType == exports.ResponseType.ReadableStream ? response.body :
|
|
24668
|
+
return responseType == exports.ResponseType.ReadableStream ? response.body : response.text();
|
|
24664
24669
|
});
|
|
24665
24670
|
}
|
|
24666
24671
|
try {
|
|
24667
|
-
if (responseType == exports.ResponseType.Text) {
|
|
24668
|
-
return promises.readFile(resolved.absolute, 'utf-8');
|
|
24669
|
-
}
|
|
24670
|
-
if (responseType == exports.ResponseType.ArrayBuffer) {
|
|
24671
|
-
return promises.readFile(resolved.absolute).then(buffer => buffer.buffer);
|
|
24672
|
-
}
|
|
24673
24672
|
const stats = await promises.lstat(resolved.absolute);
|
|
24674
24673
|
if (stats.isFile()) {
|
|
24674
|
+
if (responseType == exports.ResponseType.Text) {
|
|
24675
|
+
return promises.readFile(resolved.absolute, 'utf-8');
|
|
24676
|
+
}
|
|
24677
|
+
if (responseType == exports.ResponseType.ArrayBuffer) {
|
|
24678
|
+
return promises.readFile(resolved.absolute).then(buffer => buffer.buffer);
|
|
24679
|
+
}
|
|
24675
24680
|
return node_stream.Readable.toWeb(node_fs.createReadStream(resolved.absolute, {
|
|
24676
24681
|
encoding: 'utf-8',
|
|
24677
24682
|
highWaterMark: 64 * 1024
|
package/dist/index.d.ts
CHANGED
|
@@ -3521,11 +3521,11 @@ export declare interface ParserOptions extends MinifyOptions, MinifyFeatureOptio
|
|
|
3521
3521
|
/**
|
|
3522
3522
|
* url and file loader
|
|
3523
3523
|
* @param url
|
|
3524
|
-
* @param
|
|
3525
|
-
* @param
|
|
3524
|
+
* @param currentDirectory
|
|
3525
|
+
* @param responseType
|
|
3526
3526
|
*
|
|
3527
3527
|
*/
|
|
3528
|
-
load?: (url: string,
|
|
3528
|
+
load?: (url: string, currentDirectory: string, responseType?: boolean | ResponseType) => Promise<string | ArrayBuffer | ReadableStream<Uint8Array<ArrayBufferLike>>>;
|
|
3529
3529
|
/**
|
|
3530
3530
|
* get directory name
|
|
3531
3531
|
* @param path
|
|
@@ -4027,7 +4027,7 @@ declare function resolve(url: string, currentDirectory: string, cwd?: string): {
|
|
|
4027
4027
|
/**
|
|
4028
4028
|
* response type
|
|
4029
4029
|
*/
|
|
4030
|
-
declare enum ResponseType {
|
|
4030
|
+
declare enum ResponseType$1 {
|
|
4031
4031
|
/**
|
|
4032
4032
|
* return text
|
|
4033
4033
|
*/
|
|
@@ -4043,15 +4043,18 @@ declare enum ResponseType {
|
|
|
4043
4043
|
}
|
|
4044
4044
|
|
|
4045
4045
|
/**
|
|
4046
|
-
* load file or url
|
|
4046
|
+
* load file or url
|
|
4047
4047
|
* @param url
|
|
4048
|
-
* @param
|
|
4048
|
+
* @param currentDirectory
|
|
4049
4049
|
* @param responseType
|
|
4050
4050
|
* @throws Error file not found
|
|
4051
4051
|
*
|
|
4052
|
-
*
|
|
4052
|
+
* ```ts
|
|
4053
|
+
* import {load, ResponseType} from '@tbela99/css-parser';
|
|
4054
|
+
* const result = await load(file, '.', ResponseType.ArrayBuffer) as ArrayBuffer;
|
|
4055
|
+
* ```
|
|
4053
4056
|
*/
|
|
4054
|
-
declare function load(url: string,
|
|
4057
|
+
declare function load(url: string, currentDirectory?: string, responseType?: boolean | ResponseType$1): Promise<string | ArrayBuffer | ReadableStream<Uint8Array<ArrayBufferLike>>>;
|
|
4055
4058
|
/**
|
|
4056
4059
|
* render the ast tree
|
|
4057
4060
|
* @param data
|
|
@@ -4222,5 +4225,5 @@ declare function transformFile(file: string, options?: TransformOptions, asStrea
|
|
|
4222
4225
|
*/
|
|
4223
4226
|
declare function transform(css: string | ReadableStream<Uint8Array>, options?: TransformOptions): Promise<TransformResult>;
|
|
4224
4227
|
|
|
4225
|
-
export { ColorType, EnumToken, FeatureWalkMode, ModuleCaseTransformEnum, ModuleScopeEnumOptions, ResponseType, SourceMap, ValidationLevel, WalkerEvent, WalkerOptionEnum, convertColor, dirname, expand, isOkLabClose, load, mathFuncs, minify, okLabDistance, parse, parseDeclarations, parseFile, parseString, parseTokens, render, renderToken, resolve, transform, transformFile, transformFunctions, walk, walkValues };
|
|
4228
|
+
export { ColorType, EnumToken, FeatureWalkMode, ModuleCaseTransformEnum, ModuleScopeEnumOptions, ResponseType$1 as ResponseType, SourceMap, ValidationLevel, WalkerEvent, WalkerOptionEnum, convertColor, dirname, expand, isOkLabClose, load, mathFuncs, minify, okLabDistance, parse, parseDeclarations, parseFile, parseString, parseTokens, render, renderToken, resolve, transform, transformFile, transformFunctions, walk, walkValues };
|
|
4226
4229
|
export type { AddToken, AngleToken, AstAtRule, AstComment, AstDeclaration, AstInvalidAtRule, AstInvalidDeclaration, AstInvalidRule, AstKeyFrameRule, AstKeyframesAtRule, AstKeyframesRule, AstNode$1 as AstNode, AstRule, AstRuleList, AstStyleSheet, AtRuleToken, AtRuleVisitorHandler, AttrEndToken, AttrStartToken, AttrToken, Background, BackgroundAttachmentMapping, BackgroundPosition, BackgroundPositionClass, BackgroundPositionConstraints, BackgroundPositionMapping, BackgroundProperties, BackgroundRepeat, BackgroundRepeatMapping, BackgroundSize, BackgroundSizeMapping, BadCDOCommentToken, BadCommentToken, BadStringToken, BadUrlToken, BaseToken, BinaryExpressionNode, BinaryExpressionToken, BlockEndToken, BlockStartToken, Border, BorderColor, BorderColorClass, BorderProperties, BorderRadius, CDOCommentToken, ChildCombinatorToken, ClassSelectorToken, ColonToken, ColorToken, ColumnCombinatorToken, CommaToken, CommentToken, ComposesSelectorToken, ConstraintsMapping, ContainMatchToken, Context, CssVariableImportTokenType$1 as CssVariableImportTokenType, CssVariableMapTokenType, CssVariableToken$1 as CssVariableToken, DashMatchToken, DashedIdentToken, DeclarationVisitorHandler, DelimToken, DescendantCombinatorToken, DimensionToken, DivToken, EOFToken, EndMatchToken, EqualMatchToken, ErrorDescription, FlexToken, Font, FontFamily, FontProperties, FontWeight, FontWeightConstraints, FontWeightMapping, FractionToken, FrequencyToken, FunctionImageToken, FunctionToken, FunctionURLToken, GenericVisitorAstNodeHandlerMap, GenericVisitorHandler, GenericVisitorResult, GreaterThanOrEqualToken, GreaterThanToken, GridTemplateFuncToken, HashToken, IdentListToken, IdentToken, ImportantToken, IncludeMatchToken, InvalidAttrToken, InvalidClassSelectorToken, LengthToken, LessThanOrEqualToken, LessThanToken, LineHeight, ListToken, LiteralToken, LoadResult, Location, Map$1 as Map, MatchExpressionToken, MatchedSelector, MediaFeatureAndToken, MediaFeatureNotToken, MediaFeatureOnlyToken, MediaFeatureOrToken, MediaFeatureToken, MediaQueryConditionToken, MinifyFeature, MinifyFeatureOptions, MinifyOptions, ModuleOptions, MulToken, NameSpaceAttributeToken, NestingSelectorToken, NextSiblingCombinatorToken, NumberToken, OptimizedSelector, OptimizedSelectorToken, Outline, OutlineProperties, ParensEndToken, ParensStartToken, ParensToken, ParseInfo, ParseResult, ParseResultStats, ParseTokenOptions, ParserOptions, PercentageToken, Position, Prefix, PropertiesConfig, PropertiesConfigProperties, PropertyListOptions, PropertyMapType, PropertySetType, PropertyType, PseudoClassFunctionToken, PseudoClassToken, PseudoElementToken, PseudoPageToken, PurpleBackgroundAttachment, RawSelectorTokens, RenderOptions, RenderResult, ResolutionToken, ResolvedPath, RuleVisitorHandler, SemiColonToken, Separator, ShorthandDef, ShorthandMapType, ShorthandProperties, ShorthandPropertyType, ShorthandType, SourceMapObject, StartMatchToken, StringToken, SubToken, SubsequentCombinatorToken, TimeToken, TimelineFunctionToken, TimingFunctionToken, Token$1 as Token, TokenizeResult, TransformOptions, TransformResult, UnaryExpression, UnaryExpressionNode, UnclosedStringToken, UniversalSelectorToken, UrlToken, ValidationConfiguration, ValidationOptions, ValidationResult, ValidationSelectorOptions, ValidationSyntaxNode, ValidationSyntaxResult, Value, ValueVisitorHandler, VariableScopeInfo, VisitorNodeMap, WalkAttributesResult, WalkResult, WalkerFilter, WalkerOption, WalkerValueFilter, WhitespaceToken };
|
package/dist/lib/ast/minify.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { parseString, replaceToken } from '../parser/parse.js';
|
|
2
2
|
import '../parser/tokenize.js';
|
|
3
3
|
import '../parser/utils/config.js';
|
|
4
4
|
import { EnumToken } from './types.js';
|
|
@@ -13,6 +13,7 @@ import { FeatureWalkMode } from './features/type.js';
|
|
|
13
13
|
const combinators = ['+', '>', '~', '||', '|'];
|
|
14
14
|
const definedPropertySettings = { configurable: true, enumerable: false, writable: true };
|
|
15
15
|
const notEndingWith = ['(', '['].concat(combinators);
|
|
16
|
+
const rules = [EnumToken.AtRuleNodeType, EnumToken.RuleNodeType, EnumToken.AtRuleTokenType, EnumToken.KeyFramesRuleNodeType];
|
|
16
17
|
// @ts-ignore
|
|
17
18
|
const features = Object.values(index).sort((a, b) => a.ordering - b.ordering);
|
|
18
19
|
/**
|
|
@@ -64,6 +65,9 @@ function minify(ast, options = {}, recursive = false, errors, nestingContent, co
|
|
|
64
65
|
if ((feature.processMode & FeatureWalkMode.Pre) === 0 || (feature.accept != null && !feature.accept.has(parent.typ))) {
|
|
65
66
|
continue;
|
|
66
67
|
}
|
|
68
|
+
if (rules.includes(replacement.typ) && !Array.isArray(replacement.tokens)) {
|
|
69
|
+
Object.defineProperty(replacement, 'tokens', { ...definedPropertySettings, value: parseString(replacement.typ == EnumToken.RuleNodeType || replacement.typ == EnumToken.KeyFramesRuleNodeType ? replacement.sel : replacement.val) });
|
|
70
|
+
}
|
|
67
71
|
const result = feature.run(replacement, options, parent.parent ?? ast, context, FeatureWalkMode.Pre);
|
|
68
72
|
if (result != null) {
|
|
69
73
|
replacement = result;
|
package/dist/lib/parser/parse.js
CHANGED
package/dist/node.js
CHANGED
|
@@ -21,21 +21,24 @@ import './lib/validation/syntax.js';
|
|
|
21
21
|
import { resolve, matchUrl, dirname } from './lib/fs/resolve.js';
|
|
22
22
|
import { Readable } from 'node:stream';
|
|
23
23
|
import { createReadStream } from 'node:fs';
|
|
24
|
-
import {
|
|
24
|
+
import { lstat, readFile } from 'node:fs/promises';
|
|
25
25
|
import { ResponseType } from './types.js';
|
|
26
26
|
export { FeatureWalkMode } from './lib/ast/features/type.js';
|
|
27
27
|
|
|
28
28
|
/**
|
|
29
|
-
* load file or url
|
|
29
|
+
* load file or url
|
|
30
30
|
* @param url
|
|
31
|
-
* @param
|
|
31
|
+
* @param currentDirectory
|
|
32
32
|
* @param responseType
|
|
33
33
|
* @throws Error file not found
|
|
34
34
|
*
|
|
35
|
-
*
|
|
35
|
+
* ```ts
|
|
36
|
+
* import {load, ResponseType} from '@tbela99/css-parser';
|
|
37
|
+
* const result = await load(file, '.', ResponseType.ArrayBuffer) as ArrayBuffer;
|
|
38
|
+
* ```
|
|
36
39
|
*/
|
|
37
|
-
async function load(url,
|
|
38
|
-
const resolved = resolve(url,
|
|
40
|
+
async function load(url, currentDirectory = '.', responseType = false) {
|
|
41
|
+
const resolved = resolve(url, currentDirectory);
|
|
39
42
|
if (typeof responseType == 'boolean') {
|
|
40
43
|
responseType = responseType ? ResponseType.ReadableStream : ResponseType.Text;
|
|
41
44
|
}
|
|
@@ -47,18 +50,18 @@ async function load(url, currentFile = '.', responseType = false) {
|
|
|
47
50
|
if (responseType == ResponseType.ArrayBuffer) {
|
|
48
51
|
return response.arrayBuffer();
|
|
49
52
|
}
|
|
50
|
-
return responseType == ResponseType.ReadableStream ? response.body :
|
|
53
|
+
return responseType == ResponseType.ReadableStream ? response.body : response.text();
|
|
51
54
|
});
|
|
52
55
|
}
|
|
53
56
|
try {
|
|
54
|
-
if (responseType == ResponseType.Text) {
|
|
55
|
-
return readFile(resolved.absolute, 'utf-8');
|
|
56
|
-
}
|
|
57
|
-
if (responseType == ResponseType.ArrayBuffer) {
|
|
58
|
-
return readFile(resolved.absolute).then(buffer => buffer.buffer);
|
|
59
|
-
}
|
|
60
57
|
const stats = await lstat(resolved.absolute);
|
|
61
58
|
if (stats.isFile()) {
|
|
59
|
+
if (responseType == ResponseType.Text) {
|
|
60
|
+
return readFile(resolved.absolute, 'utf-8');
|
|
61
|
+
}
|
|
62
|
+
if (responseType == ResponseType.ArrayBuffer) {
|
|
63
|
+
return readFile(resolved.absolute).then(buffer => buffer.buffer);
|
|
64
|
+
}
|
|
62
65
|
return Readable.toWeb(createReadStream(resolved.absolute, {
|
|
63
66
|
encoding: 'utf-8',
|
|
64
67
|
highWaterMark: 64 * 1024
|
package/dist/web.js
CHANGED
|
@@ -22,14 +22,18 @@ import { ResponseType } from './types.js';
|
|
|
22
22
|
export { FeatureWalkMode } from './lib/ast/features/type.js';
|
|
23
23
|
|
|
24
24
|
/**
|
|
25
|
-
*
|
|
25
|
+
* load file or url
|
|
26
26
|
* @param url
|
|
27
|
-
* @param
|
|
28
|
-
*
|
|
27
|
+
* @param currentDirectory
|
|
29
28
|
* @param responseType
|
|
30
|
-
* @
|
|
29
|
+
* @throws Error file not found
|
|
30
|
+
*
|
|
31
|
+
* ```ts
|
|
32
|
+
* import {load, ResponseType} from '@tbela99/css-parser';
|
|
33
|
+
* const result = await load(file, '.', ResponseType.ArrayBuffer) as ArrayBuffer;
|
|
34
|
+
* ```
|
|
31
35
|
*/
|
|
32
|
-
async function load(url,
|
|
36
|
+
async function load(url, currentDirectory = '.', responseType = false) {
|
|
33
37
|
if (typeof responseType == 'boolean') {
|
|
34
38
|
responseType = responseType ? ResponseType.ReadableStream : ResponseType.Text;
|
|
35
39
|
}
|
|
@@ -37,11 +41,11 @@ async function load(url, currentFile = '.', responseType = false) {
|
|
|
37
41
|
if (matchUrl.test(url)) {
|
|
38
42
|
t = new URL(url);
|
|
39
43
|
}
|
|
40
|
-
else if (
|
|
41
|
-
t = new URL(url,
|
|
44
|
+
else if (currentDirectory != null && matchUrl.test(currentDirectory)) {
|
|
45
|
+
t = new URL(url, currentDirectory);
|
|
42
46
|
}
|
|
43
47
|
else {
|
|
44
|
-
const path = resolve(url,
|
|
48
|
+
const path = resolve(url, currentDirectory).absolute;
|
|
45
49
|
t = new URL(path, self.origin);
|
|
46
50
|
}
|
|
47
51
|
return fetch(t, t.origin != self.origin ? { mode: 'cors' } : {}).then(async (response) => {
|
|
@@ -51,7 +55,7 @@ async function load(url, currentFile = '.', responseType = false) {
|
|
|
51
55
|
if (responseType == ResponseType.ArrayBuffer) {
|
|
52
56
|
return response.arrayBuffer();
|
|
53
57
|
}
|
|
54
|
-
return responseType == ResponseType.ReadableStream ? response.body :
|
|
58
|
+
return responseType == ResponseType.ReadableStream ? response.body : response.text();
|
|
55
59
|
});
|
|
56
60
|
}
|
|
57
61
|
/**
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tbela99/css-parser",
|
|
3
3
|
"description": "CSS parser, minifier and validator for node and the browser",
|
|
4
|
-
"version": "v1.4.
|
|
4
|
+
"version": "v1.4.2",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./dist/node.js",
|
|
7
7
|
"./node": "./dist/node.js",
|
|
@@ -32,6 +32,8 @@
|
|
|
32
32
|
"keywords": [
|
|
33
33
|
"parser",
|
|
34
34
|
"css",
|
|
35
|
+
"modules",
|
|
36
|
+
"css-modules",
|
|
35
37
|
"css-parser",
|
|
36
38
|
"node",
|
|
37
39
|
"ast",
|
|
@@ -48,7 +50,7 @@
|
|
|
48
50
|
"streaming-parser"
|
|
49
51
|
],
|
|
50
52
|
"author": "Thierry Bela",
|
|
51
|
-
"license": "MIT
|
|
53
|
+
"license": "MIT",
|
|
52
54
|
"bugs": {
|
|
53
55
|
"url": "https://github.com/tbela99/css-parser/issues"
|
|
54
56
|
},
|