Skip to content

Commit 1130c1c

Browse files
authored
feat(build): Add various rollup plugins to sucrase build (#4993)
This adds four rollup plugins for use in the new build process: - A plugin which converts all `const`s in the output to `var`s, both after sucrase's code modifications and after those done by rollup itself, to make sure that all instances of `const` are caught. This transformation has two advantages: - It prevents errors arising from the way we redefine `global` (as the return value of `getGlobalObject`), since `var`s can be redeclared where `const`s can't. - It replaces a very common token which is four letters with one which is three letters. Not a huge bundle size savings, but it at least prevents us from going the wrong direction. (Our current builds use `var` because they are ES5, so this puts the new build in line with the current build, number-of-characters-in-variable-specifiers-wise.) - A plugin which strips eslint-style comments and another which reduces multiple consecutive blank lines down to one. Neither of these affects either code behavior or bundle size, but they do make the eventual output a little less messy. One of the odd quirks of sucrase is that it has the goal of never changing the line number of an expression[1], in order that even stacktraces from un-sourcemapped code are meaningful, and this can result in some odd-looking files. These two plugins don't totally undo the oddness, but they're simple and fast and certainly don't hurt readability. - A plugin to be used during our development, which allows you to place a debugger in the rollup hook[2] of your choice. [1] alangpierce/sucrase#452 (comment) [2] https://rollupjs.org/guide/en/#build-hooks
1 parent 69e6275 commit 1130c1c

File tree

4 files changed

+127
-2
lines changed

4 files changed

+127
-2
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
"rimraf": "^3.0.2",
8686
"rollup": "^2.67.1",
8787
"rollup-plugin-license": "^2.6.1",
88+
"rollup-plugin-re": "^1.0.7",
8889
"rollup-plugin-terser": "^7.0.2",
8990
"rollup-plugin-typescript2": "^0.31.2",
9091
"sinon": "^7.3.2",

rollup/npmHelpers.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ import * as path from 'path';
77

88
import deepMerge from 'deepmerge';
99

10-
import { makeNodeResolvePlugin, makeSucrasePlugin } from './plugins/index.js';
10+
import {
11+
makeConstToVarPlugin,
12+
makeNodeResolvePlugin,
13+
makeRemoveBlankLinesPlugin,
14+
makeRemoveESLintCommentsPlugin,
15+
makeSucrasePlugin,
16+
} from './plugins/index.js';
1117

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

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

2228
const nodeResolvePlugin = makeNodeResolvePlugin();
2329
const sucrasePlugin = makeSucrasePlugin();
30+
const constToVarPlugin = makeConstToVarPlugin();
31+
const removeESLintCommentsPlugin = makeRemoveESLintCommentsPlugin();
32+
const removeBlankLinesPlugin = makeRemoveBlankLinesPlugin();
2433

2534
// return {
2635
const config = {
@@ -62,7 +71,7 @@ export function makeBaseNPMConfig(options = {}) {
6271
interop: esModuleInterop ? 'auto' : 'esModule',
6372
},
6473

65-
plugins: [nodeResolvePlugin, sucrasePlugin],
74+
plugins: [nodeResolvePlugin, sucrasePlugin, constToVarPlugin, removeESLintCommentsPlugin, removeBlankLinesPlugin],
6675

6776
// don't include imported modules from outside the package in the final output
6877
external: [

rollup/plugins/npmPlugins.js

+83
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
/**
2+
* Regex Replace plugin docs: https://github.com/jetiny/rollup-plugin-re
3+
* Replace plugin docs: https://github.com/rollup/plugins/tree/master/packages/replace
24
* Sucrase plugin docs: https://github.com/rollup/plugins/tree/master/packages/sucrase
35
*/
46

7+
// We need both replacement plugins because one handles regex and the other runs both before and after rollup does its
8+
// bundling work.
9+
import regexReplace from 'rollup-plugin-re';
10+
import replace from '@rollup/plugin-replace';
511
import sucrase from '@rollup/plugin-sucrase';
612

713
/**
@@ -14,3 +20,80 @@ export function makeSucrasePlugin() {
1420
transforms: ['typescript', 'jsx'],
1521
});
1622
}
23+
24+
/**
25+
* Create a plugin to switch all instances of `const` to `var`, both to prevent problems when we shadow `global` and
26+
* because it's fewer characters.
27+
*
28+
* Note that the generated plugin runs the replacement both before and after rollup does its code manipulation, to
29+
* increase the chances that nothing is missed.
30+
*
31+
* TODO This is pretty brute-force-y. Perhaps we could switch to using a parser, the way we (will) do for both our jest
32+
* transformer and the polyfill build script.
33+
*
34+
* @returns An instance of the `@rollup/plugin-replace` plugin
35+
*/
36+
export function makeConstToVarPlugin() {
37+
return replace({
38+
// TODO `preventAssignment` will default to true in version 5.x of the replace plugin, at which point we can get rid
39+
// of this. (It actually makes no difference in this case whether it's true or false, since we never assign to
40+
// `const`, but if we don't give it a value, it will spam with warnings.)
41+
preventAssignment: true,
42+
values: {
43+
// Include a space at the end to guarantee we're not accidentally catching the beginning of the words "constant,"
44+
// "constantly," etc.
45+
'const ': 'var ',
46+
},
47+
});
48+
}
49+
50+
/**
51+
* Create a plugin which can be used to pause the build process at the given hook.
52+
*
53+
* Hooks can be found here: https://rollupjs.org/guide/en/#build-hooks
54+
*
55+
* @param hookName The name of the hook at which to pause.
56+
* @returns A plugin which inserts a debugger statement in the phase represented by the given hook
57+
*/
58+
export function makeDebuggerPlugin(hookName) {
59+
return {
60+
name: 'debugger-plugin',
61+
[hookName]: () => {
62+
// eslint-disable-next-line no-debugger
63+
debugger;
64+
return null;
65+
},
66+
};
67+
}
68+
69+
/**
70+
* Create a plugin to strip eslint-style comments from the output.
71+
*
72+
* @returns A `rollup-plugin-re` instance.
73+
*/
74+
export function makeRemoveESLintCommentsPlugin() {
75+
return regexReplace({
76+
patterns: [
77+
{
78+
test: /\/[/*] eslint-disable.*\n/g,
79+
replace: '',
80+
},
81+
],
82+
});
83+
}
84+
85+
/**
86+
* Create a plugin to strip multiple consecutive blank lines, with or without whitespace in them. from the output.
87+
*
88+
* @returns A `rollup-plugin-re` instance.
89+
*/
90+
export function makeRemoveBlankLinesPlugin() {
91+
return regexReplace({
92+
patterns: [
93+
{
94+
test: /\n(\n\s*)+\n/g,
95+
replace: '\n\n',
96+
},
97+
],
98+
});
99+
}

yarn.lock

+32
Original file line numberDiff line numberDiff line change
@@ -12051,6 +12051,11 @@ estraverse@^5.1.0, estraverse@^5.2.0:
1205112051
resolved "https://registry.yarnpkg.com/estraverse/-/estraverse-5.2.0.tgz#307df42547e6cc7324d3cf03c155d5cdb8c53880"
1205212052
integrity sha512-BxbNGGNm0RyRYvUdHpIwv9IWzeM9XClbOxwoATuFdOE7ZE6wHL+HQ5T8hoPM+zHvmKzzsEqhgy0GrQ5X13afiQ==
1205312053

12054+
estree-walker@^0.6.1:
12055+
version "0.6.1"
12056+
resolved "https://registry.yarnpkg.com/estree-walker/-/estree-walker-0.6.1.tgz#53049143f40c6eb918b23671d1fe3219f3a1b362"
12057+
integrity sha512-SqmZANLWS0mnatqbSfRP5g8OXZC12Fgg1IwNtLsyHDzJizORW4khDfjPqJZsemPWBB2uqykUah5YpQ6epsqC/w==
12058+
1205412059
estree-walker@^1.0.1:
1205512060
version "1.0.1"
1205612061
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:
1730717312
dependencies:
1730817313
sourcemap-codec "^1.4.4"
1730917314

17315+
magic-string@^0.16.0:
17316+
version "0.16.0"
17317+
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.16.0.tgz#970ebb0da7193301285fb1aa650f39bdd81eb45a"
17318+
integrity sha1-lw67DacZMwEoX7GqZQ85vdgetFo=
17319+
dependencies:
17320+
vlq "^0.2.1"
17321+
1731017322
magic-string@^0.25.0:
1731117323
version "0.25.9"
1731217324
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.25.9.tgz#de7f9faf91ef8a1c91d02c2e5314c8277dbcdd1c"
@@ -21872,6 +21884,14 @@ rollup-plugin-license@^2.6.1:
2187221884
spdx-expression-validate "2.0.0"
2187321885
spdx-satisfies "5.0.1"
2187421886

21887+
rollup-plugin-re@^1.0.7:
21888+
version "1.0.7"
21889+
resolved "https://registry.yarnpkg.com/rollup-plugin-re/-/rollup-plugin-re-1.0.7.tgz#fe174704ed59cda84caf02bd013b582e6fdaa4f6"
21890+
integrity sha1-/hdHBO1ZzahMrwK9ATtYLm/apPY=
21891+
dependencies:
21892+
magic-string "^0.16.0"
21893+
rollup-pluginutils "^2.0.1"
21894+
2187521895
rollup-plugin-sourcemaps@^0.6.0:
2187621896
version "0.6.3"
2187721897
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:
2190221922
resolve "^1.20.0"
2190321923
tslib "^2.3.1"
2190421924

21925+
rollup-pluginutils@^2.0.1:
21926+
version "2.8.2"
21927+
resolved "https://registry.yarnpkg.com/rollup-pluginutils/-/rollup-pluginutils-2.8.2.tgz#72f2af0748b592364dbd3389e600e5a9444a351e"
21928+
integrity sha512-EEp9NhnUkwY8aif6bxgovPHMoMoNr2FulJziTndpt5H9RdwC47GSGuII9XxpSdzVGM0GWrNPHV6ie1LTNJPaLQ==
21929+
dependencies:
21930+
estree-walker "^0.6.1"
21931+
2190521932
rollup@2.26.5:
2190621933
version "2.26.5"
2190721934
resolved "https://registry.yarnpkg.com/rollup/-/rollup-2.26.5.tgz#5562ec36fcba3eed65cfd630bd78e037ad0e0307"
@@ -24953,6 +24980,11 @@ verror@1.10.0:
2495324980
core-util-is "1.0.2"
2495424981
extsprintf "^1.2.0"
2495524982

24983+
vlq@^0.2.1:
24984+
version "0.2.3"
24985+
resolved "https://registry.yarnpkg.com/vlq/-/vlq-0.2.3.tgz#8f3e4328cf63b1540c0d67e1b2778386f8975b26"
24986+
integrity sha512-DRibZL6DsNhIgYQ+wNdWDL2SL3bKPlVrRiBqV5yuMm++op8W4kGFtaQfCs4KEJn0wBZcHVHJ3eoywX8983k1ow==
24987+
2495624988
vm-browserify@1.1.0:
2495724989
version "1.1.0"
2495824990
resolved "https://registry.yarnpkg.com/vm-browserify/-/vm-browserify-1.1.0.tgz#bd76d6a23323e2ca8ffa12028dc04559c75f9019"

0 commit comments

Comments
 (0)