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

Function esbuildCompile

build-system/tasks/helpers.js:259–442  ·  view source on GitHub ↗

* Transforms a given JavaScript file entry point with esbuild and babel, and * watches it for changes (if required). * * @param {string} srcDir * @param {string} srcFilename * @param {string} destDir * @param {!Object} options * @return {!Promise}

(srcDir, srcFilename, destDir, options)

Source from the content-addressed store, hash-verified

257 * @return {!Promise}
258 */
259async function esbuildCompile(srcDir, srcFilename, destDir, options) {
260 const startTime = Date.now();
261 const entryPoint = path.join(srcDir, srcFilename);
262 const filename = options.minify
263 ? options.minifiedName
264 : (options.toName ?? srcFilename);
265 // This guards against someone passing `minify: true` but no `minifiedName`.
266 if (!filename) {
267 throw new Error('No minifiedName provided for ' + srcFilename);
268 }
269 const destFilename = maybeToEsmName(filename);
270 const destFile = path.join(destDir, destFilename);
271
272 if (watchedTargets.has(entryPoint)) {
273 return watchedTargets.get(entryPoint).rebuild();
274 }
275
276 /**
277 * Splits up the wrapper to compute the banner and footer
278 * @return {object}
279 */
280 function splitWrapper() {
281 const wrapper = options.wrapper ?? wrappers.none;
282 const sentinel = '<%= contents %>';
283 const start = wrapper.indexOf(sentinel);
284 return {
285 banner: {js: wrapper.slice(0, start)},
286 footer: {js: wrapper.slice(start + sentinel.length)},
287 };
288 }
289 const {banner, footer} = splitWrapper();
290 const config = await getAmpConfigForFile(destFilename, options);
291 const compiledFile = await getCompiledFile(srcFilename);
292 banner.js = config + banner.js + compiledFile;
293
294 let babelCaller =
295 options.babelCaller ?? (options.minify ? 'minified' : 'unminified');
296
297 // We read from the current binary configuration options if it is an
298 // no css binary output. (removes CSS installation)
299 if (options.ssrCss) {
300 babelCaller += '-ssr-css';
301 }
302
303 const babelPlugin = getEsbuildBabelPlugin(
304 babelCaller,
305 /* enableCache */ true,
306 {plugins: options.babelPlugins}
307 );
308 const plugins = [babelPlugin];
309
310 if (options.remapDependencies) {
311 const {externalDependencies: externals, remapDependencies: remaps} =
312 options;
313
314 plugins.unshift(
315 remapDependenciesPlugin({externals, remaps, resolve: ampResolve})
316 );

Callers 4

buildBinariesFunction · 0.85
doCompileJsFunction · 0.85
buildVendorConfigsFunction · 0.85
doBuild3pVendorFunction · 0.85

Calls 12

maybeToEsmNameFunction · 0.85
splitWrapperFunction · 0.85
getAmpConfigForFileFunction · 0.85
getCompiledFileFunction · 0.85
getEsbuildBabelPluginFunction · 0.85
remapDependenciesPluginFunction · 0.85
handleBundleErrorFunction · 0.85
nowMethod · 0.80
buildFunction · 0.70
hasMethod · 0.45
getMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected