Skip to content

Commit

Permalink
feat(gulp-purgecss): add support for gulp-sourcemaps #257
Browse files Browse the repository at this point in the history
  • Loading branch information
Ffloriel committed Feb 23, 2022
1 parent b3f73ed commit 55c26d2
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 6 deletions.
32 changes: 32 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/gulp-purgecss/package.json
Expand Up @@ -47,7 +47,8 @@
"@types/through2": "^2.0.34",
"@types/vinyl": "^2.0.4",
"event-stream": "^4.0.1",
"vinyl": "^2.2.0"
"vinyl": "^2.2.0",
"vinyl-sourcemaps-apply": "^0.2.1"
},
"bugs": {
"url": "https://github.com/FullHuman/purgecss/issues"
Expand Down
18 changes: 15 additions & 3 deletions packages/gulp-purgecss/src/index.ts
Expand Up @@ -4,6 +4,7 @@ import { PurgeCSS } from "purgecss";
import * as internal from "stream";
import through from "through2";
import VinylFile from "vinyl";
import applySourceMap from "vinyl-sourcemaps-apply";
import { UserDefinedOptions } from "./types";

export { UserDefinedOptions };
Expand All @@ -17,10 +18,10 @@ function getFiles(contentArray: string[]): string[] {
}

/**
*
*
* @param options - options
* @returns
*
* @returns
*
* @public
*/
function gulpPurgeCSS(options: UserDefinedOptions): internal.Transform {
Expand All @@ -39,6 +40,7 @@ function gulpPurgeCSS(options: UserDefinedOptions): internal.Transform {
},
],
stdin: true,
sourceMap: !!file.sourceMap
};
const purgedCSSResults = await new PurgeCSS().purge(optionsGulp);
const purge = purgedCSSResults[0];
Expand All @@ -47,6 +49,11 @@ function gulpPurgeCSS(options: UserDefinedOptions): internal.Transform {
? purge.rejected.join(" {}\n") + " {}"
: purge.css;
file.contents = Buffer.from(result, "utf-8");

// apply source map to the chain
if (file.sourceMap) {
applySourceMap(file, purge.sourceMap);
}
callback(null, file);
} catch (e: unknown) {
if (e instanceof Error) {
Expand All @@ -70,6 +77,7 @@ function gulpPurgeCSS(options: UserDefinedOptions): internal.Transform {
raw: css,
},
],
sourceMap: !!file.sourceMap
};

const purgedCSSResults = await new PurgeCSS().purge(optionsGulp);
Expand All @@ -82,6 +90,10 @@ function gulpPurgeCSS(options: UserDefinedOptions): internal.Transform {
const streamResult = through();
streamResult.write(Buffer.from(result, "utf-8"));
file.contents = file.contents.pipe(streamResult);
// apply source map to the chain
if (file.sourceMap) {
applySourceMap(file, purge.sourceMap);
}
callback(null, file);
} catch (e: unknown) {
if (e instanceof Error) {
Expand Down
5 changes: 3 additions & 2 deletions packages/gulp-purgecss/tsconfig.json
Expand Up @@ -7,8 +7,9 @@
"baseUrl": ".",
"paths": {
"purgecss": ["../purgecss/src"],
}
},
"typeRoots": ["./../../types", "./../../node_modules/@types"]
},
"include": ["./src/**/*", "./rollup.config.ts", "./../purgecss/src/*"],
"include": ["src", "./../purgecss/src/*", "./../../types/**/*"],
"exclude": ["./lib"]
}
1 change: 1 addition & 0 deletions types/vinyl-sourcemaps-apply/index.d.ts
@@ -0,0 +1 @@
declare module 'vinyl-sourcemaps-apply';

0 comments on commit 55c26d2

Please sign in to comment.