Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: replace precinct with esbuild (#659)
* refactor: replace precinct with esbuild * refactor: add feature flag * fix: add feature flags object * refactor: turn feature flag off by default * chore: add test * refactor: use intermediate variable * refactor: await `safeUnlink` call Co-authored-by: ehmicky <ehmicky@users.noreply.github.com> Co-authored-by: ehmicky <ehmicky@users.noreply.github.com> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
- Loading branch information
1 parent
c28eaaf
commit 5bf51d0
Showing
16 changed files
with
177 additions
and
30 deletions.
There are no files selected for viewing
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
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
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
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const esbuild = require('@netlify/esbuild') | ||
const isBuiltinModule = require('is-builtin-module') | ||
const { tmpName } = require('tmp-promise') | ||
|
||
const { safeUnlink } = require('../../utils/fs') | ||
|
||
const getListImportsPlugin = ({ imports, path }) => ({ | ||
name: 'list-imports', | ||
setup(build) { | ||
build.onResolve({ filter: /.*/ }, (args) => { | ||
const isEntryPoint = args.path === path | ||
const isImport = !isEntryPoint && !isBuiltinModule(args.path) | ||
|
||
if (isImport) { | ||
imports.add(args.path) | ||
} | ||
|
||
return { | ||
namespace: 'list-imports', | ||
external: isImport, | ||
} | ||
}) | ||
}, | ||
}) | ||
|
||
const listImports = async ({ path }) => { | ||
// We're not interested in the output that esbuild generates, we're just | ||
// using it for its parsing capabilities in order to find import/require | ||
// statements. However, if we don't give esbuild a path in `outfile`, it | ||
// will pipe the output to stdout, which we also don't want. So we create | ||
// a temporary file to serve as the esbuild output and then get rid of it | ||
// when we're done. | ||
const targetPath = await tmpName() | ||
const imports = new Set() | ||
|
||
try { | ||
await esbuild.build({ | ||
entryPoints: [path], | ||
bundle: true, | ||
outfile: targetPath, | ||
platform: 'node', | ||
plugins: [getListImportsPlugin({ imports, path })], | ||
}) | ||
} finally { | ||
await safeUnlink(targetPath) | ||
} | ||
|
||
return [...imports] | ||
} | ||
|
||
module.exports = { listImports } |
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
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
const mock = require('@netlify/mock-package') | ||
const stack = require('@netlify/mock-package/stack') | ||
|
||
module.exports = { mock, stack } |
3 changes: 3 additions & 0 deletions
3
tests/fixtures/local-node-module-deep-require/node_modules/@netlify/mock-package/index.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
4 changes: 4 additions & 0 deletions
4
...s/fixtures/local-node-module-deep-require/node_modules/@netlify/mock-package/package.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
tests/fixtures/local-node-module-deep-require/node_modules/@netlify/mock-package/stack.js
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"name": "local-node-module", | ||
"version": "1.0.0", | ||
"dependencies": { | ||
"@netlify/mock-package": "1.0.0" | ||
} | ||
} |
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.
5bf51d0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
⏱ Benchmark results
largeDepsEsbuild: 12.2s
largeDepsZisi: 1m 5.8s