Skip to content

Commit

Permalink
Require Node.js 18 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Nov 1, 2023
1 parent 55805ee commit bb97b27
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 261 deletions.
3 changes: 1 addition & 2 deletions .gitattributes
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
21 changes: 21 additions & 0 deletions .github/workflows/main.yml
@@ -0,0 +1,21 @@
name: CI
on:
- push
- pull_request
jobs:
test:
name: Node.js ${{ matrix.node-version }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version:
- 20
- 18
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
5 changes: 0 additions & 5 deletions .travis.yml

This file was deleted.

43 changes: 14 additions & 29 deletions index.js
@@ -1,34 +1,19 @@
'use strict';
const PluginError = require('plugin-error');
const through = require('through2');
const _ = require('lodash');
const Buffer = require('safe-buffer').Buffer;

const template = _.template;
import {Buffer} from 'node:buffer';
import _ from 'lodash';
import {gulpPlugin} from 'gulp-plugin-extras';

function compile(options, data, render) {
return through.obj(function (file, enc, cb) {
if (file.isNull()) {
cb(null, file);
return;
}

if (file.isStream()) {
cb(new PluginError('gulp-template', 'Streaming not supported'));
return;
}

try {
const tpl = template(file.contents.toString(), options);
file.contents = Buffer.from(render ? tpl(_.merge({}, file.data, data)) : tpl.toString());
this.push(file);
} catch (err) {
this.emit('error', new PluginError('gulp-template', err, {fileName: file.path}));
}

cb();
return gulpPlugin('gulp-template', file => {
const template = _.template(file.contents.toString(), options);
file.contents = Buffer.from(render ? template(_.merge({}, file.data, data)) : template.toString());
return file;
});
}

module.exports = (data, options) => compile(options, data, true);
module.exports.precompile = options => compile(options);
export default function gulpTemplate(data, options) {
return compile(options, data, true);
}

export function precompile(options) {
return compile(options);
}
2 changes: 1 addition & 1 deletion license
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
31 changes: 20 additions & 11 deletions package.json
Expand Up @@ -4,16 +4,19 @@
"description": "Render/precompile Lodash/Underscore templates",
"license": "MIT",
"repository": "sindresorhus/gulp-template",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=4"
"node": ">=18"
},
"scripts": {
"test": "xo && mocha"
"test": "xo && ava"
},
"files": [
"index.js"
Expand All @@ -30,15 +33,21 @@
"precompile"
],
"dependencies": {
"lodash": "^4.8.2",
"plugin-error": "^0.1.2",
"safe-buffer": "^5.1.1",
"through2": "^2.0.0"
"gulp-plugin-extras": "^0.2.2",
"lodash": "^4.17.21"
},
"devDependencies": {
"gulp-data": "^1.0.2",
"mocha": "*",
"vinyl": "^2.1.0",
"xo": "*"
"ava": "^5.3.1",
"gulp-data": "^1.3.1",
"vinyl": "^3.0.0",
"xo": "^0.56.0"
},
"peerDependencies": {
"gulp": ">=4"
},
"peerDependenciesMeta": {
"gulp": {
"optional": true
}
}
}
66 changes: 43 additions & 23 deletions readme.md
@@ -1,16 +1,14 @@
# gulp-template [![Build Status](https://travis-ci.org/sindresorhus/gulp-template.svg?branch=master)](https://travis-ci.org/sindresorhus/gulp-template)
# gulp-template

> Render/precompile [Lodash/Underscore templates](http://lodash.com/docs#template)
> Render/precompile [Lodash/Underscore templates](https://lodash.com/docs#template)
*Issues with the output should be reported on the Lodash [issue tracker](https://github.com/lodash/lodash/issues).*


## Install

```sh
npm install --save-dev gulp-template
```
$ npm install --save-dev gulp-template
```


## Usage

Expand All @@ -23,10 +21,10 @@ $ npm install --save-dev gulp-template
### `gulpfile.js`

```js
const gulp = require('gulp');
const template = require('gulp-template');
import gulp from 'gulp';
import template from 'gulp-template';

gulp.task('default', () =>
export default () => (
gulp.src('src/greeting.html')
.pipe(template({name: 'Sindre'}))
.pipe(gulp.dest('dist'))
Expand All @@ -36,11 +34,11 @@ gulp.task('default', () =>
You can alternatively use [gulp-data](https://github.com/colynb/gulp-data) to inject the data:

```js
const gulp = require('gulp');
const template = require('gulp-template');
const data = require('gulp-data');
import gulp from 'gulp';
import template from 'gulp-template';
import data from 'gulp-data';

gulp.task('default', () =>
export default () => (
gulp.src('src/greeting.html')
.pipe(data(() => ({name: 'Sindre'})))
.pipe(template())
Expand All @@ -54,35 +52,57 @@ gulp.task('default', () =>
<h1>Hello Sindre</h1>
```


## API

### template(data, [options])
### template(data, options?)

Render a template using the provided `data`.

### template.precompile([options])
### template.precompile(options?)

Precompile a template for rendering dynamically at a later time.

#### data

Type: `Object`
Type: `object`

Data object used to populate the text.

#### options

Type: `Object`
Type: `object`

[Lodash `_.template` options](http://lodash.com/docs#template).
[Lodash `_.template` options](https://lodash.com/docs#template).

## Tip

## Related
You can also provide your own [interpolation string](https://lodash.com/docs#template) for custom templates.

- [grunt-template](https://github.com/mathiasbynens/grunt-template) - Grunt version
### `src/greeting.html`

```html
<h1>Hello {{ name }}</h1>
```

## License
### `gulpfile.js`

MIT © [Sindre Sorhus](https://sindresorhus.com)
```js
import gulp from 'gulp';
import template from 'gulp-template';
import data from 'gulp-data';

export default () => (
gulp.src('src/greeting.html')
.pipe(data(() => ({name: 'Sindre'})))
.pipe(template(null, {
interpolate: /{{(.+?)}}/gs
}))
.pipe(gulp.dest('dist'))
);
```

### `dist/greeting.html`

```html
<h1>Hello Sindre</h1>
```

0 comments on commit bb97b27

Please sign in to comment.