wc-compiler 0.13.0 → 0.14.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.13.0",
3
+ "version": "0.14.0",
4
4
  "description": "Experimental native Web Components compiler.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -32,9 +32,9 @@
32
32
  "docs:dev": "concurrently \"nodemon --watch src --watch docs -e js,md,css,html,jsx ./build.js\" \"http-server ./dist --open\"",
33
33
  "docs:build": "node ./build.js",
34
34
  "docs:serve": "npm run clean && npm run docs:build && http-server ./dist --open",
35
- "sandbox": "npm run clean && concurrently \"nodemon --loader ./test-exp-loader.js --watch src --watch sandbox -e js,md,css,html,jsx ./sandbox.js\" \"http-server ./dist --open\" \"livereload ./dist\"",
35
+ "sandbox": "npm run clean && concurrently \"nodemon --loader ./test-exp-loader.js --watch src --watch sandbox -e js,md,css,html,jsx,ts ./sandbox.js\" \"http-server ./dist --open\" \"livereload ./dist\"",
36
36
  "start": "npm run docs:serve",
37
- "test": "mocha --exclude \"./test/cases/jsx*/**\" --exclude \"./test/cases/custom-extension/**\" \"./test/**/**/*.spec.js\"",
37
+ "test": "mocha --exclude \"./test/cases/jsx*/**\" --exclude \"./test/cases/ts*/**\" --exclude \"./test/cases/custom-extension/**\" \"./test/**/**/*.spec.js\"",
38
38
  "test:exp": "c8 node --loader ./test-exp-loader.js ./node_modules/mocha/bin/mocha \"./test/**/**/*.spec.js\"",
39
39
  "test:tdd": "npm run test -- --watch",
40
40
  "test:tdd:exp": "npm run test:exp -- --watch",
@@ -47,7 +47,8 @@
47
47
  "acorn": "^8.7.0",
48
48
  "acorn-import-attributes": "^1.9.5",
49
49
  "acorn-walk": "^8.2.0",
50
- "parse5": "^6.0.1"
50
+ "parse5": "^6.0.1",
51
+ "sucrase": "^3.35.0"
51
52
  },
