Skip to content

Commit

Permalink
Merge pull request #21 from peschee/bugfix/failing-resolve-extends
Browse files Browse the repository at this point in the history
Use `get-tsconfig` to parse a given tsconfig file
  • Loading branch information
thepassle authored Feb 5, 2024
2 parents 26be27a + 2d3b43a commit cc6d80e
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 1 deletion.
20 changes: 20 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"@mdn/browser-compat-data": "^4.0.0",
"@web/dev-server-core": "^0.7.0",
"esbuild": "^0.19.11",
"get-tsconfig": "^4.7.2",
"parse5": "^6.0.1",
"ua-parser-js": "^1.0.33"
},
Expand Down
4 changes: 3 additions & 1 deletion src/EsbuildPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
setTextContent,
} from '@web/dev-server-core/dist/dom5';
import { parse as parseHtml, serialize as serializeHtml } from 'parse5';
import { parseTsconfig } from 'get-tsconfig';

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

Expand Down Expand Up @@ -61,7 +62,8 @@ export class EsbuildPlugin implements Plugin {
this.config = config;
this.logger = logger;
if (this.esbuildConfig.tsconfig) {
this.tsconfigRaw = await promisify(fs.readFile)(this.esbuildConfig.tsconfig, 'utf8');
const parsedTsconfig = await parseTsconfig(this.esbuildConfig.tsconfig);
this.tsconfigRaw = JSON.stringify(parsedTsconfig);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./tsconfig-with-experimental-decorators.json"
}
55 changes: 55 additions & 0 deletions test/ts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,61 @@ class Bar {
}
});

it('transforms TS decorators with parent tsconfig', async () => {
const { server, host } = await createTestServer({
rootDir: __dirname,
plugins: [
{
name: 'test',
serve(context) {
if (context.path === '/foo.ts') {
return `
@foo
class Bar {
@prop
x = 'y';
}`;
}
},
},
esbuildPlugin({
ts: true,
tsconfig: path.join(
__dirname,
'fixture',
'tsconfig-with-experimental-decorators-parent.json',
),
}),
],
});

try {
const response = await fetch(`${host}/foo.ts`);
const text = await response.text();

expect(response.status).to.equal(200);
expect(response.headers.get('content-type')).to.equal(
'application/javascript; charset=utf-8',
);
expectIncludes(text, '__decorate');
expectIncludes(text, '__publicField(this, "x", "y");');
expectIncludes(
text,
`__decorateClass([
prop
], Bar.prototype, "x", 2);`,
);
expectIncludes(
text,
`Bar = __decorateClass([
foo
], Bar);`,
);
} finally {
server.stop();
}
});

it('resolves relative ending with .js to .ts files', async () => {
const { server, host } = await createTestServer({
rootDir: path.join(__dirname, 'fixture'),
Expand Down

0 comments on commit cc6d80e

Please sign in to comment.