wc-compiler 0.17.0 → 0.18.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wc-compiler",
3
- "version": "0.17.0",
3
+ "version": "0.18.0",
4
4
  "description": "Experimental native Web Components compiler.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -15,7 +15,8 @@
15
15
  "types": "./src/index.d.ts"
16
16
  },
17
17
  "./register": "./src/register.js",
18
- "./src/jsx-loader.js": "./src/jsx-loader.js"
18
+ "./src/jsx-loader.js": "./src/jsx-loader.js",
19
+ "./jsx-runtime": "./src/jsx-runtime.ts"
19
20
  },
20
21
  "author": "Owen Buckley <owen@thegreenhouse.io>",
21
22
  "keywords": [
@@ -37,11 +38,11 @@
37
38
  "scripts": {
38
39
  "clean": "rimraf ./dist",
39
40
  "lint": "eslint",
40
- "lint:types": "tsc --project tsconfig.json",
41
- "docs:dev": "concurrently \"nodemon --watch src --watch docs -e js,md,css,html,jsx ./build.js\" \"http-server ./dist --open\"",
41
+ "lint:types": "tsc",
42
+ "docs:dev": "concurrently \"nodemon --watch src --watch docs -e js,md,css,html,jsx,ts,tsx ./build.js\" \"http-server ./dist --open\"",
42
43
  "docs:build": "node ./build.js",
43
44
  "docs:serve": "npm run clean && npm run docs:build && http-server ./dist --open",
44
- "sandbox": "npm run clean && concurrently \"nodemon --loader ./test-loader.js --watch src --watch sandbox -e js,md,css,html,jsx,ts ./sandbox.js\" \"http-server ./dist --open\" \"livereload ./dist\"",
45
+ "sandbox": "npm run clean && concurrently \"nodemon --experimental-strip-types --loader ./test-loader.js --watch src --watch sandbox -e js,md,css,html,jsx,ts ./sandbox.js\" \"http-server ./dist --open\" \"livereload ./dist\"",
45
46
  "start": "npm run docs:serve",
46
47
  "test": "mocha --exclude \"./test/cases/jsx*/**\" --exclude \"./test/cases/ts*/**\" --exclude \"./test/cases/custom-extension/**\" \"./test/**/**/*.spec.js\"",
47
48
  "test:jsx": "c8 node --import ./test-register.js --experimental-strip-types ./node_modules/mocha/bin/mocha \"./test/**/**/*.spec.js\"",
@@ -87,6 +88,7 @@
87
88
  "rimraf": "^3.0.2",
88
89
  "simple.css": "^0.1.3",
89
90
  "typescript": "^5.8.2",
91
+ "typescript-eslint": "^8.46.2",
90
92
  "unified": "^10.1.2"
91
93
  }
92
94
  }
package/src/index.d.ts CHANGED
@@ -16,4 +16,7 @@ export type renderFromHTML = (html: string, elementURLs: URL[]) => Promise<{
16
16
  metadata: Metadata
17
17
  }>
18
18
 
19
- declare module "wc-compiler" { }
19
+ declare module "wc-compiler" {
20
+ export const renderToString: renderToString;
21
+ export const renderFromHTML: renderFromHTML;
22
+ }
package/src/jsx-loader.js CHANGED
@@ -12,6 +12,7 @@ import { parse, parseFragment, serialize } from 'parse5';
12
12
  import { transform } from 'sucrase';
13
13
 
14
14
  const jsxRegex = /\.(jsx)$/;
15
+ const tsxRegex = /\.(tsx)$/;
15
16
 
16
17
  // TODO same hack as definitions
17
18
  // https://github.com/ProjectEvergreen/wcc/discussions/74
@@ -26,7 +27,8 @@ function getParse(html) {
26
27
  }
27
28
 
