Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
gulp-transform
Advanced tools
A Gulp plugin for applying custom transformations to the contents of files
A Gulp plugin for applying custom transformations to the contents of files.
Install via npm:
npm install --save-dev gulp gulp-transform
This example adds a timestamp to the beginning of each source file. The comment format is inferred from the file extension. Files with unrecognized extensions are not modified.
const gulp = require('gulp');
const transform = require('gulp-transform');
const path = require('path');
const TIMESTAMP = Date();
gulp.task('timestamp', () => {
return gulp.src('src/**/*')
.pipe(transform('utf8', timestamp))
.pipe(gulp.dest('out'));
});
function timestamp(content, file) {
switch (path.extname(file.path)) {
case '.js':
case '.ts':
return `// ${TIMESTAMP}\n\n${content}`;
case '.coffee':
return `# ${TIMESTAMP}\n\n${content}`;
default:
return content;
}
}
console.log('Hello, world.');
// Thu Jul 27 2017 15:56:14 GMT-0700 (PDT)
console.log('Hello, world.');
This example uses xml2js to convert XML to JSON. The callback returns a Promise since the operation is asynchronous.
const gulp = require('gulp');
const transform = require('gulp-transform');
const rename = require('gulp-rename');
const xml2js = require('xml2js');
gulp.task('xml-to-json', () => {
return gulp.src('src/**/*.xml')
.pipe(transform('utf8', xmlToJson))
.pipe(rename({ extname: '.json' }))
.pipe(gulp.dest('out'));
});
function xmlToJson(content) {
return new Promise((resolve, reject) => {
xml2js.parseString(content, (error, data) => {
if (error) {
reject(error);
} else {
resolve(JSON.stringify(data, null, 2));
}
});
});
}
<cities>
<city>Amsterdam</city>
<city>Rotterdam</city>
<city>The Hague</city>
</cities>
{
"cities": {
"city": [
"Amsterdam",
"Rotterdam",
"The Hague"
]
}
}
Creates a stream that transforms the contents of File objects. Files in both streaming and buffer mode are accepted.
To transform contents as a string, a character encoding must be specified; otherwise, contents will be passed to the callback as a Buffer.
The contents of each File are replaced with the return value of the callback. Or, to perform an asynchronous transformation, a Promise may be returned.
Parameter | Type | Description |
---|---|---|
[options] |
|
An optional options object or a value indicating an encoding. If passed as an object, the following properties are are accepted as options:
If passed as a string or
If no encoding is given, callback is called with a
|
callback |
function
|
A function that transforms the contents of each file. It is invoked with two arguments:
The contents of the file are replaced with the return value of the
callback. For asynchronous transformations, a
The value of |
TypeScript declarations are included in the package.
npm i -D typescript ts-node gulp @types/gulp gulp-transform
import gulp = require("gulp");
import transform = require("gulp-transform");
gulp.task("build", () => {
gulp.src("src/**/*")
.pipe(transform("utf8", () => { /* transform contents */ }))
.pipe(gulp.dest("out"));
});
Copyright © 2016–2017 Akim McMath. Licensed under the MIT License.
FAQs
A Gulp plugin for applying custom transformations to the contents of files
The npm package gulp-transform receives a total of 3,337 weekly downloads. As such, gulp-transform popularity was classified as popular.
We found that gulp-transform demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.