Skip to content

Commit 12c19d0

Browse files
authored
ref(build): Switch from using rollup-plugin-re to rollup-plugin-cleanup (#5824)
This switches from using regexes to clean up our built code to using a dedicated plugin, in order to be more confident in the correctness of the output.
1 parent 0cfda74 commit 12c19d0

File tree

6 files changed

+50
-86
lines changed

6 files changed

+50
-86
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@
9191
"replace-in-file": "^4.0.0",
9292
"rimraf": "^3.0.2",
9393
"rollup": "^2.67.1",
94+
"rollup-plugin-cleanup": "3.2.1",
9495
"rollup-plugin-license": "^2.6.1",
95-
"rollup-plugin-re": "^1.0.7",
9696
"rollup-plugin-terser": "^7.0.2",
9797
"rollup-plugin-typescript2": "^0.31.2",
9898
"sinon": "^7.3.2",

packages/nextjs/rollup.npm.config.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { makeBaseNPMConfig, makeNPMConfigVariants, plugins } from '../../rollup/index.js';
1+
import { makeBaseNPMConfig, makeNPMConfigVariants } from '../../rollup/index.js';
22

33
export default [
44
...makeNPMConfigVariants(
@@ -21,7 +21,6 @@ export default [
2121
],
2222

2323
packageSpecificConfig: {
24-
plugins: [plugins.makeRemoveMultiLineCommentsPlugin()],
2524
output: {
2625
// Preserve the original file structure (i.e., so that everything is still relative to `src`)
2726
entryFileNames: 'config/templates/[name].js',

rollup/bundleHelpers.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ import {
1212
makeIsDebugBuildPlugin,
1313
makeLicensePlugin,
1414
makeNodeResolvePlugin,
15-
makeRemoveBlankLinesPlugin,
16-
makeRemoveESLintCommentsPlugin,
15+
makeCleanupPlugin,
1716
makeSucrasePlugin,
1817
makeTerserPlugin,
1918
makeTSPlugin,
@@ -27,8 +26,7 @@ export function makeBaseBundleConfig(options) {
2726

2827
const nodeResolvePlugin = makeNodeResolvePlugin();
2928
const sucrasePlugin = makeSucrasePlugin();
30-
const removeBlankLinesPlugin = makeRemoveBlankLinesPlugin();
31-
const removeESLintCommentsPlugin = makeRemoveESLintCommentsPlugin();
29+
const cleanupPlugin = makeCleanupPlugin();
3230
const markAsBrowserBuildPlugin = makeBrowserBuildPlugin(true);
3331
const licensePlugin = makeLicensePlugin(licenseTitle);
3432
const tsPlugin = makeTSPlugin(jsVersion.toLowerCase());
@@ -104,8 +102,8 @@ export function makeBaseBundleConfig(options) {
104102
},
105103
plugins:
106104
jsVersion === 'es5'
107-
? [tsPlugin, nodeResolvePlugin, licensePlugin]
108-
: [sucrasePlugin, removeBlankLinesPlugin, removeESLintCommentsPlugin, nodeResolvePlugin, licensePlugin],
105+
? [tsPlugin, nodeResolvePlugin, cleanupPlugin, licensePlugin]
106+
: [sucrasePlugin, nodeResolvePlugin, cleanupPlugin, licensePlugin],
109107
treeshake: 'smallest',
110108
};
111109

rollup/npmHelpers.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ import {
1111
makeConstToVarPlugin,
1212
makeExtractPolyfillsPlugin,
1313
makeNodeResolvePlugin,
14-
makeRemoveBlankLinesPlugin,
15-
makeRemoveESLintCommentsPlugin,
14+
makeCleanupPlugin,
1615
makeSucrasePlugin,
1716
makeDebugBuildStatementReplacePlugin,
1817
} from './plugins/index.js';
@@ -32,8 +31,7 @@ export function makeBaseNPMConfig(options = {}) {
3231
const sucrasePlugin = makeSucrasePlugin();
3332
const debugBuildStatementReplacePlugin = makeDebugBuildStatementReplacePlugin();
3433
const constToVarPlugin = makeConstToVarPlugin();
35-
const removeESLintCommentsPlugin = makeRemoveESLintCommentsPlugin();
36-
const removeBlankLinesPlugin = makeRemoveBlankLinesPlugin();
34+
const cleanupPlugin = makeCleanupPlugin();
3735
const extractPolyfillsPlugin = makeExtractPolyfillsPlugin();
3836

3937
const defaultBaseConfig = {
@@ -84,8 +82,7 @@ export function makeBaseNPMConfig(options = {}) {
8482
sucrasePlugin,
8583
debugBuildStatementReplacePlugin,
8684
constToVarPlugin,
87-
removeESLintCommentsPlugin,
88-
removeBlankLinesPlugin,
85+
cleanupPlugin,
8986
extractPolyfillsPlugin,
9087
],
9188

rollup/plugins/npmPlugins.js

+13-50
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22
* Rollup plugin hooks docs: https://rollupjs.org/guide/en/#build-hooks and
33
* https://rollupjs.org/guide/en/#output-generation-hooks
44
*
5-
* Regex Replace plugin docs: https://github.com/jetiny/rollup-plugin-re
5+
* Cleanup plugin docs: https://github.com/aMarCruz/rollup-plugin-cleanup
66
* Replace plugin docs: https://github.com/rollup/plugins/tree/master/packages/replace
77
* Sucrase plugin docs: https://github.com/rollup/plugins/tree/master/packages/sucrase
88
*/
99

10-
// We need both replacement plugins because one handles regex and the other runs both before and after rollup does its
11-
// bundling work.
12-
import regexReplace from 'rollup-plugin-re';
10+
import cleanup from 'rollup-plugin-cleanup';
1311
import replace from '@rollup/plugin-replace';
1412
import sucrase from '@rollup/plugin-sucrase';
1513

@@ -100,54 +98,19 @@ export function makeDebuggerPlugin(hookName) {
10098
}
10199

102100
/**
103-
* Create a plugin to strip eslint-style comments from the output.
101+
* Create a plugin to clean up output files by:
102+
* - Converting line endings unix line endings
103+
* - Removing consecutive empty lines
104104
*
105-
* @returns A `rollup-plugin-re` instance.
105+
* @returns A `rollup-plugin-cleanup` instance.
106106
*/
107-
export function makeRemoveESLintCommentsPlugin() {
108-
return regexReplace({
109-
patterns: [
110-
{
111-
test: /\/[/*] eslint-.*\n/g,
112-
replace: '',
113-
},
114-
],
115-
});
116-
}
117-
118-
/**
119-
* Create a plugin to strip multiple consecutive blank lines, with or without whitespace in them. from the output.
120-
*
121-
* @returns A `rollup-plugin-re` instance.
122-
*/
123-
export function makeRemoveBlankLinesPlugin() {
124-
return regexReplace({
125-
patterns: [
126-
{
127-
test: /\n(\n\s*)+\n/g,
128-
replace: '\n\n',
129-
},
130-
],
131-
});
132-
}
133-
134-
/**
135-
* Create a plugin to strip multi-line comments from the output.
136-
*
137-
* @returns A `rollup-plugin-re` instance.
138-
*/
139-
export function makeRemoveMultiLineCommentsPlugin() {
140-
return regexReplace({
141-
patterns: [
142-
{
143-
// If we ever want to remove all comments instead of just /* ... */ ones, the regex is
144-
// /\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm. We also might consider a plugin like
145-
// https://github.com/aMarCruz/rollup-plugin-cleanup (though to remove only multi-line comments we'd end up with
146-
// a regex there, too).
147-
test: /\/\*[\s\S]*?\*\//gm,
148-
replace: '',
149-
},
150-
],
107+
export function makeCleanupPlugin() {
108+
return cleanup({
109+
// line endings are unix-ized by default
110+
comments: 'all', // comments to keep
111+
compactComments: 'false', // don't remove blank lines in multi-line comments
112+
maxEmptyLines: 1,
113+
extensions: ['js', 'jsx', 'ts', 'tsx'],
151114
});
152115
}
153116

yarn.lock

+28-21
Original file line numberDiff line numberDiff line change
@@ -16871,6 +16871,15 @@ jquery@^3.5.0:
1687116871
resolved "https://registry.yarnpkg.com/jquery/-/jquery-3.6.0.tgz#c72a09f15c1bdce142f49dbf1170bdf8adac2470"
1687216872
integrity sha512-JVzAR/AjBvVt2BmYhxRCSYysDsPcssdmTFnzyLEts9qNwmjmu4JTAMYubEfwVOSwpQ1I1sKKFcxhZCI2buerfw==
1687316873

16874+
js-cleanup@^1.2.0:
16875+
version "1.2.0"
16876+
resolved "https://registry.yarnpkg.com/js-cleanup/-/js-cleanup-1.2.0.tgz#8dbc65954b1d38b255f1e8cf02cd17b3f7a053f9"
16877+
integrity sha512-JeDD0yiiSt80fXzAVa/crrS0JDPQljyBG/RpOtaSbyDq03VHa9szJWMaWOYU/bcTn412uMN2MxApXq8v79cUiQ==
16878+
dependencies:
16879+
magic-string "^0.25.7"
16880+
perf-regexes "^1.0.1"
16881+
skip-regex "^1.0.2"
16882+
1687416883
1687516884
version "1.2.3"
1687616885
resolved "https://registry.yarnpkg.com/js-reporters/-/js-reporters-1.2.3.tgz#8febcab370539df62e09b95da133da04b11f6168"
@@ -18122,13 +18131,6 @@ [email protected], magic-string@^0.25.1, magic-string@^0.25.7:
1812218131
dependencies:
1812318132
sourcemap-codec "^1.4.4"
1812418133

18125-
magic-string@^0.16.0:
18126-
version "0.16.0"
18127-
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.16.0.tgz#970ebb0da7193301285fb1aa650f39bdd81eb45a"
18128-
integrity sha1-lw67DacZMwEoX7GqZQ85vdgetFo=
18129-
dependencies:
18130-
vlq "^0.2.1"
18131-
1813218134
magic-string@^0.25.0:
1813318135
version "0.25.9"
1813418136
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.25.9.tgz#de7f9faf91ef8a1c91d02c2e5314c8277dbcdd1c"
@@ -20547,6 +20549,11 @@ pend@~1.2.0:
2054720549
resolved "https://registry.yarnpkg.com/pend/-/pend-1.2.0.tgz#7a57eb550a6783f9115331fcf4663d5c8e007a50"
2054820550
integrity sha1-elfrVQpng/kRUzH89GY9XI4AelA=
2054920551

20552+
perf-regexes@^1.0.1:
20553+
version "1.0.1"
20554+
resolved "https://registry.yarnpkg.com/perf-regexes/-/perf-regexes-1.0.1.tgz#6da1d62f5a94bf9353a0451bccacf69068b75d0b"
20555+
integrity sha512-L7MXxUDtqr4PUaLFCDCXBfGV/6KLIuSEccizDI7JxT+c9x1G1v04BQ4+4oag84SHaCdrBgQAIs/Cqn+flwFPng==
20556+
2055020557
performance-now@^2.1.0:
2055120558
version "2.1.0"
2055220559
resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b"
@@ -22823,6 +22830,14 @@ ripemd160@^2.0.0, ripemd160@^2.0.1:
2282322830
hash-base "^3.0.0"
2282422831
inherits "^2.0.1"
2282522832

22833+
22834+
version "3.2.1"
22835+
resolved "https://registry.yarnpkg.com/rollup-plugin-cleanup/-/rollup-plugin-cleanup-3.2.1.tgz#8cbc92ecf58babd7c210051929797f137bbf777c"
22836+
integrity sha512-zuv8EhoO3TpnrU8MX8W7YxSbO4gmOR0ny06Lm3nkFfq0IVKdBUtHwhVzY1OAJyNCIAdLiyPnOrU0KnO0Fri1GQ==
22837+
dependencies:
22838+
js-cleanup "^1.2.0"
22839+
rollup-pluginutils "^2.8.2"
22840+
2282622841
rollup-plugin-license@^2.6.1:
2282722842
version "2.6.1"
2282822843
resolved "https://registry.yarnpkg.com/rollup-plugin-license/-/rollup-plugin-license-2.6.1.tgz#20f15cc37950f362f8eefdc6e3a2e659d0cad9eb"
@@ -22838,14 +22853,6 @@ rollup-plugin-license@^2.6.1:
2283822853
spdx-expression-validate "2.0.0"
2283922854
spdx-satisfies "5.0.1"
2284022855

22841-
rollup-plugin-re@^1.0.7:
22842-
version "1.0.7"
22843-
resolved "https://registry.yarnpkg.com/rollup-plugin-re/-/rollup-plugin-re-1.0.7.tgz#fe174704ed59cda84caf02bd013b582e6fdaa4f6"
22844-
integrity sha1-/hdHBO1ZzahMrwK9ATtYLm/apPY=
22845-
dependencies:
22846-
magic-string "^0.16.0"
22847-
rollup-pluginutils "^2.0.1"
22848-
2284922856
rollup-plugin-sourcemaps@^0.6.0:
2285022857
version "0.6.3"
2285122858
resolved "https://registry.yarnpkg.com/rollup-plugin-sourcemaps/-/rollup-plugin-sourcemaps-0.6.3.tgz#bf93913ffe056e414419607f1d02780d7ece84ed"
@@ -22876,7 +22883,7 @@ rollup-plugin-typescript2@^0.31.2:
2287622883
resolve "^1.20.0"
2287722884
tslib "^2.3.1"
2287822885

22879-
rollup-pluginutils@^2.0.1:
22886+
rollup-pluginutils@^2.8.2:
2288022887
version "2.8.2"
2288122888
resolved "https://registry.yarnpkg.com/rollup-pluginutils/-/rollup-pluginutils-2.8.2.tgz#72f2af0748b592364dbd3389e600e5a9444a351e"
2288222889
integrity sha512-EEp9NhnUkwY8aif6bxgovPHMoMoNr2FulJziTndpt5H9RdwC47GSGuII9XxpSdzVGM0GWrNPHV6ie1LTNJPaLQ==
@@ -23463,6 +23470,11 @@ size-limit@^4.5.5:
2346323470
ora "^5.3.0"
2346423471
read-pkg-up "^7.0.1"
2346523472

23473+
skip-regex@^1.0.2:
23474+
version "1.0.2"
23475+
resolved "https://registry.yarnpkg.com/skip-regex/-/skip-regex-1.0.2.tgz#ac655d77e7c771ac2b9f37585fea37bff56ad65b"
23476+
integrity sha512-pEjMUbwJ5Pl/6Vn6FsamXHXItJXSRftcibixDmNCWbWhic0hzHrwkMZo0IZ7fMRH9KxcWDFSkzhccB4285PutA==
23477+
2346623478
slash@^1.0.0:
2346723479
version "1.0.0"
2346823480
resolved "https://registry.yarnpkg.com/slash/-/slash-1.0.0.tgz#c41f2f6c39fc16d1cd17ad4b5d896114ae470d55"
@@ -26083,11 +26095,6 @@ [email protected]:
2608326095
core-util-is "1.0.2"
2608426096
extsprintf "^1.2.0"
2608526097

26086-
vlq@^0.2.1:
26087-
version "0.2.3"
26088-
resolved "https://registry.yarnpkg.com/vlq/-/vlq-0.2.3.tgz#8f3e4328cf63b1540c0d67e1b2778386f8975b26"
26089-
integrity sha512-DRibZL6DsNhIgYQ+wNdWDL2SL3bKPlVrRiBqV5yuMm++op8W4kGFtaQfCs4KEJn0wBZcHVHJ3eoywX8983k1ow==
26090-
2609126098
2609226099
version "1.1.0"
2609326100
resolved "https://registry.yarnpkg.com/vm-browserify/-/vm-browserify-1.1.0.tgz#bd76d6a23323e2ca8ffa12028dc04559c75f9019"

0 commit comments

Comments
 (0)