From dc4956ae5d8341b358d2c881b5f61b4578810358 Mon Sep 17 00:00:00 2001 From: Katie Byers Date: Tue, 19 Apr 2022 01:06:49 -0700 Subject: [PATCH 1/5] add `rollup-plugin-re` --- package.json | 1 + yarn.lock | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/package.json b/package.json index e3cccf304ea0..f2b81a98123e 100644 --- a/package.json +++ b/package.json @@ -85,6 +85,7 @@ "rimraf": "^3.0.2", "rollup": "^2.67.1", "rollup-plugin-license": "^2.6.1", + "rollup-plugin-re": "^1.0.7", "rollup-plugin-terser": "^7.0.2", "rollup-plugin-typescript2": "^0.31.2", "sinon": "^7.3.2", diff --git a/yarn.lock b/yarn.lock index f6ba304563d7..bf506395377a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -12051,6 +12051,11 @@ estraverse@^5.1.0, estraverse@^5.2.0: resolved "https://registry.yarnpkg.com/estraverse/-/estraverse-5.2.0.tgz#307df42547e6cc7324d3cf03c155d5cdb8c53880" integrity sha512-BxbNGGNm0RyRYvUdHpIwv9IWzeM9XClbOxwoATuFdOE7ZE6wHL+HQ5T8hoPM+zHvmKzzsEqhgy0GrQ5X13afiQ== +estree-walker@^0.6.1: + version "0.6.1" + resolved "https://registry.yarnpkg.com/estree-walker/-/estree-walker-0.6.1.tgz#53049143f40c6eb918b23671d1fe3219f3a1b362" + integrity sha512-SqmZANLWS0mnatqbSfRP5g8OXZC12Fgg1IwNtLsyHDzJizORW4khDfjPqJZsemPWBB2uqykUah5YpQ6epsqC/w== + estree-walker@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/estree-walker/-/estree-walker-1.0.1.tgz#31bc5d612c96b704106b477e6dd5d8aa138cb700" @@ -17307,6 +17312,13 @@ magic-string@0.25.7, magic-string@^0.25.1, magic-string@^0.25.7: dependencies: sourcemap-codec "^1.4.4" +magic-string@^0.16.0: + version "0.16.0" + resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.16.0.tgz#970ebb0da7193301285fb1aa650f39bdd81eb45a" + integrity sha1-lw67DacZMwEoX7GqZQ85vdgetFo= + dependencies: + vlq "^0.2.1" + magic-string@^0.25.0: version "0.25.9" resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.25.9.tgz#de7f9faf91ef8a1c91d02c2e5314c8277dbcdd1c" @@ -21872,6 +21884,14 @@ rollup-plugin-license@^2.6.1: spdx-expression-validate "2.0.0" spdx-satisfies "5.0.1" +rollup-plugin-re@^1.0.7: + version "1.0.7" + resolved "https://registry.yarnpkg.com/rollup-plugin-re/-/rollup-plugin-re-1.0.7.tgz#fe174704ed59cda84caf02bd013b582e6fdaa4f6" + integrity sha1-/hdHBO1ZzahMrwK9ATtYLm/apPY= + dependencies: + magic-string "^0.16.0" + rollup-pluginutils "^2.0.1" + rollup-plugin-sourcemaps@^0.6.0: version "0.6.3" resolved "https://registry.yarnpkg.com/rollup-plugin-sourcemaps/-/rollup-plugin-sourcemaps-0.6.3.tgz#bf93913ffe056e414419607f1d02780d7ece84ed" @@ -21902,6 +21922,13 @@ rollup-plugin-typescript2@^0.31.2: resolve "^1.20.0" tslib "^2.3.1" +rollup-pluginutils@^2.0.1: + version "2.8.2" + resolved "https://registry.yarnpkg.com/rollup-pluginutils/-/rollup-pluginutils-2.8.2.tgz#72f2af0748b592364dbd3389e600e5a9444a351e" + integrity sha512-EEp9NhnUkwY8aif6bxgovPHMoMoNr2FulJziTndpt5H9RdwC47GSGuII9XxpSdzVGM0GWrNPHV6ie1LTNJPaLQ== + dependencies: + estree-walker "^0.6.1" + rollup@2.26.5: version "2.26.5" resolved "https://registry.yarnpkg.com/rollup/-/rollup-2.26.5.tgz#5562ec36fcba3eed65cfd630bd78e037ad0e0307" @@ -24953,6 +24980,11 @@ verror@1.10.0: core-util-is "1.0.2" extsprintf "^1.2.0" +vlq@^0.2.1: + version "0.2.3" + resolved "https://registry.yarnpkg.com/vlq/-/vlq-0.2.3.tgz#8f3e4328cf63b1540c0d67e1b2778386f8975b26" + integrity sha512-DRibZL6DsNhIgYQ+wNdWDL2SL3bKPlVrRiBqV5yuMm++op8W4kGFtaQfCs4KEJn0wBZcHVHJ3eoywX8983k1ow== + vm-browserify@1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/vm-browserify/-/vm-browserify-1.1.0.tgz#bd76d6a23323e2ca8ffa12028dc04559c75f9019" From 025667131e0644fd62cfe959f28034c72ff05a01 Mon Sep 17 00:00:00 2001 From: Katie Byers Date: Tue, 19 Apr 2022 01:33:15 -0700 Subject: [PATCH 2/5] add `const`-to-`var` plugin --- rollup/npmHelpers.js | 5 +++-- rollup/plugins/npmPlugins.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/rollup/npmHelpers.js b/rollup/npmHelpers.js index 71bdc58fb953..66fb02be8f6a 100644 --- a/rollup/npmHelpers.js +++ b/rollup/npmHelpers.js @@ -7,7 +7,7 @@ import * as path from 'path'; import deepMerge from 'deepmerge'; -import { makeNodeResolvePlugin, makeSucrasePlugin } from './plugins/index.js'; +import { makeConstToVarPlugin, makeNodeResolvePlugin, makeSucrasePlugin } from './plugins/index.js'; const packageDotJSON = require(path.resolve(process.cwd(), './package.json')); @@ -21,6 +21,7 @@ export function makeBaseNPMConfig(options = {}) { const nodeResolvePlugin = makeNodeResolvePlugin(); const sucrasePlugin = makeSucrasePlugin(); + const constToVarPlugin = makeConstToVarPlugin(); // return { const config = { @@ -62,7 +63,7 @@ export function makeBaseNPMConfig(options = {}) { interop: esModuleInterop ? 'auto' : 'esModule', }, - plugins: [nodeResolvePlugin, sucrasePlugin], + plugins: [nodeResolvePlugin, sucrasePlugin, constToVarPlugin], // don't include imported modules from outside the package in the final output external: [ diff --git a/rollup/plugins/npmPlugins.js b/rollup/plugins/npmPlugins.js index 5f4c443682ef..1ec09797ef3d 100644 --- a/rollup/plugins/npmPlugins.js +++ b/rollup/plugins/npmPlugins.js @@ -1,7 +1,9 @@ /** + * Replace plugin docs: https://github.com/rollup/plugins/tree/master/packages/replace * Sucrase plugin docs: https://github.com/rollup/plugins/tree/master/packages/sucrase */ +import replace from '@rollup/plugin-replace'; import sucrase from '@rollup/plugin-sucrase'; /** @@ -14,3 +16,29 @@ export function makeSucrasePlugin() { transforms: ['typescript', 'jsx'], }); } + +/** + * Create a plugin to switch all instances of `const` to `var`, both to prevent problems when we shadow `global` and + * because it's fewer characters. + * + * Note that the generated plugin runs the replacement both before and after rollup does its code manipulation, to + * increase the chances that nothing is missed. + * + * TODO This is pretty brute-force-y. Perhaps we could switch to using a parser, the way we (will) do for both our jest + * transformer and the polyfill build script. + * + * @returns An instance of the `@rollup/plugin-replace` plugin + */ +export function makeConstToVarPlugin() { + return replace({ + // TODO `preventAssignment` will default to true in version 5.x of the replace plugin, at which point we can get rid + // of this. (It actually makes no difference in this case whether it's true or false, since we never assign to + // `const`, but if we don't give it a value, it will spam with warnings.) + preventAssignment: true, + values: { + // Include a space at the end to guarantee we're not accidentally catching the beginning of the words "constant," + // "constantly," etc. + 'const ': 'var ', + }, + }); +} From da163002925e2abf8dc94bb23b4fd435f783bf31 Mon Sep 17 00:00:00 2001 From: Katie Byers Date: Wed, 20 Apr 2022 21:30:52 -0700 Subject: [PATCH 3/5] add debugger plugin --- rollup/plugins/npmPlugins.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/rollup/plugins/npmPlugins.js b/rollup/plugins/npmPlugins.js index 1ec09797ef3d..1146cb5ad213 100644 --- a/rollup/plugins/npmPlugins.js +++ b/rollup/plugins/npmPlugins.js @@ -42,3 +42,22 @@ export function makeConstToVarPlugin() { }, }); } + +/** + * Create a plugin which can be used to pause the build process at the given hook. + * + * Hooks can be found here: https://rollupjs.org/guide/en/#build-hooks + * + * @param hookName The name of the hook at which to pause. + * @returns A plugin which inserts a debugger statement in the phase represented by the given hook + */ +export function makeDebuggerPlugin(hookName) { + return { + name: 'debugger-plugin', + [hookName]: () => { + // eslint-disable-next-line no-debugger + debugger; + return null; + }, + }; +} From 7710e684c1d358c9b5bebdfee8dd848cfdb876d6 Mon Sep 17 00:00:00 2001 From: Katie Byers Date: Tue, 26 Apr 2022 22:56:10 -0700 Subject: [PATCH 4/5] add eslint-comment-stripping plugin --- rollup/npmHelpers.js | 10 ++++++++-- rollup/plugins/npmPlugins.js | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/rollup/npmHelpers.js b/rollup/npmHelpers.js index 66fb02be8f6a..8655ffc41ad6 100644 --- a/rollup/npmHelpers.js +++ b/rollup/npmHelpers.js @@ -7,7 +7,12 @@ import * as path from 'path'; import deepMerge from 'deepmerge'; -import { makeConstToVarPlugin, makeNodeResolvePlugin, makeSucrasePlugin } from './plugins/index.js'; +import { + makeConstToVarPlugin, + makeNodeResolvePlugin, + makeRemoveESLintCommentsPlugin, + makeSucrasePlugin, +} from './plugins/index.js'; const packageDotJSON = require(path.resolve(process.cwd(), './package.json')); @@ -22,6 +27,7 @@ export function makeBaseNPMConfig(options = {}) { const nodeResolvePlugin = makeNodeResolvePlugin(); const sucrasePlugin = makeSucrasePlugin(); const constToVarPlugin = makeConstToVarPlugin(); + const removeESLintCommentsPlugin = makeRemoveESLintCommentsPlugin(); // return { const config = { @@ -63,7 +69,7 @@ export function makeBaseNPMConfig(options = {}) { interop: esModuleInterop ? 'auto' : 'esModule', }, - plugins: [nodeResolvePlugin, sucrasePlugin, constToVarPlugin], + plugins: [nodeResolvePlugin, sucrasePlugin, constToVarPlugin, removeESLintCommentsPlugin], // don't include imported modules from outside the package in the final output external: [ diff --git a/rollup/plugins/npmPlugins.js b/rollup/plugins/npmPlugins.js index 1146cb5ad213..83ae0e409406 100644 --- a/rollup/plugins/npmPlugins.js +++ b/rollup/plugins/npmPlugins.js @@ -1,8 +1,12 @@ /** + * Regex Replace plugin docs: https://github.com/jetiny/rollup-plugin-re * Replace plugin docs: https://github.com/rollup/plugins/tree/master/packages/replace * Sucrase plugin docs: https://github.com/rollup/plugins/tree/master/packages/sucrase */ +// We need both replacement plugins because one handles regex and the other runs both before and after rollup does its +// bundling work. +import regexReplace from 'rollup-plugin-re'; import replace from '@rollup/plugin-replace'; import sucrase from '@rollup/plugin-sucrase'; @@ -61,3 +65,19 @@ export function makeDebuggerPlugin(hookName) { }, }; } + +/** + * Create a plugin to strip eslint-style comments from the output. + * + * @returns A `rollup-plugin-re` instance. + */ +export function makeRemoveESLintCommentsPlugin() { + return regexReplace({ + patterns: [ + { + test: /\/[/*] eslint-disable.*\n/g, + replace: '', + }, + ], + }); +} From a25873c6b41f24aa4531c85cd5e769d65bfc24e8 Mon Sep 17 00:00:00 2001 From: Katie Byers Date: Tue, 26 Apr 2022 22:56:31 -0700 Subject: [PATCH 5/5] add blank-line-removal plugin --- rollup/npmHelpers.js | 4 +++- rollup/plugins/npmPlugins.js | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/rollup/npmHelpers.js b/rollup/npmHelpers.js index 8655ffc41ad6..79a14189a880 100644 --- a/rollup/npmHelpers.js +++ b/rollup/npmHelpers.js @@ -10,6 +10,7 @@ import deepMerge from 'deepmerge'; import { makeConstToVarPlugin, makeNodeResolvePlugin, + makeRemoveBlankLinesPlugin, makeRemoveESLintCommentsPlugin, makeSucrasePlugin, } from './plugins/index.js'; @@ -28,6 +29,7 @@ export function makeBaseNPMConfig(options = {}) { const sucrasePlugin = makeSucrasePlugin(); const constToVarPlugin = makeConstToVarPlugin(); const removeESLintCommentsPlugin = makeRemoveESLintCommentsPlugin(); + const removeBlankLinesPlugin = makeRemoveBlankLinesPlugin(); // return { const config = { @@ -69,7 +71,7 @@ export function makeBaseNPMConfig(options = {}) { interop: esModuleInterop ? 'auto' : 'esModule', }, - plugins: [nodeResolvePlugin, sucrasePlugin, constToVarPlugin, removeESLintCommentsPlugin], + plugins: [nodeResolvePlugin, sucrasePlugin, constToVarPlugin, removeESLintCommentsPlugin, removeBlankLinesPlugin], // don't include imported modules from outside the package in the final output external: [ diff --git a/rollup/plugins/npmPlugins.js b/rollup/plugins/npmPlugins.js index 83ae0e409406..568ef6b69e10 100644 --- a/rollup/plugins/npmPlugins.js +++ b/rollup/plugins/npmPlugins.js @@ -81,3 +81,19 @@ export function makeRemoveESLintCommentsPlugin() { ], }); } + +/** + * Create a plugin to strip multiple consecutive blank lines, with or without whitespace in them. from the output. + * + * @returns A `rollup-plugin-re` instance. + */ +export function makeRemoveBlankLinesPlugin() { + return regexReplace({ + patterns: [ + { + test: /\n(\n\s*)+\n/g, + replace: '\n\n', + }, + ], + }); +}