ember-scoped-css 3.5.1 → 3.5.3

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/dist/cjs/all.cjs CHANGED
@@ -1,3 +1,3 @@
1
- const require_all = require('./all-B28NLPAG.cjs');
1
+ const require_all = require('./all-BuNCMMma.cjs');
2
2
 
3
3
  exports.scopedCSS = require_all.scopedCSS;
@@ -1,4 +1,4 @@
1
- const require_all = require('./all-B28NLPAG.cjs');
1
+ const require_all = require('./all-BuNCMMma.cjs');
2
2
 
3
3
  //#region src/build/public-exports/babel.js
4
4
  const scopedCSS = require_all.scopedCSS.babel;
@@ -1,4 +1,4 @@
1
- const require_all = require('./all-B28NLPAG.cjs');
1
+ const require_all = require('./all-BuNCMMma.cjs');
2
2
 
3
3
  //#region src/build/public-exports/rollup.js
4
4
  const scopedCSS = require_all.scopedCSS.rollup;
package/dist/cjs/vite.cjs CHANGED
@@ -1,4 +1,4 @@
1
- const require_all = require('./all-B28NLPAG.cjs');
1
+ const require_all = require('./all-BuNCMMma.cjs');
2
2
 
3
3
  //#region src/build/public-exports/vite.js
4
4
  const scopedCSS = require_all.scopedCSS.vite;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ember-scoped-css",
3
- "version": "3.5.1",
3
+ "version": "3.5.3",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "ember-addon"
@@ -27,17 +27,36 @@
27
27
  "types": "./declarations/runtime/index.d.ts",
28
28
  "default": "./dist/runtime/index.js"
29
29
  },
30
+ "./__private_do_not_use_are_you_serious__/style-tag": "./src/build/style-tag.js",
30
31
  "./babel": {
31
- "import": "./src/build/public-exports/babel.js",
32
- "require": "./dist/cjs/babel.cjs"
32
+ "import": {
33
+ "types": "./src/build/public-exports/babel.d.ts",
34
+ "default": "./src/build/public-exports/babel.js"
35
+ },
36
+ "require": {
37
+ "types": "./src/build/public-exports/babel.d.cts",
38
+ "default": "./dist/cjs/babel.cjs"
39
+ }
33
40
  },
34
41
  "./rollup": {
35
- "import": "./src/build/public-exports/rollup.js",
36
- "require": "./dist/cjs/rollup.cjs"
42
+ "import": {
43
+ "types": "./src/build/public-exports/rollup.d.ts",
44
+ "default": "./src/build/public-exports/rollup.js"
45
+ },
46
+ "require": {
47
+ "types": "./src/build/public-exports/rollup.d.cts",
48
+ "default": "./dist/cjs/rollup.cjs"
49
+ }
37
50
  },
38
51
  "./vite": {
39
- "import": "./src/build/public-exports/vite.js",
40
- "require": "./dist/cjs/vite.cjs"
52
+ "import": {
53
+ "types": "./src/build/public-exports/vite.d.ts",
54
+ "default": "./src/build/public-exports/vite.js"
55
+ },
56
+ "require": {
57
+ "types": "./src/build/public-exports/vite.d.cts",
58
+ "default": "./dist/cjs/vite.cjs"
59
+ }
41
60
  },
42
61
  "./template-registry": {
43
62
  "types": "./template-registry.d.ts",
@@ -90,8 +109,10 @@
90
109
  "eslint": "^9.36.0",
91
110
  "jscodeshift": "^17.3.0",
92
111
  "prettier": "^3.6.2",
112
+ "rollup": "^4.34.9",
93
113
  "tsdown": "^0.15.12",
94
114
  "typescript": "^5.2.2",
115
+ "vite": "^6.2.0",
95
116
  "vitest": "^4.1.0",
96
117
  "webpack": "^5.98.0"
97
118
  },
@@ -132,6 +153,7 @@
132
153
  "lint:js:fix": "eslint . --fix",
133
154
  "lint:prettier:fix": "prettier . -w",
134
155
  "lint:prettier": "prettier . -c",
156
+ "lint:types": "tsc -p type-tests/tsconfig.bundler.json && tsc -p type-tests/tsconfig.nodenext.json",
135
157
  "start": "tsdown --watch",
136
158
  "test": "vitest"
137
159
  }
@@ -0,0 +1,29 @@
1
+ import type {
2
+ AdditionalRootsOptions,
3
+ ScopedCSSOptions,
4
+ TemplateOptions,
5
+ } from './options.cjs';
6
+
7
+ export type { AdditionalRootsOptions, ScopedCSSOptions, TemplateOptions };
8
+
9
+ export interface TemplateTransform {
10
+ (env: any): { name: string; visitor: object };
11
+ }
12
+
13
+ export interface ScopedCSSBabelPlugin {
14
+ (
15
+ api: unknown,
16
+ options?: AdditionalRootsOptions,
17
+ workingDirectory?: string,
18
+ ): { visitor: object };
19
+ }
20
+
21
+ export declare const scopedCSS: ((
22
+ options?: AdditionalRootsOptions,
23
+ ) => ScopedCSSBabelPlugin) & {
24
+ /**
25
+ * The template transform, for the `transforms` option of
26
+ * `babel-plugin-ember-template-compilation`.
27
+ */
28
+ template: (options: TemplateOptions) => TemplateTransform;
29
+ };
@@ -0,0 +1,29 @@
1
+ import type {
2
+ AdditionalRootsOptions,
3
+ ScopedCSSOptions,
4
+ TemplateOptions,
5
+ } from './options.cjs';
6
+
7
+ export type { AdditionalRootsOptions, ScopedCSSOptions, TemplateOptions };
8
+
9
+ export interface TemplateTransform {
10
+ (env: any): { name: string; visitor: object };
11
+ }
12
+
13
+ export interface ScopedCSSBabelPlugin {
14
+ (
15
+ api: unknown,
16
+ options?: AdditionalRootsOptions,
17
+ workingDirectory?: string,
18
+ ): { visitor: object };
19
+ }
20
+
21
+ export declare const scopedCSS: ((
22
+ options?: AdditionalRootsOptions,
23
+ ) => ScopedCSSBabelPlugin) & {
24
+ /**
25
+ * The template transform, for the `transforms` option of
26
+ * `babel-plugin-ember-template-compilation`.
27
+ */
28
+ template: (options: TemplateOptions) => TemplateTransform;
29
+ };
@@ -0,0 +1,18 @@
1
+ export interface ScopedCSSOptions {
2
+ /**
3
+ * Wrap your CSS in a `@layer` with this given name.
4
+ */
5
+ layerName?: string;
6
+ }
7
+
8
+ export interface AdditionalRootsOptions {
9
+ /**
10
+ * Process additional directories, for example pods or directories from other
11
+ * packages in a monorepo.
12
+ */
13
+ additionalRoots?: string[];
14
+ }
15
+
16
+ export interface TemplateOptions
17
+ extends ScopedCSSOptions,
18
+ AdditionalRootsOptions {}
@@ -0,0 +1,10 @@
1
+ import type { Plugin } from 'rollup';
2
+
3
+ import type { ScopedCSSOptions } from './options.cjs';
4
+
5
+ export type { ScopedCSSOptions };
6
+
7
+ /**
8
+ * The Rollup plugins that scope co-located CSS files and `<style scoped>` elements.
9
+ */
10
+ export declare function scopedCSS(options?: ScopedCSSOptions): Plugin[];
@@ -0,0 +1,10 @@
1
+ import type { Plugin } from 'rollup';
2
+
3
+ import type { ScopedCSSOptions } from './options.cjs';
4
+
5
+ export type { ScopedCSSOptions };
6
+
7
+ /**
8
+ * The Rollup plugins that scope co-located CSS files and `<style scoped>` elements.
9
+ */
10
+ export declare function scopedCSS(options?: ScopedCSSOptions): Plugin[];
@@ -0,0 +1,10 @@
1
+ import type { Plugin } from 'vite' with { 'resolution-mode': 'import' };
2
+
3
+ import type { ScopedCSSOptions } from './options.cjs';
4
+
5
+ export type { ScopedCSSOptions };
6
+
7
+ /**
8
+ * The Vite plugins that scope co-located CSS files and `<style scoped>` elements.
9
+ */
10
+ export declare function scopedCSS(options?: ScopedCSSOptions): Plugin[];
@@ -0,0 +1,10 @@
1
+ import type { Plugin } from 'vite';
2
+
3
+ import type { ScopedCSSOptions } from './options.cjs';
4
+
5
+ export type { ScopedCSSOptions };
6
+
7
+ /**
8
+ * The Vite plugins that scope co-located CSS files and `<style scoped>` elements.
9
+ */
10
+ export declare function scopedCSS(options?: ScopedCSSOptions): Plugin[];
@@ -0,0 +1,77 @@
1
+ /**
2
+ * How a `<style>` element in a template is read: whether the build scopes it,
3
+ * and whether its contents need a preprocessor before anything can treat them
4
+ * as CSS.
5
+ *
6
+ * Thanks, CardStack and @ef4 for this code.
7
+ */
8
+
9
+ const SCOPED_ATTRIBUTE_NAME = 'scoped';
10
+ const INLINE_ATTRIBUTE_NAME = 'inline';
11
+ const LANG_ATTRIBUTE_NAME = 'lang';
12
+
13
+ /**
14
+ * @param {object} [node]
15
+ * @returns {boolean | undefined}
16
+ */
17
+ export function hasScopedAttribute(node) {
18
+ if (!node) return;
19
+ if (node.tag !== 'style') return;
20
+ if (node.type !== 'ElementNode') return;
21
+
22
+ return node.attributes.some(
23
+ (attribute) => attribute.name === SCOPED_ATTRIBUTE_NAME,
24
+ );
25
+ }
26
+
27
+ /**
28
+ * @param {object} [node]
29
+ * @returns {boolean | undefined}
30
+ */
31
+ export function hasInlineAttributeWithoutLang(node) {
32
+ if (!node) return;
33
+ if (node.tag !== 'style') return;
34
+ if (node.type !== 'ElementNode') return;
35
+
36
+ if (getLangAttribute(node)) {
37
+ return false;
38
+ }
39
+
40
+ return node.attributes.some(
41
+ (attribute) => attribute.name === INLINE_ATTRIBUTE_NAME,
42
+ );
43
+ }
44
+
45
+ /**
46
+ * Returns the value of the `lang` attribute on a `<style>` node, or null if absent.
47
+ *
48
+ * @param {object} [node]
49
+ * @returns {string | null}
50
+ */
51
+ export function getLangAttribute(node) {
52
+ if (!node) return null;
53
+ if (node.tag !== 'style') return null;
54
+ if (node.type !== 'ElementNode') return null;
55
+
56
+ const attr = node.attributes.find(
57
+ (attribute) => attribute.name === LANG_ATTRIBUTE_NAME,
58
+ );
59
+
60
+ if (!attr) return null;
61
+
62
+ // The attribute value is a TextNode child of the AttrNode's value
63
+ const value = attr.value;
64
+
65
+ if (value?.type === 'TextNode') return value.chars || null;
66
+
67
+ return null;
68
+ }
69
+
70
+ /** File extensions that Vite can preprocess via its CSS preprocessor pipeline */
71
+ export const PREPROCESSED_EXTENSIONS = new Set([
72
+ '.scss',
73
+ '.sass',
74
+ '.less',
75
+ '.styl',
76
+ '.stylus',
77
+ ]);
@@ -19,6 +19,11 @@ import {
19
19
  } from '../lib/path/utils.js';
