@stencil/unplugin 5.0.0-beta.9 → 5.0.0-rc.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 CHANGED
@@ -77,6 +77,78 @@ module.exports = {
77
77
  };
78
78
  ```
79
79
 
80
+ ## Testing with Vitest
81
+
82
+ ### `newSpecPage()` tests
83
+
84
+ `stencilSpecPage()` is a Vite plugin for your `vitest.config.ts`. It transpiles components so they work with `newSpecPage()` from `@stencil/core/testing`, which lets spec tests written for Stencil v4 run under Vitest without changing their imports.
85
+
86
+ ```ts
87
+ // vitest.config.ts
88
+ import { defineConfig } from 'vitest/config';
89
+ import { stencilSpecPage } from '@stencil/unplugin';
90
+
91
+ export default defineConfig({
92
+ plugins: [stencilSpecPage()],
93
+ test: {
94
+ include: ['src/**/*.spec.{ts,tsx}'],
95
+ },
96
+ });
97
+ ```
98
+
99
+ ```ts
100
+ // src/components/my-button/my-button.spec.ts
101
+ import { newSpecPage } from '@stencil/core/testing';
102
+ import { expect, it } from 'vitest';
103
+
104
+ import { MyButton } from './my-button';
105
+
106
+ it('renders its label', async () => {
107
+ const page = await newSpecPage({
108
+ components: [MyButton],
109
+ html: `<my-button label="Save"></my-button>`,
110
+ });
111
+ expect(page.root!.shadowRoot!.textContent).toBe('Save');
112
+
113
+ page.root!.label = 'Cancel';
114
+ await page.waitForChanges();
115
+ expect(page.root!.shadowRoot!.textContent).toBe('Cancel');
116
+ });
117
+ ```
118
+
119
+ In this mode components don't register themselves as custom elements. `newSpecPage()` registers the ones you pass it, in Stencil's mock DOM.
120
+
121
+ The plugin also resolves a bare `@stencil/core` import to `@stencil/core/testing` in test files, so something like `setMode` imported from `@stencil/core` affects the same runtime `newSpecPage()` uses.
122
+
123
+ It takes the same [options](#options) as the other plugins.
124
+
125
+ ### Browser and Node tests
126
+
127
+ For tests that import a component and use it as a real custom element, use `stencilVite()`. Components are compiled on the fly, so no Stencil build is needed first, and each one registers itself when its file is imported. A test must import every component it renders, including ones rendered by other components.
128
+
129
+ ```ts
130
+ // vitest.config.ts
131
+ import { playwright } from '@vitest/browser-playwright';
132
+ import { defineConfig } from 'vitest/config';
133
+ import { stencilVite } from '@stencil/unplugin';
134
+
135
+ export default defineConfig({
136
+ plugins: [stencilVite()],
137
+ test: {
138
+ browser: {
139
+ enabled: true,
140
+ provider: playwright(),
141
+ headless: true,
142
+ instances: [{ browser: 'chromium' }],
143
+ },
144
+ },
145
+ });
146
+ ```
147
+
148
+ The same plugin works for tests that run in Node with a simulated DOM. Replace the `browser` block with an `environment`: `'jsdom'`, `'happy-dom'`, or the `'stencil'` environment from [`@stencil/vitest`](https://github.com/stenciljs/vitest), which uses Stencil's mock-doc by default.
149
+
150
+ To have `components.d.ts` written while tests run, so the JSX in them type-checks, pass `types: true`.
151
+
80
152
  ## Options
81
153
 
82
154
  All options are optional.
@@ -97,9 +169,11 @@ stencilVite({
97
169
  docs: true,
98
170
 
99
171
  // Extra options forwarded to transpileSync for every transformed file.
100
- // The plugin manages: file, resolveImport, styleImportData, componentExport — don't set those here.
172
+ // The plugin manages: file, resolveImport, styleImportData, componentMetadata, coreImportPath — don't set those here.
101
173
  transpileOptions: {
102
174
  sourceMap: true,
175
+ // 'customelement' (default) self-registers on import; 'module' only exports the class.
176
+ componentExport: 'customelement',
103
177
  },
104
178
  });
105
179
  ```
