Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function buildExport({exportName, namespace, propName, localName}) {
const exportDefault = exportName === 'default';
const head = exportDefault ? 'module.exports' : `exports.${exportName}`;
let opt = {};
// FIXME
// Does `PRIORITY`, `LOCATION_PARAMS` recognised as babel-template placeholder?
// We have to do this for workaround temporarily.
if (/^[A-Z0-9_]+$/.test(localName)) {
opt[localName] = localName;
}
return babelTemplate.statement(
localName
? `${head} = ${localName};`
: `${head} = ${namespace}.${propName};`
)(opt);
}
types.identifier(localImportName),
loadExpr
)
]);
reqHeader.loc = metadata.loc;
headers.push(reqHeader);
if (!localImportDefaultName) {
// src:
// import {someInZrUtil1 as someInZrUtil1Alias, zz} from 'zrender/core/util';
// metadata.imports:
// Map { 'someInZrUtil1Alias' => 'someInZrUtil1', 'zz' => 'zz' }
for (const importEntry of metadata.imports) {
headers.push(
babelTemplate.statement(`var IMPORTNAME = NAMESPACE.PROPNAME;`)({
NAMESPACE: localImportName,
IMPORTNAME: importEntry[0],
PROPNAME: importEntry[1]
})
);
}
}
}
return headers;
}
function buildNamespaceReexport(meta, namespace, checkExport) {
checkExport();
return babelTemplate.statement(`
(function() {
for (var key in NAMESPACE) {
if (NAMESPACE == null || !NAMESPACE.hasOwnProperty(key) || key === 'default' || key === '__esModule') return;
VERIFY_NAME_LIST;
exports[key] = NAMESPACE[key];
}
})();
`)({
NAMESPACE: namespace,
VERIFY_NAME_LIST: meta.exportNameListName
? babelTemplate.statement(`
if (Object.prototype.hasOwnProperty.call(EXPORTS_LIST, key)) return;
`)({EXPORTS_LIST: meta.exportNameListName})
: null
});
}
function buildNamespaceReexport(meta, namespace, checkExport) {
checkExport();
return babelTemplate.statement(`
(function() {
for (var key in NAMESPACE) {
if (NAMESPACE == null || !NAMESPACE.hasOwnProperty(key) || key === 'default' || key === '__esModule') return;
VERIFY_NAME_LIST;
exports[key] = NAMESPACE[key];
}
})();
`)({
NAMESPACE: namespace,
VERIFY_NAME_LIST: meta.exportNameListName
? babelTemplate.statement(`
if (Object.prototype.hasOwnProperty.call(EXPORTS_LIST, key)) return;
`)({EXPORTS_LIST: meta.exportNameListName})
: null
});
}
export function statement(tpl: string, vars: { [x: string]: OneOrMore }): t.Statement {
return template.statement(tpl, {
placeholderPattern: PLACEHOLDER_PATTERN,
})(vars);
}