Skip to content

CLI throws TypeError when used with --safelist and doesn't purge #513

@dwbruhn

Description

@dwbruhn

This is pretty simple to reproduce with version 3.0.0.

Create a test.js file:

var a = "class1";

And a test.css file:

.class1 {
    color: blue;
}

.class2 {
    color:red;
}

.class3 {
    color: green;
}

Then run purgecss --content test.js --css test.css --safelist class2 (as documented):

$ npx purgecss --content test.js --css test.css --safelist class2                                                                                                                                      [14:28:56]
[{"css":".class1 {\n    color: blue;\n}\n\n.class2 {\n    color:red;\n}\n\n.class3 {\n    color: green;\n}\n","file":"test.css"}]
(node:15305) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'length' of undefined
    at U.shouldKeepSelector (/Users/dbruhn/repos/temp/node_modules/purgecss/lib/purgecss.js:1:9032)

The output is created despite the error, but all classes are preserved. (class3 should have been purged.)

Purging works fine without the whitelist:

$ npx purgecss --content test.js --css test.css                                                                                                                                                        [14:28:59]
[{"css":".class1 {\n    color: blue;\n}\n","file":"test.css"}]

The code is throwing because this.options.safelist.greedy is undefined:

if (this.options.safelist.greedy.length > 0) {

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions