Coder Social home page Coder Social logo

amarcruz / rollup-plugin-cleanup Goto Github PK

View Code? Open in Web Editor NEW
100.0 4.0 5.0 449 KB

Rollup plugin to remove comments, trim trailing spaces, compact empty lines, and normalize line endings

License: MIT License

JavaScript 97.27% Makefile 2.73%
rollup rollup-plugin javascript es2015 modules cleanup filter

rollup-plugin-cleanup's Issues

Doesn't work for d.ts files

For some reason, it didn't work for d.ts files (output.file = '*.d.ts') with folowing options:

cleanup({
    comments: 'none',
    extensions: ['ts', 'd.ts']
}),

Oddly formatted Turf.js jsDoc comments not removed

I have no idea why, but Turf.js's jsDoc comments have multiple initial spaces after the initial /**:

/**
     * Earth Radius used with the Harvesine formula and approximates using a spherical (non-ellipsoid) Earth.
     *
     * @memberof helpers
     * @type {number}
     */

This causes cleanup({ comments: 'none' }), to not remove them.

Is there a reasonable way for cleanup() to handle this?

Or could you post a regex that just might work? I tried it but my regex fu is lacking! :)

Using hooks as filters

For example:

jsCleanup(code, null, {
    comments: [
        (commentContent) => {
            return commentContent.includes("preserve");
        }
    ]
})

This allows more granular control over which comments to keep and which ones to discard.

I think I might be able to do a PR if you like.

Errors out on spread operator

[!] (cleanup plugin) SyntaxError: Unexpected token (14:18)

Line of the file:
info.push({ ...profiles.splice(pos, 1)[0].output, index });

Char 18 is the space before the three dots...

Rollup.js: cleanup didn't generate a sourcemap for the transformation

I am using cleanup for four rollup'ed files in the same project; they share cleanup logic by importing the same configuration. Two of the files worked as intended, two gave the same sourcemap warning. Here's my cleanup configuration.

cleanup({
    maxEmptyLines: -1,
    comments: [
        // NOTE: remove per-file copyright notices. It will be re-added as a single banner.
        // NOTE: keeping everything else not matching.
        /^(?!\*\s*This file is part of Talkie)/,
    ],
})

Narrowed down the issue to having files missing the (correctly formatted) license notice, which was of course positive as it's an improvement for my code.

As my regexp is basically inverted, blocking if the pattern is found, it meant that if all comments matched, then the below warning was given. Does that make sense? Either way, this shows that there are cases where cleanup will generate the below sourcemap warning.

See the original files in the Talkie, text-to-speech for the browser and specifically look at the commit adding cleanup as well as the commit fixing the warning by formatting/adding the copyright notice.


Original rollup.js warning:

⚠️ Sourcemap is likely to be incorrect: a plugin ('cleanup') was used to transform files, but didn't generate a sourcemap for the transformation. Consult the plugin documentation for help
https://github.com/rollup/rollup/wiki/Troubleshooting#sourcemap-is-likely-to-be-incorrect

From the documentation in the link:

You'll see this warning if you generate a sourcemap with your bundle (sourceMap: true or sourceMap: 'inline') but you're using one or more plugins that transformed code without generating a sourcemap for the transformation.

Usually, a plugin will only omit the sourcemap if it (the plugin, not the bundle) was configured with sourceMap: false – so all you need to do is change that. If the plugin doesn't generate a sourcemap, consider raising an issue with the plugin author.

Maintain comments starting with /*!

It's normal for post-processors working on code to maintain comment blocks which start with /*! (note the !). Take a look as jQuery sizzle or other popular libraries, and you'll see this. Please consider adding or just close this. Thanks!

Empty line after deleting comment

Is it possible to remove an empty line that appears after removing a comment?

Consider config:

cleanup({
  comments: [/^\*-/],
  maxEmptyLines: 1
})

Code:

const helpers = {
/**
 * Very big comment explaning method purpose
 */
 findThing() { ... }
}

And result is:

const helpers = {

 findThing() { ... }
}

blank lines within template literals are erroneously removed

Thanks a lot for this plugin; we're happily using it for faucet-pipeline.

However, I've noticed that it swallows blank lines within template literals:

// index.js
let txt = `foo

bar`;

console.log(txt);
// rollup.config.js
import { rollup } from "rollup";
import cleanup from "rollup-plugin-cleanup";

export default {
    input: "index.js",
    output: {
        file: "bundle.js",
        format: "cjs"
    },
    plugins: [
        cleanup()
    ]
};

rollup -c rollup.config.js generates the following bundle.js:

'use strict';

let txt = `foo
bar`;
console.log(txt);

As you can see, the string's blank line has been removed. cleanup({ maxEmptyLines: -1 }) avoids this issue.

(Tested with Rollup v0.56.3 and rollup-plugin-cleanup v2.0.0 as well as v0.1.0.)

feature request: removing arbitrary chunks of code

