-
-
Notifications
You must be signed in to change notification settings - Fork 33
feat(cjs-to-esm): context-local variables → import.meta.*
#399
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
Open
AugustinMauroy
wants to merge
5
commits into
wip/cjs-to-esm
Choose a base branch
from
augustin/issue#348
base: wip/cjs-to-esm
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
db4afa6
feat(`cjs-esm/context-local-variable-process`): wip
AugustinMauroy 94f17be
finish
AugustinMauroy 2da5e04
Apply suggestions from code review
AugustinMauroy 7b85665
Update context-local-variable-process.ts
AugustinMauroy b33b499
Simplify require.main -> import.meta.main handling
AugustinMauroy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
121 changes: 119 additions & 2 deletions
121
recipes/cjs-to-esm/src/context-local-variable-process.ts
This file contains hidden or 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
8 changes: 8 additions & 0 deletions
8
recipes/cjs-to-esm/tests/context-local-variable/all-shadowed-locals/expected.js
This file contains hidden or 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,8 @@ | ||
| const __filename = '/tmp/file.js'; | ||
| const __dirname = '/tmp'; | ||
|
|
||
| function print(__filename, __dirname) { | ||
| return `${__filename}::${__dirname}`; | ||
| } | ||
|
|
||
| console.log(__filename, __dirname, print('a', 'b')); |
8 changes: 8 additions & 0 deletions
8
recipes/cjs-to-esm/tests/context-local-variable/all-shadowed-locals/input.js
This file contains hidden or 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,8 @@ | ||
| const __filename = '/tmp/file.js'; | ||
| const __dirname = '/tmp'; | ||
|
|
||
| function print(__filename, __dirname) { | ||
| return `${__filename}::${__dirname}`; | ||
| } | ||
|
|
||
| console.log(__filename, __dirname, print('a', 'b')); |
8 changes: 8 additions & 0 deletions
8
recipes/cjs-to-esm/tests/context-local-variable/already-esm/expected.js
This file contains hidden or 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,8 @@ | ||
| const info = { | ||
| filename: import.meta.filename, | ||
| dirname: import.meta.dirname, | ||
| isMain: import.meta.main, | ||
| resolved: import.meta.resolve('./foo.js'), | ||
| }; | ||
|
|
||
| console.log(info); |
8 changes: 8 additions & 0 deletions
8
recipes/cjs-to-esm/tests/context-local-variable/already-esm/input.js
This file contains hidden or 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,8 @@ | ||
| const info = { | ||
| filename: import.meta.filename, | ||
| dirname: import.meta.dirname, | ||
| isMain: import.meta.main, | ||
| resolved: import.meta.resolve('./foo.js'), | ||
| }; | ||
|
|
||
| console.log(info); |
8 changes: 8 additions & 0 deletions
8
recipes/cjs-to-esm/tests/context-local-variable/basic/expected.js
This file contains hidden or 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,8 @@ | ||
| const info = { | ||
| filename: import.meta.filename, | ||
| dirname: import.meta.dirname, | ||
| isMain: import.meta.main, | ||
| resolved: import.meta.resolve('./foo.js'), | ||
| }; | ||
|
|
||
| console.log(info, import.meta.main); |
8 changes: 8 additions & 0 deletions
8
recipes/cjs-to-esm/tests/context-local-variable/basic/input.js
This file contains hidden or 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,8 @@ | ||
| const info = { | ||
| filename: __filename, | ||
| dirname: __dirname, | ||
| isMain: require.main === module, | ||
| resolved: require.resolve('./foo.js'), | ||
| }; | ||
|
|
||
| console.log(info, require.main); |
8 changes: 8 additions & 0 deletions
8
recipes/cjs-to-esm/tests/context-local-variable/mixed-transformations/expected.js
This file contains hidden or 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,8 @@ | ||
| if (import.meta.main) { | ||
| boot(); | ||
| } | ||
|
|
||
| const resolved = import.meta.resolve('./foo.js', { paths: [__dirname] }); | ||
| const filePath = `${import.meta.filename}`; | ||
|
|
||
| console.log(import.meta.main, resolved, filePath); |
8 changes: 8 additions & 0 deletions
8
recipes/cjs-to-esm/tests/context-local-variable/mixed-transformations/input.js
This file contains hidden or 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,8 @@ | ||
| if (require.main === module) { | ||
| boot(); | ||
| } | ||
|
|
||
| const resolved = require.resolve('./foo.js', { paths: [__dirname] }); | ||
| const filePath = `${__filename}`; | ||
|
|
||
| console.log(require.main, resolved, filePath); |
2 changes: 2 additions & 0 deletions
2
recipes/cjs-to-esm/tests/context-local-variable/module-exports-read/expected.js
This file contains hidden or 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,2 @@ | ||
| const currentModule = module; | ||
| console.log(exports.value, currentModule.id); |
2 changes: 2 additions & 0 deletions
2
recipes/cjs-to-esm/tests/context-local-variable/module-exports-read/input.js
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand this one. This looks like CJS, but it isn't transformed.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh I didn't recall why I was requiring this test |
This file contains hidden or 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,2 @@ | ||
| const currentModule = module; | ||
| console.log(exports.value, currentModule.id); |
5 changes: 5 additions & 0 deletions
5
recipes/cjs-to-esm/tests/context-local-variable/require-main-non-comparison/expected.js
This file contains hidden or 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,5 @@ | ||
| const shouldRun = Boolean(import.meta.main); | ||
|
|
||
| if (shouldRun) { | ||
| run(); | ||
| } |
5 changes: 5 additions & 0 deletions
5
recipes/cjs-to-esm/tests/context-local-variable/require-main-non-comparison/input.js
This file contains hidden or 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,5 @@ | ||
| const shouldRun = Boolean(require.main); | ||
|
|
||
| if (shouldRun) { | ||
| run(); | ||
| } |
3 changes: 3 additions & 0 deletions
3
recipes/cjs-to-esm/tests/context-local-variable/reverse-comparison/expected.js
This file contains hidden or 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,3 @@ | ||
| if (import.meta.main) { | ||
| start(); | ||
| } |
3 changes: 3 additions & 0 deletions
3
recipes/cjs-to-esm/tests/context-local-variable/reverse-comparison/input.js
This file contains hidden or 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,3 @@ | ||
| if (module === require.main) { | ||
| start(); | ||
| } |
12 changes: 12 additions & 0 deletions
12
recipes/cjs-to-esm/tests/context-local-variable/shadowed/expected.js
This file contains hidden or 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,12 @@ | ||
| console.log(import.meta.dirname); | ||
|
|
||
| function read(__filename) { | ||
| return __filename; | ||
| } | ||
|
|
||
| { | ||
| const __dirname = 'tmp'; | ||
| console.log(__dirname); | ||
| } | ||
|
|
||
| console.log(import.meta.filename); |
12 changes: 12 additions & 0 deletions
12
recipes/cjs-to-esm/tests/context-local-variable/shadowed/input.js
This file contains hidden or 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,12 @@ | ||
| console.log(__dirname); | ||
|
|
||
| function read(__filename) { | ||
| return __filename; | ||
| } | ||
|
|
||
| { | ||
| const __dirname = 'tmp'; | ||
| console.log(__dirname); | ||
| } | ||
|
|
||
| console.log(__filename); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nit: it's a little difficult to quickly see how these lines are different. I think this would both help with that and DRY things up a tiny bit
or if result is just potentially nullish