20
20
  import { request } from '../lib/request.js';
21
21
  import { templatePlugin } from '../lib/rewriteHbs.js';
22
+ import {
23
+ getLangAttribute,
24
+ hasInlineAttributeWithoutLang,
25
+ hasScopedAttribute,
26
+ } from './style-tag.js';
22
27
 
23
28
  const noopPlugin = {
24
29
  name: 'ember-scoped-css:noop',
@@ -129,41 +134,53 @@ export function createPlugin(config) {
129
134
  /**
130
135
  * We only allow a scoped <style> at the root
131
136
  */
132
- let styleTag = node.body.find(
133
- (n) => n.type === 'ElementNode' && n.tag === 'style',
137
+ let styleTags = node.body.filter(
138
+ (n) =>
139
+ n.type === 'ElementNode' &&
140
+ n.tag === 'style' &&
141
+ hasScopedAttribute(n),
134
142
  );
135
143
 
136
- if (hasScopedAttribute(styleTag)) {
137
- let css = textContent(styleTag);
138
- let lang = getLangAttribute(styleTag);
139
- let info = getCSSContentInfo(css, lang);
144
+ if (styleTags.length > 1) {
145
+ throw new Error(
146
+ 'Only one <style scoped> is supported per template, ' +
147
+ `but ${styleTags.length} were found. Merge them into one.`,
148
+ );
149
+ }
140
150
 
141
- addInfo(info);
151
+ let styleTag = styleTags[0];
142
152
 
143
- if (hasInlineAttributeWithoutLang(styleTag)) {
144
- /**
145
- * This will be handled in ElementNode traversal
146
- */
147
- return;
148
- }
153
+ if (!styleTag) return;
149
154
 
150
- if (lang) {
151
- /**
152
- * For <style scoped inline lang="..."> we cannot preprocess at Babel-time
153
- * (preprocessing is async and requires Vite's ResolvedConfig).
154
- * Remove the tag and inject via virtual CSS module and warn user.
155
- */
156
- console.warn(
157
- `[ember-scoped-css] <style scoped inline lang="${lang}"> is not supported ` +
158
- `(preprocessing is async and cannot run at Babel-time). ` +
159
- `Downgrading to non-inline: the style tag will be removed and injected as a virtual CSS module.`,
160
- );
161
- }
155
+ let css = textContent(styleTag);
156
+ let lang = getLangAttribute(styleTag);
157
+ let info = getCSSContentInfo(css, lang);
158
+
159
+ addInfo(info);
162
160
 
163
- let cssRequest = request.inline.create(info.id, postfix, css, lang);
161
+ if (hasInlineAttributeWithoutLang(styleTag)) {
162
+ /**
163
+ * This will be handled in ElementNode traversal
164
+ */
165
+ return;
166
+ }
164
167
 
165
- env.meta.jsutils.importForSideEffect(cssRequest);
168
+ if (lang) {
169
+ /**
170
+ * For <style scoped inline lang="..."> we cannot preprocess at Babel-time
171
+ * (preprocessing is async and requires Vite's ResolvedConfig).
172
+ * Remove the tag and inject via virtual CSS module and warn user.
173
+ */
174
+ console.warn(
175
+ `[ember-scoped-css] <style scoped inline lang="${lang}"> is not supported ` +
176
+ `(preprocessing is async and cannot run at Babel-time). ` +
177
+ `Downgrading to non-inline: the style tag will be removed and injected as a virtual CSS module.`,
178
+ );
166
179
  }
180
+
181
+ let cssRequest = request.inline.create(info.id, postfix, css, lang);
182
+
183
+ env.meta.jsutils.importForSideEffect(cssRequest);
167
184
  },
168
185
 
169
186
  // Visitors broken out like this so we can conditionally
@@ -224,62 +241,6 @@ export function createPlugin(config) {
224
241
  };
225
242
  }
226
243
 
227
- /**
228
- * Thanks, CardStack and @ef4 for this code.
229
- */
230
- const SCOPED_ATTRIBUTE_NAME = 'scoped';
231
- const INLINE_ATTRIBUTE_NAME = 'inline';
232
- const LANG_ATTRIBUTE_NAME = 'lang';
233
-
234
- function hasScopedAttribute(node) {
235
- if (!node) return;
236
- if (node.tag !== 'style') return;
237
- if (node.type !== 'ElementNode') return;
238
-
239
- return node.attributes.some(
240
- (attribute) => attribute.name === SCOPED_ATTRIBUTE_NAME,
241
- );
242
- }
243
-
244
- function hasInlineAttributeWithoutLang(node) {
245
- if (!node) return;
246
- if (node.tag !== 'style') return;
247
- if (node.type !== 'ElementNode') return;
248
-
249
- if (getLangAttribute(node)) {
250
- return false;
251
- }
252
-
253
- return node.attributes.some(
254
- (attribute) => attribute.name === INLINE_ATTRIBUTE_NAME,
255
- );
256
- }
257
-
258
- /**
259
- * Returns the value of the `lang` attribute on a `<style>` node, or null if absent.
260
- *
261
- * @param {object} node
262
- * @returns {string | null}
263
- */
264
- function getLangAttribute(node) {
265
- if (!node) return null;
266
- if (node.tag !== 'style') return null;
267
- if (node.type !== 'ElementNode') return null;
268
-
269
- const attr = node.attributes.find(
270
- (attribute) => attribute.name === LANG_ATTRIBUTE_NAME,
271
- );
272
-
273
- if (!attr) return null;
274
-
275
- // The attribute value is a TextNode child of the AttrNode's value
276
- const value = attr.value;
277
-
278
- if (value?.type === 'TextNode') return value.chars || null;
279
-
280
- return null;
281
- }
282
-
283
244
  function textContent(node) {
284
245
  let textChildren = node.children.filter((c) => c.type === 'TextNode');
285
246
 
@@ -333,3 +333,54 @@ describe('lang attribute (SCSS preprocessor)', () => {
333
333
  `);
334
334
  });
335
335
  });
336
+
337
+ /**
338
+ * https://github.com/auditboard/ember-scoped-css/issues/423
339
+ */
340
+ describe('a scoped <style> is found regardless of what precedes it', () => {
341
+ it('extracts the scoped CSS when a global <style> comes first', async () => {
342
+ let output = await transform(`
343
+ export const Foo = <template>
344
+ <div class="scoped">hi</div>
345
+ <style>
346
+ .global { color: red; }
347
+ </style>
348
+ <style scoped>
349
+ .scoped { color: blue; }
350
+ </style>
351
+ </template>;
352
+ `);
353
+
354
+ expect(templateContentsOf(output)).toMatchInlineSnapshot(`
355
+ [
356
+ "<div class="scoped_e65d154a1">hi</div>
357
+ <style>
358
+ .global { color: red; }
359
+ </style>",
360
+ ]
361
+ `);
362
+ expect(virtualImportUrlsOf(output)).toMatchInlineSnapshot(`
363
+ [
364
+ "./e65d154a1___css-68ede36d709bfa7f8a2994e1702ef010.ember-scoped.css?css=%0A%20%20%20%20%20%20%20%20%20%20.scoped%20%7B%20color%3A%20blue%3B%20%7D%0A%20%20%20%20%20%20%20%20",
365
+ ]
366
+ `);
367
+ });
368
+
369
+ it('refuses a second <style scoped> rather than dropping its CSS', async () => {
370
+ let build = transform(`
371
+ export const Foo = <template>
372
+ <div class="first second">hi</div>
373
+ <style scoped>
374
+ .first { color: blue; }
375
+ </style>
376
+ <style scoped>
377
+ .second { color: green; }
378
+ </style>
379
+ </template>;
380
+ `);
381
+
382
+ await expect(build).rejects.toThrow(
383
+ /Only one <style scoped> is supported per template, but 2 were found/,
384
+ );
385
+ });
386
+ });
@@ -4,18 +4,10 @@ import path from 'node:path';
4
4
 
5
5
  import { rewriteCss } from '../lib/css/rewrite.js';
6
6
  import { request } from '../lib/request.js';
7
+ import { PREPROCESSED_EXTENSIONS } from './style-tag.js';
7
8
 
8
9
  const META = 'scoped-css:colocated';
9
10
 
10
- /** File extensions that Vite can preprocess via its CSS preprocessor pipeline */
11
- const PREPROCESSED_EXTENSIONS = new Set([
12
- '.scss',
13
- '.sass',
14
- '.less',
15
- '.styl',
16
- '.stylus',
17
- ]);
18
-
19
11
  /**
20
12
  * Plugin for supporting colocated styles
21
13
  *