-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
feat(@parcel/transformer-js): Rewrite __dirname/__filename to be relative to asset.filePath
#7727
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
Merged
Merged
Changes from all commits
Commits
Show all changes
50 commits
Select commit
Hold shift + click to select a range
2c3b500
initial
LekoArts c24fccb
revert some changes
LekoArts a7e7b3a
add path import
LekoArts eca555e
test wip
LekoArts f3a29cb
Merge branch 'v2' into dirname-relative-path
LekoArts b7b49e3
re-add example
LekoArts 8d73fe0
update swc
LekoArts 2a45008
update example
LekoArts 08bd388
move logic to new replacer
LekoArts a2d6f12
edit packager
LekoArts fd1a569
Merge branch 'v2' into dirname-relative-path
LekoArts a231c46
delete example once more
LekoArts 1646589
revert a change
LekoArts 87df079
add js transformer test
LekoArts e8e9a4d
fix filename
LekoArts a634a00
revert typo
LekoArts 9da76e9
apply first batch of review comments
LekoArts 1d820d5
test changes
LekoArts 84b9301
add comment
LekoArts 6251b3d
Merge branch 'v2' into dirname-relative-path
LekoArts ef6cebf
trigger ci?
LekoArts e8c6f4d
test changes
LekoArts 95c093f
correct pathing in all os?
LekoArts 748eac3
Merge branch 'v2' into dirname-relative-path
mischnic 1b186df
pray to the testing gods
LekoArts ee9faaf
Merge branch 'v2' into dirname-relative-path
LekoArts 2c7a4c8
use path util
LekoArts e5d78e0
fix lint error
LekoArts b3a92ab
trigger ci
LekoArts ffac3ec
trigger ci
LekoArts 1f0ebae
replace assets individually
LekoArts f5e8337
Merge branch 'v2' into dirname-relative-path
LekoArts 3cba5c2
update rust code
LekoArts ad46e2e
trigger ci
LekoArts 5286520
trigger ci
LekoArts d249b88
add asset.meta.has_node_replacements
LekoArts f351f21
flow is weird 🙈
LekoArts 1d1b927
trigger ci
LekoArts 8cabe10
Merge branch 'v2' into dirname-relative-path
LekoArts 762b3c8
trigger ci
LekoArts 256b7c2
Merge branch 'v2' into dirname-relative-path
LekoArts 997e407
use filename OsStr instead of pathdiff in __filename
LekoArts 151a54e
trigger ci
LekoArts 0b45828
Merge branch 'v2' into dirname-relative-path
mischnic 7d313ca
use simpler fold_with
LekoArts b415560
move replace call up in scopeHoistingPackager
LekoArts 0d96930
trigger ci
LekoArts c7bc555
Merge branch 'v2' into dirname-relative-path
mischnic ae7ce09
Merge branch 'v2' into dirname-relative-path
devongovett 43e16a9
Merge branch 'v2' into dirname-relative-path
mischnic 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
File renamed without changes.
24 changes: 24 additions & 0 deletions
24
packages/core/integration-tests/test/integration/env-node-replacements/index.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,24 @@ | ||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
| const otherFunction = require('./other/function') | ||
| const subFunction = require('./sub/index') | ||
|
|
||
| module.exports = function () { | ||
| const data = fs.readFileSync(path.join(__dirname, 'data', 'test.txt'), 'utf8') | ||
| const firstDirnameTest = path.join(__dirname, 'data') | ||
| const secondDirnameTest = path.join(__dirname, 'other-data') | ||
| const firstFilenameTest = __filename | ||
| const secondFilenameTest = `${__filename}?query-string=test` | ||
| const other = otherFunction() | ||
| const sub = subFunction() | ||
|
|
||
| return { | ||
| data, | ||
| firstDirnameTest, | ||
| secondDirnameTest, | ||
| firstFilenameTest, | ||
| secondFilenameTest, | ||
| other, | ||
| sub, | ||
| } | ||
| } |
6 changes: 6 additions & 0 deletions
6
packages/core/integration-tests/test/integration/env-node-replacements/other/function.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,6 @@ | ||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
|
|
||
| module.exports = function () { | ||
| return fs.readFileSync(path.join(__dirname, '..', 'data', 'test.txt'), 'utf8') | ||
| } |
6 changes: 6 additions & 0 deletions
6
packages/core/integration-tests/test/integration/env-node-replacements/package.json
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,6 @@ | ||
| { | ||
| "name": "env-node-replacements", | ||
| "engines": { | ||
| "node": ">=14" | ||
| } | ||
| } |
6 changes: 6 additions & 0 deletions
6
packages/core/integration-tests/test/integration/env-node-replacements/sub/index.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,6 @@ | ||
| module.exports = function () { | ||
| return { | ||
| dirname: __dirname, | ||
| filename: __filename | ||
| } | ||
| } |
Empty file.
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
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 |
|---|---|---|
| @@ -1,7 +1,12 @@ | ||
| // @flow strict-local | ||
| import type {BundleGraph, PluginOptions, NamedBundle} from '@parcel/types'; | ||
|
|
||
| import {PromiseQueue, relativeBundlePath, countLines} from '@parcel/utils'; | ||
| import { | ||
| PromiseQueue, | ||
| relativeBundlePath, | ||
| countLines, | ||
| normalizeSeparators, | ||
| } from '@parcel/utils'; | ||
| import SourceMap from '@parcel/source-map'; | ||
| import invariant from 'assert'; | ||
| import path from 'path'; | ||
|
|
@@ -116,6 +121,20 @@ export class DevPackager { | |
| wrapped += JSON.stringify(deps); | ||
| wrapped += ']'; | ||
|
|
||
| if ( | ||
| this.bundle.env.isNode() && | ||
| asset.meta.has_node_replacements === true | ||
| ) { | ||
| const relPath = normalizeSeparators( | ||
| path.relative( | ||
| this.bundle.target.distDir, | ||
| path.dirname(asset.filePath), | ||
| ), | ||
| ); | ||
| wrapped = wrapped.replace('$parcel$dirnameReplace', relPath); | ||
| wrapped = wrapped.replace('$parcel$filenameReplace', relPath); | ||
|
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. Is there a reason we have two different variables that get replaced with the same path? Were these meant to be different, or do we only need one of them? |
||
| } | ||
|
|
||
| if (this.bundle.env.sourceMap) { | ||
| if (mapBuffer) { | ||
| map.addBuffer(mapBuffer, lineOffset); | ||
|
|
||
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.