-
-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests for transform (#397)
- Loading branch information
1 parent
497f69a
commit 5bdf022
Showing
8 changed files
with
306 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,63 @@ | ||
import path from 'path'; | ||
import type { TransformOptions } from 'esbuild'; | ||
|
||
const nodeVersion = process.versions.node; | ||
import type { TransformOptions, TransformResult } from 'esbuild'; | ||
|
||
export const baseConfig = Object.freeze({ | ||
target: `node${process.versions.node}`, | ||
|
||
// "default" tells esbuild to infer loader from file name | ||
// https://github.com/evanw/esbuild/blob/4a07b17adad23e40cbca7d2f8931e8fb81b47c33/internal/bundler/bundler.go#L158 | ||
loader: 'default', | ||
}); | ||
|
||
export const cacheConfig = { | ||
...baseConfig, | ||
|
||
sourcemap: true, | ||
|
||
/** | ||
* Smaller output for cache and marginal performance improvement: | ||
* https://twitter.com/evanwallace/status/1396336348366180359?s=20 | ||
* | ||
* minifyIdentifiers is disabled because debuggers don't use the | ||
* `names` property from the source map | ||
* | ||
* minifySyntax is disabled because it does some tree-shaking | ||
* eg. unused try-catch error variable | ||
*/ | ||
minifyWhitespace: true, | ||
|
||
// TODO: Is this necessary if minifyIdentifiers is false? | ||
keepNames: true, | ||
}; | ||
|
||
export const getEsbuildOptions = ( | ||
extendOptions: TransformOptions, | ||
export const patchOptions = ( | ||
options: TransformOptions, | ||
) => { | ||
const options: TransformOptions = { | ||
target: `node${nodeVersion}`, | ||
|
||
// "default" tells esbuild to infer loader from file name | ||
// https://github.com/evanw/esbuild/blob/4a07b17adad23e40cbca7d2f8931e8fb81b47c33/internal/bundler/bundler.go#L158 | ||
loader: 'default', | ||
|
||
sourcemap: true, | ||
|
||
/** | ||
* Smaller output for cache and marginal performance improvement: | ||
* https://twitter.com/evanwallace/status/1396336348366180359?s=20 | ||
* | ||
* minifyIdentifiers is disabled because debuggers don't use the | ||
* `names` property from the source map | ||
* | ||
* minifySyntax is disabled because it does some tree-shaking | ||
* eg. unused try-catch error variable | ||
*/ | ||
minifyWhitespace: true, | ||
keepNames: true, | ||
|
||
...extendOptions, | ||
}; | ||
const originalSourcefile = options.sourcefile; | ||
|
||
if (options.sourcefile) { | ||
const { sourcefile } = options; | ||
const extension = path.extname(sourcefile); | ||
if (originalSourcefile) { | ||
const extension = path.extname(originalSourcefile); | ||
|
||
if (extension) { | ||
// https://github.com/evanw/esbuild/issues/1932 | ||
if (extension === '.cts' || extension === '.mts') { | ||
options.sourcefile = `${sourcefile.slice(0, -3)}ts`; | ||
options.sourcefile = `${originalSourcefile.slice(0, -3)}ts`; | ||
} | ||
} else { | ||
// esbuild errors to detect loader when a file doesn't have an extension | ||
options.sourcefile += '.js'; | ||
} | ||
} | ||
|
||
return options; | ||
return ( | ||
result: TransformResult, | ||
) => { | ||
if (options.sourcefile !== originalSourcefile) { | ||
result.map = result.map.replace( | ||
JSON.stringify(options.sourcefile), | ||
JSON.stringify(originalSourcefile), | ||
); | ||
} | ||
return result; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.