@@ -110,15 +184,32 @@ stencilVite({
110
184
  | `exclude` | `['node_modules/**']` | Files to skip |
111
185
  | `dev` | `false` (auto in Vite) | Inject HMR client code into component output |
112
186
  | `docs` | `false` | Scan and accumulate component metadata for docs/CEM |
187
+ | `types` | `false` | Write `<srcDir>/components.d.ts` at build start and keep it current as components change |
113
188
  | `transpileOptions` | — | Passed through to `transpileSync` (see `TranspileOptions`) |
114
189
 
115
190
  ## How it works
116
191
 
117
192
  ### Component transform
118
193
 
119
- Only files containing Stencil decorators (`@Component`, `@Prop`, `@State`, etc.) are transformed. Everything else passes through untouched.
194
+ Only files containing Stencil decorators (`@Component`, `@Prop`, `@State`, etc.) are compiled. Everything else passes through untouched, apart from `Build` flag folding (below).
195
+
196
+ Matching files are compiled with `componentExport: 'customelement'` by default, which means each component self-registers via `customElements.define` (if not already defined) when the module is imported — no separate registration step needed.
197
+
198
+ #### Opting out of self-registration
199
+
200
+ Set `transpileOptions.componentExport: 'module'` to get modules that only export the component class, with no registration side effect on import. Registering is then up to the consumer:
201
+
202
+ ```ts
203
+ import { MyButton } from './my-button';
204
+
205
+ customElements.define('my-button', MyButton);
206
+ ```
207
+
208
+ In dev mode, HMR looks instances up by the tag from `@Component({ tag })`, so components registered under a different name aren't hot-updated.
209
+
210
+ ### `Build` flags
120
211
 
121
- Matching files are compiled with `componentExport: 'customelement'`, which means each component self-registers via `customElements.define` (if not already defined) when the module is imported — no separate registration step needed.
212
+ In production builds, `Build.isBrowser` / `Build.isServer` checks are replaced with `true` / `false`, so server-only code is tree-shaken out of browser bundles.
122
213
 
123
214
  ### CSS pipeline
124
215
 
package/dist/index.d.mts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { ConfigCompat, CustomElementsManifest, StencilConfig, TranspileOptions } from "@stencil/core/compiler";
2
+ import { UnpluginInstance } from "unplugin";
2
3
  //#region src/options.d.ts
3
4
  type TranspileCompatKeys = 'lightDomPatches' | 'lifecycleDOMEvents' | 'initializeNextTick';
4
5
  /**
@@ -7,16 +8,27 @@ type TranspileCompatKeys = 'lightDomPatches' | 'lifecycleDOMEvents' | 'initializ
7
8
  *
8
9
  * Auto-populated from the project's `stencil.config.ts`; values set here override the auto-detected ones.
9
10
  */
10
- type StencilConfigSubset = Pick<StencilConfig, 'signalBacking'> & {
11
+ type StencilConfigSubset = Pick<StencilConfig, 'signalBacking' | 'hydratedFlag' | 'srcDir'> & {
11
12
  compat?: Pick<ConfigCompat, TranspileCompatKeys>;
12
13
  };
13
14
  interface StencilPluginOptions {
14
15
  /**
15
16
  * Additional options forwarded to `transpileSync` for every `.tsx`/`.ts` file.
16
- * `file`, `resolveImport`, `styleImportData`, `componentExport`, `componentMetadata`,
17
- * and `coreImportPath` are managed by the plugin and should not be set here.
17
+ * `file`, `resolveImport`, `styleImportData`, `componentMetadata`, and `coreImportPath`
18
+ * are managed by the plugin and should not be set here.
18
19
  */
19
- transpileOptions?: Omit<TranspileOptions, 'file' | 'resolveImport' | 'styleImportData' | 'componentExport' | 'componentMetadata' | 'coreImportPath'>;
20
+ transpileOptions?: Omit<TranspileOptions, 'file' | 'resolveImport' | 'styleImportData' | 'componentExport' | 'componentMetadata' | 'coreImportPath'> & {
21
+ /**
22
+ * `'customelement'` (default) - each component module registers its tag
23
+ * (`customElements.define`) when imported.
24
+ *
25
+ * `'module'` - the module only exports the component class, with no
26
+ * registration side effect; the consumer calls `customElements.define()` itself.
27
+ *
28
+ * Ignored in `mode: 'spec-page'`.
29
+ */
30
+ componentExport?: 'customelement' | 'module';
31
+ };
20
32
  /**
21
33
  * `'build'` (default) transpiles components into self-registering custom
22
34
  * elements for a real bundler/DOM - the output used by `stencilVite`,
@@ -56,6 +68,14 @@ interface StencilPluginOptions {
56
68
  * preset) to avoid paying the startup scan cost for non-docs builds.
57
69
  */
58
70
  docs?: boolean;
71
+ /**
72
+ * When `true`, the plugin writes `<srcDir>/components.d.ts` - the same typings `stencil build`
73
+ * generates, CSS-only components included - at build start, and keeps it current as component
74
+ * sources change. For projects whose dev loop doesn't run the Stencil compiler (e.g. Storybook).
75
+ *
76
+ * Uses the same project scan as `docs`. Off by default.
77
+ */
78
+ types?: boolean;
59
79
  /**
60
80
  * Stencil config flags that affect transpilation. The plugin automatically
61
81
  * detects and reads `stencil.config.ts` from the project root — set this only
@@ -67,21 +87,24 @@ interface StencilPluginOptions {
67
87
  stencilConfig?: StencilConfigSubset;
68
88
  }
69
89
  //#endregion
70
- //#region src/plugin.d.ts
71
- declare const STENCIL_DOCS_ID = "@stencil/unplugin/docs";
90
+ //#region src/project-scan.d.ts
72
91
  /**
73
92
  * Returns the current CEM. Only populated when `docs: true` is set.
74
93
  * @returns the current CEM, or an empty CEM if `docs: true` was not set.
75
94
  */
76
95
  declare function getStencilCEM(): CustomElementsManifest;
96
+ //#endregion
97
+ //#region src/plugin.d.ts
98
+ declare const STENCIL_DOCS_ID = "@stencil/unplugin/docs";
77
99
  declare const unpluginStencil: import("unplugin").UnpluginInstance<StencilPluginOptions | undefined, boolean>;
78
100
  //#endregion
79
101
  //#region src/index.d.ts
80
- declare const stencilVite: (options?: StencilPluginOptions | undefined) => import("unplugin").VitePlugin<any> | import("unplugin").VitePlugin<any>[];
81
- declare const stencilRollup: (options?: StencilPluginOptions | undefined) => import("unplugin").RollupPlugin<any> | import("unplugin").RollupPlugin<any>[];
82
- declare const stencilWebpack: (options?: StencilPluginOptions | undefined) => WebpackPluginInstance;
83
- declare const stencilEsbuild: (options?: StencilPluginOptions | undefined) => import("unplugin").EsbuildPlugin;
84
- declare const stencilRspack: (options?: StencilPluginOptions | undefined) => RspackPluginInstance;
102
+ type StencilUnplugin = UnpluginInstance<StencilPluginOptions | undefined, boolean>;
103
+ declare const stencilVite: StencilUnplugin['vite'];
104
+ declare const stencilRollup: StencilUnplugin['rollup'];
105
+ declare const stencilWebpack: StencilUnplugin['webpack'];
106
+ declare const stencilEsbuild: StencilUnplugin['esbuild'];
107
+ declare const stencilRspack: StencilUnplugin['rspack'];
85
108
  /**
86
109
  * Vite plugin that transpiles components for use with `newSpecPage()` from
87
110
  * `@stencil/core/testing`, instead of the self-registering custom-element
@@ -93,7 +116,7 @@ declare const stencilRspack: (options?: StencilPluginOptions | undefined) => Rsp
93
116
  * @param options - plugin options; `mode` is fixed to `'spec-page'` and cannot be overridden
94
117
  * @returns a Vite plugin
95
118
  */
96
- declare const stencilSpecPage: (options?: Omit<StencilPluginOptions, "mode">) => import("unplugin").VitePlugin<any> | import("unplugin").VitePlugin<any>[];
119
+ declare const stencilSpecPage: (options?: Omit<StencilPluginOptions, "mode">) => import("vite").Plugin<any> | import("vite").Plugin<any>[];
97
120
  //#endregion
98
121
  export { STENCIL_DOCS_ID, type StencilPluginOptions, getStencilCEM, stencilEsbuild, stencilRollup, stencilRspack, stencilSpecPage, stencilVite, stencilWebpack, unpluginStencil };
99
122
  //# sourceMappingURL=index.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","names":[],"sources":["../src/options.ts","../src/plugin.ts","../src/index.ts"],"mappings":";;KAEK;;;;;;;KAQO,sBAAsB,KAAK;EACrC,SAAS,KAAK,cAAc;;UAGb;;;;;;EAMf,mBAAmB,KACjB;;;;;;;;;;;;;;EAsBF;;;;;EAMA;;;;EAKA;;;;;;EAOA;;;;;;;;;;EAWA;;;;;;;;;EAUA,gBAAgB;;;;cChCL;;;;;iBAWG,iBAAiB;cAwFpB,oCAAe,iBAAA;;;cC/If,cAAW,UAAA,wDAAA,qCAAA;cACX,gBAAa,UAAA,wDAAA,uCAAA;cACb,iBAAc,UAAA,qCAAA;cACd,iBAAc,UAAA,wDAAA;cACd,gBAAa,UAAA,qCAAA;;;;;;;;;;;;cAab,kBAAmB,UAAS,KAAK,qDAAkC,qCAAA"}
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../src/options.ts","../src/project-scan.ts","../src/plugin.ts","../src/index.ts"],"mappings":";;;KAEK;;;;;;;KAQO,sBAAsB,KAChC;EAGA,SAAS,KAAK,cAAc;;UAGb;;;;;;EAMf,mBAAmB,KACjB;;;;;;;;;;IAiBA;;;;;;;;;;;;;;;EAgBF;;;;;EAMA;;;;EAKA;;;;;;EAOA;;;;;;;;;;EAWA;;;;;;;;EASA;;;;;;;;;EAUA,gBAAgB;;;;;;;;iBCjDF,iBAAiB;;;cCwBpB;cAqFA,oCAAe,iBAAA;;;KC3JvB,kBAAkB,iBAAiB;cAE3B,aAAa;cACb,eAAe;cACf,gBAAgB;cAChB,gBAAgB;cAChB,eAAe;;;;;;;;;;;;cAaf,kBAAmB,UAAS,KAAK,iDAAkC,6BAAA"}