Skip to content

Commit cc6d80e

Browse files
authored
Merge pull request #21 from peschee/bugfix/failing-resolve-extends
Use `get-tsconfig` to parse a given tsconfig file
2 parents 26be27a + 2d3b43a commit cc6d80e

File tree

5 files changed

+82
-1
lines changed

5 files changed

+82
-1
lines changed

package-lock.json

+20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"@mdn/browser-compat-data": "^4.0.0",
6161
"@web/dev-server-core": "^0.7.0",
6262
"esbuild": "^0.19.11",
63+
"get-tsconfig": "^4.7.2",
6364
"parse5": "^6.0.1",
6465
"ua-parser-js": "^1.0.33"
6566
},

src/EsbuildPlugin.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
setTextContent,
1919
} from '@web/dev-server-core/dist/dom5';
2020
import { parse as parseHtml, serialize as serializeHtml } from 'parse5';
21+
import { parseTsconfig } from 'get-tsconfig';
2122

2223
import { getEsbuildTarget } from './getEsbuildTarget.js';
2324

@@ -61,7 +62,8 @@ export class EsbuildPlugin implements Plugin {
6162
this.config = config;
6263
this.logger = logger;
6364
if (this.esbuildConfig.tsconfig) {
64-
this.tsconfigRaw = await promisify(fs.readFile)(this.esbuildConfig.tsconfig, 'utf8');
65+
const parsedTsconfig = await parseTsconfig(this.esbuildConfig.tsconfig);
66+
this.tsconfigRaw = JSON.stringify(parsedTsconfig);
6567
}
6668
}
6769

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "./tsconfig-with-experimental-decorators.json"
3+
}

test/ts.test.ts

+55
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,61 @@ class Bar {
104104
}
105105
});
106106

107+
it('transforms TS decorators with parent tsconfig', async () => {
108+
const { server, host } = await createTestServer({
109+
rootDir: __dirname,
110+
plugins: [
111+
{
112+
name: 'test',
113+
serve(context) {
114+
if (context.path === '/foo.ts') {
115+
return `
116+
@foo
117+
class Bar {
118+
@prop
119+
x = 'y';
120+
}`;
121+
}
122+
},
123+
},
124+
esbuildPlugin({
125+
ts: true,
126+
tsconfig: path.join(
127+
__dirname,
128+
'fixture',
129+
'tsconfig-with-experimental-decorators-parent.json',
130+
),
131+
}),
132+
],
133+
});
134+
135+
try {
136+
const response = await fetch(`${host}/foo.ts`);
137+
const text = await response.text();
138+
139+
expect(response.status).to.equal(200);
140+
expect(response.headers.get('content-type')).to.equal(
141+
'application/javascript; charset=utf-8',
142+
);
143+
expectIncludes(text, '__decorate');
144+
expectIncludes(text, '__publicField(this, "x", "y");');
145+
expectIncludes(
146+
text,
147+
`__decorateClass([
148+
prop
149+
], Bar.prototype, "x", 2);`,
150+
);
151+
expectIncludes(
152+
text,
153+
`Bar = __decorateClass([
154+
foo
155+
], Bar);`,
156+
);
157+
} finally {
158+
server.stop();
159+
}
160+
});
161+
107162
it('resolves relative ending with .js to .ts files', async () => {
108163
const { server, host } = await createTestServer({
109164
rootDir: path.join(__dirname, 'fixture'),

0 commit comments

Comments
 (0)