hello!
First of all thanks for this plugin.
It's awesome but sometimes my console.logs need an extra calculation and I need a way to delete those lines of code too.

Solution would be either

  • adding an extra options item where user could provide regex and this plugin would delete all findings of that regex, OR
  • at least proprietary, hard-coded comment in the code à la //----CLEANUP DELETE FROM and //----CLEANUP DELETE TO

...or something else...

But I'd prefer the regex way.

Maintainers, would you consider adding such feature?

comments: 'none' not removing all comments

My plugins section looks like this:

plugins: [
    typescript({
      typescript: require('typescript'),
      importHelpers: true
    }),
    hypothetical({
      allowFallthrough: true
    }),
    resolve(),
    commonJS({
      include: 'node_modules/**'
    }),
    buble(),
    cleanup({
      comments: 'none'
    })
  ]

but in the output code I have things like this:

  // check type
...
  /**
   * blablabla
   */

I basically want to remove any kind of comment, is it possible with this plugin?

Not cleaning up properly /**/ comments

input examples:

   return function (/* ...args */) {
    return fn.apply(that, arguments);
  };
 ....
 catch (e) { /* empty */ }

output:

  return function (             ) {
    return fn.apply(that, arguments);
  };
 ....
 catch (e) {             }

expected:

  return function () {
    return fn.apply(that, arguments);
  };
  ....
  catch (e) {}

maxEmptyLines doesn't seem to work

Hi,

I'm trying to use your plugin,
cleaning comments seems to work, however, maxEmptyLines doesn't.
tried to use both 0 and then 1, however the output is the same. (10+ empty lines)

Thanks!

rollup hang up when running with this plugin in node.js

(async function build() {
    let b = await rollup.rollup({
        input: 'absolute_path_entry.js',
        plugins: [rollupCleanup()],
    }); // hang up here

    let out = {
        format: 'esm',
        sourcemap: true,
        file: path.resolve(__dirname, "dist/test.js"),
    };

    await b.generate(out);
    await b.write(out);
})();

Pass more options to acorn parser

I am trying to 'rollup' the monaco-editor. I get an error with cleanup,

node_modules/monaco-editor/esm/vs/language/typescript/monaco.contribution.js
SyntaxError: Unexpected token (157:31)
at Parser.pp$4.raise (.../node_modules/acorn/dist/acorn.js:2756:13)

the offending line in monaco.contribution.js is

return monaco.Promise.wrap(import('./tsMode.js'));

i.e. use of an import statement.

the documentation for the acorn parser options has an entry

allowImportExportEverywhere: By default, import and export declarations can only appear at a program's top level. Setting this option to true allows them anywhere where a statement is allowed.

I think I need to be able to set this option, and maybe some others.

'lineEndings: "mac"' disrupts generated sourcemap

Building a chromium browser extension, and as in the title, if I use the option lineEndings: "mac", the generated sourcemap no longer works - all line numbers in the console refer to the generated file rather than the source.

Not a dealbreaker - for now I've just turned the option off and made peace with my generated bundled output file looking a bit strange, but at least I can pinpoint errors and logged data correctly while building and testing.

Fails to process jsx file if there are multiple file extensions in the extensions array

For example, while processing this file:

import React, { Component } from 'react';

export default class App extends Component {
    render() {
        return (
            <h1>Some super important stuff.</h1>
        );
    }
}

with the options set like so:

cleanup({
    extensions: ['.jsx']
})

it works fine. But if I add .js:

cleanup({
    extensions: ['.js', '.jsx']
})

It fails to compile and complains about an unexpected token (the jsx)

Using Acorn plugins doesn't work as specified by README

Hey there, I'm having trouble using the acorn-dynamic-import plugin using the example in your README (related #11).

First off, the following line is no longer accurate:

import inject from 'acorn-dynamic-import/lib/inject'

It throws Error: Cannot find module 'acorn-dynamic-import/lib/inject', seemingly because the file was renamed to lib/walk. That can be fixed by updating the import to the following:

import inject from 'acorn-dynamic-import/lib/walk'

However, now I'm getting the following error:

(cleanup plugin) Error: Plugin 'dynamicImport' not found

After doing some digging, it looks like this is the result of a pre-existing issue with acorn: acornjs/acorn#746. The closure compiler Rollup plugin ran into the same problem (ampproject/rollup-plugin-closure-compiler#48), so maybe their solution could be useful.

Steps to Reproduce

  1. Clone the following repo (make sure branch is correct): https://github.com/regal/regal-bundler/tree/bug-repro/rollup-plugin-cleanup/1
  2. npm install
  3. npm run build

Full rollup.config.js:

import typescript from "rollup-plugin-typescript2";
import replace from "rollup-plugin-re";
import cleanup from "rollup-plugin-cleanup";
import json from "rollup-plugin-json";
import acorn from "acorn";
import inject from "acorn-dynamic-import/lib/walk";

import pkg from "./package.json";

