Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional name for RawCSS option #703

Merged
merged 1 commit into from
Jul 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions packages/purgecss/__tests__/raw-css-name.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import PurgeCSS from "../src/index";

describe("Raw CSS optional filename", () => {
it("Should return the `name` in the result's `file` property when provided for raw CSS option", async () => {
return new PurgeCSS()
.purge({
content: [
{ raw: "<html><body><h1></h1></body></html>", extension: "html" },
],
css: [
{ raw: "body{margin:0;}", name: "test.css" },
{ raw: "h1{margin:1;}" },
],
})
.then((results) => {
expect(results.length).toBe(2);
expect(
results.some((result) => {
return result.file && result.file.endsWith("test.css");
})
).toBe(true);
results.forEach((result) => expect(typeof result.css).toBe("string"));
});
});
it("Should NOT return the `name` in the result's `file` property when NOT provided for raw CSS option", async () => {
return new PurgeCSS()
.purge({
content: [
{ raw: "<html><body><h1></h1></body></html>", extension: "html" },
],
css: [{ raw: "body{margin:0;}" }],
})
.then((results) => {
expect(results.length).toBe(1);
expect(results[0].file).toBe(undefined);
results.forEach((result) => expect(typeof result.css).toBe("string"));
});
});
});
6 changes: 1 addition & 5 deletions packages/purgecss/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -542,13 +542,9 @@ class PurgeCSS {

const result: ResultPurge = {
css: root.toString(),
file: typeof option === "string" ? option : undefined,
file: typeof option === "string" ? option : option.name,
};

if (typeof option === "string") {
result.file = option;
}

if (this.options.rejected) {
result.rejected = Array.from(this.selectorsRemoved);
this.selectorsRemoved.clear();
Expand Down
1 change: 1 addition & 0 deletions packages/purgecss/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface RawContent<T = string> {

export interface RawCSS {
raw: string;
name?: string;
}

export interface ExtractorResultDetailed {
Expand Down