28
29
  export function getParser(moduleURL) {
29
- const isJSX = moduleURL.pathname.split('.').pop() === 'jsx';
30
+ const ext = moduleURL.pathname.split('.').pop();
31
+ const isJSX = ext === 'jsx' || ext === 'tsx';
30
32
 
31
33
  if (!isJSX) {
32
34
  return;
@@ -390,7 +392,7 @@ export function parseJsx(moduleURL) {
390
392
  export function resolve(specifier, context, defaultResolve) {
391
393
  const { parentURL } = context;
392
394
 
393
- if (jsxRegex.test(specifier)) {
395
+ if (jsxRegex.test(specifier) || tsxRegex.test(specifier)) {
394
396
  return {
395
397
  url: new URL(specifier, parentURL).href,
396
398
  shortCircuit: true
@@ -401,7 +403,7 @@ export function resolve(specifier, context, defaultResolve) {
401
403
  }
402
404
 
403
405
  export async function load(url, context, defaultLoad) {
404
- if (jsxRegex.test(url)) {
406
+ if (jsxRegex.test(url) || tsxRegex.test(url)) {
405
407
  const jsFromJsx = parseJsx(new URL(url));
406
408
 
407
409
  return {
@@ -0,0 +1 @@
1
+ import './jsx.d.ts';
package/src/jsx.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ // to support `style` attributes, we override `CSSStyleDeclaration` with `string`
2
+ type IsCSSStyleDeclaration<T> = T extends CSSStyleDeclaration ? string : T;
3
+
4
+ // create a utility type to extract the attributes from any given element's DOM interface.
5
+ type ElementAttributes<E extends HTMLElement> = {
6
+ // Extract all properties from the element, including inherited ones.
7
+ [A in keyof E]?: E[A] extends (...args: any) => any ? any : IsCSSStyleDeclaration<E[A]>;
8
+ } & {
9
+ class?: string;
10
+ };
11
+
12
+ // map each HTML tag to a union of its attributes and the global attributes.
13
+ type IntrinsicElementsFromDom = {
14
+ [E in keyof HTMLElementTagNameMap]: ElementAttributes<HTMLElementTagNameMap[E]>;
15
+ };
16
+
17
+ // declare the global JSX namespace with your generated intrinsic elements.
18
+ declare namespace JSX {
19
+ interface IntrinsicElements extends IntrinsicElementsFromDom {}
20
+ }
package/src/wcc.js CHANGED
@@ -68,7 +68,8 @@ function registerDependencies(moduleURL, definitions, depth = 0) {
68
68
  const moduleContents = fs.readFileSync(moduleURL, 'utf-8');
69
69
  const result = transform(moduleContents, {
70
70
  transforms: ['typescript', 'jsx'],
71
- jsxRuntime: 'preserve'
71
+ jsxRuntime: 'automatic',
72
+ production: true,
72
73
  });
73
74
  const nextDepth = depth += 1;
74
75
  const customParser = getParser(moduleURL);
@@ -90,7 +91,7 @@ function registerDependencies(moduleURL, definitions, depth = 0) {
90
91
 
91
92
  // would like to decouple .jsx from the core, ideally
92
93
  // https://github.com/ProjectEvergreen/wcc/issues/122
93
- if (!isBareSpecifier && ['js', 'jsx', 'ts'].includes(extension)) {
94
+ if (!isBareSpecifier && ['js', 'jsx', 'ts', 'tsx'].includes(extension)) {
94
95
  const dependencyModuleURL = new URL(specifier, moduleURL);
95
96
 
96
97
  registerDependencies(dependencyModuleURL, definitions, nextDepth);
@@ -123,7 +124,8 @@ async function getTagName(moduleURL) {
123
124
  const moduleContents = await fs.promises.readFile(moduleURL, 'utf-8');
124
125
  const result = transform(moduleContents, {
125
126
  transforms: ['typescript', 'jsx'],
126
- jsxRuntime: 'preserve'
127
+ jsxRuntime: 'automatic',
128
+ production: true,
127
129
  });
128
130
  const customParser = getParser(moduleURL);
129
131
  const parser = customParser ? customParser.parser : acorn.Parser;
@@ -169,9 +171,7 @@ async function initializeCustomElement(elementURL, tagName, node = {}, definitio
169
171
  }
170
172
  }
171
173
 
172
- /** @type {import('./index.d.ts').renderToString} */
173
174
  async function renderToString(elementURL, wrappingEntryTag = true, props = {}) {
174
- /** @type {import('./index.d.ts').Metadata} */
175
175
  const definitions = {};
176
176
  const elementTagName = wrappingEntryTag && await getTagName(elementURL);
177
177
  const isEntry = !!elementTagName;
@@ -211,9 +211,7 @@ async function renderToString(elementURL, wrappingEntryTag = true, props = {}) {
211
211
  };
212
212
  }
213
213
 
214
- /** @type {import('./index.d.ts').renderFromHTML} */
215
214
  async function renderFromHTML(html, elements = []) {
216
- /** @type {import('./index.d.ts').Metadata} */
217
215
  const definitions = {};
218
216
 
219
217
  for (const url of elements) {