-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use dependencies instead of bundle ids for markers and make the packa…
…gers do replacement
- Loading branch information
Will Binns-Smith
committed
Nov 4, 2019
1 parent
9b123ed
commit 2a8a310
Showing
15 changed files
with
192 additions
and
175 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
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
3 changes: 3 additions & 0 deletions
3
packages/core/integration-tests/test/integration/data-url/binary.css
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,3 @@ | ||
.webp-img { | ||
background-image: url('data-url:parcel.webp') | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/core/integration-tests/test/integration/data-url/text.css
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,3 @@ | ||
.svg-img { | ||
background-image: url('data-url:img.svg'); | ||
} |
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,88 @@ | ||
// @flow strict-local | ||
|
||
import type SourceMap from '@parcel/source-map'; | ||
import type {Async, Blob, Bundle, BundleGraph} from '@parcel/types'; | ||
|
||
import {Readable} from 'stream'; | ||
import {bufferStream, urlJoin} from '../'; | ||
|
||
import nullthrows from 'nullthrows'; | ||
|
||
/* | ||
* Replaces references to dependency ids with either: | ||
* - in the case of an inline bundle, the packaged contents of that bundle | ||
* - in the case of another bundle reference, the bundle's url from the publicUrl root | ||
* - in the case of a url dependency that Parcel did not handle, | ||
* the original moduleSpecifier. These are external requests. | ||
*/ | ||
export default async function replaceBundleReferences({ | ||
bundle, | ||
bundleGraph, | ||
contents, | ||
map, | ||
formatInline = str => str, | ||
getInlineBundleContents | ||
}: {| | ||
bundle: Bundle, | ||
bundleGraph: BundleGraph, | ||
contents: string, | ||
formatInline: string => string, | ||
getInlineBundleContents: ( | ||
Bundle, | ||
BundleGraph | ||
) => Async<{|contents: Blob, map: ?(Readable | string)|}>, | ||
map?: ?SourceMap | ||
|}) { | ||
let replacements = new Map(); | ||
|
||
for (let { | ||
dependency, | ||
bundleGroup | ||
} of bundleGraph.getBundleGroupsReferencedByBundle(bundle)) { | ||
let [entryBundle] = bundleGraph.getBundlesInBundleGroup(bundleGroup); | ||
if (entryBundle.isInline) { | ||
// inline bundles | ||
let packagedBundle = await getInlineBundleContents( | ||
entryBundle, | ||
bundleGraph | ||
); | ||
let packagedContents = (packagedBundle.contents instanceof Readable | ||
? await bufferStream(packagedBundle.contents) | ||
: packagedBundle.contents | ||
).toString(); | ||
replacements.set(dependency.id, formatInline(packagedContents)); | ||
} else { | ||
// url references | ||
replacements.set( | ||
dependency.id, | ||
urlJoin( | ||
entryBundle.target.publicUrl ?? '/', | ||
nullthrows(entryBundle.name) | ||
) | ||
); | ||
} | ||
} | ||
|
||
// external url references | ||
bundle.traverse(node => { | ||
if (node.type !== 'dependency') { | ||
return; | ||
} | ||
|
||
let dependency = node.value; | ||
if (dependency.isURL && !replacements.has(dependency.id)) { | ||
replacements.set(dependency.id, dependency.moduleSpecifier); | ||
} | ||
}); | ||
|
||
let finalContents = contents; | ||
for (let [depId, replacement] of replacements) { | ||
finalContents = finalContents.replace(new RegExp(depId, 'g'), replacement); | ||
} | ||
|
||
return { | ||
contents: finalContents, | ||
// TODO: Update sourcemap with adjusted contents | ||
map | ||
}; | ||
} |
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.