@atlaspack/packager-js
Advanced tools
Comparing version
@@ -56,2 +56,9 @@ "use strict"; | ||
} | ||
function _featureFlags() { | ||
const data = require("@atlaspack/feature-flags"); | ||
_featureFlags = function () { | ||
return data; | ||
}; | ||
return data; | ||
} | ||
var _ESMOutputFormat = require("./ESMOutputFormat"); | ||
@@ -67,2 +74,5 @@ var _CJSOutputFormat = require("./CJSOutputFormat"); | ||
// and count the number of newlines in the file for source maps. | ||
// | ||
// For conditional bundling the only difference in this regex is adding `importCond` where we have `importAsync` etc.. | ||
const REPLACEMENT_RE_CONDITIONAL = /\n|import\s+"([0-9a-f]{16,20}:.+?)";|(?:\$[0-9a-f]{16,20}\$exports)|(?:\$[0-9a-f]{16,20}\$(?:import|importAsync|require|importCond)\$[0-9a-f]+(?:\$[0-9a-f]+)?)/g; | ||
const REPLACEMENT_RE = /\n|import\s+"([0-9a-f]{16,20}:.+?)";|(?:\$[0-9a-f]{16,20}\$exports)|(?:\$[0-9a-f]{16,20}\$(?:import|importAsync|require)\$[0-9a-f]+(?:\$[0-9a-f]+)?)/g; | ||
@@ -432,3 +442,3 @@ const BUILTINS = Object.keys(_globals().default.builtin); | ||
let columnStartIndex = 0; | ||
code = code.replace(REPLACEMENT_RE, (m, d, i) => { | ||
code = code.replace((0, _featureFlags().getFeatureFlag)('conditionalBundlingApi') ? REPLACEMENT_RE_CONDITIONAL : REPLACEMENT_RE, (m, d, i) => { | ||
if (m === '\n') { | ||
@@ -580,3 +590,3 @@ columnStartIndex = i + offset + 1; | ||
// symbols so that we don't mark all symbols as used. | ||
if (dep.priority === 'lazy' && dep.meta.promiseSymbol) { | ||
if ((dep.priority === 'lazy' || dep.priority === 'conditional') && dep.meta.promiseSymbol) { | ||
let promiseSymbol = dep.meta.promiseSymbol; | ||
@@ -1015,3 +1025,5 @@ (0, _assert().default)(typeof promiseSymbol === 'string'); | ||
let parentBundles = this.bundleGraph.getParentBundles(this.bundle); | ||
let mightBeFirstJS = parentBundles.length === 0 || parentBundles.some(b => b.type !== 'js') || this.bundleGraph.getBundleGroupsContainingBundle(this.bundle).some(g => this.bundleGraph.isEntryBundleGroup(g)) || this.bundle.env.isIsolated() || this.bundle.bundleBehavior === 'isolated'; | ||
let mightBeFirstJS = parentBundles.length === 0 || parentBundles.some(b => b.type !== 'js') || this.bundleGraph.getBundleGroupsContainingBundle(this.bundle).some(g => this.bundleGraph.isEntryBundleGroup(g)) || this.bundle.env.isIsolated() || this.bundle.bundleBehavior === 'isolated' || | ||
// Conditional deps may be loaded before entrypoints on the server | ||
this.hasConditionalDependency(); | ||
if (mightBeFirstJS) { | ||
@@ -1075,3 +1087,9 @@ let preludeCode = (0, _helpers.prelude)(this.parcelRequireName); | ||
} | ||
hasConditionalDependency() { | ||
if (!(0, _featureFlags().getFeatureFlag)('conditionalBundlingApi')) { | ||
return false; | ||
} | ||
return this.bundle.getEntryAssets().some(entry => this.bundleGraph.getIncomingDependencies(entry).some(dep => dep.priority === 'conditional')); | ||
} | ||
} | ||
exports.ScopeHoistingPackager = ScopeHoistingPackager; |
{ | ||
"name": "@atlaspack/packager-js", | ||
"version": "2.12.1-canary.3432+415929ab6", | ||
"version": "2.12.1-canary.3434+c61bf3cde", | ||
"license": "(MIT OR Apache-2.0)", | ||
@@ -16,11 +16,11 @@ "publishConfig": { | ||
"node": ">= 16.0.0", | ||
"parcel": "2.12.1-canary.3432+415929ab6" | ||
"parcel": "2.12.1-canary.3434+c61bf3cde" | ||
}, | ||
"dependencies": { | ||
"@atlaspack/diagnostic": "2.12.1-canary.3432+415929ab6", | ||
"@atlaspack/feature-flags": "2.12.1-canary.3432+415929ab6", | ||
"@atlaspack/plugin": "2.12.1-canary.3432+415929ab6", | ||
"@atlaspack/rust": "2.12.1-canary.3432+415929ab6", | ||
"@atlaspack/types": "2.12.1-canary.3432+415929ab6", | ||
"@atlaspack/utils": "2.12.1-canary.3432+415929ab6", | ||
"@atlaspack/diagnostic": "2.12.1-canary.3434+c61bf3cde", | ||
"@atlaspack/feature-flags": "2.12.1-canary.3434+c61bf3cde", | ||
"@atlaspack/plugin": "2.12.1-canary.3434+c61bf3cde", | ||
"@atlaspack/rust": "2.12.1-canary.3434+c61bf3cde", | ||
"@atlaspack/types": "2.12.1-canary.3434+c61bf3cde", | ||
"@atlaspack/utils": "2.12.1-canary.3434+c61bf3cde", | ||
"@parcel/source-map": "^2.1.1", | ||
@@ -30,3 +30,3 @@ "globals": "^13.2.0", | ||
}, | ||
"gitHead": "415929ab63f5314913c27d3dfdb965f2e76bedf2" | ||
"gitHead": "c61bf3cde53d1cdb5ae13541c644a6e3ef5521e1" | ||
} |
@@ -27,2 +27,3 @@ // @flow | ||
import path from 'path'; | ||
import {getFeatureFlag} from '@atlaspack/feature-flags'; | ||
@@ -42,2 +43,6 @@ import {ESMOutputFormat} from './ESMOutputFormat'; | ||
// and count the number of newlines in the file for source maps. | ||
// | ||
// For conditional bundling the only difference in this regex is adding `importCond` where we have `importAsync` etc.. | ||
const REPLACEMENT_RE_CONDITIONAL = | ||
/\n|import\s+"([0-9a-f]{16,20}:.+?)";|(?:\$[0-9a-f]{16,20}\$exports)|(?:\$[0-9a-f]{16,20}\$(?:import|importAsync|require|importCond)\$[0-9a-f]+(?:\$[0-9a-f]+)?)/g; | ||
const REPLACEMENT_RE = | ||
@@ -569,88 +574,96 @@ /\n|import\s+"([0-9a-f]{16,20}:.+?)";|(?:\$[0-9a-f]{16,20}\$exports)|(?:\$[0-9a-f]{16,20}\$(?:import|importAsync|require)\$[0-9a-f]+(?:\$[0-9a-f]+)?)/g; | ||
let columnStartIndex = 0; | ||
code = code.replace(REPLACEMENT_RE, (m, d, i) => { | ||
if (m === '\n') { | ||
columnStartIndex = i + offset + 1; | ||
lineCount++; | ||
return '\n'; | ||
} | ||
// If we matched an import, replace with the source code for the dependency. | ||
if (d != null) { | ||
let deps = depMap.get(d); | ||
if (!deps) { | ||
return m; | ||
code = code.replace( | ||
getFeatureFlag('conditionalBundlingApi') | ||
? REPLACEMENT_RE_CONDITIONAL | ||
: REPLACEMENT_RE, | ||
(m, d, i) => { | ||
if (m === '\n') { | ||
columnStartIndex = i + offset + 1; | ||
lineCount++; | ||
return '\n'; | ||
} | ||
let replacement = ''; | ||
// If we matched an import, replace with the source code for the dependency. | ||
if (d != null) { | ||
let deps = depMap.get(d); | ||
if (!deps) { | ||
return m; | ||
} | ||
// A single `${id}:${specifier}:esm` might have been resolved to multiple assets due to | ||
// reexports. | ||
for (let dep of deps) { | ||
let resolved = this.bundleGraph.getResolvedAsset(dep, this.bundle); | ||
let skipped = this.bundleGraph.isDependencySkipped(dep); | ||
if (resolved && !skipped) { | ||
// Hoist variable declarations for the referenced parcelRequire dependencies | ||
// after the dependency is declared. This handles the case where the resulting asset | ||
// is wrapped, but the dependency in this asset is not marked as wrapped. This means | ||
// that it was imported/required at the top-level, so its side effects should run immediately. | ||
let [res, lines] = this.getHoistedParcelRequires( | ||
asset, | ||
let replacement = ''; | ||
// A single `${id}:${specifier}:esm` might have been resolved to multiple assets due to | ||
// reexports. | ||
for (let dep of deps) { | ||
let resolved = this.bundleGraph.getResolvedAsset( | ||
dep, | ||
resolved, | ||
this.bundle, | ||
); | ||
let map; | ||
if ( | ||
this.bundle.hasAsset(resolved) && | ||
!this.seenAssets.has(resolved.id) | ||
) { | ||
// If this asset is wrapped, we need to hoist the code for the dependency | ||
// outside our parcelRequire.register wrapper. This is safe because all | ||
// assets referenced by this asset will also be wrapped. Otherwise, inline the | ||
// asset content where the import statement was. | ||
if (shouldWrap) { | ||
depContent.push(this.visitAsset(resolved)); | ||
} else { | ||
let [depCode, depMap, depLines] = this.visitAsset(resolved); | ||
res = depCode + '\n' + res; | ||
lines += 1 + depLines; | ||
map = depMap; | ||
let skipped = this.bundleGraph.isDependencySkipped(dep); | ||
if (resolved && !skipped) { | ||
// Hoist variable declarations for the referenced parcelRequire dependencies | ||
// after the dependency is declared. This handles the case where the resulting asset | ||
// is wrapped, but the dependency in this asset is not marked as wrapped. This means | ||
// that it was imported/required at the top-level, so its side effects should run immediately. | ||
let [res, lines] = this.getHoistedParcelRequires( | ||
asset, | ||
dep, | ||
resolved, | ||
); | ||
let map; | ||
if ( | ||
this.bundle.hasAsset(resolved) && | ||
!this.seenAssets.has(resolved.id) | ||
) { | ||
// If this asset is wrapped, we need to hoist the code for the dependency | ||
// outside our parcelRequire.register wrapper. This is safe because all | ||
// assets referenced by this asset will also be wrapped. Otherwise, inline the | ||
// asset content where the import statement was. | ||
if (shouldWrap) { | ||
depContent.push(this.visitAsset(resolved)); | ||
} else { | ||
let [depCode, depMap, depLines] = this.visitAsset(resolved); | ||
res = depCode + '\n' + res; | ||
lines += 1 + depLines; | ||
map = depMap; | ||
} | ||
} | ||
} | ||
// Push this asset's source mappings down by the number of lines in the dependency | ||
// plus the number of hoisted parcelRequires. Then insert the source map for the dependency. | ||
if (sourceMap) { | ||
if (lines > 0) { | ||
sourceMap.offsetLines(lineCount + 1, lines); | ||
// Push this asset's source mappings down by the number of lines in the dependency | ||
// plus the number of hoisted parcelRequires. Then insert the source map for the dependency. | ||
if (sourceMap) { | ||
if (lines > 0) { | ||
sourceMap.offsetLines(lineCount + 1, lines); | ||
} | ||
if (map) { | ||
sourceMap.addSourceMap(map, lineCount); | ||
} | ||
} | ||
if (map) { | ||
sourceMap.addSourceMap(map, lineCount); | ||
} | ||
replacement += res; | ||
lineCount += lines; | ||
} | ||
} | ||
return replacement; | ||
} | ||
replacement += res; | ||
lineCount += lines; | ||
// If it wasn't a dependency, then it was an inline replacement (e.g. $id$import$foo -> $id$export$foo). | ||
let replacement = replacements.get(m) ?? m; | ||
if (sourceMap) { | ||
// Offset the source map columns for this line if the replacement was a different length. | ||
// This assumes that the match and replacement both do not contain any newlines. | ||
let lengthDifference = replacement.length - m.length; | ||
if (lengthDifference !== 0) { | ||
sourceMap.offsetColumns( | ||
lineCount + 1, | ||
i + offset - columnStartIndex + m.length, | ||
lengthDifference, | ||
); | ||
offset += lengthDifference; | ||
} | ||
} | ||
return replacement; | ||
} | ||
// If it wasn't a dependency, then it was an inline replacement (e.g. $id$import$foo -> $id$export$foo). | ||
let replacement = replacements.get(m) ?? m; | ||
if (sourceMap) { | ||
// Offset the source map columns for this line if the replacement was a different length. | ||
// This assumes that the match and replacement both do not contain any newlines. | ||
let lengthDifference = replacement.length - m.length; | ||
if (lengthDifference !== 0) { | ||
sourceMap.offsetColumns( | ||
lineCount + 1, | ||
i + offset - columnStartIndex + m.length, | ||
lengthDifference, | ||
); | ||
offset += lengthDifference; | ||
} | ||
} | ||
return replacement; | ||
}); | ||
}, | ||
); | ||
} | ||
@@ -777,3 +790,6 @@ | ||
// symbols so that we don't mark all symbols as used. | ||
if (dep.priority === 'lazy' && dep.meta.promiseSymbol) { | ||
if ( | ||
(dep.priority === 'lazy' || dep.priority === 'conditional') && | ||
dep.meta.promiseSymbol | ||
) { | ||
let promiseSymbol = dep.meta.promiseSymbol; | ||
@@ -1411,3 +1427,5 @@ invariant(typeof promiseSymbol === 'string'); | ||
this.bundle.env.isIsolated() || | ||
this.bundle.bundleBehavior === 'isolated'; | ||
this.bundle.bundleBehavior === 'isolated' || | ||
// Conditional deps may be loaded before entrypoints on the server | ||
this.hasConditionalDependency(); | ||
@@ -1497,2 +1515,16 @@ if (mightBeFirstJS) { | ||
} | ||
hasConditionalDependency(): boolean { | ||
if (!getFeatureFlag('conditionalBundlingApi')) { | ||
return false; | ||
} | ||
return this.bundle | ||
.getEntryAssets() | ||
.some(entry => | ||
this.bundleGraph | ||
.getIncomingDependencies(entry) | ||
.some(dep => dep.priority === 'conditional'), | ||
); | ||
} | ||
} |
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
172006
1.3%4117
1.18%