@stencil/vitest 1.11.2 → 1.11.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/plugin.d.ts CHANGED
@@ -30,8 +30,10 @@ import type { Plugin } from 'vitest/config';
30
30
  * },
31
31
  * });
32
32
  * ```
33
- *
33
+ * @param opts Optional configuration for the plugin
34
34
  * @returns a Vite plugin configuration object
35
35
  */
36
- export declare function stencilVitestPlugin(): Plugin;
36
+ export declare function stencilVitestPlugin(opts?: {
37
+ css?: boolean;
38
+ }): Plugin;
37
39
  //# sourceMappingURL=plugin.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAE5C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,wBAAgB,mBAAmB,IAAI,MAAM,CAuD5C"}
1
+ {"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAE5C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,GAAE;IAAE,GAAG,CAAC,EAAE,OAAO,CAAA;CAAO,GAAG,MAAM,CAkFxE"}
package/dist/plugin.js CHANGED
@@ -1,3 +1,6 @@
1
+ import { transpile } from '@stencil/core/compiler';
2
+ import { existsSync, readFileSync } from 'node:fs';
3
+ import { dirname, resolve } from 'node:path';
1
4
  /**
2
5
  * A Vite/Vitest plugin that transforms Stencil component source files (.tsx) on-the-fly,
3
6
  * enabling module mocking and direct source imports during tests.
@@ -29,10 +32,10 @@
29
32
  * },
30
33
  * });
31
34
  * ```
32
- *
35
+ * @param opts Optional configuration for the plugin
33
36
  * @returns a Vite plugin configuration object
34
37
  */
35
- export function stencilVitestPlugin() {
38
+ export function stencilVitestPlugin(opts = {}) {
36
39
  return {
37
40
  name: 'stencil-vitest-transform',
38
41
  enforce: 'pre',
@@ -52,31 +55,56 @@ export function stencilVitestPlugin() {
52
55
  if (!hasStencilDecorator) {
53
56
  return null;
54
57
  }
55
- const { transpileSync } = await import('@stencil/core/compiler');
56
- const result = transpileSync(code, {
57
- file: id,
58
- // 'customelement' appends a customElements.define() call so the component
59
- // self-registers the moment this module is imported — no loader needed.
60
- componentExport: 'customelement',
61
- componentMetadata: 'compilerstatic',
62
- currentDirectory: process.cwd(),
63
- module: 'esm',
64
- proxy: null,
65
- sourceMap: false,
66
- style: 'static',
67
- styleImportData: 'queryparams',
68
- target: 'es2022',
69
- // Don't rewrite import paths — let Vite handle resolution via aliases
70
- transformAliasedImportPaths: false,
71
- });
72
- const errors = result.diagnostics?.filter((d) => d.level === 'error') ?? [];
73
- if (errors.length > 0) {
74
- const messages = errors.map((d) => d.messageText).join('\n');
75
- throw new Error(`[stencil-vitest-plugin] Transform error in ${id}:\n${messages}`);
58
+ try {
59
+ const result = await transpile(code, {
60
+ file: id,
61
+ // 'customelement' appends a customElements.define() call so the component
62
+ // self-registers the moment this module is imported — no loader needed.
63
+ componentExport: 'customelement',
64
+ componentMetadata: 'compilerstatic',
65
+ currentDirectory: process.cwd(),
66
+ module: 'esm',
67
+ proxy: null,
68
+ sourceMap: false,
69
+ style: opts.css ? 'inline' : null,
70
+ styleImportData: opts.css ? 'queryparams' : null,
71
+ target: 'es2022',
72
+ // Don't rewrite import paths — let Vite handle resolution via aliases
73
+ transformAliasedImportPaths: false,
74
+ });
75
+ const errors = result.diagnostics?.filter((d) => d.level === 'error') ?? [];
76
+ if (errors.length > 0) {
77
+ const messages = errors.map((d) => d.messageText).join('\n');
78
+ throw new Error(`[stencil-vitest-plugin] Transform error in ${id}:\n${messages}`);
79
+ }
80
+ let transformedCode = result.code;
81
+ // If CSS is enabled, inline the CSS imports as functions, - Stencil v4 always expects css to be a function, Vite
82
+ // tries to handle the import as a string and errors.
83
+ // In v5, Stencil supports both string and function styles, but the plugin opts for function style for consistency across versions.
84
+ if (opts.css) {
85
+ // Match: import SomeVar from "./path.css?tag=...&encapsulation=...";
86
+ const cssImportRegex = /import\s+(\w+)\s+from\s+['"]([^'"]+\.css\?tag[^'"]+)['"]\s*;?/g;
87
+ let match;
88
+ while ((match = cssImportRegex.exec(result.code)) !== null) {
89
+ const [fullMatch, varName, importPath] = match;
90
+ const [relPath] = importPath.split('?');
91
+ const absolutePath = resolve(dirname(id), relPath);
92
+ if (existsSync(absolutePath)) {
93
+ const css = readFileSync(absolutePath, 'utf-8');
94
+ // Replace the import with an inline function
95
+ const inlineFunc = `const ${varName} = () => ${JSON.stringify(css)};`;
96
+ transformedCode = transformedCode.replace(fullMatch, inlineFunc);
97
+ }
98
+ }
99
+ }
100
+ return {
101
+ code: transformedCode,
102
+ };
103
+ }
104
+ catch (err) {
105
+ console.error(`[stencil-vitest-plugin] Failed to transform ${id}:`, err);
106
+ throw err;
76
107
  }
77
- return {
78
- code: result.code,
79
- };
80
108
  },
81
109
  };
82
110
  }
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "type": "git",
5
5
  "url": "https://github.com/stenciljs/vitest"
6
6
  },
7
- "version": "1.11.2",
7
+ "version": "1.11.3",
8
8
  "description": "First-class testing utilities for Stencil design systems with Vitest",
9
9
  "license": "MIT",
10
10
  "type": "module",
@@ -104,7 +104,7 @@
104
104
  "dependencies": {
105
105
  "jiti": "^2.6.1",
106
106
  "local-pkg": "^1.1.2",
107
- "vitest-environment-stencil": "1.11.2"
107
+ "vitest-environment-stencil": "1.11.3"
108
108
  },
109
109
  "devDependencies": {
110
110
  "@eslint/js": "^9.39.2",