11import { Buffer } from 'node:buffer' ;
22import path from 'node:path' ;
3- import transformStream from 'easy-transform-stream' ;
43import { vinylFile } from 'vinyl-file' ;
54import revHash from 'rev-hash' ;
65import { revPath } from 'rev-path' ;
76import sortKeys from 'sort-keys' ;
87import modifyFilename from 'modify-filename' ;
98import Vinyl from 'vinyl' ;
10- import PluginError from 'plugin-error ' ;
9+ import { gulpPlugin } from 'gulp- plugin-extras ' ;
1110
1211function relativePath ( base , filePath ) {
13- filePath = filePath . replace ( / \\ / g , '/' ) ;
14- base = base . replace ( / \\ / g , '/' ) ;
12+ filePath = filePath . replaceAll ( '\\' , '/' ) ;
13+ base = base . replaceAll ( '\\' , '/' ) ;
1514
1615 if ( ! filePath . startsWith ( base ) ) {
1716 return filePath ;
@@ -55,20 +54,12 @@ const getManifestFile = async options => {
5554 }
5655} ;
5756
58- const plugin = ( ) => {
57+ export default function gulpRev ( ) {
5958 const sourcemaps = [ ] ;
6059 const pathMap = { } ;
6160
62- return transformStream ( { objectMode : true } , file => {
63- if ( file . isNull ( ) ) {
64- return file ;
65- }
66-
67- if ( file . isStream ( ) ) {
68- throw new PluginError ( 'gulp-rev' , 'Streaming not supported' ) ;
69- }
70-
71- // This is a sourcemap, hold until the end
61+ return gulpPlugin ( 'gulp-rev' , file => {
62+ // This is a sourcemap, hold until the end.
7263 if ( path . extname ( file . path ) === '.map' ) {
7364 sourcemaps . push ( file ) ;
7465 return ;
@@ -79,40 +70,38 @@ const plugin = () => {
7970 pathMap [ oldPath ] = file . revHash ;
8071
8172 return file ;
82- } , ( ) => {
83- const files = [ ] ;
84-
85- for ( const file of sourcemaps ) {
86- let reverseFilename ;
87-
88- // Attempt to parse the sourcemap's JSON to get the reverse filename
89- try {
90- reverseFilename = JSON . parse ( file . contents . toString ( ) ) . file ;
91- } catch { }
92-
93- if ( ! reverseFilename ) {
94- reverseFilename = path . relative ( path . dirname ( file . path ) , path . basename ( file . path , '.map' ) ) ;
73+ } , {
74+ async * onFinish ( ) {
75+ for ( const file of sourcemaps ) {
76+ let reverseFilename ;
77+
78+ // Attempt to parse the sourcemap's JSON to get the reverse filename
79+ try {
80+ reverseFilename = JSON . parse ( file . contents . toString ( ) ) . file ;
81+ } catch { }
82+
83+ if ( ! reverseFilename ) {
84+ reverseFilename = path . relative ( path . dirname ( file . path ) , path . basename ( file . path , '.map' ) ) ;
85+ }
86+
87+ if ( pathMap [ reverseFilename ] ) {
88+ // Save the old path for later
89+ file . revOrigPath = file . path ;
90+ file . revOrigBase = file . base ;
91+
92+ const hash = pathMap [ reverseFilename ] ;
93+ file . path = revPath ( file . path . replace ( / \. m a p $ / , '' ) , hash ) + '.map' ;
94+ } else {
95+ transformFilename ( file ) ;
96+ }
97+
98+ yield file ;
9599 }
96-
97- if ( pathMap [ reverseFilename ] ) {
98- // Save the old path for later
99- file . revOrigPath = file . path ;
100- file . revOrigBase = file . base ;
101-
102- const hash = pathMap [ reverseFilename ] ;
103- file . path = revPath ( file . path . replace ( / \. m a p $ / , '' ) , hash ) + '.map' ;
104- } else {
105- transformFilename ( file ) ;
106- }
107-
108- files . push ( file ) ;
109- }
110-
111- return files ;
100+ } ,
112101 } ) ;
113- } ;
102+ }
114103
115- plugin . manifest = ( path_ , options ) => {
104+ gulpRev . manifest = ( path_ , options ) => {
116105 if ( typeof path_ === 'string' ) {
117106 path_ = { path : path_ } ;
118107 }
@@ -127,38 +116,38 @@ plugin.manifest = (path_, options) => {
127116
128117 let manifest = { } ;
129118
130- return transformStream ( { objectMode : true } , file => {
119+ return gulpPlugin ( 'gulp-rev' , file => {
131120 // Ignore all non-rev'd files
132121 if ( ! file . path || ! file . revOrigPath ) {
133122 return ;
134123 }
135124
136125 const revisionedFile = relativePath ( path . resolve ( file . cwd , file . base ) , path . resolve ( file . cwd , file . path ) ) ;
137- const originalFile = path . join ( path . dirname ( revisionedFile ) , path . basename ( file . revOrigPath ) ) . replace ( / \\ / g , '/' ) ;
126+ const originalFile = path . join ( path . dirname ( revisionedFile ) , path . basename ( file . revOrigPath ) ) . replaceAll ( '\\' , '/' ) ;
138127
139128 manifest [ originalFile ] = revisionedFile ;
140- } , async function * ( ) {
141- // No need to write a manifest file if there's nothing to manifest
142- if ( Object . keys ( manifest ) . length === 0 ) {
143- return ;
144- }
129+ } , {
130+ async * onFinish ( ) {
131+ // No need to write a manifest file if there's nothing to manifest
132+ if ( Object . keys ( manifest ) . length === 0 ) {
133+ return ;
134+ }
145135
146- const manifestFile = await getManifestFile ( options ) ;
136+ const manifestFile = await getManifestFile ( options ) ;
147137
148- if ( options . merge && ! manifestFile . isNull ( ) ) {
149- let oldManifest = { } ;
138+ if ( options . merge && ! manifestFile . isNull ( ) ) {
139+ let oldManifest = { } ;
150140
151- try {
152- oldManifest = options . transformer . parse ( manifestFile . contents . toString ( ) ) ;
153- } catch { }
141+ try {
142+ oldManifest = options . transformer . parse ( manifestFile . contents . toString ( ) ) ;
143+ } catch { }
154144
155- manifest = Object . assign ( oldManifest , manifest ) ;
156- }
145+ manifest = Object . assign ( oldManifest , manifest ) ;
146+ }
157147
158- manifestFile . contents = Buffer . from ( options . transformer . stringify ( sortKeys ( manifest ) , undefined , ' ' ) ) ;
148+ manifestFile . contents = Buffer . from ( options . transformer . stringify ( sortKeys ( manifest ) , undefined , ' ' ) ) ;
159149
160- yield manifestFile ;
150+ yield manifestFile ;
151+ } ,
161152 } ) ;
162153} ;
163-
164- export default plugin ;
0 commit comments