diff --git a/build-system/common/esbuild-babel.js b/build-system/common/esbuild-babel.js index fa660e5882b4..c0be441d57da 100644 --- a/build-system/common/esbuild-babel.js +++ b/build-system/common/esbuild-babel.js @@ -83,17 +83,51 @@ function getEsbuildBabelPlugin( build.onLoad({filter: /\.[cm]?js$/, namespace: ''}, async (file) => { const filename = file.path; const {contents, hash} = await batchedRead(filename, optionsHash); - const transformed = await transformContents(filename, contents, hash, { - ...babelOptions, + + const transformed = await transformContents( filename, - filenameRelative: path.basename(filename), - }); + contents, + hash, + getFileBabelOptions(babelOptions, filename) + ); return {contents: transformed}; }); }, }; } +const CJS_TRANSFORMS = new Set([ + 'transform-modules-commonjs', + 'proposal-dynamic-import', + 'syntax-dynamic-import', + 'proposal-export-namespace-from', + 'syntax-export-namespace-from', +]); + +/** + * @param {!Object} babelOptions + * @param {string} filename + * @return {!Object} + */ +function getFileBabelOptions(babelOptions, filename) { + // Patch for leaving files within node_modules as esm, since esbuild will break when trying + // to process a module file that contains CJS exports. This function is called after + // babel.loadOptions, therefore all of the plugins from preset-env have already been applied. + // and must be disabled individually. + if (filename.includes('node_modules')) { + const plugins = babelOptions.plugins.filter( + ({key}) => !CJS_TRANSFORMS.has(key) + ); + babelOptions = {...babelOptions, plugins}; + } + + return { + ...babelOptions, + filename, + filenameRelative: path.basename(filename), + }; +} + module.exports = { getEsbuildBabelPlugin, }; diff --git a/build-system/tasks/runtime-test/runtime-test-base.js b/build-system/tasks/runtime-test/runtime-test-base.js index 8b32f8cd4b81..e5ab75cc95ea 100644 --- a/build-system/tasks/runtime-test/runtime-test-base.js +++ b/build-system/tasks/runtime-test/runtime-test-base.js @@ -314,6 +314,10 @@ class RuntimeTestConfig { }, plugins: [importPathPlugin, babelPlugin], sourcemap: 'inline', + // TODO(rsimha): Remove this workaround once evanw/esbuild#1202 is fixed. + banner: { + js: "function require(x) { throw new Error('Cannot require ' + x) }", + }, }; } diff --git a/package-lock.json b/package-lock.json index 5ede8f2a3348..5dd3255ac570 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6639,9 +6639,9 @@ } }, "esbuild": { - "version": "0.9.7", - "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.9.7.tgz", - "integrity": "sha512-VtUf6aQ89VTmMLKrWHYG50uByMF4JQlVysb8dmg6cOgW8JnFCipmz7p+HNBl+RR3LLCuBxFGVauAe2wfnF9bLg==", + "version": "0.11.19", + "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.11.19.tgz", + "integrity": "sha512-X2h8UThAwKLxmc1OChHVegIScphS/qU9cUB5vCEV2T0A024E8Ptpg9xssXXcs+j1uEgXrDJZuVRzx2JsmGzq7A==", "dev": true }, "escalade": { diff --git a/package.json b/package.json index f76c74380841..2884449fb630 100644 --- a/package.json +++ b/package.json @@ -85,7 +85,7 @@ "del": "6.0.0", "enzyme": "3.11.0", "enzyme-adapter-preact-pure": "3.1.0", - "esbuild": "0.9.7", + "esbuild": "0.11.19", "escodegen": "2.0.0", "eslint": "7.26.0", "eslint-config-prettier": "8.3.0",