-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Mix esbuild-loader and ts-loader #915
Comments
Yes, I think that should be fairly straightforward. You could try writing an on-load plugin that uses a regular expression to detect whether there is probably a decorator in the file or not. If there is, you could call out to the TypeScript compiler and return the JavaScript to esbuild instead of letting esbuild convert the TypeScript to JavaScript for those files. |
Since you mentioned esbuild-loader and ts-loader, I assume you're using Webpack. I'm not familiar with writing Webpack plugins at all so I'm not going to be able to help too much. Doing this is possible with an esbuild plugin but it may be that the right way to do this is to write a Webpack plugin instead that "composes" esbuild-loader and ts-loader (i.e. decides which one to run based on the regular expression and the file contents). Or perhaps the authors of esbuild-loader and/or ts-loader might be interested in integrating something like this into their plugin? I'm not sure what the right approach is because I don't know anything about Webpack myself. I'm going to close this issue since it's not an issue with esbuild itself, and this is not something esbuild itself can fix. |
Thank you for your extensive reply! I looked into the plugins but sadly saw that they are not currently available for the transform api and esbuild-loader is using that. I am now using webpacks loaders to achieve this: https://github.com/privatenumber/esbuild-loader/discussions/117 I am interested to see what speeds I can achieve with esbuild without webpack and will try to get something running by using the Typescript Compiler API if I notice a decorator present. |
@evanw it's very hard for me to detect if a file contains a decorator. Thus I'm thinking of compiling the code with esbuild and then check for |
I was imagining you could do something like let tscDecoratorPlugin = {
name: 'tsc-decorator',
setup(build) {
let options = {
// Note: This would be more complicated in practice
experimentalDecorators: true,
emitDecoratorMetadata: true,
};
// Note: May want to support "*.tsx" too
build.onLoad({ filter: /\.ts$/ }, async (args) => {
let ts = await require('fs').promises.readFile(args.path, 'utf8')
if (/@\w+/.test(ts)) {
let program = require('typescript').createProgram([args.path], options);
let contents;
program.emit(void 0, (path, js) => contents = js);
return { contents };
}
})
},
}
require('esbuild').build({
entryPoints: ['entry.ts'],
bundle: true,
outfile: 'out.js',
plugins: [tscDecoratorPlugin],
}).catch(() => process.exit(1)) |
Just checking for an |
Well in that case checking for // First pass: is there "@word" without being at the start of a string?
let probablyHasDecorator = /(?<!['"])@\w+/.test(tsCode)
if (probablyHasDecorator) {
// Second pass: is it likely that esbuild detected a decorator?
let jsCode = await esbuild.transform(tsCode, { loader: 'ts' })
probablyHasDecorator = jsCode.includes('__decorate')
} |
Edit: @evanw I have enhanced your example here. How should I handle the sourcemaps? Should I override the
|
Looks good to me. Searching through the string like this should be the fastest option by far.
Yes that's right. I would also enable |
I have done some testing on a project of mine and actually having some false positives is faster than having to pipe everything through javascript with
I released the plugin here: https://github.com/thomaschaaf/esbuild-plugin-tsc Thank you for your help! |
@thomaschaaf but how you can use this plugin in webpack build? |
I would like to use
emitDecoratorMetadata
but it is not supported by esbuild would it be possible to use ts-loader only for files that use decorators?The text was updated successfully, but these errors were encountered: