MCPcopy
hub / github.com/ampproject/amphtml / buildExtensionJs

Function buildExtensionJs

build-system/tasks/extension-helpers.js:549–607  ·  view source on GitHub ↗

* Build the JavaScript for the extension specified * * @param {string} dir Path to the extension's directory * @param {string} name Name of the extension. Must be the sub directory in * the extensions directory and the name of the JS and optional CSS file. * @param {!Object} options * @ret

(dir, name, options)

Source from the content-addressed store, hash-verified

547 * @return {!Promise}
548 */
549async function buildExtensionJs(dir, name, options) {
550 const isLatest = legacyLatestVersions[options.name] === options.version;
551 const {
552 filename = await findJsSourceFilename(name, dir),
553 version,
554 wrapper = 'extension',
555 } = options;
556
557 const wrapperOrFn = wrappers[wrapper];
558 if (!wrapperOrFn) {
559 throw new Error(
560 `Unknown options.wrapper "${wrapper}" (${name}:${version})\n` +
561 `Expected one of: ${Object.keys(wrappers).join(', ')}`
562 );
563 }
564 const resolvedWrapper =
565 typeof wrapperOrFn === 'function'
566 ? wrapperOrFn(name, version, argv.esm, options.loadPriority)
567 : wrapperOrFn;
568
569 const additionalSuffix = options.additionalSuffix
570 ? `.${options.additionalSuffix}`
571 : '';
572 await compileJs(`${dir}/`, filename, './dist/v0', {
573 ...options,
574 toName: `${name}-${version}.max${additionalSuffix}.js`,
575 minifiedName: `${name}-${version}${additionalSuffix}.js`,
576 aliasName: isLatest ? `${name}-latest${additionalSuffix}.js` : '',
577 wrapper: resolvedWrapper,
578 babelPlugins: wrapper === 'extension' ? extensionBabelPlugins : null,
579 });
580
581 // If an incremental watch build fails, simply return.
582 if (options.errored) {
583 return;
584 }
585
586 const aliasBundle = extensionAliasBundles[name];
587 const isAliased = aliasBundle && aliasBundle.version == version;
588
589 if (isAliased) {
590 const {aliasedVersion} = aliasBundle;
591 const src = maybeToEsmName(
592 `${name}-${version}${options.minify ? '' : '.max'}${additionalSuffix}.js`
593 );
594 const dest = maybeToEsmName(
595 `${name}-${aliasedVersion}${
596 options.minify ? '' : '.max'
597 }${additionalSuffix}.js`
598 );
599 fs.copySync(`dist/v0/${src}`, `dist/v0/${dest}`);
600 fs.copySync(`dist/v0/${src}.map`, `dist/v0/${dest}.map`);
601 }
602
603 if (name === 'amp-script') {
604 await copyWorkerDomResources(version);
605 await buildSandboxedProxyIframe(options.minify);
606 }

Callers 1

buildExtensionFunction · 0.85

Calls 5

findJsSourceFilenameFunction · 0.85
compileJsFunction · 0.85
maybeToEsmNameFunction · 0.85
copyWorkerDomResourcesFunction · 0.85

Tested by

no test coverage detected