Skip to content

Commit 195df4e

Browse files
callmenickjoakimbeng
authored andcommitted
feat: support pug templates (PR #185, closes #181)
* adds pug tags to list * updates tag test files to include pug * updates inject tests to include pug files * adds fixtures and expected results for pug files * adds pug extensions for transform * updates transform tests to include pug * updates doc to include pug
1 parent 667ff63 commit 195df4e

File tree

10 files changed

+192
-3
lines changed

10 files changed

+192
-3
lines changed

README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
77
`gulp-inject` takes a stream of source files, transforms each file to a string and injects each transformed string into placeholders in the target stream files. See [Basic usage](#basic-usage) and [More examples](#more-examples) below.
88

9-
Default [transforms](#optionstransform) and [placeholders](#optionsstarttag) exists for injecting files into `html`, `jade`, `jsx` , `less`, `slm`, `haml` and `sass` / `scss` files.
9+
Default [transforms](#optionstransform) and [placeholders](#optionsstarttag) exists for injecting files into `html`, `jade`, `pug`, `jsx` , `less`, `slm`, `haml` and `sass` / `scss` files.
1010

1111
**Note:** As of `gulp-inject` v4.0.0, NodeJS v4 or above is required. To use `gulp-inject` for older versions of Node install a specific version: `npm install gulp-inject@3`.
1212

@@ -630,6 +630,7 @@ A function dependent on target file type and source file type that returns:
630630
* html as target: `<!-- {{name}}:{{ext}} -->`
631631
* haml as target: `-# {{name}}:{{ext}}`
632632
* jade as target: `//- {{name}}:{{ext}}`
633+
* pug as target: `//- {{name}}:{{ext}}`
633634
* jsx as target: `{/* {{name}}:{{ext}} */}`
634635
* slm as target: `/ {{name}}:{{ext}}`
635636
* less as target: `/* {{name}}:{{ext}} */`
@@ -655,6 +656,7 @@ A function dependent on target file type and source file type that returns:
655656
* html as target: `<!-- endinject -->`
656657
* haml as target: `-# endinject`
657658
* jade as target: `//- endinject`
659+
* pug as target: `//- endinject`
658660
* jsx as target: `{/* endinject */}`
659661
* slm as target: `/ endinject`
660662
* less as target: `/* endinject */`
@@ -707,6 +709,17 @@ The same as for injecting into `html` above with [`options.selfClosingTag`](#opt
707709
* jpg files: `img(src="<filename>.jpg")`
708710
* jpeg files: `img(src="<filename>.jpeg")`
709711

712+
**Injecting into `pug`**
713+
714+
* css files: `link(rel="stylesheet", href="<filename>.css")`
715+
* js files: `script(src="<filename>.js")`
716+
* coffee files: `script(type="text/coffeescript", src="<filename>.coffee")`
717+
* html files: `link(rel="import", href="<filename>.html")`
718+
* png files: `img(src="<filename>.png")`
719+
* gif files: `img(src="<filename>.gif")`
720+
* jpg files: `img(src="<filename>.jpg")`
721+
* jpeg files: `img(src="<filename>.jpeg")`
722+
710723
**Injecting into `slm`**
711724

712725
* css files: `link rel="stylesheet" href="<filename>.css"`
@@ -802,12 +815,16 @@ For more details see [the code with tests](https://github.com/klei/gulp-inject/t
802815

803816
#### inject.transform.html
804817

805-
The default transform function for files into `html`, or other file types not `jade`, `jsx`, `slm`, `less`, `scss`, `sass` or `haml`.
818+
The default transform function for files into `html`, or other file types not `jade`, `pug`, `jsx`, `slm`, `less`, `scss`, `sass` or `haml`.
806819

807820
#### inject.transform.jade
808821

809822
The default transform function for files into `jade`.
810823

824+
#### inject.transform.pug
825+
826+
The default transform function for files into `pug`.
827+
811828
#### inject.transform.jsx
812829

813830
The default transform function for files into `jsx`.

src/inject/expected/defaults.pug

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
doctype html
2+
html
3+
head
4+
title gulp-inject
5+
//- inject:html
6+
link(rel="import", href="/fixtures/component.html")
7+
//- endinject
8+
//- inject:css
9+
link(rel="stylesheet", href="/fixtures/styles.css")
10+
//- endinject
11+
body
12+
13+
//- inject:js
14+
script(src="/fixtures/lib.js")
15+
//- endinject

src/inject/expected/issue144.pug

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//- inject:js
2+
script(src="/fixtures/lib.js")
3+
//- endinject
4+
5+
//- inject:jsx
6+
script(type="text/jsx", src="/fixtures/component.jsx")
7+
//- endinject

src/inject/fixtures/issue144.pug

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//- inject:js
2+
//- endinject
3+
4+
//- inject:jsx
5+
//- endinject

src/inject/fixtures/template.pug

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
doctype html
2+
html
3+
head
4+
title gulp-inject
5+
//- inject:html
6+
//- endinject
7+
//- inject:css
8+
//- endinject
9+
body
10+
11+
//- inject:js
12+
//- endinject

src/inject/inject_test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,19 @@ describe('gulp-inject', function () {
284284
streamShouldContain(stream, ['defaults.jade'], done);
285285
});
286286

287+
it('should use special default tags when injecting into pug files', function (done) {
288+
var target = src(['template.pug'], {read: true});
289+
var sources = src([
290+
'lib.js',
291+
'component.html',
292+
'styles.css'
293+
]);
294+
295+
var stream = target.pipe(inject(sources));
296+
297+
streamShouldContain(stream, ['defaults.pug'], done);
298+
});
299+
287300
it('should be able to inject jsx into jade files (Issue #144)', function (done) {
288301
var target = src(['issue144.jade'], {read: true});
289302
var sources = src([
@@ -296,6 +309,18 @@ describe('gulp-inject', function () {
296309
streamShouldContain(stream, ['issue144.jade'], done);
297310
});
298311

312+
it('should be able to inject jsx into pug files (Issue #144)', function (done) {
313+
var target = src(['issue144.pug'], {read: true});
314+
var sources = src([
315+
'lib.js',
316+
'component.jsx'
317+
]);
318+
319+
var stream = target.pipe(inject(sources));
320+
321+
streamShouldContain(stream, ['issue144.pug'], done);
322+
});
323+
299324
it('should use special default tags when injecting into slm files', function (done) {
300325
var target = src(['template.slm'], {read: true});
301326
var sources = src([

src/tags/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var DEFAULTS = {
88
html: '<!-- {{name}}:{{ext}} -->',
99
jsx: '{/* {{name}}:{{ext}} */}',
1010
jade: '//- {{name}}:{{ext}}',
11+
pug: '//- {{name}}:{{ext}}',
1112
slm: '/ {{name}}:{{ext}}',
1213
slim: '/ {{name}}:{{ext}}',
1314
haml: '-# {{name}}:{{ext}}',
@@ -19,6 +20,7 @@ var DEFAULTS = {
1920
html: '<!-- endinject -->',
2021
jsx: '{/* endinject */}',
2122
jade: '//- endinject',
23+
pug: '//- endinject',
2224
slm: '/ endinject',
2325
slim: '/ endinject',
2426
haml: '-# endinject',

src/tags/tags_test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ describe('tags', function () {
3434
tags.start('jade', 'css').should.equal('//- {{name}}:{{ext}}');
3535
});
3636

37+
it('should return pug comments for pug target files', function () {
38+
tags.start('pug', 'css').should.equal('//- {{name}}:{{ext}}');
39+
});
40+
3741
it('should return slm comments for slm target files', function () {
3842
tags.start('slm').should.equal('/ {{name}}:{{ext}}');
3943
});
@@ -95,6 +99,10 @@ describe('tags', function () {
9599
tags.end('jade').should.equal('//- endinject');
96100
});
97101

102+
it('should return pug comments for pug target files', function () {
103+
tags.end('pug').should.equal('//- endinject');
104+
});
105+
98106
it('should return slm comments for slm target files', function () {
99107
tags.end('slm').should.equal('/ endinject');
100108
});

src/transform/index.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var extname = require('../extname');
44
/**
55
* Constants
66
*/
7-
var TARGET_TYPES = ['html', 'jade', 'slm', 'slim', 'jsx', 'haml', 'less', 'sass', 'scss'];
7+
var TARGET_TYPES = ['html', 'jade', 'pug', 'slm', 'slim', 'jsx', 'haml', 'less', 'sass', 'scss'];
88
var IMAGES = ['jpeg', 'jpg', 'png', 'gif'];
99
var DEFAULT_TARGET = TARGET_TYPES[0];
1010

@@ -98,6 +98,34 @@ transform.jade.image = function (filepath) {
9898
return 'img(src="' + filepath + '")';
9999
};
100100

101+
transform.pug.css = function (filepath) {
102+
return 'link(rel="stylesheet", href="' + filepath + '")';
103+
};
104+
105+
transform.pug.js = function (filepath) {
106+
return 'script(src="' + filepath + '")';
107+
};
108+
109+
transform.pug.jsx = function (filepath) {
110+
return 'script(type="text/jsx", src="' + filepath + '")';
111+
};
112+
113+
transform.pug.pug = function (filepath) {
114+
return 'include ' + filepath;
115+
};
116+
117+
transform.pug.html = function (filepath) {
118+
return 'link(rel="import", href="' + filepath + '")';
119+
};
120+
121+
transform.pug.coffee = function (filepath) {
122+
return 'script(type="text/coffeescript", src="' + filepath + '")';
123+
};
124+
125+
transform.pug.image = function (filepath) {
126+
return 'img(src="' + filepath + '")';
127+
};
128+
101129
transform.slm.css = function (filepath) {
102130
return 'link rel="stylesheet" href="' + filepath + '"';
103131
};

src/transform/transform_test.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ describe('transform', function () {
3131
transform.jade.should.be.type('function');
3232
});
3333

34+
it('should have a transform function for pug target files', function () {
35+
transform.pug.should.be.type('function');
36+
});
37+
3438
it('should have a transform function for slm target files', function () {
3539
transform.slm.should.be.type('function');
3640
});
@@ -234,6 +238,65 @@ describe('transform', function () {
234238
});
235239
});
236240

241+
describe('pug as target', function () {
242+
it('should transform css to a pug link tag', function () {
243+
transform.pug.css.should.be.type('function');
244+
transform.pug.css('test-file.css').should.equal('link(rel="stylesheet", href="test-file.css")');
245+
});
246+
247+
it('should transform pug to a pug include tag', function () {
248+
transform.pug.html.should.be.type('function');
249+
transform.pug.pug('test-file.pug').should.equal('include test-file.pug');
250+
});
251+
252+
it('should transform html to a self closing link tag', function () {
253+
transform.pug.html.should.be.type('function');
254+
transform.pug.html('test-file.html').should.equal('link(rel="import", href="test-file.html")');
255+
});
256+
257+
it('should transform javascript to a script tag', function () {
258+
transform.pug.js.should.be.type('function');
259+
transform.pug.js('test-file.js').should.equal('script(src="test-file.js")');
260+
});
261+
262+
it('should transform coffeescript to a script tag', function () {
263+
transform.pug.coffee.should.be.type('function');
264+
transform.pug.coffee('test-file.coffee').should.equal('script(type="text/coffeescript", src="test-file.coffee")');
265+
});
266+
267+
it('should transform an image to a self closing img tag', function () {
268+
transform.pug.image.should.be.type('function');
269+
transform.pug.image('test-file.png').should.equal('img(src="test-file.png")');
270+
});
271+
272+
it('should use the css transformer for css files automatically', function () {
273+
transform.pug('test-file.css').should.equal(transform.pug.css('test-file.css'));
274+
});
275+
276+
it('should use the pug transformer for pug files automatically', function () {
277+
transform.pug('test-file.pug').should.equal(transform.pug.pug('test-file.pug'));
278+
});
279+
280+
it('should use the html transformer for html files automatically', function () {
281+
transform.pug('test-file.html').should.equal(transform.pug.html('test-file.html'));
282+
});
283+
284+
it('should use the js transformer for js files automatically', function () {
285+
transform.pug('test-file.js').should.equal(transform.pug.js('test-file.js'));
286+
});
287+
288+
it('should use the coffee transformer for coffee files automatically', function () {
289+
transform.pug('test-file.coffee').should.equal(transform.pug.coffee('test-file.coffee'));
290+
});
291+
292+
it('should use the image transformer for png, gif, jpg and jpeg files automatically', function () {
293+
transform.pug('test-file.png').should.equal(transform.pug.image('test-file.png'));
294+
transform.pug('test-file.gif').should.equal(transform.pug.image('test-file.gif'));
295+
transform.pug('test-file.jpg').should.equal(transform.pug.image('test-file.jpg'));
296+
transform.pug('test-file.jpeg').should.equal(transform.pug.image('test-file.jpeg'));
297+
});
298+
});
299+
237300
describe('slm as target', function () {
238301
it('should transform css to a slm link tag', function () {
239302
transform.slm.css.should.be.type('function');
@@ -433,6 +496,13 @@ describe('transform', function () {
433496
.should.equal(transform.jade.image(sourceFile.path));
434497
});
435498

499+
it('should pick the correct target transformer for pug targets', function () {
500+
var targetFile = fixture('index.pug');
501+
var sourceFile = fixture('image.gif');
502+
transform(sourceFile.path, null, null, sourceFile, targetFile)
503+
.should.equal(transform.pug.image(sourceFile.path));
504+
});
505+
436506
it('should pick the correct target transformer for slm targets', function () {
437507
var targetFile = fixture('index.slm');
438508
var sourceFile = fixture('image.gif');

0 commit comments

Comments
 (0)