Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
.map(b => {
let loader = loaders[b.type];
if (!loader) {
return;
}
// Use esmodule loader if possible
if (b.type === 'js' && b.env.outputFormat === 'esmodule') {
if (!needsDynamicImportPolyfill) {
return `import('' + '${relativeBundlePath(bundle, b)}')`;
}
loader = IMPORT_POLYFILL;
} else if (b.type === 'js' && b.env.outputFormat === 'commonjs') {
return `Promise.resolve(require('' + '${relativeBundlePath(
bundle,
b
)}'))`;
}
let url = urlJoin(nullthrows(b.target.publicUrl), nullthrows(b.name));
return `require(${JSON.stringify(loader)})('${url}')`;
})
.filter(Boolean);
let entryAsset = entries[entries.length - 1];
// $FlowFixMe
interpreter = bundle.target.env.isBrowser()
? null
: entryAsset.meta.interpreter;
} else if (bundle.env.outputFormat === 'global') {
// The last entry is the main entry, but in async bundles we don't want it to execute until we require it
// as there might be dependencies in a sibling bundle that hasn't loaded yet.
entries.pop();
}
let importScripts = '';
if (bundle.env.isWorker()) {
let bundles = bundleGraph.getSiblingBundles(bundle);
for (let b of bundles) {
importScripts += `importScripts("${relativeBundlePath(bundle, b)}");\n`;
}
}
let sourceMapReference = await getSourceMapReference(map);
return replaceReferences({
contents:
// If the entry asset included a hashbang, repeat it at the top of the bundle
(interpreter != null ? `#!${interpreter}\n` : '') +
importScripts +
(PRELUDE +
'({' +
assets +
'},{},' +
JSON.stringify(entries.map(asset => asset.id)) +
', ' +
export function generateBundleImports(
from: Bundle,
bundle: Bundle,
assets: Set,
scope: any,
) {
let specifiers = [...assets].map(asset => {
let id = t.identifier(asset.meta.exportsIdentifier);
return t.objectProperty(id, id, false, true);
});
let statement = REQUIRE_TEMPLATE({
BUNDLE: t.stringLiteral(relativeBundlePath(from, bundle)),
});
if (specifiers.length > 0) {
return generateDestructuringAssignment(
bundle.env,
specifiers,
statement.expression,
scope,
);
}
return [statement];
}
export function generateBundleImports(
from: Bundle,
bundle: Bundle,
assets: Set,
) {
let specifiers = [...assets].map(asset => {
let id = t.identifier(asset.meta.exportsIdentifier);
return t.importSpecifier(id, id);
});
return [
t.importDeclaration(
specifiers,
t.stringLiteral(relativeBundlePath(from, bundle)),
),
];
}
export function generateBundleImports(
from: Bundle,
bundle: Bundle,
assets: Set,
) {
let statements = [];
if (from.env.isWorker()) {
statements.push(
IMPORTSCRIPTS_TEMPLATE({
BUNDLE: t.stringLiteral(relativeBundlePath(from, bundle)),
}),
);
}
for (let asset of assets) {
statements.push(
IMPORT_TEMPLATE({
IDENTIFIER: t.identifier(asset.meta.exportsIdentifier),
ASSET_ID: t.stringLiteral(asset.id),
}),
);
}
return statements;
}