ember-scoped-css 1.1.1 → 2.0.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/README.md +121 -39
- package/dist/cjs/all-Du43cBa9.cjs +1308 -0
- package/dist/cjs/all.cjs +3 -0
- package/dist/cjs/babel.cjs +9 -0
- package/dist/cjs/rollup.cjs +8 -0
- package/dist/cjs/vite.cjs +8 -0
- package/dist/runtime/index.d.ts +11 -0
- package/dist/runtime/index.d.ts.map +1 -0
- package/dist/runtime/index.js +12 -8
- package/dist/runtime/index.js.map +1 -7
- package/{declarations → dist}/runtime/test-support.d.ts +5 -2
- package/dist/runtime/test-support.d.ts.map +1 -0
- package/dist/runtime/test-support.js +357 -182
- package/dist/runtime/test-support.js.map +1 -7
- package/package.json +21 -24
- package/src/build/babel-plugin.js +4 -1
- package/src/build/public-exports/all.js +10 -0
- package/src/build/public-exports/babel.js +5 -0
- package/src/build/public-exports/rollup.js +3 -0
- package/src/build/public-exports/vite.js +3 -0
- package/src/build/scoped-css-unplugin.js +10 -33
- package/src/build/template-plugin.js +5 -16
- package/src/build/template-plugin.test.ts +0 -8
- package/src/build/unplugin-colocated.js +103 -0
- package/src/build/unplugin-inline.js +71 -0
- package/src/lib/css/rewrite.js +8 -8
- package/src/lib/css/rewrite.test.ts +69 -128
- package/src/lib/request.js +58 -37
- package/src/runtime/index.ts +1 -1
- package/declarations/build/template-plugin.d.ts +0 -7
- package/declarations/build/template-plugin.d.ts.map +0 -1
- package/declarations/build/template-plugin.test.d.ts +0 -2
- package/declarations/build/template-plugin.test.d.ts.map +0 -1
- package/declarations/lib/css/rewrite.d.ts +0 -2
- package/declarations/lib/css/rewrite.d.ts.map +0 -1
- package/declarations/lib/css/rewrite.test.d.ts +0 -2
- package/declarations/lib/css/rewrite.test.d.ts.map +0 -1
- package/declarations/lib/css/utils.d.ts +0 -29
- package/declarations/lib/css/utils.d.ts.map +0 -1
- package/declarations/lib/path/const.d.ts +0 -13
- package/declarations/lib/path/const.d.ts.map +0 -1
- package/declarations/lib/path/hash-from-absolute-path.d.ts +0 -3
- package/declarations/lib/path/hash-from-absolute-path.d.ts.map +0 -1
- package/declarations/lib/path/hash-from-absolute-path.test.d.ts +0 -2
- package/declarations/lib/path/hash-from-absolute-path.test.d.ts.map +0 -1
- package/declarations/lib/path/hash-from-module-path.d.ts +0 -31
- package/declarations/lib/path/hash-from-module-path.d.ts.map +0 -1
- package/declarations/lib/path/md5.d.ts +0 -12
- package/declarations/lib/path/md5.d.ts.map +0 -1
- package/declarations/lib/path/template-transform-paths.d.ts +0 -11
- package/declarations/lib/path/template-transform-paths.d.ts.map +0 -1
- package/declarations/lib/path/template-transform-paths.test.d.ts +0 -2
- package/declarations/lib/path/template-transform-paths.test.d.ts.map +0 -1
- package/declarations/lib/path/utils.appPath.test.d.ts +0 -2
- package/declarations/lib/path/utils.appPath.test.d.ts.map +0 -1
- package/declarations/lib/path/utils.d.ts +0 -103
- package/declarations/lib/path/utils.d.ts.map +0 -1
- package/declarations/lib/path/utils.findWorkspacePath.test.d.ts +0 -2
- package/declarations/lib/path/utils.findWorkspacePath.test.d.ts.map +0 -1
- package/declarations/lib/path/utils.hashFrom.test.d.ts +0 -2
- package/declarations/lib/path/utils.hashFrom.test.d.ts.map +0 -1
- package/declarations/lib/path/utils.isRelevantFile.test.d.ts +0 -2
- package/declarations/lib/path/utils.isRelevantFile.test.d.ts.map +0 -1
- package/declarations/lib/path/utils.paths.test.d.ts +0 -5
- package/declarations/lib/path/utils.paths.test.d.ts.map +0 -1
- package/declarations/lib/renameClass.d.ts +0 -9
- package/declarations/lib/renameClass.d.ts.map +0 -1
- package/declarations/lib/request.d.ts +0 -8
- package/declarations/lib/request.d.ts.map +0 -1
- package/declarations/lib/rewriteHbs.d.ts +0 -17
- package/declarations/lib/rewriteHbs.d.ts.map +0 -1
- package/declarations/runtime/index.d.ts +0 -8
- package/declarations/runtime/index.d.ts.map +0 -1
- package/declarations/runtime/test-support.d.ts.map +0 -1
- package/dist/cjs/babel-plugin.cjs +0 -202
- package/dist/cjs/index.cjs +0 -1012
- package/dist/cjs/template-plugin.cjs +0 -898
- package/src/build/index.js +0 -7
- package/src/build/vite.js +0 -3
package/README.md
CHANGED
|
@@ -15,7 +15,9 @@ const greeting = "hello world";
|
|
|
15
15
|
</style>
|
|
16
16
|
</template>
|
|
17
17
|
```
|
|
18
|
+
|
|
18
19
|
becomes the equivelent of;
|
|
20
|
+
|
|
19
21
|
```gjs
|
|
20
22
|
import './abcd1234.css'; // containing your CSS, but with selectors scoped
|
|
21
23
|
const greeting = "hello world";
|
|
@@ -28,7 +30,7 @@ const greeting = "hello world";
|
|
|
28
30
|
This is a build-time-only addon, so there is no need to worry about runtime performance.
|
|
29
31
|
|
|
30
32
|
You can also write your styles as a co-located `.css` file, right next to your `.gjs`/`.gts` files.
|
|
31
|
-
Every selector you write in your styles is automatically scoped to the component.
|
|
33
|
+
Every selector you write in your styles is automatically scoped to the component.
|
|
32
34
|
So you can develop your component with styles isolated from the rest of the application and you don't have to worry about CSS selectors collisions or issues with the CSS cascade.
|
|
33
35
|
|
|
34
36
|
_See [Usage](#usage) for details_.
|
|
@@ -37,7 +39,7 @@ If you want to read more specifics on how this addon achieves isolation with CSS
|
|
|
37
39
|
|
|
38
40
|
As selectors are scoped/renamed during the build process. So there is no performance hit when running the app.
|
|
39
41
|
|
|
40
|
-
The philosophy of `ember-scoped-css` is to stick as close to CSS and HTML as possible and not introduce new syntax or concepts unless it is absolutely necessary.
|
|
42
|
+
The philosophy of `ember-scoped-css` is to stick as close to CSS and HTML as possible and not introduce new syntax or concepts unless it is absolutely necessary.
|
|
41
43
|
|
|
42
44
|
You may also find the docs on [CSS `@layer`](https://developer.mozilla.org/en-US/docs/Web/CSS/@layer) interesting.
|
|
43
45
|
This build tool can emit CSS in a `@layer`.
|
|
@@ -53,7 +55,7 @@ This build tool can emit CSS in a `@layer`.
|
|
|
53
55
|
| -------- | ----------- | ---------------------- | --- |
|
|
54
56
|
| vite | >= 1.0.0 | 🚫 | [main][docs-main]
|
|
55
57
|
| gjs / gts library (no hbs) | >= 1.0.0 | 🚫 | [main][docs-main]
|
|
56
|
-
| webpack | <= 0.24.3 | <= 10.0.0 | [0.24.3][docs-2]
|
|
58
|
+
| webpack | <= 0.24.3 | <= 10.0.0 | [0.24.3][docs-2]
|
|
57
59
|
| hbs | <= 0.24.3 | <= 10.0.0 | [0.24.3][docs-2]
|
|
58
60
|
| ember-template-imports@v4 or babel-plugin-ember-template-compilation@2.2.5+ | 0.19.0 | 10.0.0 | [0.19][docs-3] - [0.24][docs-2]
|
|
59
61
|
| ember-template-imports@v3 or babel-plugin-ember-template-compilation@2.2.1 or rollup-plugin-glimmer-template-tag | <= 0.18.0 | <= 9.0.0 | [0.18][docs-4]
|
|
@@ -73,7 +75,16 @@ npm install --save-dev ember-scoped-css
|
|
|
73
75
|
|
|
74
76
|
### Configuration
|
|
75
77
|
|
|
76
|
-
|
|
78
|
+
Configuration happens in multiple locations to take over the need portion of your build steps.
|
|
79
|
+
|
|
80
|
+
- `vite.config.*`
|
|
81
|
+
- `rollup.config.*`
|
|
82
|
+
- `babel.config.*`
|
|
83
|
+
|
|
84
|
+
#### Vite
|
|
85
|
+
|
|
86
|
+
In your `vite.config.*`, import and add the `scopedCSS` plugin:
|
|
87
|
+
|
|
77
88
|
```js
|
|
78
89
|
import { defineConfig } from 'vite';
|
|
79
90
|
import { scopedCSS } from 'ember-scoped-css/vite';
|
|
@@ -87,57 +98,133 @@ export default defineConfig({
|
|
|
87
98
|
});
|
|
88
99
|
```
|
|
89
100
|
|
|
90
|
-
|
|
101
|
+
|
|
102
|
+
<details><summary>notes for vite projects</summary>
|
|
103
|
+
|
|
104
|
+
If you're `import.meta.glob`ing large chunks of your codebase, you'll want to make sure that your globs exclude CSS files
|
|
105
|
+
|
|
106
|
+
For example:
|
|
107
|
+
|
|
108
|
+
❌ Don't do this
|
|
109
|
+
|
|
110
|
+
```js
|
|
111
|
+
...import.meta.glob('./templates/**/*', { eager: true })
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
✅ Do this
|
|
115
|
+
|
|
116
|
+
```js
|
|
117
|
+
...import.meta.glob('./templates/**/*.{gjs,gts}', { eager: true }),
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
or better yet, for small projects:
|
|
121
|
+
|
|
122
|
+
```js
|
|
123
|
+
...import.meta.glob('./templates/{top,level,folders}/{sub,folders}.{gjs,gts}', { eager: true }),
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
This way you don't import CSS yourself, which would make CSS go through Vite's default CSS processing.
|
|
127
|
+
We need the scoped-css plugins to process the CSS instead of Vite's default behaviors.
|
|
128
|
+
|
|
129
|
+
</details>
|
|
130
|
+
|
|
131
|
+
##### Configuration Options
|
|
132
|
+
|
|
133
|
+
- `layerName: string` - Wrap your CSS in a `@layer` with this given name
|
|
134
|
+
|
|
135
|
+
#### Rollup
|
|
136
|
+
|
|
137
|
+
If you have a rollup config:
|
|
138
|
+
|
|
91
139
|
```js
|
|
92
|
-
import
|
|
140
|
+
import { scopedCSS } from 'ember-scoped-css/rollup';
|
|
93
141
|
|
|
94
|
-
|
|
142
|
+
// ...
|
|
143
|
+
plugins: [
|
|
144
|
+
scopedCSS(),
|
|
145
|
+
]
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
##### Configuration Options
|
|
149
|
+
|
|
150
|
+
- `layerName: string` - Wrap your CSS in a `@layer` with this given name
|
|
151
|
+
|
|
152
|
+
#### Babel
|
|
153
|
+
|
|
154
|
+
In your `babel.config.*`, add the plugins:
|
|
155
|
+
|
|
156
|
+
```js
|
|
157
|
+
import { scopedCSS } from 'ember-scoped-css/babel';
|
|
158
|
+
|
|
159
|
+
export default {
|
|
95
160
|
plugins: [
|
|
96
161
|
// ...
|
|
97
|
-
|
|
162
|
+
scopedCSS(),
|
|
98
163
|
[
|
|
99
164
|
'babel-plugin-ember-template-compilation',
|
|
100
165
|
{
|
|
101
166
|
// ...
|
|
102
|
-
transforms: [scopedCSS.
|
|
167
|
+
transforms: [scopedCSS.template({})],
|
|
103
168
|
},
|
|
104
169
|
],
|
|
105
170
|
// ...
|
|
106
171
|
],
|
|
107
172
|
// ...
|
|
108
173
|
};
|
|
109
|
-
|
|
110
174
|
```
|
|
111
175
|
|
|
112
|
-
|
|
113
|
-
```js
|
|
114
|
-
import * as scopedCss from 'ember-scoped-css/build';
|
|
176
|
+
There are two plugins, but you made only need one:
|
|
115
177
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
scopedCss.rollupPlugin(),
|
|
119
|
-
]
|
|
120
|
-
```
|
|
178
|
+
- `scopedCSS()` - handles removing the import for scopedCSS (which you'd use in GJS and GTS for intellisense, and TypeScript)
|
|
179
|
+
- `scopedCSS.template()` - transforms your template
|
|
121
180
|
|
|
122
|
-
|
|
181
|
+
##### Configuration Options (`scopedCSS.template()`)
|
|
123
182
|
|
|
124
|
-
|
|
183
|
+
- `layerName: string` - Wrap your CSS in a `@layer` with this given name
|
|
184
|
+
- `additionalRoots: string[]` - When you want to procss more folders. For example you want to support pods structure
|
|
125
185
|
|
|
126
|
-
|
|
186
|
+
### TypeScript
|
|
127
187
|
|
|
188
|
+
If you use TypeScript, or rely on information from TypeScript, you'll want to add the special attributes to the `<style>` element's attributes list.
|
|
189
|
+
These attributes don't exist in normal HTML, which is why they'd error without this change:
|
|
128
190
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
191
|
+
Requires
|
|
192
|
+
|
|
193
|
+
- @glint/ember-tsc 1.0.5 or higher
|
|
194
|
+
- @glint/template 1.7.0 or higher
|
|
195
|
+
- @glint/tsserver-plugin 2.0.5 or higher
|
|
196
|
+
|
|
197
|
+
In `types/index.d.ts` (or similar for apps) or `unpublished-development-types/index.d.ts` (for libraries), we'll declaration merge merge the known attributes for the `<style>` tag:
|
|
198
|
+
|
|
199
|
+
```ts
|
|
200
|
+
import '@glint/template';
|
|
201
|
+
|
|
202
|
+
declare global {
|
|
203
|
+
interface HTMLStyleElementAttributes {
|
|
204
|
+
scoped: '';
|
|
205
|
+
inline: '';
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
### template-lint
|
|
211
|
+
|
|
212
|
+
If you use [ember-template-lint](https://github.com/ember-template-lint/ember-template-lint) for linting the `<template>...</template>` regions of your components, you'll need to allow the `<style>` attribute to be used. The easiest way is to disable the [`no-forbidden-elements`](https://github.com/ember-template-lint/ember-template-lint/blob/main/docs/rule/no-forbidden-elements.md) rule:
|
|
132
213
|
|
|
133
|
-
|
|
134
|
-
|
|
214
|
+
```js
|
|
215
|
+
// the template-lintrc
|
|
216
|
+
{ // ...
|
|
217
|
+
overrides: [
|
|
135
218
|
{
|
|
136
|
-
|
|
137
|
-
|
|
219
|
+
files: ['**/*'],
|
|
220
|
+
rules: {
|
|
221
|
+
'no-forbidden-elements': false,
|
|
222
|
+
// or
|
|
223
|
+
'no-forbidden-elements': ['meta', 'html', 'script']
|
|
224
|
+
},
|
|
138
225
|
},
|
|
139
226
|
],
|
|
140
|
-
|
|
227
|
+
}
|
|
141
228
|
```
|
|
142
229
|
|
|
143
230
|
## Usage
|
|
@@ -235,10 +322,9 @@ NOTE: that if you're using pods, css co-located with templates/routes/etc will n
|
|
|
235
322
|
|
|
236
323
|
</details>
|
|
237
324
|
|
|
238
|
-
|
|
239
325
|
<details><summary>Difference with @scope</summary>
|
|
240
326
|
|
|
241
|
-
The [`@scope`](https://developer.mozilla.org/en-US/docs/Web/CSS/@scope) at-rule will scope all CSS defined within a `<style>` tag to the parent element.
|
|
327
|
+
The [`@scope`](https://developer.mozilla.org/en-US/docs/Web/CSS/@scope) at-rule will scope all CSS defined within a `<style>` tag to the parent element.
|
|
242
328
|
|
|
243
329
|
```html
|
|
244
330
|
<div>
|
|
@@ -274,6 +360,7 @@ But the nice thing is that you don't need to use classes with `@scope`.
|
|
|
274
360
|
A _potential_ downside to `@scope` is that it operates deeply -- where as `<style scoped>` will only apply its styles to the immediate component -- meaning that nested elements / components don't accidentally get surprise styles.
|
|
275
361
|
|
|
276
362
|
For example:
|
|
363
|
+
|
|
277
364
|
```gjs
|
|
278
365
|
const Inner = <template>
|
|
279
366
|
<p>
|
|
@@ -297,8 +384,8 @@ const Outer = <template>
|
|
|
297
384
|
```
|
|
298
385
|
|
|
299
386
|
Where as with `<style scoped>`
|
|
300
|
-
```gjs
|
|
301
387
|
|
|
388
|
+
```gjs
|
|
302
389
|
const Inner = <template>
|
|
303
390
|
<p>
|
|
304
391
|
inner (not red)
|
|
@@ -318,11 +405,8 @@ const Outer = <template>
|
|
|
318
405
|
</template>
|
|
319
406
|
```
|
|
320
407
|
|
|
321
|
-
|
|
322
408
|
</details>
|
|
323
409
|
|
|
324
|
-
|
|
325
|
-
|
|
326
410
|
### Passing classes as arguments to a component
|
|
327
411
|
|
|
328
412
|
There is a `scopedClass` helper that you can use to pass a class name as an argument to a component. The helper takes a class name and returns a scoped class name. `scopedClass` helper is replaced at build time so there is no performance hit when running the app.
|
|
@@ -376,10 +460,9 @@ test('MyComponent has hello-class', async function (assert) {
|
|
|
376
460
|
'use strict';
|
|
377
461
|
|
|
378
462
|
module.exports = {
|
|
379
|
-
|
|
463
|
+
plugins: [
|
|
380
464
|
+ 'ember-scoped-css/src/template-lint/plugin'
|
|
381
465
|
],
|
|
382
|
-
|
|
383
466
|
```
|
|
384
467
|
|
|
385
468
|
2. Add `scoped-class-helper` rule to `.template-lintrc.js`
|
|
@@ -388,14 +471,13 @@ module.exports = {
|
|
|
388
471
|
'use strict';
|
|
389
472
|
|
|
390
473
|
module.exports = {
|
|
391
|
-
|
|
474
|
+
plugins: [
|
|
392
475
|
'ember-scoped-css/src/template-lint/plugin'
|
|
393
476
|
],
|
|
394
477
|
rules: {
|
|
395
478
|
+ 'scoped-class-helper': 'error',
|
|
396
479
|
+ 'no-forbidden-elements': ['meta', 'html', 'script'], // style removed
|
|
397
480
|
}
|
|
398
|
-
|
|
399
481
|
```
|
|
400
482
|
|
|
401
483
|
## License
|