Skip to content

Use get-tsconfig to parse a given tsconfig file #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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