From 55c26d2790b8502f115180cfe02aba5720c84b7b Mon Sep 17 00:00:00 2001 From: Floriel Date: Wed, 23 Feb 2022 19:19:54 +0000 Subject: [PATCH] feat(gulp-purgecss): add support for gulp-sourcemaps #257 --- package-lock.json | 32 +++++++++++++++++++++++++ packages/gulp-purgecss/package.json | 3 ++- packages/gulp-purgecss/src/index.ts | 18 +++++++++++--- packages/gulp-purgecss/tsconfig.json | 5 ++-- types/vinyl-sourcemaps-apply/index.d.ts | 1 + 5 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 types/vinyl-sourcemaps-apply/index.d.ts diff --git a/package-lock.json b/package-lock.json index 77dd0443..c26a45a8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -48,6 +48,7 @@ "through2": "^4.0.1", "twing": "^5.1.0", "vinyl": "^2.2.0", + "vinyl-sourcemaps-apply": "^0.2.1", "webpack": ">=5.0.0" }, "devDependencies": { @@ -15338,6 +15339,22 @@ "node": ">= 0.10" } }, + "node_modules/vinyl-sourcemaps-apply": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/vinyl-sourcemaps-apply/-/vinyl-sourcemaps-apply-0.2.1.tgz", + "integrity": "sha1-q2VJ1h0XLCsbh75cUI0jnI74dwU=", + "dependencies": { + "source-map": "^0.5.1" + } + }, + "node_modules/vinyl-sourcemaps-apply/node_modules/source-map": { + "version": "0.5.7", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.5.7.tgz", + "integrity": "sha1-igOdLRAh0i0eoUyA2OpGi6LvP8w=", + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/vinyl/node_modules/clone": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/clone/-/clone-2.1.2.tgz", @@ -28111,6 +28128,21 @@ } } }, + "vinyl-sourcemaps-apply": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/vinyl-sourcemaps-apply/-/vinyl-sourcemaps-apply-0.2.1.tgz", + "integrity": "sha1-q2VJ1h0XLCsbh75cUI0jnI74dwU=", + "requires": { + "source-map": "^0.5.1" + }, + "dependencies": { + "source-map": { + "version": "0.5.7", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.5.7.tgz", + "integrity": "sha1-igOdLRAh0i0eoUyA2OpGi6LvP8w=" + } + } + }, "vite": { "version": "2.8.4", "resolved": "https://registry.npmjs.org/vite/-/vite-2.8.4.tgz", diff --git a/packages/gulp-purgecss/package.json b/packages/gulp-purgecss/package.json index f453c8b3..3db982cc 100644 --- a/packages/gulp-purgecss/package.json +++ b/packages/gulp-purgecss/package.json @@ -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" diff --git a/packages/gulp-purgecss/src/index.ts b/packages/gulp-purgecss/src/index.ts index 44bddeff..f0ad2c37 100644 --- a/packages/gulp-purgecss/src/index.ts +++ b/packages/gulp-purgecss/src/index.ts @@ -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 }; @@ -17,10 +18,10 @@ function getFiles(contentArray: string[]): string[] { } /** - * + * * @param options - options - * @returns - * + * @returns + * * @public */ function gulpPurgeCSS(options: UserDefinedOptions): internal.Transform { @@ -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]; @@ -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) { @@ -70,6 +77,7 @@ function gulpPurgeCSS(options: UserDefinedOptions): internal.Transform { raw: css, }, ], + sourceMap: !!file.sourceMap }; const purgedCSSResults = await new PurgeCSS().purge(optionsGulp); @@ -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) { diff --git a/packages/gulp-purgecss/tsconfig.json b/packages/gulp-purgecss/tsconfig.json index 9b6f4c1c..3e40fbc6 100644 --- a/packages/gulp-purgecss/tsconfig.json +++ b/packages/gulp-purgecss/tsconfig.json @@ -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"] } \ No newline at end of file diff --git a/types/vinyl-sourcemaps-apply/index.d.ts b/types/vinyl-sourcemaps-apply/index.d.ts new file mode 100644 index 00000000..f9846d69 --- /dev/null +++ b/types/vinyl-sourcemaps-apply/index.d.ts @@ -0,0 +1 @@ +declare module 'vinyl-sourcemaps-apply'; \ No newline at end of file