@localnerve/sass-asset-functions 6.8.0 → 6.10.0-rc.1
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 +7 -0
- package/README.md +53 -0
- package/cjs/index.cjs +2 -2
- package/cjs/lib/legacyAPI.cjs +13 -0
- package/cjs/lib/modernAPIAsync.cjs +19 -0
- package/cjs/lib/modernAPISync.cjs +13 -0
- package/cjs/lib/processor.cjs +15 -3
- package/cjs/lib/types.cjs +106 -0
- package/lib/legacyAPI.js +13 -0
- package/lib/modernAPIAsync.js +19 -0
- package/lib/modernAPISync.js +14 -0
- package/lib/processor.js +10 -1
- package/lib/types.js +101 -0
- package/package.json +8 -6
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -33,6 +33,7 @@ This module provides some of the asset functions that came with [Compass](http:/
|
|
|
33
33
|
- `font-url($filename: null, $only-path: false)`
|
|
34
34
|
- `font-files($filenames...)`
|
|
35
35
|
- `inline-image($filename: null, $mime-type: null)`
|
|
36
|
+
- `lookup($keys...)`
|
|
36
37
|
|
|
37
38
|
## Usage
|
|
38
39
|
|
|
@@ -75,6 +76,7 @@ All options are optional.
|
|
|
75
76
|
| `http_fonts_path` | String | The path to images as seen from the web (nothing to do with http). Defaults to `/fonts` |
|
|
76
77
|
| `asset_cache_buster` | Function | Signature (http_path, real_path, callback(new_url)). Supply to perform url transform for `image-url` or `font-url`, presumably for asset cache busting, but useful for any change to the url path (before fragment) |
|
|
77
78
|
| `asset_host` | Function | Signature (http_path, callback(new_url)). Supply to perform url transform for `image-url` or `font-url`, presumably to define an asset host, but useful for any change to the url before the path |
|
|
79
|
+
| `data` | Object | An object of arbitrary data to reference at build-time. Defaults to `(empty)` |
|
|
78
80
|
|
|
79
81
|
### Examples
|
|
80
82
|
|
|
@@ -148,6 +150,57 @@ const result = sass.compile(scss_filename, {
|
|
|
148
150
|
});
|
|
149
151
|
```
|
|
150
152
|
|
|
153
|
+
#### `lookup`: a function to use arbitrary data in scss stylesheets
|
|
154
|
+
|
|
155
|
+
This function retrieves arbitrary build-time data for reference in stylesheet compilation. Could be an asset name, prefix, or could be a whole list or map of things. Here's the list of javascript types supported (everything returns SassNull):
|
|
156
|
+
* Boolean => SassBoolean
|
|
157
|
+
* Number => SassNumber
|
|
158
|
+
* String => SassString
|
|
159
|
+
* Array => SassList
|
|
160
|
+
* Set => SassList
|
|
161
|
+
* Object => SassMap
|
|
162
|
+
* Map => SassMap
|
|
163
|
+
|
|
164
|
+
```js
|
|
165
|
+
const result = sass.compile(scss_filename, {
|
|
166
|
+
functions: assetFunctions({
|
|
167
|
+
data: {
|
|
168
|
+
'hero-image-names': {
|
|
169
|
+
big: 'hero-1920x300.webp',
|
|
170
|
+
medium: 'hero-1440x300.webp',
|
|
171
|
+
small: 'hero-1024x300.webp'
|
|
172
|
+
},
|
|
173
|
+
nested: {
|
|
174
|
+
process: true,
|
|
175
|
+
'process-map': {
|
|
176
|
+
one: 'one',
|
|
177
|
+
two: 'two'
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
})
|
|
182
|
+
});
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
```scss
|
|
186
|
+
$hero-images: lookup('hero-image-names');
|
|
187
|
+
@each $key, $val in $hero-images {
|
|
188
|
+
.hero-image-#{$key} {
|
|
189
|
+
background-image: image-url($val);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
$nested-process: lookup('nested', 'process');
|
|
194
|
+
$nested-data: lookup('nested', 'process-map');
|
|
195
|
+
@if $nested-process {
|
|
196
|
+
@each $key, $val in $nested-data {
|
|
197
|
+
.process-#{$key} {
|
|
198
|
+
content: '#{$val}';
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
```
|
|
203
|
+
|
|
151
204
|
##### A more advanced example:
|
|
152
205
|
|
|
153
206
|
Here we include the file's hexdigest in the path, using the [`hexdigest`](https://github.com/koenpunt/node-hexdigest) module.
|
package/cjs/index.cjs
CHANGED
|
@@ -9,9 +9,9 @@ var _processor = _interopRequireDefault(require("./lib/processor.cjs"));
|
|
|
9
9
|
var _legacyAPI = _interopRequireDefault(require("./lib/legacyAPI.cjs"));
|
|
10
10
|
var _modernAPISync = _interopRequireDefault(require("./lib/modernAPISync.cjs"));
|
|
11
11
|
var _modernAPIAsync = _interopRequireDefault(require("./lib/modernAPIAsync.cjs"));
|
|
12
|
-
function _interopRequireDefault(
|
|
12
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
13
13
|
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
14
|
-
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u &&
|
|
14
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
15
15
|
/**
|
|
16
16
|
* Sass asset function suite.
|
|
17
17
|
*
|
package/cjs/lib/legacyAPI.cjs
CHANGED
|
@@ -4,6 +4,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = legacyAPI;
|
|
7
|
+
var _types = require("./types.cjs");
|
|
7
8
|
/**
|
|
8
9
|
* Custom Functions for sass Legacy JS API.
|
|
9
10
|
*
|
|
@@ -57,6 +58,18 @@ function legacyAPI(sass, processor) {
|
|
|
57
58
|
}
|
|
58
59
|
done(list);
|
|
59
60
|
});
|
|
61
|
+
},
|
|
62
|
+
'lookup($keys...)': (list, done) => {
|
|
63
|
+
let len = list.getLength(),
|
|
64
|
+
i = 0;
|
|
65
|
+
const keys = [];
|
|
66
|
+
for (; i < len; ++i) {
|
|
67
|
+
keys[i] = list.getValue(i).getValue();
|
|
68
|
+
}
|
|
69
|
+
processor.lookup(keys, value => {
|
|
70
|
+
const result = (0, _types.convertToSassTypesLegacy)(sass, value);
|
|
71
|
+
done(result);
|
|
72
|
+
});
|
|
60
73
|
}
|
|
61
74
|
};
|
|
62
75
|
}
|
|
@@ -4,6 +4,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = modernAPIAsync;
|
|
7
|
+
var _types = require("./types.cjs");
|
|
7
8
|
/**
|
|
8
9
|
* Custom Functions for asynchronous, sass modern JS API (compileAsync).
|
|
9
10
|
*
|
|
@@ -105,6 +106,24 @@ function modernAPIAsync(sass, processor) {
|
|
|
105
106
|
reject(err);
|
|
106
107
|
}
|
|
107
108
|
});
|
|
109
|
+
},
|
|
110
|
+
'lookup($keys...)': args => {
|
|
111
|
+
return new Promise((resolve, reject) => {
|
|
112
|
+
try {
|
|
113
|
+
const input = args[0].asList;
|
|
114
|
+
const keys = [];
|
|
115
|
+
for (let i = 0; i < input.size; ++i) {
|
|
116
|
+
keys[i] = input.get(i).assertString().text;
|
|
117
|
+
}
|
|
118
|
+
let result = sass.sassNull;
|
|
119
|
+
processor.lookup(keys, value => {
|
|
120
|
+
result = (0, _types.convertToSassTypesModern)(sass, value);
|
|
121
|
+
});
|
|
122
|
+
resolve(result);
|
|
123
|
+
} catch (err) {
|
|
124
|
+
reject(err);
|
|
125
|
+
}
|
|
126
|
+
});
|
|
108
127
|
}
|
|
109
128
|
};
|
|
110
129
|
}
|
|
@@ -4,6 +4,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = modernAPISync;
|
|
7
|
+
var _types = require("./types.cjs");
|
|
7
8
|
/**
|
|
8
9
|
* Custom Functions for synchronous, sass modern JS API (compile).
|
|
9
10
|
*
|
|
@@ -79,6 +80,18 @@ function modernAPISync(sass, processor) {
|
|
|
79
80
|
}
|
|
80
81
|
});
|
|
81
82
|
return new sass.SassList(result);
|
|
83
|
+
},
|
|
84
|
+
'lookup($keys...)': args => {
|
|
85
|
+
const input = args[0].asList;
|
|
86
|
+
const keys = [];
|
|
87
|
+
for (let i = 0; i < input.size; ++i) {
|
|
88
|
+
keys[i] = input.get(i).assertString().text;
|
|
89
|
+
}
|
|
90
|
+
let result = sass.sassNull;
|
|
91
|
+
processor.lookup(keys, value => {
|
|
92
|
+
result = (0, _types.convertToSassTypesModern)(sass, value);
|
|
93
|
+
});
|
|
94
|
+
return result;
|
|
82
95
|
}
|
|
83
96
|
};
|
|
84
97
|
}
|
package/cjs/lib/processor.cjs
CHANGED
|
@@ -9,9 +9,9 @@ var path = _interopRequireWildcard(require("node:path"));
|
|
|
9
9
|
var url = _interopRequireWildcard(require("node:url"));
|
|
10
10
|
var _mimeTypes = _interopRequireDefault(require("mime-types"));
|
|
11
11
|
var _imageSize = require("image-size");
|
|
12
|
-
function _interopRequireDefault(
|
|
12
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
13
13
|
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
14
|
-
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u &&
|
|
14
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
15
15
|
/**
|
|
16
16
|
* Internal processor for the asset function suite.
|
|
17
17
|
*
|
|
@@ -37,7 +37,12 @@ const FONT_TYPES = {
|
|
|
37
37
|
};
|
|
38
38
|
class Processor {
|
|
39
39
|
constructor(options) {
|
|
40
|
-
this.options =
|
|
40
|
+
this.options = {
|
|
41
|
+
...{
|
|
42
|
+
data: {}
|
|
43
|
+
},
|
|
44
|
+
...options
|
|
45
|
+
};
|
|
41
46
|
const {
|
|
42
47
|
images_path = defaultPaths.images_path,
|
|
43
48
|
fonts_path = defaultPaths.fonts_path,
|
|
@@ -191,5 +196,12 @@ class Processor {
|
|
|
191
196
|
this.font_url(file, complete(i, type));
|
|
192
197
|
}
|
|
193
198
|
}
|
|
199
|
+
lookup(keys, done) {
|
|
200
|
+
let data = this.options.data;
|
|
201
|
+
for (let key of keys) {
|
|
202
|
+
data = data[key];
|
|
203
|
+
}
|
|
204
|
+
done(data !== this.options.data ? data : null);
|
|
205
|
+
}
|
|
194
206
|
}
|
|
195
207
|
exports.default = Processor;
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.convertToSassTypesLegacy = convertToSassTypesLegacy;
|
|
7
|
+
exports.convertToSassTypesModern = convertToSassTypesModern;
|
|
8
|
+
var _immutable = require("immutable");
|
|
9
|
+
/**
|
|
10
|
+
* Convert javascript types to sass types.
|
|
11
|
+
*
|
|
12
|
+
* Supported:
|
|
13
|
+
* Boolean => Sass.Boolean
|
|
14
|
+
* Number => Sass.Number
|
|
15
|
+
* String => Sass.String
|
|
16
|
+
* Array => Sass.List
|
|
17
|
+
* Set => Sass.List
|
|
18
|
+
* Object => Sass.Map
|
|
19
|
+
* Map => Sass.Map
|
|
20
|
+
* BigInt => Sass.Null
|
|
21
|
+
* Function => Sass.Null
|
|
22
|
+
* Null => Sass.Null
|
|
23
|
+
* Undefined => Sass.Null
|
|
24
|
+
* Symbol => Sass.Null
|
|
25
|
+
* AnythingElse => Sass.Null
|
|
26
|
+
*
|
|
27
|
+
* Copyright (c) 2023-2025 Alex Grant (@localnerve), LocalNerve LLC
|
|
28
|
+
* Licensed under the MIT license.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
function convertToSassTypesModern(sass, value) {
|
|
32
|
+
switch (typeof value) {
|
|
33
|
+
case 'boolean':
|
|
34
|
+
return value ? sass.sassTrue : sass.sassFalse;
|
|
35
|
+
case 'number':
|
|
36
|
+
return new sass.SassNumber(value);
|
|
37
|
+
case 'string':
|
|
38
|
+
return new sass.SassString(value, {
|
|
39
|
+
quotes: false
|
|
40
|
+
});
|
|
41
|
+
case 'object':
|
|
42
|
+
if (value === null) {
|
|
43
|
+
return sass.sassNull;
|
|
44
|
+
} else if (Array.isArray(value)) {
|
|
45
|
+
const sassArray = [];
|
|
46
|
+
for (let i = 0; i < value.length; ++i) {
|
|
47
|
+
sassArray[i] = convertToSassTypesModern(sass, value[i]);
|
|
48
|
+
}
|
|
49
|
+
return new sass.SassList(sassArray);
|
|
50
|
+
} else if (value instanceof Set) {
|
|
51
|
+
let i = 0;
|
|
52
|
+
const sassArray = [];
|
|
53
|
+
for (const item of value) {
|
|
54
|
+
sassArray[i++] = convertToSassTypesModern(sass, item);
|
|
55
|
+
}
|
|
56
|
+
return new sass.SassList(sassArray);
|
|
57
|
+
} else {
|
|
58
|
+
const sassArray = [];
|
|
59
|
+
const obj = value instanceof Map ? Object.fromEntries(value) : value;
|
|
60
|
+
for (const [key, val] of Object.entries(obj)) {
|
|
61
|
+
sassArray.push([convertToSassTypesModern(sass, key), convertToSassTypesModern(sass, val)]);
|
|
62
|
+
}
|
|
63
|
+
return new sass.SassMap(new _immutable.OrderedMap(sassArray));
|
|
64
|
+
}
|
|
65
|
+
default:
|
|
66
|
+
return sass.sassNull;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
function convertToSassTypesLegacy(sass, value) {
|
|
70
|
+
switch (typeof value) {
|
|
71
|
+
case 'boolean':
|
|
72
|
+
return value ? sass.types.Boolean.TRUE : sass.types.Boolean.FALSE;
|
|
73
|
+
case 'number':
|
|
74
|
+
return new sass.types.Number(value);
|
|
75
|
+
case 'string':
|
|
76
|
+
return new sass.types.String(value);
|
|
77
|
+
case 'object':
|
|
78
|
+
if (value === null) {
|
|
79
|
+
return sass.types.Null.NULL;
|
|
80
|
+
} else if (Array.isArray(value)) {
|
|
81
|
+
const list = new sass.types.List(value.length);
|
|
82
|
+
for (let i = 0; i < value.length; ++i) {
|
|
83
|
+
list.setValue(i, convertToSassTypesLegacy(sass, value[i]));
|
|
84
|
+
}
|
|
85
|
+
return list;
|
|
86
|
+
} else if (value instanceof Set) {
|
|
87
|
+
const list = new sass.types.List(value.size);
|
|
88
|
+
let i = 0;
|
|
89
|
+
for (let val of value) {
|
|
90
|
+
list.setValue(i++, convertToSassTypesLegacy(sass, val));
|
|
91
|
+
}
|
|
92
|
+
return list;
|
|
93
|
+
} else {
|
|
94
|
+
const obj = value instanceof Map ? Object.fromEntries(value) : value;
|
|
95
|
+
const map = new sass.types.Map(Object.keys(obj).length);
|
|
96
|
+
let i = 0;
|
|
97
|
+
for (const [key, val] of Object.entries(obj)) {
|
|
98
|
+
map.setKey(i, convertToSassTypesLegacy(sass, key));
|
|
99
|
+
map.setValue(i++, convertToSassTypesLegacy(sass, val));
|
|
100
|
+
}
|
|
101
|
+
return map;
|
|
102
|
+
}
|
|
103
|
+
default:
|
|
104
|
+
return sass.types.Null.NULL;
|
|
105
|
+
}
|
|
106
|
+
}
|
package/lib/legacyAPI.js
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* Copyright (c) 2023-2025 Alex Grant (@localnerve), LocalNerve LLC
|
|
5
5
|
* Licensed under the MIT license.
|
|
6
6
|
*/
|
|
7
|
+
import { convertToSassTypesLegacy } from "./types.js";
|
|
7
8
|
|
|
8
9
|
export default function legacyAPI (sass, processor) {
|
|
9
10
|
return {
|
|
@@ -51,6 +52,18 @@ export default function legacyAPI (sass, processor) {
|
|
|
51
52
|
}
|
|
52
53
|
done(list);
|
|
53
54
|
});
|
|
55
|
+
},
|
|
56
|
+
'lookup($keys...)': (list, done) => {
|
|
57
|
+
let len = list.getLength(), i = 0;
|
|
58
|
+
const keys = [];
|
|
59
|
+
for(; i < len; ++i) {
|
|
60
|
+
keys[i] = list.getValue(i).getValue();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
processor.lookup(keys, value => {
|
|
64
|
+
const result = convertToSassTypesLegacy(sass, value);
|
|
65
|
+
done(result);
|
|
66
|
+
});
|
|
54
67
|
}
|
|
55
68
|
};
|
|
56
69
|
}
|
package/lib/modernAPIAsync.js
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* Copyright (c) 2023-2025 Alex Grant (@localnerve), LocalNerve LLC
|
|
5
5
|
* Licensed under the MIT license.
|
|
6
6
|
*/
|
|
7
|
+
import { convertToSassTypesModern } from "./types.js";
|
|
7
8
|
|
|
8
9
|
export default function modernAPIAsync (sass, processor) {
|
|
9
10
|
return {
|
|
@@ -91,6 +92,24 @@ export default function modernAPIAsync (sass, processor) {
|
|
|
91
92
|
reject(err);
|
|
92
93
|
}
|
|
93
94
|
});
|
|
95
|
+
},
|
|
96
|
+
'lookup($keys...)': args => {
|
|
97
|
+
return new Promise((resolve, reject) => {
|
|
98
|
+
try {
|
|
99
|
+
const input = args[0].asList;
|
|
100
|
+
const keys = [];
|
|
101
|
+
for (let i = 0; i < input.size; ++i) {
|
|
102
|
+
keys[i] = input.get(i).assertString().text;
|
|
103
|
+
}
|
|
104
|
+
let result = sass.sassNull;
|
|
105
|
+
processor.lookup(keys, value => {
|
|
106
|
+
result = convertToSassTypesModern(sass, value);
|
|
107
|
+
});
|
|
108
|
+
resolve(result);
|
|
109
|
+
} catch (err) {
|
|
110
|
+
reject(err);
|
|
111
|
+
}
|
|
112
|
+
});
|
|
94
113
|
}
|
|
95
114
|
};
|
|
96
115
|
}
|
package/lib/modernAPISync.js
CHANGED
|
@@ -5,6 +5,8 @@
|
|
|
5
5
|
* Licensed under the MIT license.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
+
import { convertToSassTypesModern } from './types.js';
|
|
9
|
+
|
|
8
10
|
export default function modernAPISync (sass, processor) {
|
|
9
11
|
return {
|
|
10
12
|
'image-url($filename, $only_path: false)': args => {
|
|
@@ -65,6 +67,18 @@ export default function modernAPISync (sass, processor) {
|
|
|
65
67
|
}
|
|
66
68
|
});
|
|
67
69
|
return new sass.SassList(result);
|
|
70
|
+
},
|
|
71
|
+
'lookup($keys...)': args => {
|
|
72
|
+
const input = args[0].asList;
|
|
73
|
+
const keys = [];
|
|
74
|
+
for (let i = 0; i < input.size; ++i) {
|
|
75
|
+
keys[i] = input.get(i).assertString().text;
|
|
76
|
+
}
|
|
77
|
+
let result = sass.sassNull;
|
|
78
|
+
processor.lookup(keys, value => {
|
|
79
|
+
result = convertToSassTypesModern(sass, value);
|
|
80
|
+
});
|
|
81
|
+
return result;
|
|
68
82
|
}
|
|
69
83
|
};
|
|
70
84
|
}
|
package/lib/processor.js
CHANGED
|
@@ -30,7 +30,7 @@ const FONT_TYPES = {
|
|
|
30
30
|
|
|
31
31
|
export default class Processor {
|
|
32
32
|
constructor (options) {
|
|
33
|
-
this.options = options;
|
|
33
|
+
this.options = { ...{ data: {} }, ...options };
|
|
34
34
|
const {
|
|
35
35
|
images_path = defaultPaths.images_path,
|
|
36
36
|
fonts_path = defaultPaths.fonts_path,
|
|
@@ -202,4 +202,13 @@ export default class Processor {
|
|
|
202
202
|
this.font_url(file, complete(i, type));
|
|
203
203
|
}
|
|
204
204
|
}
|
|
205
|
+
|
|
206
|
+
lookup (keys, done) {
|
|
207
|
+
let data = this.options.data;
|
|
208
|
+
for (let key of keys) {
|
|
209
|
+
data = data[key];
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
done(data !== this.options.data ? data : null);
|
|
213
|
+
}
|
|
205
214
|
}
|
package/lib/types.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert javascript types to sass types.
|
|
3
|
+
*
|
|
4
|
+
* Supported:
|
|
5
|
+
* Boolean => Sass.Boolean
|
|
6
|
+
* Number => Sass.Number
|
|
7
|
+
* String => Sass.String
|
|
8
|
+
* Array => Sass.List
|
|
9
|
+
* Set => Sass.List
|
|
10
|
+
* Object => Sass.Map
|
|
11
|
+
* Map => Sass.Map
|
|
12
|
+
* BigInt => Sass.Null
|
|
13
|
+
* Function => Sass.Null
|
|
14
|
+
* Null => Sass.Null
|
|
15
|
+
* Undefined => Sass.Null
|
|
16
|
+
* Symbol => Sass.Null
|
|
17
|
+
* AnythingElse => Sass.Null
|
|
18
|
+
*
|
|
19
|
+
* Copyright (c) 2023-2025 Alex Grant (@localnerve), LocalNerve LLC
|
|
20
|
+
* Licensed under the MIT license.
|
|
21
|
+
*/
|
|
22
|
+
import { OrderedMap } from 'immutable';
|
|
23
|
+
|
|
24
|
+
export function convertToSassTypesModern (sass, value) {
|
|
25
|
+
switch (typeof value) {
|
|
26
|
+
case 'boolean':
|
|
27
|
+
return value ? sass.sassTrue : sass.sassFalse;
|
|
28
|
+
case 'number':
|
|
29
|
+
return new sass.SassNumber(value);
|
|
30
|
+
case 'string':
|
|
31
|
+
return new sass.SassString(value, { quotes: false });
|
|
32
|
+
case 'object':
|
|
33
|
+
if (value === null) {
|
|
34
|
+
return sass.sassNull;
|
|
35
|
+
} else if (Array.isArray(value)) {
|
|
36
|
+
const sassArray = [];
|
|
37
|
+
for (let i = 0; i < value.length; ++i) {
|
|
38
|
+
sassArray[i] = convertToSassTypesModern(sass, value[i]);
|
|
39
|
+
}
|
|
40
|
+
return new sass.SassList(sassArray);
|
|
41
|
+
} else if (value instanceof Set) {
|
|
42
|
+
let i = 0;
|
|
43
|
+
const sassArray = [];
|
|
44
|
+
for (const item of value) {
|
|
45
|
+
sassArray[i++] = convertToSassTypesModern(sass, item);
|
|
46
|
+
}
|
|
47
|
+
return new sass.SassList(sassArray);
|
|
48
|
+
} else {
|
|
49
|
+
const sassArray = [];
|
|
50
|
+
const obj = value instanceof Map ? Object.fromEntries(value) : value;
|
|
51
|
+
for (const [key, val] of Object.entries(obj)) {
|
|
52
|
+
sassArray.push([
|
|
53
|
+
convertToSassTypesModern(sass, key),
|
|
54
|
+
convertToSassTypesModern(sass, val)
|
|
55
|
+
]);
|
|
56
|
+
}
|
|
57
|
+
return new sass.SassMap(new OrderedMap(sassArray));
|
|
58
|
+
}
|
|
59
|
+
default:
|
|
60
|
+
return sass.sassNull;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function convertToSassTypesLegacy (sass, value) {
|
|
65
|
+
switch (typeof value) {
|
|
66
|
+
case 'boolean':
|
|
67
|
+
return value ? sass.types.Boolean.TRUE : sass.types.Boolean.FALSE;
|
|
68
|
+
case 'number':
|
|
69
|
+
return new sass.types.Number(value);
|
|
70
|
+
case 'string':
|
|
71
|
+
return new sass.types.String(value);
|
|
72
|
+
case 'object':
|
|
73
|
+
if (value === null) {
|
|
74
|
+
return sass.types.Null.NULL;
|
|
75
|
+
} else if (Array.isArray(value)) {
|
|
76
|
+
const list = new sass.types.List(value.length);
|
|
77
|
+
for (let i = 0; i < value.length; ++i) {
|
|
78
|
+
list.setValue(i, convertToSassTypesLegacy(sass, value[i]));
|
|
79
|
+
}
|
|
80
|
+
return list;
|
|
81
|
+
} else if (value instanceof Set) {
|
|
82
|
+
const list = new sass.types.List(value.size);
|
|
83
|
+
let i = 0;
|
|
84
|
+
for (let val of value) {
|
|
85
|
+
list.setValue(i++, convertToSassTypesLegacy(sass, val));
|
|
86
|
+
}
|
|
87
|
+
return list;
|
|
88
|
+
} else {
|
|
89
|
+
const obj = value instanceof Map ? Object.fromEntries(value) : value;
|
|
90
|
+
const map = new sass.types.Map(Object.keys(obj).length);
|
|
91
|
+
let i = 0;
|
|
92
|
+
for (const [key, val] of Object.entries(obj)) {
|
|
93
|
+
map.setKey(i, convertToSassTypesLegacy(sass, key));
|
|
94
|
+
map.setValue(i++, convertToSassTypesLegacy(sass, val));
|
|
95
|
+
}
|
|
96
|
+
return map;
|
|
97
|
+
}
|
|
98
|
+
default:
|
|
99
|
+
return sass.types.Null.NULL;
|
|
100
|
+
}
|
|
101
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@localnerve/sass-asset-functions",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.10.0-rc.1",
|
|
4
4
|
"description": "compass-style asset functions for dart-sass or other sass compilers",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"exports": {
|
|
@@ -34,7 +34,8 @@
|
|
|
34
34
|
"image-width",
|
|
35
35
|
"image-height",
|
|
36
36
|
"font-url",
|
|
37
|
-
"font-files"
|
|
37
|
+
"font-files",
|
|
38
|
+
"lookup"
|
|
38
39
|
],
|
|
39
40
|
"author": "Alex Grant <alex@localnerve.com>",
|
|
40
41
|
"maintainers": [
|
|
@@ -50,15 +51,16 @@
|
|
|
50
51
|
},
|
|
51
52
|
"homepage": "https://github.com/localnerve/sass-asset-functions",
|
|
52
53
|
"dependencies": {
|
|
53
|
-
"image-size": "^2.0.
|
|
54
|
+
"image-size": "^2.0.1",
|
|
55
|
+
"immutable": "^5.0.3",
|
|
54
56
|
"mime-types": "^2.1.35",
|
|
55
|
-
"sass": "^1.
|
|
57
|
+
"sass": "^1.86.0"
|
|
56
58
|
},
|
|
57
59
|
"devDependencies": {
|
|
58
60
|
"@babel/cli": "^7.26.4",
|
|
59
61
|
"@babel/preset-env": "^7.26.9",
|
|
60
|
-
"@eslint/js": "^9.
|
|
61
|
-
"eslint": "^9.
|
|
62
|
+
"@eslint/js": "^9.22.0",
|
|
63
|
+
"eslint": "^9.22.0",
|
|
62
64
|
"eslint-plugin-jest": "^28.11.0",
|
|
63
65
|
"glob": "^11.0.1",
|
|
64
66
|
"globals": "^16.0.0",
|