52
53
  "devDependencies": {
53
54
  "@babel/core": "^7.24.4",
package/src/jsx-loader.js CHANGED
@@ -8,6 +8,7 @@ import jsx from '@projectevergreen/acorn-jsx-esm';
8
8
  import { parse, parseFragment, serialize } from 'parse5';
9
9
  // Need an acorn plugin for now - https://github.com/ProjectEvergreen/greenwood/issues/1218
10
10
  import { importAttributes } from 'acorn-import-attributes';
11
+ import { transform } from 'sucrase';
11
12
 
12
13
  const jsxRegex = /\.(jsx)$/;
13
14
 
@@ -232,13 +233,17 @@ function findThisReferences(context, statement) {
232
233
 
233
234
  export function parseJsx(moduleURL) {
234
235
  const moduleContents = fs.readFileSync(moduleURL, 'utf-8');
236
+ const result = transform(moduleContents, {
237
+ transforms: ['typescript', 'jsx'],
238
+ jsxRuntime: 'preserve'
239
+ });
235
240
  // would be nice if we could do this instead, so we could know ahead of time
236
241
  // const { inferredObservability } = await import(moduleURL);
237
242
  // however, this requires making parseJsx async, but WCC acorn walking is done sync
238
243
  const hasOwnObservedAttributes = undefined;
239
244
  let inferredObservability = false;
240
245
  let observedAttributes = [];
241
- let tree = acorn.Parser.extend(jsx(), importAttributes).parse(moduleContents, {
246
+ let tree = acorn.Parser.extend(jsx(), importAttributes).parse(result.code, {
242
247
  ecmaVersion: 'latest',
243
248
  sourceType: 'module'
244
249
  });
@@ -0,0 +1,32 @@
1
+ import fs from 'fs/promises';
2
+ import { transform } from 'sucrase';
3
+
4
+ const tsRegex = /\.(ts)$/;
5
+
6
+ export function resolve(specifier, context, defaultResolve) {
7
+ const { parentURL } = context;
8
+
9
+ if (tsRegex.test(specifier)) {
10
+ return {
11
+ url: new URL(specifier, parentURL).href,
12
+ shortCircuit: true
13
+ };
14
+ }
15
+
16
+ return defaultResolve(specifier, context, defaultResolve);
17
+ }
18
+
19
+ export async function load(url, context, defaultLoad) {
20
+ if (tsRegex.test(url)) {
21
+ const contents = await fs.readFile(new URL(url), 'utf-8');
22
+ const result = transform(contents, { transforms: ['typescript'] });
23
+
24
+ return {
25
+ format: 'module',
26
+ shortCircuit: true,
27
+ source: result.code
28
+ };
29
+ }
30
+
31
+ return defaultLoad(url, context, defaultLoad);
32
+ }
package/src/wcc.js CHANGED
@@ -9,6 +9,7 @@ import { getParser, parseJsx } from './jsx-loader.js';
9
9
  import { parse, parseFragment, serialize } from 'parse5';
10
10
  // Need an acorn plugin for now - https://github.com/ProjectEvergreen/greenwood/issues/1218
11
11
  import { importAttributes } from 'acorn-import-attributes';
12
+ import { transform } from 'sucrase';
12
13
  import fs from 'fs';
13
14
 
14
15
  function getParse(html) {
@@ -61,6 +62,10 @@ async function renderComponentRoots(tree, definitions) {
61
62
 
62
63
  function registerDependencies(moduleURL, definitions, depth = 0) {
63
64
  const moduleContents = fs.readFileSync(moduleURL, 'utf-8');
65
+ const result = transform(moduleContents, {
66
+ transforms: ['typescript', 'jsx'],
67
+ jsxRuntime: 'preserve'
68
+ });
64
69
  const nextDepth = depth += 1;
65
70
  const customParser = getParser(moduleURL);
66
71
  const parser = customParser ? customParser.parser : acorn.Parser;
@@ -68,7 +73,7 @@ function registerDependencies(moduleURL, definitions, depth = 0) {
68
73
  ...walk.base
69
74
  };
70
75
 
71
- walk.simple(parser.extend(importAttributes).parse(moduleContents, {
76
+ walk.simple(parser.extend(importAttributes).parse(result.code, {
72
77
  ecmaVersion: 'latest',
73
78
  sourceType: 'module'
74
79
  }), {
@@ -78,7 +83,8 @@ function registerDependencies(moduleURL, definitions, depth = 0) {
78
83
  const extension = specifier.split('.').pop();
79
84
 
80
85
  // TODO would like to decouple .jsx from the core, ideally
81
- if (!isBareSpecifier && ['js', 'jsx'].includes(extension)) {
86
+ // https://github.com/ProjectEvergreen/wcc/issues/122
87
+ if (!isBareSpecifier && ['js', 'jsx', 'ts'].includes(extension)) {
82
88
  const dependencyModuleURL = new URL(node.source.value, moduleURL);
83
89
 
84
90
  registerDependencies(dependencyModuleURL, definitions, nextDepth);
@@ -107,6 +113,10 @@ function registerDependencies(moduleURL, definitions, depth = 0) {
107
113
 
108
114
  async function getTagName(moduleURL) {
109
115
  const moduleContents = await fs.promises.readFile(moduleURL, 'utf-8');
116
+ const result = transform(moduleContents, {
117
+ transforms: ['typescript', 'jsx'],
118
+ jsxRuntime: 'preserve'
119
+ });
110
120
  const customParser = getParser(moduleURL);
111
121
  const parser = customParser ? customParser.parser : acorn.Parser;
112
122
  const config = customParser ? customParser.config : {
@@ -114,7 +124,7 @@ async function getTagName(moduleURL) {
114
124
  };
115
125
  let tagName;
116
126
 
117
- walk.simple(parser.extend(importAttributes).parse(moduleContents, {
127
+ walk.simple(parser.extend(importAttributes).parse(result.code, {
118
128
  ecmaVersion: 'latest',
119
129
  sourceType: 'module'
120
130
  }), {