@atlaspack/runtime-js
Advanced tools
Comparing version
@@ -96,2 +96,15 @@ "use strict"; | ||
type: 'number' | ||
}, | ||
domainSharding: { | ||
type: 'object', | ||
properties: { | ||
maxShards: { | ||
type: 'number' | ||
}, | ||
cookieName: { | ||
type: 'string' | ||
} | ||
}, | ||
additionalProperties: false, | ||
required: ['maxShards', 'cookieName'] | ||
} | ||
@@ -185,3 +198,4 @@ }, | ||
bundleGroup: resolved.value, | ||
options | ||
options, | ||
shardingConfig: config.domainSharding | ||
}); | ||
@@ -262,3 +276,3 @@ if (loaderRuntime != null) { | ||
// URL dependency or not, fall back to including a runtime that exports the url | ||
assets.push(getURLRuntime(dependency, bundle, mainBundle, options)); | ||
assets.push(getURLRuntime(dependency, bundle, mainBundle, options, config.domainSharding)); | ||
} | ||
@@ -281,3 +295,3 @@ | ||
let relativePathExpr = getRelativePathExpr(bundle, referencedBundle, options); | ||
let loaderCode = `require(${JSON.stringify(loader)})( ${getAbsoluteUrlExpr(relativePathExpr, bundle)})`; | ||
let loaderCode = `require(${JSON.stringify(loader)})(${getAbsoluteUrlExpr(relativePathExpr, bundle, referencedBundle, config.domainSharding)})`; | ||
assets.push({ | ||
@@ -345,3 +359,4 @@ filePath: __filename, | ||
bundleGraph, | ||
options | ||
options, | ||
shardingConfig | ||
}) { | ||
@@ -380,3 +395,3 @@ let loaders = getLoaders(bundle.env); | ||
let loaderModules = []; | ||
function getLoaderForBundle(bundle, to) { | ||
function getLoaderForBundle(bundle, to, shardingConfig) { | ||
let loader = loaders[to.type]; | ||
@@ -401,3 +416,3 @@ if (!loader) { | ||
} | ||
let absoluteUrlExpr = shouldUseRuntimeManifest(bundle, options) ? `require('./helpers/bundle-manifest').resolve(${JSON.stringify(to.publicId)})` : getAbsoluteUrlExpr(relativePathExpr, bundle); | ||
let absoluteUrlExpr = shouldUseRuntimeManifest(bundle, options) ? `require('./helpers/bundle-manifest').resolve(${JSON.stringify(to.publicId)})` : getAbsoluteUrlExpr(relativePathExpr, bundle, to, shardingConfig); | ||
let code = `require(${JSON.stringify(loader)})(${absoluteUrlExpr})`; | ||
@@ -425,3 +440,3 @@ | ||
for (let to of externalBundles) { | ||
let loaderModule = getLoaderForBundle(bundle, to); | ||
let loaderModule = getLoaderForBundle(bundle, to, shardingConfig); | ||
if (loaderModule !== undefined) loaderModules.push(loaderModule); | ||
@@ -524,3 +539,3 @@ } | ||
let priority = TYPE_TO_RESOURCE_PRIORITY[bundleToPreload.type]; | ||
hintLoaders.push(`require(${JSON.stringify(loader)})(${getAbsoluteUrlExpr(relativePathExpr, from)}, ${priority ? JSON.stringify(priority) : 'null'}, ${JSON.stringify(bundleToPreload.target.env.outputFormat === 'esmodule')})`); | ||
hintLoaders.push(`require(${JSON.stringify(loader)})(${getAbsoluteUrlExpr(relativePathExpr, from, bundleToPreload)}, ${priority ? JSON.stringify(priority) : 'null'}, ${JSON.stringify(bundleToPreload.target.env.outputFormat === 'esmodule')})`); | ||
} | ||
@@ -535,3 +550,3 @@ } | ||
} | ||
function getURLRuntime(dependency, from, to, options) { | ||
function getURLRuntime(dependency, from, to, options, shardingConfig) { | ||
let relativePathExpr = getRelativePathExpr(from, to, options); | ||
@@ -550,3 +565,3 @@ let code; | ||
} else { | ||
code = `module.exports = ${getAbsoluteUrlExpr(relativePathExpr, from)};`; | ||
code = `module.exports = ${getAbsoluteUrlExpr(relativePathExpr, from, to, shardingConfig)};`; | ||
} | ||
@@ -599,9 +614,12 @@ return { | ||
} | ||
function getAbsoluteUrlExpr(relativePathExpr, bundle) { | ||
if (bundle.env.outputFormat === 'esmodule' && bundle.env.supports('import-meta-url') || bundle.env.outputFormat === 'commonjs') { | ||
function getAbsoluteUrlExpr(relativePathExpr, fromBundle, toBundle, shardingConfig) { | ||
if (fromBundle.env.outputFormat === 'esmodule' && fromBundle.env.supports('import-meta-url') || fromBundle.env.outputFormat === 'commonjs') { | ||
// This will be compiled to new URL(url, import.meta.url) or new URL(url, 'file:' + __filename). | ||
return `new __parcel__URL__(${relativePathExpr}).toString()`; | ||
} else { | ||
return `require('./helpers/bundle-url').getBundleURL('${bundle.publicId}') + ${relativePathExpr}`; | ||
} | ||
if (shardingConfig) { | ||
const bundleUrlArgs = [`'${toBundle.name}'`, `'${shardingConfig.cookieName}'`, 'document.cookie', shardingConfig.maxShards].join(', '); | ||
return `require('./helpers/bundle-url-shards').getShardedBundleURL(${bundleUrlArgs}) + ${relativePathExpr}`; | ||
} | ||
return `require('./helpers/bundle-url').getBundleURL('${fromBundle.publicId}') + ${relativePathExpr}`; | ||
} | ||
@@ -608,0 +626,0 @@ function shouldUseRuntimeManifest(bundle, options) { |
{ | ||
"name": "@atlaspack/runtime-js", | ||
"version": "2.12.1-canary.3581+f0f92f212", | ||
"version": "2.12.1-canary.3582+6edef3532", | ||
"license": "(MIT OR Apache-2.0)", | ||
@@ -15,13 +15,13 @@ "publishConfig": { | ||
"engines": { | ||
"atlaspack": "2.12.1-canary.3581+f0f92f212", | ||
"atlaspack": "2.12.1-canary.3582+6edef3532", | ||
"node": ">= 16.0.0" | ||
}, | ||
"dependencies": { | ||
"@atlaspack/diagnostic": "2.12.1-canary.3581+f0f92f212", | ||
"@atlaspack/feature-flags": "2.12.1-canary.3581+f0f92f212", | ||
"@atlaspack/plugin": "2.12.1-canary.3581+f0f92f212", | ||
"@atlaspack/utils": "2.12.1-canary.3581+f0f92f212", | ||
"@atlaspack/diagnostic": "2.12.1-canary.3582+6edef3532", | ||
"@atlaspack/feature-flags": "2.12.1-canary.3582+6edef3532", | ||
"@atlaspack/plugin": "2.12.1-canary.3582+6edef3532", | ||
"@atlaspack/utils": "2.12.1-canary.3582+6edef3532", | ||
"nullthrows": "^1.1.1" | ||
}, | ||
"gitHead": "f0f92f21248b1577c6ad3673e2eca9a9bb39940c" | ||
"gitHead": "6edef3532c70652f9f294c1399c1ba7318937181" | ||
} |
@@ -77,2 +77,6 @@ // @flow strict-local | ||
splitManifestThreshold: number, | ||
domainSharding?: {| | ||
maxShards: number, | ||
cookieName: string, | ||
|}, | ||
|}; | ||
@@ -90,2 +94,15 @@ | ||
}, | ||
domainSharding: { | ||
type: 'object', | ||
properties: { | ||
maxShards: { | ||
type: 'number', | ||
}, | ||
cookieName: { | ||
type: 'string', | ||
}, | ||
}, | ||
additionalProperties: false, | ||
required: ['maxShards', 'cookieName'], | ||
}, | ||
}, | ||
@@ -186,2 +203,3 @@ additionalProperties: false, | ||
options, | ||
shardingConfig: config.domainSharding, | ||
}); | ||
@@ -277,3 +295,11 @@ | ||
// URL dependency or not, fall back to including a runtime that exports the url | ||
assets.push(getURLRuntime(dependency, bundle, mainBundle, options)); | ||
assets.push( | ||
getURLRuntime( | ||
dependency, | ||
bundle, | ||
mainBundle, | ||
options, | ||
config.domainSharding, | ||
), | ||
); | ||
} | ||
@@ -304,3 +330,8 @@ | ||
loader, | ||
)})( ${getAbsoluteUrlExpr(relativePathExpr, bundle)})`; | ||
)})(${getAbsoluteUrlExpr( | ||
relativePathExpr, | ||
bundle, | ||
referencedBundle, | ||
config.domainSharding, | ||
)})`; | ||
assets.push({ | ||
@@ -384,2 +415,3 @@ filePath: __filename, | ||
options, | ||
shardingConfig, | ||
}: {| | ||
@@ -391,2 +423,3 @@ bundle: NamedBundle, | ||
options: PluginOptions, | ||
shardingConfig: JSRuntimeConfig['domainSharding'], | ||
|}): ?RuntimeAsset { | ||
@@ -433,2 +466,3 @@ let loaders = getLoaders(bundle.env); | ||
to: NamedBundle, | ||
shardingConfig: JSRuntimeConfig['domainSharding'], | ||
): string | void { | ||
@@ -470,3 +504,3 @@ let loader = loaders[to.type]; | ||
)})` | ||
: getAbsoluteUrlExpr(relativePathExpr, bundle); | ||
: getAbsoluteUrlExpr(relativePathExpr, bundle, to, shardingConfig); | ||
let code = `require(${JSON.stringify(loader)})(${absoluteUrlExpr})`; | ||
@@ -520,3 +554,3 @@ | ||
for (let to of externalBundles) { | ||
let loaderModule = getLoaderForBundle(bundle, to); | ||
let loaderModule = getLoaderForBundle(bundle, to, shardingConfig); | ||
if (loaderModule !== undefined) loaderModules.push(loaderModule); | ||
@@ -663,2 +697,3 @@ } | ||
from, | ||
bundleToPreload, | ||
)}, ${priority ? JSON.stringify(priority) : 'null'}, ${JSON.stringify( | ||
@@ -697,2 +732,3 @@ bundleToPreload.target.env.outputFormat === 'esmodule', | ||
options: PluginOptions, | ||
shardingConfig: JSRuntimeConfig['domainSharding'], | ||
): RuntimeAsset { | ||
@@ -720,3 +756,8 @@ let relativePathExpr = getRelativePathExpr(from, to, options); | ||
} else { | ||
code = `module.exports = ${getAbsoluteUrlExpr(relativePathExpr, from)};`; | ||
code = `module.exports = ${getAbsoluteUrlExpr( | ||
relativePathExpr, | ||
from, | ||
to, | ||
shardingConfig, | ||
)};`; | ||
} | ||
@@ -791,13 +832,29 @@ | ||
function getAbsoluteUrlExpr(relativePathExpr: string, bundle: NamedBundle) { | ||
function getAbsoluteUrlExpr( | ||
relativePathExpr: string, | ||
fromBundle: NamedBundle, | ||
toBundle: NamedBundle, | ||
shardingConfig: JSRuntimeConfig['domainSharding'], | ||
) { | ||
if ( | ||
(bundle.env.outputFormat === 'esmodule' && | ||
bundle.env.supports('import-meta-url')) || | ||
bundle.env.outputFormat === 'commonjs' | ||
(fromBundle.env.outputFormat === 'esmodule' && | ||
fromBundle.env.supports('import-meta-url')) || | ||
fromBundle.env.outputFormat === 'commonjs' | ||
) { | ||
// This will be compiled to new URL(url, import.meta.url) or new URL(url, 'file:' + __filename). | ||
return `new __parcel__URL__(${relativePathExpr}).toString()`; | ||
} else { | ||
return `require('./helpers/bundle-url').getBundleURL('${bundle.publicId}') + ${relativePathExpr}`; | ||
} | ||
if (shardingConfig) { | ||
const bundleUrlArgs = [ | ||
`'${toBundle.name}'`, | ||
`'${shardingConfig.cookieName}'`, | ||
'document.cookie', | ||
shardingConfig.maxShards, | ||
].join(', '); | ||
return `require('./helpers/bundle-url-shards').getShardedBundleURL(${bundleUrlArgs}) + ${relativePathExpr}`; | ||
} | ||
return `require('./helpers/bundle-url').getBundleURL('${fromBundle.publicId}') + ${relativePathExpr}`; | ||
} | ||
@@ -804,0 +861,0 @@ |
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
104493
8.67%51
6.25%2353
11.04%