Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@parcel/runtime-js

Package Overview
Dependencies
Maintainers
1
Versions
889
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@parcel/runtime-js - npm Package Compare versions

Comparing version 2.0.0-nightly.201 to 2.0.0-nightly.203

207

lib/JSRuntime.js

@@ -8,4 +8,2 @@ "use strict";

var _assert = _interopRequireDefault(require("assert"));
var _plugin = require("@parcel/plugin");

@@ -62,3 +60,4 @@

bundle,
bundleGraph
bundleGraph,
options
}) {

@@ -75,29 +74,65 @@ // Dependency ids in code replaced with referenced bundle names

let loaders = getLoaders(bundle.env); // Determine if we need to add a dynamic import() polyfill, or if all target browsers support it natively.
let asyncDependencies = [];
let otherDependencies = [];
bundle.traverse(node => {
if (node.type !== 'dependency') {
return;
}
let needsDynamicImportPolyfill = false;
let dependency = node.value;
if (bundle.env.isBrowser() && bundle.env.outputFormat === 'esmodule') {
needsDynamicImportPolyfill = !bundle.env.matchesEngines(DYNAMIC_IMPORT_BROWSERS);
}
if (dependency.isAsync && !dependency.isURL) {
asyncDependencies.push(dependency);
} else {
otherDependencies.push(dependency);
}
});
let assets = [];
for (let dependency of bundleGraph.getExternalDependencies(bundle)) {
let bundleGroup = bundleGraph.resolveExternalDependency(dependency);
for (let dependency of asyncDependencies) {
let resolved = bundleGraph.resolveExternalDependency(dependency, bundle);
if (bundleGroup == null) {
if (dependency.isURL) {
// If a URL dependency was not able to be resolved, add a runtime that
// exports the original moduleSpecifier.
assets.push({
filePath: __filename,
code: `module.exports = ${JSON.stringify(dependency.moduleSpecifier)}`,
dependency
});
}
if (resolved == null) {
continue;
}
if (resolved.type === 'asset') {
// If this bundle already has the asset this dependency references,
// return a simple runtime of `Promise.resolve(require("path/to/asset"))`.
assets.push({
filePath: _path.default.join(options.projectRoot, 'JSRuntime.js'),
// Using Promise['resolve'] to prevent Parcel from inferring this is an async dependency.
// TODO: Find a better way of doing this.
code: `module.exports = Promise['resolve'](require(${JSON.stringify('./' + _path.default.relative(options.projectRoot, resolved.value.filePath))}))`,
dependency
});
} else {
assets.push(...getLoaderRuntimes({
bundle,
dependency,
bundleGraph,
bundleGroup: resolved.value
}));
}
}
for (let dependency of otherDependencies) {
let resolved = bundleGraph.resolveExternalDependency(dependency, bundle);
if (dependency.isURL && resolved == null) {
// If a URL dependency was not able to be resolved, add a runtime that
// exports the original moduleSpecifier.
assets.push({
filePath: __filename,
code: `module.exports = ${JSON.stringify(dependency.moduleSpecifier)}`,
dependency
});
continue;
}
if (resolved == null || resolved.type !== 'bundle_group') {
continue;
}
let bundleGroup = resolved.value;
let bundlesInGroup = bundleGraph.getBundlesInBundleGroup(bundleGroup);

@@ -113,85 +148,97 @@ let [firstBundle] = bundlesInGroup;

continue;
} // URL dependencies should always resolve to a runtime that exports a url
} // URL dependency or not, fall back to including a runtime that exports the url
if (dependency.isURL) {
assets.push(getURLRuntime(dependency, bundle, firstBundle));
continue;
} // Sort so the bundles containing the entry asset appear last
assets.push(getURLRuntime(dependency, bundle, firstBundle));
}
if (shouldUseRuntimeManifest(bundle) && bundleGraph.getChildBundles(bundle).length > 0 && isNewContext(bundle, bundleGraph)) {
assets.push({
filePath: __filename,
code: getRegisterCode(bundle, bundleGraph),
isEntry: true
});
}
let externalBundles = bundlesInGroup.filter(bundle => !bundle.isInline).sort(bundle => bundle.getEntryAssets().map(asset => asset.id).includes(bundleGroup.entryAssetId) ? 1 : -1); // CommonJS is a synchronous module system, so there is no need to load bundles in parallel.
// Importing of the other bundles will be handled by the bundle group entry.
// Do the same thing in library mode for ES modules, as we are building for another bundler
// and the imports for sibling bundles will be in the target bundle.
return assets;
}
if (bundle.env.outputFormat === 'commonjs' || bundle.env.isLibrary) {
externalBundles = externalBundles.slice(-1);
}
});
let loaderModules = loaders ? externalBundles.map(to => {
let loader = loaders[to.type];
exports.default = _default;
if (!loader) {
return;
}
function getLoaderRuntimes({
bundle,
dependency,
bundleGroup,
bundleGraph
}) {
let assets = []; // Sort so the bundles containing the entry asset appear last
let relativePathExpr = getRelativePathExpr(bundle, to); // Use esmodule loader if possible
let externalBundles = bundleGraph.getBundlesInBundleGroup(bundleGroup).filter(bundle => !bundle.isInline).sort(bundle => bundle.getEntryAssets().map(asset => asset.id).includes(bundleGroup.entryAssetId) ? 1 : -1); // CommonJS is a synchronous module system, so there is no need to load bundles in parallel.
// Importing of the other bundles will be handled by the bundle group entry.
// Do the same thing in library mode for ES modules, as we are building for another bundler
// and the imports for sibling bundles will be in the target bundle.
if (to.type === 'js' && to.env.outputFormat === 'esmodule') {
if (!needsDynamicImportPolyfill) {
return `import("./" + ${relativePathExpr})`;
}
if (bundle.env.outputFormat === 'commonjs' || bundle.env.isLibrary) {
externalBundles = externalBundles.slice(-1);
}
loader = (0, _nullthrows.default)(loaders.IMPORT_POLYFILL, `No import() polyfill available for context '${bundle.env.context}'`);
} else if (to.type === 'js' && to.env.outputFormat === 'commonjs') {
return `Promise.resolve(require("./" + ${relativePathExpr}))`;
}
let loaders = getLoaders(bundle.env); // Determine if we need to add a dynamic import() polyfill, or if all target browsers support it natively.
return `require(${JSON.stringify(loader)})(require('./bundle-url').getBundleURL() + ${relativePathExpr})`;
}).filter(Boolean) : [];
let needsDynamicImportPolyfill = false;
if (loaderModules.length > 0) {
let loaders = loaderModules.join(', ');
if (bundle.env.isBrowser() && bundle.env.outputFormat === 'esmodule') {
needsDynamicImportPolyfill = !bundle.env.matchesEngines(DYNAMIC_IMPORT_BROWSERS);
}
if (loaderModules.length > 1 && (bundle.env.outputFormat === 'global' || !externalBundles.every(b => b.type === 'js'))) {
loaders = `Promise.all([${loaders}])`;
let loaderModules = loaders ? externalBundles.map(to => {
let loader = loaders[to.type];
if (bundle.env.outputFormat !== 'global') {
loaders += `.then(r => r[r.length - 1])`;
}
} else {
loaders = `(${loaders})`;
}
if (!loader) {
return;
}
if (bundle.env.outputFormat === 'global') {
loaders += `.then(() => parcelRequire('${bundleGroup.entryAssetId}'))`;
}
let relativePathExpr = getRelativePathExpr(bundle, to); // Use esmodule loader if possible
assets.push({
filePath: __filename,
code: `module.exports = ${loaders};`,
dependency
});
} else {
(0, _assert.default)(externalBundles.length === 1);
assets.push(getURLRuntime(dependency, bundle, externalBundles[0]));
if (to.type === 'js' && to.env.outputFormat === 'esmodule') {
if (!needsDynamicImportPolyfill) {
return `import("./" + ${relativePathExpr})`;
}
loader = (0, _nullthrows.default)(loaders.IMPORT_POLYFILL, `No import() polyfill available for context '${bundle.env.context}'`);
} else if (to.type === 'js' && to.env.outputFormat === 'commonjs') {
return `Promise.resolve(require("./" + ${relativePathExpr}))`;
}
if (shouldUseRuntimeManifest(bundle) && bundleGraph.getChildBundles(bundle).length > 0 && isNewContext(bundle, bundleGraph)) {
assets.push({
filePath: __filename,
code: getRegisterCode(bundle, bundleGraph),
isEntry: true
});
return `require(${JSON.stringify(loader)})(require('./bundle-url').getBundleURL() + ${relativePathExpr})`;
}).filter(Boolean) : [];
if (loaderModules.length > 0) {
let loaders = loaderModules.join(', ');
if (loaderModules.length > 1 && (bundle.env.outputFormat === 'global' || !externalBundles.every(b => b.type === 'js'))) {
loaders = `Promise.all([${loaders}])`;
if (bundle.env.outputFormat !== 'global') {
loaders += `.then(r => r[r.length - 1])`;
}
} else {
loaders = `(${loaders})`;
}
return assets;
if (bundle.env.outputFormat === 'global') {
loaders += `.then(() => parcelRequire('${bundleGroup.entryAssetId}'))`;
}
assets.push({
filePath: __filename,
code: `module.exports = ${loaders};`,
dependency
});
}
});
return assets;
}
exports.default = _default;
function isNewContext(bundle, bundleGraph) {

@@ -198,0 +245,0 @@ return bundle.isEntry || bundleGraph.getParentBundles(bundle).some(parent => parent.env.context !== bundle.env.context || parent.type !== 'js');

{
"name": "@parcel/runtime-js",
"version": "2.0.0-nightly.201+83e272ba",
"version": "2.0.0-nightly.203+f6d2a707",
"license": "MIT",

@@ -19,7 +19,7 @@ "publishConfig": {

"dependencies": {
"@parcel/plugin": "2.0.0-nightly.201+83e272ba",
"@parcel/utils": "2.0.0-nightly.201+83e272ba",
"@parcel/plugin": "2.0.0-nightly.203+f6d2a707",
"@parcel/utils": "2.0.0-nightly.203+f6d2a707",
"nullthrows": "^1.1.1"
},
"gitHead": "83e272ba8b6fc51651f564da9edbb69576695e2f"
"gitHead": "f6d2a707c932032c39064cf53dd74b2b687e310e"
}

@@ -6,2 +6,3 @@ // @flow strict-local

BundleGraph,
BundleGroup,
Dependency,

@@ -12,3 +13,2 @@ Environment,

import assert from 'assert';
import {Runtime} from '@parcel/plugin';

@@ -60,3 +60,3 @@ import {relativeBundlePath} from '@parcel/utils';

export default new Runtime({
apply({bundle, bundleGraph}) {
apply({bundle, bundleGraph, options}) {
// Dependency ids in code replaced with referenced bundle names

@@ -73,131 +73,82 @@ // Loader runtime added for bundle groups that don't have a native loader (e.g. HTML/CSS/Worker - isURL?),

let loaders = getLoaders(bundle.env);
let asyncDependencies = [];
let otherDependencies = [];
bundle.traverse(node => {
if (node.type !== 'dependency') {
return;
}
// Determine if we need to add a dynamic import() polyfill, or if all target browsers support it natively.
let needsDynamicImportPolyfill = false;
if (bundle.env.isBrowser() && bundle.env.outputFormat === 'esmodule') {
needsDynamicImportPolyfill = !bundle.env.matchesEngines(
DYNAMIC_IMPORT_BROWSERS,
);
}
let dependency = node.value;
if (dependency.isAsync && !dependency.isURL) {
asyncDependencies.push(dependency);
} else {
otherDependencies.push(dependency);
}
});
let assets = [];
for (let dependency of bundleGraph.getExternalDependencies(bundle)) {
let bundleGroup = bundleGraph.resolveExternalDependency(dependency);
if (bundleGroup == null) {
if (dependency.isURL) {
// If a URL dependency was not able to be resolved, add a runtime that
// exports the original moduleSpecifier.
assets.push({
filePath: __filename,
code: `module.exports = ${JSON.stringify(
dependency.moduleSpecifier,
)}`,
dependency,
});
}
for (let dependency of asyncDependencies) {
let resolved = bundleGraph.resolveExternalDependency(dependency, bundle);
if (resolved == null) {
continue;
}
let bundlesInGroup = bundleGraph.getBundlesInBundleGroup(bundleGroup);
if (resolved.type === 'asset') {
// If this bundle already has the asset this dependency references,
// return a simple runtime of `Promise.resolve(require("path/to/asset"))`.
assets.push({
filePath: path.join(options.projectRoot, 'JSRuntime.js'),
// Using Promise['resolve'] to prevent Parcel from inferring this is an async dependency.
// TODO: Find a better way of doing this.
code: `module.exports = Promise['resolve'](require(${JSON.stringify(
'./' + path.relative(options.projectRoot, resolved.value.filePath),
)}))`,
dependency,
});
} else {
assets.push(
...getLoaderRuntimes({
bundle,
dependency,
bundleGraph,
bundleGroup: resolved.value,
}),
);
}
}
let [firstBundle] = bundlesInGroup;
if (firstBundle.isInline) {
for (let dependency of otherDependencies) {
let resolved = bundleGraph.resolveExternalDependency(dependency, bundle);
if (dependency.isURL && resolved == null) {
// If a URL dependency was not able to be resolved, add a runtime that
// exports the original moduleSpecifier.
assets.push({
filePath: path.join(__dirname, `/bundles/${firstBundle.id}.js`),
code: `module.exports = ${JSON.stringify(dependency.id)};`,
filePath: __filename,
code: `module.exports = ${JSON.stringify(
dependency.moduleSpecifier,
)}`,
dependency,
});
continue;
}
// URL dependencies should always resolve to a runtime that exports a url
if (dependency.isURL) {
assets.push(getURLRuntime(dependency, bundle, firstBundle));
if (resolved == null || resolved.type !== 'bundle_group') {
continue;
}
// Sort so the bundles containing the entry asset appear last
let externalBundles = bundlesInGroup
.filter(bundle => !bundle.isInline)
.sort(bundle =>
bundle
.getEntryAssets()
.map(asset => asset.id)
.includes(bundleGroup.entryAssetId)
? 1
: -1,
);
let bundleGroup = resolved.value;
let bundlesInGroup = bundleGraph.getBundlesInBundleGroup(bundleGroup);
// CommonJS is a synchronous module system, so there is no need to load bundles in parallel.
// Importing of the other bundles will be handled by the bundle group entry.
// Do the same thing in library mode for ES modules, as we are building for another bundler
// and the imports for sibling bundles will be in the target bundle.
if (bundle.env.outputFormat === 'commonjs' || bundle.env.isLibrary) {
externalBundles = externalBundles.slice(-1);
}
let loaderModules = loaders
? externalBundles
.map(to => {
let loader = loaders[to.type];
if (!loader) {
return;
}
let relativePathExpr = getRelativePathExpr(bundle, to);
// Use esmodule loader if possible
if (to.type === 'js' && to.env.outputFormat === 'esmodule') {
if (!needsDynamicImportPolyfill) {
return `import("./" + ${relativePathExpr})`;
}
loader = nullthrows(
loaders.IMPORT_POLYFILL,
`No import() polyfill available for context '${bundle.env.context}'`,
);
} else if (
to.type === 'js' &&
to.env.outputFormat === 'commonjs'
) {
return `Promise.resolve(require("./" + ${relativePathExpr}))`;
}
return `require(${JSON.stringify(
loader,
)})(require('./bundle-url').getBundleURL() + ${relativePathExpr})`;
})
.filter(Boolean)
: [];
if (loaderModules.length > 0) {
let loaders = loaderModules.join(', ');
if (
loaderModules.length > 1 &&
(bundle.env.outputFormat === 'global' ||
!externalBundles.every(b => b.type === 'js'))
) {
loaders = `Promise.all([${loaders}])`;
if (bundle.env.outputFormat !== 'global') {
loaders += `.then(r => r[r.length - 1])`;
}
} else {
loaders = `(${loaders})`;
}
if (bundle.env.outputFormat === 'global') {
loaders += `.then(() => parcelRequire('${bundleGroup.entryAssetId}'))`;
}
let [firstBundle] = bundlesInGroup;
if (firstBundle.isInline) {
assets.push({
filePath: __filename,
code: `module.exports = ${loaders};`,
filePath: path.join(__dirname, `/bundles/${firstBundle.id}.js`),
code: `module.exports = ${JSON.stringify(dependency.id)};`,
dependency,
});
} else {
assert(externalBundles.length === 1);
assets.push(getURLRuntime(dependency, bundle, externalBundles[0]));
continue;
}
// URL dependency or not, fall back to including a runtime that exports the url
assets.push(getURLRuntime(dependency, bundle, firstBundle));
}

@@ -221,2 +172,105 @@

function getLoaderRuntimes({
bundle,
dependency,
bundleGroup,
bundleGraph,
}: {|
bundle: Bundle,
dependency: Dependency,
bundleGroup: BundleGroup,
bundleGraph: BundleGraph,
|}) {
let assets = [];
// Sort so the bundles containing the entry asset appear last
let externalBundles = bundleGraph
.getBundlesInBundleGroup(bundleGroup)
.filter(bundle => !bundle.isInline)
.sort(bundle =>
bundle
.getEntryAssets()
.map(asset => asset.id)
.includes(bundleGroup.entryAssetId)
? 1
: -1,
);
// CommonJS is a synchronous module system, so there is no need to load bundles in parallel.
// Importing of the other bundles will be handled by the bundle group entry.
// Do the same thing in library mode for ES modules, as we are building for another bundler
// and the imports for sibling bundles will be in the target bundle.
if (bundle.env.outputFormat === 'commonjs' || bundle.env.isLibrary) {
externalBundles = externalBundles.slice(-1);
}
let loaders = getLoaders(bundle.env);
// Determine if we need to add a dynamic import() polyfill, or if all target browsers support it natively.
let needsDynamicImportPolyfill = false;
if (bundle.env.isBrowser() && bundle.env.outputFormat === 'esmodule') {
needsDynamicImportPolyfill = !bundle.env.matchesEngines(
DYNAMIC_IMPORT_BROWSERS,
);
}
let loaderModules = loaders
? externalBundles
.map(to => {
let loader = loaders[to.type];
if (!loader) {
return;
}
let relativePathExpr = getRelativePathExpr(bundle, to);
// Use esmodule loader if possible
if (to.type === 'js' && to.env.outputFormat === 'esmodule') {
if (!needsDynamicImportPolyfill) {
return `import("./" + ${relativePathExpr})`;
}
loader = nullthrows(
loaders.IMPORT_POLYFILL,
`No import() polyfill available for context '${bundle.env.context}'`,
);
} else if (to.type === 'js' && to.env.outputFormat === 'commonjs') {
return `Promise.resolve(require("./" + ${relativePathExpr}))`;
}
return `require(${JSON.stringify(
loader,
)})(require('./bundle-url').getBundleURL() + ${relativePathExpr})`;
})
.filter(Boolean)
: [];
if (loaderModules.length > 0) {
let loaders = loaderModules.join(', ');
if (
loaderModules.length > 1 &&
(bundle.env.outputFormat === 'global' ||
!externalBundles.every(b => b.type === 'js'))
) {
loaders = `Promise.all([${loaders}])`;
if (bundle.env.outputFormat !== 'global') {
loaders += `.then(r => r[r.length - 1])`;
}
} else {
loaders = `(${loaders})`;
}
if (bundle.env.outputFormat === 'global') {
loaders += `.then(() => parcelRequire('${bundleGroup.entryAssetId}'))`;
}
assets.push({
filePath: __filename,
code: `module.exports = ${loaders};`,
dependency,
});
}
return assets;
}
function isNewContext(bundle: Bundle, bundleGraph: BundleGraph): boolean {

@@ -240,3 +294,2 @@ return (

let relativePathExpr = getRelativePathExpr(from, to);
if (dependency.meta.webworker === true) {

@@ -243,0 +296,0 @@ return {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc