@tbela99/css-parser 1.4.1 → 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 -9
- package/dist/index.cjs +19 -12
- package/dist/index.d.ts +12 -9
- package/dist/lib/ast/minify.js +5 -1
- 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
|
@@ -23205,6 +23205,7 @@
|
|
|
23205
23205
|
const combinators = ['+', '>', '~', '||', '|'];
|
|
23206
23206
|
const definedPropertySettings = { configurable: true, enumerable: false, writable: true };
|
|
23207
23207
|
const notEndingWith = ['(', '['].concat(combinators);
|
|
23208
|
+
const rules = [exports.EnumToken.AtRuleNodeType, exports.EnumToken.RuleNodeType, exports.EnumToken.AtRuleTokenType, exports.EnumToken.KeyFramesRuleNodeType];
|
|
23208
23209
|
// @ts-ignore
|
|
23209
23210
|
const features = Object.values(allFeatures).sort((a, b) => a.ordering - b.ordering);
|
|
23210
23211
|
/**
|
|
@@ -23256,6 +23257,9 @@
|
|
|
23256
23257
|
if ((feature.processMode & exports.FeatureWalkMode.Pre) === 0 || (feature.accept != null && !feature.accept.has(parent.typ))) {
|
|
23257
23258
|
continue;
|
|
23258
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
|
+
}
|
|
23259
23263
|
const result = feature.run(replacement, options, parent.parent ?? ast, context, exports.FeatureWalkMode.Pre);
|
|
23260
23264
|
if (result != null) {
|
|
23261
23265
|
replacement = result;
|
|
@@ -24636,14 +24640,18 @@
|
|
|
24636
24640
|
})(exports.ResponseType || (exports.ResponseType = {}));
|
|
24637
24641
|
|
|
24638
24642
|
/**
|
|
24639
|
-
*
|
|
24643
|
+
* load file or url
|
|
24640
24644
|
* @param url
|
|
24641
|
-
* @param
|
|
24642
|
-
*
|
|
24645
|
+
* @param currentDirectory
|
|
24643
24646
|
* @param responseType
|
|
24644
|
-
* @
|
|
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
|
+
* ```
|
|
24645
24653
|
*/
|
|
24646
|
-
async function load(url,
|
|
24654
|
+
async function load(url, currentDirectory = '.', responseType = false) {
|
|
24647
24655
|
if (typeof responseType == 'boolean') {
|
|
24648
24656
|
responseType = responseType ? exports.ResponseType.ReadableStream : exports.ResponseType.Text;
|
|
24649
24657
|
}
|
|
@@ -24651,11 +24659,11 @@
|
|
|
24651
24659
|
if (matchUrl.test(url)) {
|
|
24652
24660
|
t = new URL(url);
|
|
24653
24661
|
}
|
|
24654
|
-
else if (
|
|
24655
|
-
t = new URL(url,
|
|
24662
|
+
else if (currentDirectory != null && matchUrl.test(currentDirectory)) {
|
|
24663
|
+
t = new URL(url, currentDirectory);
|
|
24656
24664
|
}
|
|
24657
24665
|
else {
|
|
24658
|
-
const path = resolve(url,
|
|
24666
|
+
const path = resolve(url, currentDirectory).absolute;
|
|
24659
24667
|
t = new URL(path, self.origin);
|
|
24660
24668
|
}
|
|
24661
24669
|
return fetch(t, t.origin != self.origin ? { mode: 'cors' } : {}).then(async (response) => {
|
|
@@ -24665,7 +24673,7 @@
|
|
|
24665
24673
|
if (responseType == exports.ResponseType.ArrayBuffer) {
|
|
24666
24674
|
return response.arrayBuffer();
|
|
24667
24675
|
}
|
|
24668
|
-
return responseType == exports.ResponseType.ReadableStream ? response.body :
|
|
24676
|
+
return responseType == exports.ResponseType.ReadableStream ? response.body : response.text();
|
|
24669
24677
|
});
|
|
24670
24678
|
}
|
|
24671
24679
|
/**
|
package/dist/index.cjs
CHANGED
|
@@ -23391,6 +23391,7 @@ var allFeatures = /*#__PURE__*/Object.freeze({
|
|
|
23391
23391
|
const combinators = ['+', '>', '~', '||', '|'];
|
|
23392
23392
|
const definedPropertySettings = { configurable: true, enumerable: false, writable: true };
|
|
23393
23393
|
const notEndingWith = ['(', '['].concat(combinators);
|
|
23394
|
+
const rules = [exports.EnumToken.AtRuleNodeType, exports.EnumToken.RuleNodeType, exports.EnumToken.AtRuleTokenType, exports.EnumToken.KeyFramesRuleNodeType];
|
|
23394
23395
|
// @ts-ignore
|
|
23395
23396
|
const features = Object.values(allFeatures).sort((a, b) => a.ordering - b.ordering);
|
|
23396
23397
|
/**
|
|
@@ -23442,6 +23443,9 @@ function minify(ast, options = {}, recursive = false, errors, nestingContent, co
|
|
|
23442
23443
|
if ((feature.processMode & exports.FeatureWalkMode.Pre) === 0 || (feature.accept != null && !feature.accept.has(parent.typ))) {
|
|
23443
23444
|
continue;
|
|
23444
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
|
+
}
|
|
23445
23449
|
const result = feature.run(replacement, options, parent.parent ?? ast, context, exports.FeatureWalkMode.Pre);
|
|
23446
23450
|
if (result != null) {
|
|
23447
23451
|
replacement = result;
|
|
@@ -24637,16 +24641,19 @@ function replaceCompoundLiteral(selector, replace) {
|
|
|
24637
24641
|
}
|
|
24638
24642
|
|
|
24639
24643
|
/**
|
|
24640
|
-
* load file or url
|
|
24644
|
+
* load file or url
|
|
24641
24645
|
* @param url
|
|
24642
|
-
* @param
|
|
24646
|
+
* @param currentDirectory
|
|
24643
24647
|
* @param responseType
|
|
24644
24648
|
* @throws Error file not found
|
|
24645
24649
|
*
|
|
24646
|
-
*
|
|
24650
|
+
* ```ts
|
|
24651
|
+
* import {load, ResponseType} from '@tbela99/css-parser';
|
|
24652
|
+
* const result = await load(file, '.', ResponseType.ArrayBuffer) as ArrayBuffer;
|
|
24653
|
+
* ```
|
|
24647
24654
|
*/
|
|
24648
|
-
async function load(url,
|
|
24649
|
-
const resolved = resolve(url,
|
|
24655
|
+
async function load(url, currentDirectory = '.', responseType = false) {
|
|
24656
|
+
const resolved = resolve(url, currentDirectory);
|
|
24650
24657
|
if (typeof responseType == 'boolean') {
|
|
24651
24658
|
responseType = responseType ? exports.ResponseType.ReadableStream : exports.ResponseType.Text;
|
|
24652
24659
|
}
|
|
@@ -24658,18 +24665,18 @@ async function load(url, currentFile = '.', responseType = false) {
|
|
|
24658
24665
|
if (responseType == exports.ResponseType.ArrayBuffer) {
|
|
24659
24666
|
return response.arrayBuffer();
|
|
24660
24667
|
}
|
|
24661
|
-
return responseType == exports.ResponseType.ReadableStream ? response.body :
|
|
24668
|
+
return responseType == exports.ResponseType.ReadableStream ? response.body : response.text();
|
|
24662
24669
|
});
|
|
24663
24670
|
}
|
|
24664
24671
|
try {
|
|
24665
|
-
if (responseType == exports.ResponseType.Text) {
|
|
24666
|
-
return promises.readFile(resolved.absolute, 'utf-8');
|
|
24667
|
-
}
|
|
24668
|
-
if (responseType == exports.ResponseType.ArrayBuffer) {
|
|
24669
|
-
return promises.readFile(resolved.absolute).then(buffer => buffer.buffer);
|
|
24670
|
-
}
|
|
24671
24672
|
const stats = await promises.lstat(resolved.absolute);
|
|
24672
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
|
+
}
|
|
24673
24680
|
return node_stream.Readable.toWeb(node_fs.createReadStream(resolved.absolute, {
|
|
24674
24681
|
encoding: 'utf-8',
|
|
24675
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/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
|
},
|