const banner = `/**
 * Regal bundler source.
 *
 * Copyright (c) Joe Cowman
 * Licensed under MIT License (see https://github.com/regal/regal-bundler)
 */`

/** Suppress Rollup's circular import warnings for TypeScript files */
const suppressCircularImportWarnings = (message, defaultFunc) => {
    if (message.code === "CIRCULAR_DEPENDENCY") {
        return;
    }
    defaultFunc(message);
}

const tsPlugin = typescript({
    tsconfigOverride: {
        compilerOptions: { module: "esNext" }
    }
});

inject(acorn);

export default [
    {
        input: "./src/index.ts",
        output: [
            { file: pkg.main, format: "cjs", banner },
            { file: pkg.module, format: "esm", banner }
        ],
        external: Object.keys(pkg.dependencies),
        plugins: [
            replace({
                exclude: "node_modules/**",
                replaces: {
                    'import \* as cosmiconfig from "cosmiconfig";': 'import cosmiconfig from "cosmiconfig";',
                    'import \* as filenamify from "filenamify";': 'import filenamify from "filenamify";',
                }
            }),
            tsPlugin,
            json(),
            cleanup({
                extensions: [".js", ".ts"],
                comments: /^((?!(Joseph R Cowman)|tslint)[\s\S])*$/, // Removes file-header comments and tslint comments
                maxEmptyLines: 0,
                acornOptions: {
                    plugins: {
                        dynamicImport: true
                    }
                }
            })
        ],
        onwarn: suppressCircularImportWarnings
    }
]

Full error:

./src/index.ts → dist/regal-bundler.cjs.js, dist/regal-bundler.esm.js...
[!] (cleanup plugin) Error: Plugin 'dynamicImport' not found
src\index.ts
Error: Plugin 'dynamicImport' not found
    at Parser.loadPlugins (C:\Users\Joe\Desktop\Projects\regal\regal-bundler\node_modules\rollup-plugin-cleanup\node_modules\acorn\dist\acorn.js:543:26)
    at new Parser (C:\Users\Joe\Desktop\Projects\regal\regal-bundler\node_modules\rollup-plugin-cleanup\node_modules\acorn\dist\acorn.js:469:8)
    at Object.parse (C:\Users\Joe\Desktop\Projects\regal\regal-bundler\node_modules\rollup-plugin-cleanup\node_modules\acorn\dist\acorn.js:5288:10)
    at blankComments (C:\Users\Joe\Desktop\Projects\regal\regal-bundler\node_modules\rollup-plugin-cleanup\dist\rollup-plugin-cleanup.js:177:9)
    at resolve (C:\Users\Joe\Desktop\Projects\regal\regal-bundler\node_modules\rollup-plugin-cleanup\dist\rollup-plugin-cleanup.js:315:14)
    at new Promise (<anonymous>)
    at cleanup (C:\Users\Joe\Desktop\Projects\regal\regal-bundler\node_modules\rollup-plugin-cleanup\dist\rollup-plugin-cleanup.js:308:10)
    at Object.transform (C:\Users\Joe\Desktop\Projects\regal\regal-bundler\node_modules\rollup-plugin-cleanup\dist\rollup-plugin-cleanup.js:356:16)
    at C:\Users\Joe\Desktop\Projects\regal\regal-bundler\node_modules\rollup\dist\rollup.js:20962:25
    at <anonymous>

Version 3.1.0 fails for certain template literals

Version 3.1.0 of the plugin fails if it encounters the following code.

const v = 10;
const s = `${v}$`;

console.log(s); // prints: 10$
index.js → output.js...
[!] (cleanup plugin) Error: Unclosed ES6 Template Literal.
index.js (2:14)
1: const v = 10;
2: const s = `${v}$`;
                 ^
3:
4: console.log(s);
Error: Unclosed ES6 Template Literal.
    at error (C:\Users\stotter\temp\cleanup-test\node_modules\rollup\dist\rollup.js:3597:30)
    at Object.error (C:\Users\stotter\temp\cleanup-test\node_modules\rollup\dist\rollup.js:16497:17)
    at Object.error (C:\Users\stotter\temp\cleanup-test\node_modules\rollup\dist\rollup.js:16906:31)
    at Promise (C:\Users\stotter\temp\cleanup-test\node_modules\rollup-plugin-cleanup\dist\rollup-plugin-cleanup.js:119:20)
    at new Promise (<anonymous>)
    at Object.transform (C:\Users\stotter\temp\cleanup-test\node_modules\rollup-plugin-cleanup\dist\rollup-plugin-cleanup.js:113:16)
    at C:\Users\stotter\temp\cleanup-test\node_modules\rollup\dist\rollup.js:16609:25
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:189:7)
    at Function.Module.runMain (module.js:696:11)

It worked fine for version 3.0.0, so this might be related to the switch from acorn to js-cleanup.

The code above works fine in firefox, chrome and node.

I've created a small repo to reproduce the error here: https://github.com/stotter/cleanup-test

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.