From f71a3d66422e403da0eaacb94b89ddf3d4b4d7a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Thu, 26 Dec 2019 14:33:50 +0100 Subject: [PATCH] Fix rollup build --- Gulpfile.js | 283 +++++++++++++------------- babel.config.js | 24 +-- scripts/rollup-plugin-babel-source.js | 15 +- 3 files changed, 159 insertions(+), 163 deletions(-) diff --git a/Gulpfile.js b/Gulpfile.js index a91d4e54b84d..700c8d158375 100644 --- a/Gulpfile.js +++ b/Gulpfile.js @@ -85,154 +85,157 @@ function buildRollup(packages) { const sourcemap = process.env.NODE_ENV === "production"; const minify = !!process.env.IS_PUBLISH; return Promise.all( - packages.map( - ({ src, format, dest, name, filename, version = babelVersion }) => { - const extraPlugins = []; - let inputExternal = undefined, - outputGlobals = undefined, - nodeResolveBrowser = false, - babelEnvName = "rollup"; - switch (src) { - case "packages/babel-standalone": - nodeResolveBrowser = true; - babelEnvName = "standalone"; - if (minify) { - extraPlugins.push( - rollupTerser({ - include: /^.+\.min\.js$/, - }) - ); - } - break; - case "packages/babel-preset-env-standalone": - nodeResolveBrowser = true; - babelEnvName = "standalone"; - if (minify) { - extraPlugins.push( - rollupTerser({ - include: /^.+\.min\.js$/, - }) - ); - } - inputExternal = ["@babel/standalone"]; - outputGlobals = { - "@babel/standalone": "Babel", - }; + packages.map(async ({ src, format, dest, name, filename, version }) => { + // TODO: Node 6 doesn't support default values in shorthand properties in + // object destructuring in async function parameters + if (version === undefined) version = babelVersion; + + const extraPlugins = []; + let inputExternal = undefined, + outputGlobals = undefined, + nodeResolveBrowser = false, + babelEnvName = "rollup"; + switch (src) { + case "packages/babel-standalone": + nodeResolveBrowser = true; + babelEnvName = "standalone"; + if (minify) { extraPlugins.push( - rollupAlias({ - entries: [ - { - find: "./available-plugins", - replacement: require.resolve( - path.join(__dirname, src, "./src/available-plugins") - ), - }, - { - find: "caniuse-lite/data/regions", - replacement: require.resolve( - path.join(__dirname, src, "./src/caniuse-lite-regions") - ), - }, - ], + rollupTerser({ + include: /^.+\.min\.js$/, }) ); - break; - } - // If this build is part of a pull request, include the pull request number in - // the version number. - if (process.env.CIRCLE_PR_NUMBER) { - const prVersion = "+pr." + process.env.CIRCLE_PR_NUMBER; - babelVersion += prVersion; - version += prVersion; - } - const input = getIndexFromPackage(src); - fancyLog(`Compiling '${chalk.cyan(input)}' with rollup ...`); - return rollup - .rollup({ - input, - external: inputExternal, - plugins: [ - ...extraPlugins, - rollupBabelSource(), - rollupReplace({ - "process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV), - BABEL_VERSION: JSON.stringify(babelVersion), - VERSION: JSON.stringify(version), - }), - rollupBabel({ - envName: babelEnvName, - babelrc: false, - extends: "./babel.config.js", - }), - rollupNodeResolve({ - browser: nodeResolveBrowser, - preferBuiltins: true, - //todo: When Yarn workspaces is enabled, remove `dedupe` option - dedupe(importee) { - return ( - importee.startsWith("lodash/") || - [ - "babel-plugin-dynamic-import-node/utils", - "esutils", - "semver", - "source-map", - ].includes(importee) - ); + } + break; + case "packages/babel-preset-env-standalone": + nodeResolveBrowser = true; + babelEnvName = "standalone"; + if (minify) { + extraPlugins.push( + rollupTerser({ + include: /^.+\.min\.js$/, + }) + ); + } + inputExternal = ["@babel/standalone"]; + outputGlobals = { + "@babel/standalone": "Babel", + }; + extraPlugins.push( + rollupAlias({ + entries: [ + { + find: "./available-plugins", + replacement: require.resolve( + path.join(__dirname, src, "./src/available-plugins") + ), }, - }), - rollupCommonJs({ - include: [/node_modules/, "packages/babel-preset-env/data/**"], - namedExports: { - "babel-plugin-dynamic-import-node/utils.js": [ - "createDynamicImportTransform", - "getImportSource", - ], - "@babel/standalone": ["availablePlugins", "registerPlugin"], + { + find: "caniuse-lite/data/regions", + replacement: require.resolve( + path.join(__dirname, src, "./src/caniuse-lite-regions") + ), }, - }), - rollupJson(), - rollupNodeBuiltins(), - rollupNodeGlobals({ sourceMap: sourcemap }), + ], + }) + ); + break; + } + // If this build is part of a pull request, include the pull request number in + // the version number. + if (process.env.CIRCLE_PR_NUMBER) { + const prVersion = "+pr." + process.env.CIRCLE_PR_NUMBER; + babelVersion += prVersion; + version += prVersion; + } + const input = getIndexFromPackage(src); + fancyLog(`Compiling '${chalk.cyan(input)}' with rollup ...`); + + const bundle = await rollup.rollup({ + input, + external: inputExternal, + plugins: [ + ...extraPlugins, + rollupBabelSource(), + rollupReplace({ + "process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV), + BABEL_VERSION: JSON.stringify(babelVersion), + VERSION: JSON.stringify(version), + }), + rollupBabel({ + envName: babelEnvName, + babelrc: false, + extends: "./babel.config.js", + }), + rollupNodeResolve({ + browser: nodeResolveBrowser, + preferBuiltins: true, + //todo: When Yarn workspaces is enabled, remove `dedupe` option + dedupe(importee) { + return ( + importee.startsWith("lodash/") || + [ + "babel-plugin-dynamic-import-node/utils", + "esutils", + "semver", + "source-map", + ].includes(importee) + ); + }, + }), + rollupCommonJs({ + include: [ + /node_modules/, + "packages/babel-preset-env/data/**", + "packages/babel-runtime/regenerator/**", ], - }) - .then(bundle => { - const outputFile = path.resolve(src, dest, filename || "index.js"); - return bundle - .write({ - file: outputFile, - format, - name, - globals: outputGlobals, - sourcemap: sourcemap, - }) - .then(() => { - if (!process.env.IS_PUBLISH) { - fancyLog( - chalk.yellow( - `Skipped minification of '${chalk.cyan( - path.relative(path.join(__dirname, ".."), outputFile) - )}' because not publishing` - ) - ); - return undefined; - } - fancyLog( - `Minifying '${chalk.cyan( - path.relative(path.join(__dirname, ".."), outputFile) - )}'...` - ); + namedExports: { + "babel-plugin-dynamic-import-node/utils.js": [ + "createDynamicImportTransform", + "getImportSource", + ], + "@babel/standalone": ["availablePlugins", "registerPlugin"], + }, + }), + rollupJson(), + rollupNodeBuiltins(), + rollupNodeGlobals({ sourceMap: sourcemap }), + ], + }); - return bundle.write({ - file: outputFile.replace(/\.js$/, ".min.js"), - format, - name, - globals: outputGlobals, - sourcemap: sourcemap, - }); - }); - }); + const outputFile = path.resolve(src, dest, filename || "index.js"); + await bundle.write({ + file: outputFile, + format, + name, + globals: outputGlobals, + sourcemap: sourcemap, + }); + + if (!process.env.IS_PUBLISH) { + fancyLog( + chalk.yellow( + `Skipped minification of '${chalk.cyan( + path.relative(path.join(__dirname, ".."), outputFile) + )}' because not publishing` + ) + ); + return undefined; } - ) + fancyLog( + `Minifying '${chalk.cyan( + path.relative(path.join(__dirname, ".."), outputFile) + )}'...` + ); + + await bundle.write({ + file: outputFile.replace(/\.js$/, ".min.js"), + format, + name, + globals: outputGlobals, + sourcemap: sourcemap, + }); + }) ); } diff --git a/babel.config.js b/babel.config.js index 0f1dbeaf4027..ecdb2f6a92bc 100644 --- a/babel.config.js +++ b/babel.config.js @@ -1,7 +1,5 @@ "use strict"; -const path = require("path"); - module.exports = function(api) { const env = api.env(); @@ -34,31 +32,20 @@ module.exports = function(api) { ]; switch (env) { - // Configs used during bundling builds. - case "rollup": - convertESM = false; - ignoreLib = false; - // rollup-commonjs will converts node_modules to ESM - unambiguousSources.push( - "**/node_modules", - // todo: remove this after it is rewritten into ESM - "packages/babel-preset-env/data" - ); - envOpts.targets = { - node: nodeVersion, - }; - break; case "standalone": + includeRegeneratorRuntime = true; + // Configs used during bundling builds. + unambiguousSources.push("packages/babel-runtime/regenerator"); + case "rollup": convertESM = false; ignoreLib = false; - includeRegeneratorRuntime = true; // rollup-commonjs will converts node_modules to ESM unambiguousSources.push( "**/node_modules", // todo: remove this after it is rewritten into ESM "packages/babel-preset-env/data" ); - // targets to browserslists: defaults + if (env === "rollup") envOpts.targets = { node: nodeVersion }; break; case "production": // Config during builds before publish. @@ -86,7 +73,6 @@ module.exports = function(api) { helpers: false, // Helpers are handled by rollup when needed regenerator: true, version: require(babelRuntimePkgPath).version, - absoluteRuntime: path.dirname(babelRuntimePkgPath), }; } diff --git a/scripts/rollup-plugin-babel-source.js b/scripts/rollup-plugin-babel-source.js index 79f1a6c4101b..54f71f34e050 100644 --- a/scripts/rollup-plugin-babel-source.js +++ b/scripts/rollup-plugin-babel-source.js @@ -37,13 +37,20 @@ module.exports = function() { return null; }, resolveId(importee) { - let packageFolderName; + if (importee === "@babel/runtime/regenerator") { + return path.join( + dirname, + "packages", + "babel-runtime", + "regenerator", + "index.js" + ); + } + const matches = importee.match(/^@babel\/([^/]+)$/); if (matches) { - packageFolderName = `babel-${matches[1]}`; - } + const packageFolderName = `babel-${matches[1]}`; - if (packageFolderName) { // resolve babel package names to their src index file const packageFolder = path.join(dirname, "packages", packageFolderName); const packageJson = require(path.join(packageFolder, "package.json"));