ember-scoped-css 1.1.0 → 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.
Files changed (83) hide show
  1. package/README.md +203 -47
  2. package/dist/cjs/all-Du43cBa9.cjs +1308 -0
  3. package/dist/cjs/all.cjs +3 -0
  4. package/dist/cjs/babel.cjs +9 -0
  5. package/dist/cjs/rollup.cjs +8 -0
  6. package/dist/cjs/vite.cjs +8 -0
  7. package/dist/runtime/index.d.ts +11 -0
  8. package/dist/runtime/index.d.ts.map +1 -0
  9. package/dist/runtime/index.js +12 -8
  10. package/dist/runtime/index.js.map +1 -7
  11. package/{declarations → dist}/runtime/test-support.d.ts +5 -2
  12. package/dist/runtime/test-support.d.ts.map +1 -0
  13. package/dist/runtime/test-support.js +357 -182
  14. package/dist/runtime/test-support.js.map +1 -7
  15. package/package.json +21 -24
  16. package/src/build/babel-plugin.js +4 -1
  17. package/src/build/public-exports/all.js +10 -0
  18. package/src/build/public-exports/babel.js +5 -0
  19. package/src/build/public-exports/rollup.js +3 -0
  20. package/src/build/public-exports/vite.js +3 -0
  21. package/src/build/scoped-css-unplugin.js +10 -33
  22. package/src/build/template-plugin.js +9 -19
  23. package/src/build/template-plugin.test.ts +108 -19
  24. package/src/build/unplugin-colocated.js +103 -0
  25. package/src/build/unplugin-inline.js +71 -0
  26. package/src/lib/css/rewrite.js +8 -8
  27. package/src/lib/css/rewrite.test.ts +69 -128
  28. package/src/lib/path/const.js +19 -0
  29. package/src/lib/path/hash-from-absolute-path.js +3 -2
  30. package/src/lib/path/template-transform-paths.js +3 -2
  31. package/src/lib/path/utils.appPath.test.ts +2 -2
  32. package/src/lib/path/utils.hashFrom.test.ts +2 -2
  33. package/src/lib/path/utils.js +63 -20
  34. package/src/lib/request.js +58 -33
  35. package/src/runtime/index.ts +1 -1
  36. package/declarations/build/template-plugin.d.ts +0 -7
  37. package/declarations/build/template-plugin.d.ts.map +0 -1
  38. package/declarations/build/template-plugin.test.d.ts +0 -2
  39. package/declarations/build/template-plugin.test.d.ts.map +0 -1
  40. package/declarations/lib/css/rewrite.d.ts +0 -2
  41. package/declarations/lib/css/rewrite.d.ts.map +0 -1
  42. package/declarations/lib/css/rewrite.test.d.ts +0 -2
  43. package/declarations/lib/css/rewrite.test.d.ts.map +0 -1
  44. package/declarations/lib/css/utils.d.ts +0 -29
  45. package/declarations/lib/css/utils.d.ts.map +0 -1
  46. package/declarations/lib/path/hash-from-absolute-path.d.ts +0 -3
  47. package/declarations/lib/path/hash-from-absolute-path.d.ts.map +0 -1
  48. package/declarations/lib/path/hash-from-absolute-path.test.d.ts +0 -2
  49. package/declarations/lib/path/hash-from-absolute-path.test.d.ts.map +0 -1
  50. package/declarations/lib/path/hash-from-module-path.d.ts +0 -31
  51. package/declarations/lib/path/hash-from-module-path.d.ts.map +0 -1
  52. package/declarations/lib/path/md5.d.ts +0 -12
  53. package/declarations/lib/path/md5.d.ts.map +0 -1
  54. package/declarations/lib/path/template-transform-paths.d.ts +0 -11
  55. package/declarations/lib/path/template-transform-paths.d.ts.map +0 -1
  56. package/declarations/lib/path/template-transform-paths.test.d.ts +0 -2
  57. package/declarations/lib/path/template-transform-paths.test.d.ts.map +0 -1
  58. package/declarations/lib/path/utils.appPath.test.d.ts +0 -2
  59. package/declarations/lib/path/utils.appPath.test.d.ts.map +0 -1
  60. package/declarations/lib/path/utils.d.ts +0 -94
  61. package/declarations/lib/path/utils.d.ts.map +0 -1
  62. package/declarations/lib/path/utils.findWorkspacePath.test.d.ts +0 -2
  63. package/declarations/lib/path/utils.findWorkspacePath.test.d.ts.map +0 -1
  64. package/declarations/lib/path/utils.hashFrom.test.d.ts +0 -2
  65. package/declarations/lib/path/utils.hashFrom.test.d.ts.map +0 -1
  66. package/declarations/lib/path/utils.isRelevantFile.test.d.ts +0 -2
  67. package/declarations/lib/path/utils.isRelevantFile.test.d.ts.map +0 -1
  68. package/declarations/lib/path/utils.paths.test.d.ts +0 -5
  69. package/declarations/lib/path/utils.paths.test.d.ts.map +0 -1
  70. package/declarations/lib/renameClass.d.ts +0 -9
  71. package/declarations/lib/renameClass.d.ts.map +0 -1
  72. package/declarations/lib/request.d.ts +0 -8
  73. package/declarations/lib/request.d.ts.map +0 -1
  74. package/declarations/lib/rewriteHbs.d.ts +0 -17
  75. package/declarations/lib/rewriteHbs.d.ts.map +0 -1
  76. package/declarations/runtime/index.d.ts +0 -8
  77. package/declarations/runtime/index.d.ts.map +0 -1
  78. package/declarations/runtime/test-support.d.ts.map +0 -1
  79. package/dist/cjs/babel-plugin.cjs +0 -176
  80. package/dist/cjs/index.cjs +0 -968
  81. package/dist/cjs/template-plugin.cjs +0 -856
  82. package/src/build/index.js +0 -7
  83. package/src/build/vite.js +0 -3
package/README.md CHANGED
@@ -15,25 +15,22 @@ const greeting = "hello world";
15
15
  </style>
16
16
  </template>
17
17
  ```
18
+
18
19
  becomes the equivelent of;
20
+
19
21
  ```gjs
22
+ import './abcd1234.css'; // containing your CSS, but with selectors scoped
20
23
  const greeting = "hello world";
21
24
 
22
25
  <template>
23
26
  <div class="abcd1234">{{greeting}}</div>
24
-
25
- <style scoped>
26
- div.abcd1234 {
27
- color: blue;
28
- }
29
- </style>
30
27
  </template>
31
28
  ```
32
29
 
33
30
  This is a build-time-only addon, so there is no need to worry about runtime performance.
34
31
 
35
32
  You can also write your styles as a co-located `.css` file, right next to your `.gjs`/`.gts` files.
36
- 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.
37
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.
38
35
 
39
36
  _See [Usage](#usage) for details_.
@@ -42,7 +39,7 @@ If you want to read more specifics on how this addon achieves isolation with CSS
42
39
 
43
40
  As selectors are scoped/renamed during the build process. So there is no performance hit when running the app.
44
41
 
45
- 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.
46
43
 
47
44
  You may also find the docs on [CSS `@layer`](https://developer.mozilla.org/en-US/docs/Web/CSS/@layer) interesting.
48
45
  This build tool can emit CSS in a `@layer`.
@@ -51,14 +48,14 @@ This build tool can emit CSS in a `@layer`.
51
48
 
52
49
  - Vite
53
50
  - V2 addons with `@embroider/addon-dev` @ v8+ (or similar)
54
-
55
- For [bugfixes for the pre-ember-scoped-css-1.0 code, PR here](https://github.com/auditboard/ember-scoped-css/tree/hbs-classic-and-webpack-support)
51
+ - For [hbs, broccoli, or webpack-based builds, view this version of the docs](https://github.com/auditboard/ember-scoped-css/tree/hbs-classic-and-webpack-support)
52
+ - For [bugfixes for the pre-ember-scoped-css-1.0 code, PR here](https://github.com/auditboard/ember-scoped-css/tree/hbs-classic-and-webpack-support)
56
53
 
57
54
  | You Have | ember-scoped-css | ember-scoped-css-compat | docs |
58
55
  | -------- | ----------- | ---------------------- | --- |
59
56
  | vite | >= 1.0.0 | 🚫 | [main][docs-main]
60
57
  | gjs / gts library (no hbs) | >= 1.0.0 | 🚫 | [main][docs-main]
61
- | 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]
62
59
  | hbs | <= 0.24.3 | <= 10.0.0 | [0.24.3][docs-2]
63
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]
64
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]
@@ -70,7 +67,7 @@ For [bugfixes for the pre-ember-scoped-css-1.0 code, PR here](https://github.com
70
67
  [docs-3]: https://github.com/auditboard/ember-scoped-css/tree/v0.19.1-ember-scoped-css
71
68
  [docs-4]: https://github.com/auditboard/ember-scoped-css/tree/ember-scoped-css%400.18.0
72
69
 
73
- ## Installation for a Vite app
70
+ ## Installation
74
71
 
75
72
  ```bash
