Skip to content
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

feat(feature-detector): introduces findGlobals() utility #234

Merged
merged 10 commits into from
Jan 18, 2023

Conversation

feugy
Copy link
Member

@feugy feugy commented Jan 12, 2023

📖 What's in there?

Second (out of 3) part of the feature detector: returning a list of globals used in a given file.

  • feat(feature-detector): introduces findGlobals() which returns the list of globals.

All global variables, that is, identifiers that are not considered as ES2019 builtins and Edge-runtime globals (AbortController, Request, Response...) will be reported.

🧪 How to test?

Besides unit tests, there's an documented example:

Here is a test file. It's in JavaScript, but TypeScript is supported as well.
1. build the repo `pnpm build`
1. make a test file
```js
// test.js
if (typeof process !== 'undefined') {
  console.log('in node', __dirname)
} else if (typeof window !== 'undefined') {
  console.log('in a browser', document)
}
setTimeout(() => console.log('setTimeout is not a global, nor console'), 0)
  1. open the node REPL (same folder):
const { findGlobals } = require('./packages/feature-detector/dist')
> undefined
findGlobals('./test.js')
> ['process', '__dirname', 'window', 'document']

:this: Notes to reviewers

We don't need patch-type-definition script any more. The previous solution was pulling to many type definition: we must not load the full DOM types because many of them are not supported in the edge runtime.
If we do, TS compiler will not consider window to be a global, for example.
I couldn't find a way to reuse @edge-runtime/types or @edge-runtime/primitives: they have too much adherence with Node.js types, and it bringing too much for the global detection.
This is documented in the type-definition.txt file.

I made a unit test to detect globals in axios. There's a new script to build the test fixture, so axios is bundled as will the user code be. This script should only run manually, when someone needs to change the fixture.

I'm very sorry about this, but I had to disable turbo cache for build scripts. We're getting poisoned built artifacts which are constantly failing the tests.

@vercel
Copy link

vercel bot commented Jan 12, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated
edge-runtime ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Jan 18, 2023 at 5:14PM (UTC)

@changeset-bot
Copy link

changeset-bot bot commented Jan 12, 2023

🦋 Changeset detected

Latest commit: 353d9a4

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@edge-runtime/feature-detector Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@feugy feugy force-pushed the damien/ec-544-build-a-minimal-ts-morph-feature-2 branch from a2d6b9b to b709b27 Compare January 12, 2023 10:47
@feugy feugy force-pushed the damien/ec-544-build-a-minimal-ts-morph-feature-2 branch 2 times, most recently from 99c3a61 to 3a4e146 Compare January 12, 2023 10:48
@feugy feugy requested a review from a team January 12, 2023 10:48
@feugy feugy changed the title feat(feature-detector): introduces findDependencies() utility feat(feature-detector): introduces findGlobals() utility Jan 16, 2023
@feugy feugy force-pushed the damien/ec-544-build-a-minimal-ts-morph-feature-2 branch from b699a76 to 9a9013e Compare January 16, 2023 09:11
packages/feature-detector/src/find-globals.ts Show resolved Hide resolved
packages/feature-detector/src/find-globals.ts Outdated Show resolved Hide resolved
packages/feature-detector/src/find-globals.ts Outdated Show resolved Hide resolved
packages/feature-detector/src/find-globals.ts Outdated Show resolved Hide resolved
packages/feature-detector/src/find-globals.ts Show resolved Hide resolved
@feugy feugy force-pushed the damien/ec-544-build-a-minimal-ts-morph-feature-2 branch from 04c847d to 3e7ee60 Compare January 16, 2023 14:15
Copy link

@nuta nuta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍 Nice work!

turbo.json Show resolved Hide resolved
@feugy feugy merged commit 328c47b into main Jan 18, 2023
@github-actions github-actions bot mentioned this pull request Jan 18, 2023
@Kikobeats Kikobeats deleted the damien/ec-544-build-a-minimal-ts-morph-feature-2 branch January 18, 2023 17:31
jridgewell pushed a commit to jridgewell/edge-runtime that referenced this pull request Jun 23, 2023
Co-authored-by: Kiko Beats <josefrancisco.verdu@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants