less-content-mapper 0.0.0-stage → 0.2.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.
- package/LICENSE.md +18 -0
- package/README.md +116 -2
- package/lib/content-mapper.js +250 -0
- package/lib/syntax.js +3 -0
- package/package.json +54 -4
package/LICENSE.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# MIT License
|
|
2
|
+
|
|
3
|
+
Copyright © 2026 Remco Haszing, Brody McKee
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
|
6
|
+
associated documentation files (the “Software”), to deal in the Software without restriction,
|
|
7
|
+
including without limitation the rights to use, copy, modify, merge, publish, distribute,
|
|
8
|
+
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
furnished to do so, subject to the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial
|
|
12
|
+
portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
|
|
15
|
+
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
16
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
|
|
17
|
+
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
18
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,117 @@
|
|
|
1
|
-
#
|
|
1
|
+
# less-content-mapper
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://github.com/remcohaszing/css-modules-content-mapper/actions/workflows/ci.yaml)
|
|
4
|
+
[](https://www.npmjs.com/package/less-content-mapper)
|
|
5
|
+
[](https://www.npmjs.com/package/less-content-mapper)
|
|
6
|
+
|
|
7
|
+
A [TypeScript](https://www.typescriptlang.org) content mapper for [Less](https://lesscss.org)
|
|
8
|
+
[CSS modules](https://github.com/css-modules/css-modules).
|
|
9
|
+
|
|
10
|
+
## Table of Contents
|
|
11
|
+
|
|
12
|
+
- [Installation](#installation)
|
|
13
|
+
- [Usage](#usage)
|
|
14
|
+
- [Editor support](#editor-support)
|
|
15
|
+
- [Options](#options)
|
|
16
|
+
- [Less syntax](#less-syntax)
|
|
17
|
+
- [Compatibility](#compatibility)
|
|
18
|
+
- [Security](#security)
|
|
19
|
+
- [License](#license)
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
npm install less-content-mapper less
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
This content mapper uses the Less implementation your project already has, so your styles are
|
|
28
|
+
compiled with the same version of Less your build uses, and the types match your build output.
|
|
29
|
+
|
|
30
|
+
## Usage
|
|
31
|
+
|
|
32
|
+
In your `tsconfig.json`, add `less-content-mapper` to the `contentMappers` array.
|
|
33
|
+
|
|
34
|
+
```jsonc
|
|
35
|
+
{
|
|
36
|
+
"compilerOptions": {
|
|
37
|
+
// …
|
|
38
|
+
},
|
|
39
|
+
"contentMappers": [
|
|
40
|
+
{
|
|
41
|
+
"package": "less-content-mapper",
|
|
42
|
+
"extensions": [".module.less"],
|
|
43
|
+
"options": {
|
|
44
|
+
// Optional options…
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
]
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Now run `tsc` with the `--runExternalCode` flag.
|
|
52
|
+
|
|
53
|
+
```sh
|
|
54
|
+
tsc --runExternalCode
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
If your code uses undefined CSS module classes, this will show an error.
|
|
58
|
+
|
|
59
|
+
## Editor support
|
|
60
|
+
|
|
61
|
+
In addition to type checking, this content mapper also provides useful editor integrations. Each
|
|
62
|
+
file is compiled with Less, and the source map is used to map classes back to your Less code.
|
|
63
|
+
|
|
64
|
+
- _“Go to Definition”_ in a TypeScript file will take you to the matching Less class or ID. For a
|
|
65
|
+
generated class, such as `.block--modifier` from `&--modifier`, it takes you to the selector that
|
|
66
|
+
generated it.
|
|
67
|
+
- Hovering an imported class in TypeScript will show the Less definitions.
|
|
68
|
+
- Like TypeScript values, Less classes and IDs can be documented with JSDoc comments (`/** */`).
|
|
69
|
+
|
|
70
|
+
Classes from other files, for example from a mixin in an imported file, are typed, but _“Go to
|
|
71
|
+
Definition”_ doesn’t work for them.
|
|
72
|
+
|
|
73
|
+
In your editor, changes to a file you `@import` don’t update the types of the files that import it
|
|
74
|
+
straight away. They’re updated when the importing file changes, or when you restart the TypeScript
|
|
75
|
+
server. This doesn’t affect `tsc`, which always compiles every file from scratch.
|
|
76
|
+
|
|
77
|
+
If a file can’t be compiled, for example while you are typing, the types from the last successful
|
|
78
|
+
compile are kept.
|
|
79
|
+
|
|
80
|
+
## Options
|
|
81
|
+
|
|
82
|
+
- `localsConvention` (`'camelCase' | 'camelCaseOnly' | 'dashes' | 'dashesOnly' | 'all' | 'none'`):
|
|
83
|
+
Style of exported classnames, the keys in your json. For more information, see
|
|
84
|
+
[`postcss-modules`](https://github.com/madyankin/postcss-modules#localsconvention).
|
|
85
|
+
|
|
86
|
+
## Less syntax
|
|
87
|
+
|
|
88
|
+
This project uses Less itself, so it supports everything your version of Less supports. Like your
|
|
89
|
+
build, Less doesn’t output rules without declarations or mixins defined with parentheses, such as
|
|
90
|
+
`.mixin()`, so their classes aren’t exported.
|
|
91
|
+
|
|
92
|
+
Files are compiled with the default Less options. Options your build sets, such as
|
|
93
|
+
`javascriptEnabled` or `globalVars`, aren’t applied, so a file that depends on them, for example on
|
|
94
|
+
inline JavaScript or on a variable your build defines, is reported as an error.
|
|
95
|
+
|
|
96
|
+
## Compatibility
|
|
97
|
+
|
|
98
|
+
This project works with TypeScript 7.1 or greater and `less` 4.2.0 or greater, and requires Node.js
|
|
99
|
+
22 or greater.
|
|
100
|
+
|
|
101
|
+
Earlier Less 4 releases aren’t supported. Their source maps point interpolated selectors such as
|
|
102
|
+
`.text-@{color}` at the wrong location, which breaks _“Go to Definition”_ and hover previews for
|
|
103
|
+
those classes.
|
|
104
|
+
|
|
105
|
+
## Security
|
|
106
|
+
|
|
107
|
+
Normally, `tsc` is a self-contained binary that doesn’t execute third party code. By using content
|
|
108
|
+
mappers, you are deliberately breaking this trust boundary.
|
|
109
|
+
|
|
110
|
+
Less can also run JavaScript from a stylesheet using `@plugin`, even with inline JavaScript
|
|
111
|
+
disabled. This content mapper compiles your Less files, so those plugins run whenever TypeScript or
|
|
112
|
+
your editor processes them.
|
|
113
|
+
|
|
114
|
+
## License
|
|
115
|
+
|
|
116
|
+
[MIT](LICENSE.md) © [Remco Haszing](https://github.com/remcohaszing),
|
|
117
|
+
[Brody McKee](https://github.com/mrmckeb)
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @import { OptionDiagnostic } from 'content-mapper-test-utils'
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { dirname, relative } from 'node:path'
|
|
6
|
+
|
|
7
|
+
import { startMapper } from 'css-modules-content-mapper'
|
|
8
|
+
|
|
9
|
+
import { syntax } from './syntax.js'
|
|
10
|
+
|
|
11
|
+
const missingLessMessage = "'less' must be installed."
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The source map of a file that compiles to no CSS, because Less doesn’t return one for those.
|
|
15
|
+
*/
|
|
16
|
+
const emptySourceMap = { version: 3, sources: [], names: [], mappings: '' }
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Get the span of a Less error in the original content.
|
|
20
|
+
*
|
|
21
|
+
* @param {string} content
|
|
22
|
+
* The original file content.
|
|
23
|
+
* @param {number} line
|
|
24
|
+
* The 1-based line of the error.
|
|
25
|
+
* @param {number} column
|
|
26
|
+
* The 0-based column of the error.
|
|
27
|
+
* @returns {{ start: number, length: number }}
|
|
28
|
+
* The span of the token at the error location.
|
|
29
|
+
*/
|
|
30
|
+
function getErrorSpan(content, line, column) {
|
|
31
|
+
let lineStart = 0
|
|
32
|
+
for (let index = 1; index < line; index += 1) {
|
|
33
|
+
lineStart = content.indexOf('\n', lineStart) + 1
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const start = lineStart + column
|
|
37
|
+
const length = /^[^\s();{}]+/.exec(content.slice(start))?.[0].length ?? 0
|
|
38
|
+
return { start, length }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Check whether an error is a Less compile error with a location.
|
|
43
|
+
*
|
|
44
|
+
* @param {unknown} error
|
|
45
|
+
* The error to check.
|
|
46
|
+
* @returns {error is Less.RenderError}
|
|
47
|
+
* Whether the error is a Less compile error.
|
|
48
|
+
*/
|
|
49
|
+
function isLessError(error) {
|
|
50
|
+
return (
|
|
51
|
+
error instanceof Error &&
|
|
52
|
+
'type' in error &&
|
|
53
|
+
typeof error.type === 'string' &&
|
|
54
|
+
'line' in error &&
|
|
55
|
+
typeof error.line === 'number' &&
|
|
56
|
+
'column' in error &&
|
|
57
|
+
typeof error.column === 'number'
|
|
58
|
+
)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* @typedef LessElement
|
|
63
|
+
* A part of a selector in the Less AST. `@types/less` doesn’t describe the AST.
|
|
64
|
+
* @property {() => number} getIndex
|
|
65
|
+
* Get the offset of the element in its original file.
|
|
66
|
+
* @property {() => { filename?: string }} fileInfo
|
|
67
|
+
* Get the original file of the element.
|
|
68
|
+
*/
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* @typedef {LessElement & { elements: LessElement[] }} LessSelector
|
|
72
|
+
* A selector in the Less AST.
|
|
73
|
+
*/
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* @typedef LessNode
|
|
77
|
+
* A node in the Less AST.
|
|
78
|
+
* @property {LessNode[]} [rules]
|
|
79
|
+
* The child nodes.
|
|
80
|
+
* @property {LessSelector[]} [selectors]
|
|
81
|
+
* The own selectors of a rule, as written in the source.
|
|
82
|
+
* @property {LessSelector[][]} [paths]
|
|
83
|
+
* The selectors of a rule, including those of its parents.
|
|
84
|
+
*/
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* @typedef LessVisitorManager
|
|
88
|
+
* The part of the Less plugin manager that `@types/less` doesn’t describe.
|
|
89
|
+
* @property {(visitor: { run: (root: LessNode) => LessNode }) => void} addVisitor
|
|
90
|
+
* Add a visitor of the Less AST.
|
|
91
|
+
*/
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Copy a Less AST node, so that it’s mapped to the position of another node.
|
|
95
|
+
*
|
|
96
|
+
* Less can share nodes between rules, so they’re copied instead of changed.
|
|
97
|
+
*
|
|
98
|
+
* @template {LessElement} Node
|
|
99
|
+
* The type of the node to copy.
|
|
100
|
+
* @param {Node} node
|
|
101
|
+
* The node to copy.
|
|
102
|
+
* @param {LessElement} target
|
|
103
|
+
* The node whose position to use.
|
|
104
|
+
* @returns {Node}
|
|
105
|
+
* The copied node.
|
|
106
|
+
*/
|
|
107
|
+
function moveNode(node, target) {
|
|
108
|
+
return Object.assign(Object.create(node), {
|
|
109
|
+
getIndex: () => target.getIndex(),
|
|
110
|
+
fileInfo: () => target.fileInfo()
|
|
111
|
+
})
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Check whether a selector is one of a rule’s own selectors, rather than one of its parents.
|
|
116
|
+
*
|
|
117
|
+
* When a rule uses `&`, such as `&-suffix` or `& .child`, Less derives the first selector of each
|
|
118
|
+
* path from the rule’s own selector, so it’s already mapped to the rule.
|
|
119
|
+
*
|
|
120
|
+
* @param {LessNode} rule
|
|
121
|
+
* The rule to check.
|
|
122
|
+
* @param {LessSelector} selector
|
|
123
|
+
* The selector to check.
|
|
124
|
+
* @returns {boolean}
|
|
125
|
+
* Whether the selector is one of the rule’s own selectors.
|
|
126
|
+
*/
|
|
127
|
+
function isOwnSelector(rule, selector) {
|
|
128
|
+
const index = selector.getIndex()
|
|
129
|
+
const { filename } = selector.fileInfo()
|
|
130
|
+
return (rule.selectors ?? []).some(
|
|
131
|
+
(own) => own.getIndex() === index && own.fileInfo().filename === filename
|
|
132
|
+
)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Map compiled nested rules to their innermost selector, as core expects. Less maps them to their
|
|
137
|
+
* outermost selector instead.
|
|
138
|
+
*
|
|
139
|
+
* @param {LessNode} node
|
|
140
|
+
* The Less AST node to process.
|
|
141
|
+
* @returns {undefined}
|
|
142
|
+
* Nothing.
|
|
143
|
+
*/
|
|
144
|
+
function mapRulesToInnermostSelector(node) {
|
|
145
|
+
for (const child of node.rules ?? []) {
|
|
146
|
+
child.paths &&= child.paths.map((path) => {
|
|
147
|
+
const [first, ...rest] = path
|
|
148
|
+
const last = rest.at(-1)
|
|
149
|
+
if (!last || isOwnSelector(child, first)) {
|
|
150
|
+
return path
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const [innermost] = last.elements
|
|
154
|
+
const [firstElement, ...elements] = first.elements
|
|
155
|
+
const selector = moveNode(first, innermost)
|
|
156
|
+
selector.elements = [moveNode(firstElement, innermost), ...elements]
|
|
157
|
+
return [selector, ...rest]
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
mapRulesToInnermostSelector(child)
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** @type {Less.Plugin} */
|
|
165
|
+
const innermostSelectorPlugin = {
|
|
166
|
+
install(_, pluginManager) {
|
|
167
|
+
// `@types/less` doesn’t describe visitors. This one runs after Less has joined the selectors.
|
|
168
|
+
const manager = /** @type {Less.PluginManager & LessVisitorManager} */ (pluginManager)
|
|
169
|
+
manager.addVisitor({
|
|
170
|
+
run(root) {
|
|
171
|
+
mapRulesToInnermostSelector(root)
|
|
172
|
+
return root
|
|
173
|
+
}
|
|
174
|
+
})
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
startMapper({
|
|
179
|
+
diagnosticSource: 'Less',
|
|
180
|
+
|
|
181
|
+
async validateOptions() {
|
|
182
|
+
/** @type {OptionDiagnostic[]} */
|
|
183
|
+
const diagnostics = []
|
|
184
|
+
|
|
185
|
+
try {
|
|
186
|
+
await import('less')
|
|
187
|
+
} catch {
|
|
188
|
+
diagnostics.push({ path: [], messageText: missingLessMessage, code: 3002 })
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
return { diagnostics, options: {} }
|
|
192
|
+
},
|
|
193
|
+
|
|
194
|
+
async preprocess({ content, fileName }) {
|
|
195
|
+
/** @type {LessStatic | undefined} */
|
|
196
|
+
let less
|
|
197
|
+
|
|
198
|
+
try {
|
|
199
|
+
;({ default: less } = await import('less'))
|
|
200
|
+
} catch {
|
|
201
|
+
return { diagnostics: [{ messageText: missingLessMessage, start: 0, length: 0, code: 4002 }] }
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
try {
|
|
205
|
+
// Imports are resolved the way Less itself does: relative to the importing file, then through
|
|
206
|
+
// Node’s module resolution for packages.
|
|
207
|
+
const result = await less.render(content, {
|
|
208
|
+
filename: fileName,
|
|
209
|
+
plugins: [innermostSelectorPlugin],
|
|
210
|
+
sourceMap: {}
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
const { css, map } = result
|
|
214
|
+
if (!map) {
|
|
215
|
+
// This can be an empty file, or a file that only contains variables or mixins.
|
|
216
|
+
if (css.trim() === '') {
|
|
217
|
+
return { css, map: emptySourceMap, syntax }
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
throw new Error('Less did not return a source map.')
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
return { css, map: JSON.parse(map), syntax }
|
|
224
|
+
} catch (error) {
|
|
225
|
+
if (!isLessError(error)) {
|
|
226
|
+
throw error
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const { column, filename, line, message } = error
|
|
230
|
+
if (filename === fileName) {
|
|
231
|
+
const { length, start } = getErrorSpan(content, line, column)
|
|
232
|
+
return {
|
|
233
|
+
diagnostics: [{ messageText: message, start, length, code: 4001 }]
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// The error is in another file, such as one loaded using `@import`. Its span can’t be
|
|
238
|
+
// reported in this file, so the location is added to the message instead.
|
|
239
|
+
let messageText = message
|
|
240
|
+
if (filename) {
|
|
241
|
+
const path = relative(dirname(fileName), filename)
|
|
242
|
+
messageText += ` (${path}:${line}:${column + 1})`
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
return {
|
|
246
|
+
diagnostics: [{ messageText, start: 0, length: 0, code: 4001 }]
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
})
|
package/lib/syntax.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,56 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "less-content-mapper",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "TypeScript content mapper for Less CSS modules",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"content-mapper",
|
|
7
|
+
"css-modules",
|
|
8
|
+
"less",
|
|
9
|
+
"typescript"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://github.com/remcohaszing/css-modules-content-mapper/tree/main/packages/less-content-mapper#readme",
|
|
12
|
+
"bugs": "https://github.com/remcohaszing/css-modules-content-mapper/issues",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/remcohaszing/css-modules-content-mapper.git",
|
|
16
|
+
"directory": "packages/less-content-mapper"
|
|
17
|
+
},
|
|
18
|
+
"funding": "https://github.com/sponsors/remcohaszing",
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"author": "Remco Haszing <remcohaszing@gmail.com>",
|
|
21
|
+
"contributors": [
|
|
22
|
+
"Brody McKee (https://github.com/mrmckeb)"
|
|
23
|
+
],
|
|
24
|
+
"type": "module",
|
|
25
|
+
"files": [
|
|
26
|
+
"lib"
|
|
27
|
+
],
|
|
28
|
+
"typescript": {
|
|
29
|
+
"contentMapper": {
|
|
30
|
+
"exec": [
|
|
31
|
+
"node",
|
|
32
|
+
"lib/content-mapper.js"
|
|
33
|
+
],
|
|
34
|
+
"compilerOptions": [],
|
|
35
|
+
"dynamicConfig": false
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"test": "node --test"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"css-modules-content-mapper": "^0.4.0"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@types/less": "^3.0.0",
|
|
46
|
+
"@types/node": "^26.0.0",
|
|
47
|
+
"content-mapper-test-utils": "^0.0.2",
|
|
48
|
+
"less": "4.2.0",
|
|
49
|
+
"snapshot-fixtures": "^1.0.0",
|
|
50
|
+
"typescript": "^6.0.0",
|
|
51
|
+
"vscode-jsonrpc": "^10.0.0"
|
|
52
|
+
},
|
|
53
|
+
"peerDependencies": {
|
|
54
|
+
"less": "^4.2.0"
|
|
55
|
+
}
|
|
56
|
+
}
|