76
73
  npm install --save-dev ember-scoped-css
@@ -78,7 +75,16 @@ npm install --save-dev ember-scoped-css
78
75
 
79
76
  ### Configuration
80
77
 
81
- In your `vite.config.js`, import and add the `scopedCSS` plugin:
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
+
82
88
  ```js
83
89
  import { defineConfig } from 'vite';
84
90
  import { scopedCSS } from 'ember-scoped-css/vite';
@@ -92,64 +98,133 @@ export default defineConfig({
92
98
  });
93
99
  ```
94
100
 
95
- and then in your `babel.config.mjs`, add a template-transform:
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
+
139
+ ```js
140
+ import { scopedCSS } from 'ember-scoped-css/rollup';
141
+
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
+
96
156
  ```js
97
- import * as scopedCSS from "ember-scoped-css/build";
157
+ import { scopedCSS } from 'ember-scoped-css/babel';
98
158
 
99
- module.exports = {
159
+ export default {
100
160
  plugins: [
101
161
  // ...
102
- [scopedCSS.babelPlugin, {}],
162
+ scopedCSS(),
103
163
  [
104
164
  'babel-plugin-ember-template-compilation',
105
165
  {
106
166
  // ...
107
- transforms: [scopedCSS.templatePlugin({})],
167
+ transforms: [scopedCSS.template({})],
108
168
  },
109
169
  ],
110
170
  // ...
111
171
  ],
112
172
  // ...
113
173
  };
114
-
115
174
  ```
116
175
 
117
- If you have a rollup config:
118
- ```js
119
- import * as scopedCss from 'ember-scoped-css/build';
176
+ There are two plugins, but you made only need one:
120
177
 
121
- // ...
122
- plugins: [
123
- scopedCss.rollupPlugin(),
124
- ]
125
- ```
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
126
180
 
181
+ ##### Configuration Options (`scopedCSS.template()`)
127
182
 
128
- ## Installation for an embroider app
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
129
185
 
130
- ```bash
131
- npm install --save-dev ember-scoped-css ember-scoped-css-compat
132
- ```
186
+ ### TypeScript
133
187
 
134
- ### Configuration
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:
135
190
 
136
- All forms of `scopedCss` take an options hash except for the rollup and vite plugins.
191
+ Requires
137
192
 
138
- Configuration in the two locations in the babel config should match, for example:
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
139
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:
140
198
 
141
- ```js
142
- plugins: [
143
- [scopedCSS.babelPlugin, { layerName: 'my-library' }],
199
+ ```ts
200
+ import '@glint/template';
144
201
 
145
- [
146
- 'babel-plugin-ember-template-compilation',
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:
213
+
214
+ ```js
215
+ // the template-lintrc
216
+ { // ...
217
+ overrides: [
147
218
  {
148
- targetFormat: 'hbs',
149
- transforms: [scopedCSS.templatePlugin({ layerName: 'my-libarry' })],
219
+ files: ['**/*'],
220
+ rules: {
221
+ 'no-forbidden-elements': false,
222
+ // or
223
+ 'no-forbidden-elements': ['meta', 'html', 'script']
224
+ },
150
225
  },
151
226
  ],
152
- ],
227
+ }
153
228
  ```
154
229
 
155
230
  ## Usage
@@ -234,7 +309,7 @@ Note that `<style>` (without `scoped`) will continue to work as it does today an
234
309
  }
235
310
 
236
311
  /* the :global() pseudo-class is used to define a global class. It mean that header class wont be scoped to that component */
237
- .hello-class:global(.header) {
312
+ :global(.header) {
238
313
  font-size: 20px;
239
314
  }
240
315
 
@@ -247,7 +322,90 @@ NOTE: that if you're using pods, css co-located with templates/routes/etc will n
247
322
 
248
323
  </details>
249
324
 
325
+ <details><summary>Difference with @scope</summary>
326
+
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.
328
+
329
+ ```html
330
+ <div>
331
+ <style>
332
+ @scope {
333
+ p { color: red; }
334
+ }
335
+ </style>
336
+
337
+ <p>this is red</p>
338
+ </div>
339
+
340
+ <p>not red</p>
341
+ ```
342
+
343
+ In this example, it is effectively the same as:
344
+
345
+ ```html
346
+ <!-- style scoped must be at the root of the template-area -->
347
+ <style scoped>
348
+ p.inner { color: red; }
349
+ </style>
350
+
351
+ <div>
352
+ <p class="inner">this is red</p>
353
+ </div>
354
+
355
+ <p>not red</p>
356
+ ```
250
357
 
358
+ But the nice thing is that you don't need to use classes with `@scope`.
359
+
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.
361
+
362
+ For example:
363
+
364
+ ```gjs
365
+ const Inner = <template>
366
+ <p>
367
+ inner (also red)
368
+ </p>
369
+ </template>;
370
+
371
+ const Outer = <template>
372
+ <div>
373
+ <style>
374
+ @scope {
375
+ p { color: red; }
376
+ }
377
+ </style>
378
+
379
+ <p>this is red</p>
380
+
381
+ <Inner />
382
+ </div>
383
+ </template>
384
+ ```
385
+
386
+ Where as with `<style scoped>`
387
+
388
+ ```gjs
389
+ const Inner = <template>
390
+ <p>
391
+ inner (not red)
392
+ </p>
393
+ </template>;
394
+
395
+ const Outer = <template>
396
+ <style scoped>
397
+ p { color: red; }
398
+ </style>
399
+
400
+ <div>
401
+ <p>this is red</p>
402
+
403
+ <Inner />
404
+ </div>
405
+ </template>
406
+ ```
407
+
408
+ </details>
251
409
 
252
410
  ### Passing classes as arguments to a component
253
411
 
@@ -302,10 +460,9 @@ test('MyComponent has hello-class', async function (assert) {
302
460
  'use strict';
303
461
 
304
462
  module.exports = {
305
- plugins: [
463
+ plugins: [
306
464
  + 'ember-scoped-css/src/template-lint/plugin'
307
465
  ],
308
-
309
466
  ```
310
467
 
311
468
  2. Add `scoped-class-helper` rule to `.template-lintrc.js`
@@ -314,14 +471,13 @@ module.exports = {
314
471
  'use strict';
315
472
 
316
473
  module.exports = {
317
- plugins: [
474
+ plugins: [
318
475
  'ember-scoped-css/src/template-lint/plugin'
319
476
  ],
320
477
  rules: {
321
478
  + 'scoped-class-helper': 'error',
322
479
  + 'no-forbidden-elements': ['meta', 'html', 'script'], // style removed
323
480
  }
324
-
325
481
  ```
326
482
 
327
483
  ## License