Skip to content

Commit

Permalink
ref(build): Switch from using rollup-plugin-re to `rollup-plugin-cl…
Browse files Browse the repository at this point in the history
…eanup` (#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.
  • Loading branch information
lobsterkatie authored Sep 27, 2022
1 parent 0cfda74 commit 12c19d0
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 86 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@
"replace-in-file": "^4.0.0",
"rimraf": "^3.0.2",
"rollup": "^2.67.1",
"rollup-plugin-cleanup": "3.2.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
3 changes: 1 addition & 2 deletions packages/nextjs/rollup.npm.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { makeBaseNPMConfig, makeNPMConfigVariants, plugins } from '../../rollup/index.js';
import { makeBaseNPMConfig, makeNPMConfigVariants } from '../../rollup/index.js';

export default [
...makeNPMConfigVariants(
Expand All @@ -21,7 +21,6 @@ export default [
],

packageSpecificConfig: {
plugins: [plugins.makeRemoveMultiLineCommentsPlugin()],
output: {
// Preserve the original file structure (i.e., so that everything is still relative to `src`)
entryFileNames: 'config/templates/[name].js',
Expand Down
10 changes: 4 additions & 6 deletions rollup/bundleHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import {
makeIsDebugBuildPlugin,
makeLicensePlugin,
makeNodeResolvePlugin,
makeRemoveBlankLinesPlugin,
makeRemoveESLintCommentsPlugin,
makeCleanupPlugin,
makeSucrasePlugin,
makeTerserPlugin,
makeTSPlugin,
Expand All @@ -27,8 +26,7 @@ export function makeBaseBundleConfig(options) {

const nodeResolvePlugin = makeNodeResolvePlugin();
const sucrasePlugin = makeSucrasePlugin();
const removeBlankLinesPlugin = makeRemoveBlankLinesPlugin();
const removeESLintCommentsPlugin = makeRemoveESLintCommentsPlugin();
const cleanupPlugin = makeCleanupPlugin();
const markAsBrowserBuildPlugin = makeBrowserBuildPlugin(true);
const licensePlugin = makeLicensePlugin(licenseTitle);
const tsPlugin = makeTSPlugin(jsVersion.toLowerCase());
Expand Down Expand Up @@ -104,8 +102,8 @@ export function makeBaseBundleConfig(options) {
},
plugins:
jsVersion === 'es5'
? [tsPlugin, nodeResolvePlugin, licensePlugin]
: [sucrasePlugin, removeBlankLinesPlugin, removeESLintCommentsPlugin, nodeResolvePlugin, licensePlugin],
? [tsPlugin, nodeResolvePlugin, cleanupPlugin, licensePlugin]
: [sucrasePlugin, nodeResolvePlugin, cleanupPlugin, licensePlugin],
treeshake: 'smallest',
};

Expand Down
9 changes: 3 additions & 6 deletions rollup/npmHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import {
makeConstToVarPlugin,
makeExtractPolyfillsPlugin,
makeNodeResolvePlugin,
makeRemoveBlankLinesPlugin,
makeRemoveESLintCommentsPlugin,
makeCleanupPlugin,
makeSucrasePlugin,
makeDebugBuildStatementReplacePlugin,
} from './plugins/index.js';
Expand All @@ -32,8 +31,7 @@ export function makeBaseNPMConfig(options = {}) {
const sucrasePlugin = makeSucrasePlugin();
const debugBuildStatementReplacePlugin = makeDebugBuildStatementReplacePlugin();
const constToVarPlugin = makeConstToVarPlugin();
const removeESLintCommentsPlugin = makeRemoveESLintCommentsPlugin();
const removeBlankLinesPlugin = makeRemoveBlankLinesPlugin();
const cleanupPlugin = makeCleanupPlugin();
const extractPolyfillsPlugin = makeExtractPolyfillsPlugin();

const defaultBaseConfig = {
Expand Down Expand Up @@ -84,8 +82,7 @@ export function makeBaseNPMConfig(options = {}) {
sucrasePlugin,
debugBuildStatementReplacePlugin,
constToVarPlugin,
removeESLintCommentsPlugin,
removeBlankLinesPlugin,
cleanupPlugin,
extractPolyfillsPlugin,
],

Expand Down
63 changes: 13 additions & 50 deletions rollup/plugins/npmPlugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
* Rollup plugin hooks docs: https://rollupjs.org/guide/en/#build-hooks and
* https://rollupjs.org/guide/en/#output-generation-hooks
*
* Regex Replace plugin docs: https://github.com/jetiny/rollup-plugin-re
* Cleanup plugin docs: https://github.com/aMarCruz/rollup-plugin-cleanup
* 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 cleanup from 'rollup-plugin-cleanup';
import replace from '@rollup/plugin-replace';
import sucrase from '@rollup/plugin-sucrase';

Expand Down Expand Up @@ -100,54 +98,19 @@ export function makeDebuggerPlugin(hookName) {
}

/**
* Create a plugin to strip eslint-style comments from the output.
* Create a plugin to clean up output files by:
* - Converting line endings unix line endings
* - Removing consecutive empty lines
*
* @returns A `rollup-plugin-re` instance.
* @returns A `rollup-plugin-cleanup` instance.
*/
export function makeRemoveESLintCommentsPlugin() {
return regexReplace({
patterns: [
{
test: /\/[/*] eslint-.*\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',
},
],
});
}

/**
* Create a plugin to strip multi-line comments from the output.
*
* @returns A `rollup-plugin-re` instance.
*/
export function makeRemoveMultiLineCommentsPlugin() {
return regexReplace({
patterns: [
{
// If we ever want to remove all comments instead of just /* ... */ ones, the regex is
// /\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm. We also might consider a plugin like
// https://github.com/aMarCruz/rollup-plugin-cleanup (though to remove only multi-line comments we'd end up with
// a regex there, too).
test: /\/\*[\s\S]*?\*\//gm,
replace: '',
},
],
export function makeCleanupPlugin() {
return cleanup({
// line endings are unix-ized by default
comments: 'all', // comments to keep
compactComments: 'false', // don't remove blank lines in multi-line comments
maxEmptyLines: 1,
extensions: ['js', 'jsx', 'ts', 'tsx'],
});
}

Expand Down
49 changes: 28 additions & 21 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -16871,6 +16871,15 @@ jquery@^3.5.0:
resolved "https://registry.yarnpkg.com/jquery/-/jquery-3.6.0.tgz#c72a09f15c1bdce142f49dbf1170bdf8adac2470"
integrity sha512-JVzAR/AjBvVt2BmYhxRCSYysDsPcssdmTFnzyLEts9qNwmjmu4JTAMYubEfwVOSwpQ1I1sKKFcxhZCI2buerfw==

js-cleanup@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/js-cleanup/-/js-cleanup-1.2.0.tgz#8dbc65954b1d38b255f1e8cf02cd17b3f7a053f9"
integrity sha512-JeDD0yiiSt80fXzAVa/crrS0JDPQljyBG/RpOtaSbyDq03VHa9szJWMaWOYU/bcTn412uMN2MxApXq8v79cUiQ==
dependencies:
magic-string "^0.25.7"
perf-regexes "^1.0.1"
skip-regex "^1.0.2"

js-reporters@1.2.3:
version "1.2.3"
resolved "https://registry.yarnpkg.com/js-reporters/-/js-reporters-1.2.3.tgz#8febcab370539df62e09b95da133da04b11f6168"
Expand Down Expand Up @@ -18122,13 +18131,6 @@ 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 @@ -20547,6 +20549,11 @@ pend@~1.2.0:
resolved "https://registry.yarnpkg.com/pend/-/pend-1.2.0.tgz#7a57eb550a6783f9115331fcf4663d5c8e007a50"
integrity sha1-elfrVQpng/kRUzH89GY9XI4AelA=

perf-regexes@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/perf-regexes/-/perf-regexes-1.0.1.tgz#6da1d62f5a94bf9353a0451bccacf69068b75d0b"
integrity sha512-L7MXxUDtqr4PUaLFCDCXBfGV/6KLIuSEccizDI7JxT+c9x1G1v04BQ4+4oag84SHaCdrBgQAIs/Cqn+flwFPng==

performance-now@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b"
Expand Down Expand Up @@ -22823,6 +22830,14 @@ ripemd160@^2.0.0, ripemd160@^2.0.1:
hash-base "^3.0.0"
inherits "^2.0.1"

rollup-plugin-cleanup@3.2.1:
version "3.2.1"
resolved "https://registry.yarnpkg.com/rollup-plugin-cleanup/-/rollup-plugin-cleanup-3.2.1.tgz#8cbc92ecf58babd7c210051929797f137bbf777c"
integrity sha512-zuv8EhoO3TpnrU8MX8W7YxSbO4gmOR0ny06Lm3nkFfq0IVKdBUtHwhVzY1OAJyNCIAdLiyPnOrU0KnO0Fri1GQ==
dependencies:
js-cleanup "^1.2.0"
rollup-pluginutils "^2.8.2"

rollup-plugin-license@^2.6.1:
version "2.6.1"
resolved "https://registry.yarnpkg.com/rollup-plugin-license/-/rollup-plugin-license-2.6.1.tgz#20f15cc37950f362f8eefdc6e3a2e659d0cad9eb"
Expand All @@ -22838,14 +22853,6 @@ 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 @@ -22876,7 +22883,7 @@ rollup-plugin-typescript2@^0.31.2:
resolve "^1.20.0"
tslib "^2.3.1"

rollup-pluginutils@^2.0.1:
rollup-pluginutils@^2.8.2:
version "2.8.2"
resolved "https://registry.yarnpkg.com/rollup-pluginutils/-/rollup-pluginutils-2.8.2.tgz#72f2af0748b592364dbd3389e600e5a9444a351e"
integrity sha512-EEp9NhnUkwY8aif6bxgovPHMoMoNr2FulJziTndpt5H9RdwC47GSGuII9XxpSdzVGM0GWrNPHV6ie1LTNJPaLQ==
Expand Down Expand Up @@ -23463,6 +23470,11 @@ size-limit@^4.5.5:
ora "^5.3.0"
read-pkg-up "^7.0.1"

skip-regex@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/skip-regex/-/skip-regex-1.0.2.tgz#ac655d77e7c771ac2b9f37585fea37bff56ad65b"
integrity sha512-pEjMUbwJ5Pl/6Vn6FsamXHXItJXSRftcibixDmNCWbWhic0hzHrwkMZo0IZ7fMRH9KxcWDFSkzhccB4285PutA==

slash@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/slash/-/slash-1.0.0.tgz#c41f2f6c39fc16d1cd17ad4b5d896114ae470d55"
Expand Down Expand Up @@ -26083,11 +26095,6 @@ 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

0 comments on commit 12c19d0

Please sign in to comment.