Skip to content

feat(build): Add various rollup plugins to sucrase build #4993

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 11 additions & 2 deletions rollup/npmHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import * as path from 'path';

import deepMerge from 'deepmerge';

import { makeNodeResolvePlugin, makeSucrasePlugin } from './plugins/index.js';
import {
makeConstToVarPlugin,
makeNodeResolvePlugin,
makeRemoveBlankLinesPlugin,
makeRemoveESLintCommentsPlugin,
makeSucrasePlugin,
} from './plugins/index.js';

const packageDotJSON = require(path.resolve(process.cwd(), './package.json'));

Expand All @@ -21,6 +27,9 @@ export function makeBaseNPMConfig(options = {}) {

const nodeResolvePlugin = makeNodeResolvePlugin();
const sucrasePlugin = makeSucrasePlugin();
const constToVarPlugin = makeConstToVarPlugin();
const removeESLintCommentsPlugin = makeRemoveESLintCommentsPlugin();
const removeBlankLinesPlugin = makeRemoveBlankLinesPlugin();

// return {
const config = {
Expand Down Expand Up @@ -62,7 +71,7 @@ export function makeBaseNPMConfig(options = {}) {
interop: esModuleInterop ? 'auto' : 'esModule',
},

plugins: [nodeResolvePlugin, sucrasePlugin],
plugins: [nodeResolvePlugin, sucrasePlugin, constToVarPlugin, removeESLintCommentsPlugin, removeBlankLinesPlugin],

// don't include imported modules from outside the package in the final output
external: [
Expand Down
83 changes: 83 additions & 0 deletions rollup/plugins/npmPlugins.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
/**
* 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';

/**
Expand All @@ -14,3 +20,80 @@ 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 ',
},
});
}

/**
* 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;
},
};
}

/**
* Create a plugin to strip eslint-style comments from the output.
*
* @returns A `rollup-plugin-re` instance.
*/
export function makeRemoveESLintCommentsPlugin() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a make a ticket somewhere to add tests for these plugins?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, though of all of our build steps, I think there are many which more urgently need testing.

return regexReplace({
patterns: [
{
test: /\/[/*] eslint-disable.*\n/g,
replace: '',
},
],
});
}

/**
* 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',
},
],
});
}
32 changes: 32 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down