gulp-preprocess 1.2.0 → 3.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc +7 -0
- package/.travis.yml +3 -1
- package/README.md +17 -17
- package/index.js +15 -12
- package/package.json +13 -11
- package/test/expected/test-base.html +2 -0
- package/test/expected/test.coffee +2 -0
- package/test/expected/test.html +2 -0
- package/test/expected/test.js +2 -0
- package/test/main.js +62 -58
- package/.npmignore +0 -1
package/.eslintrc
ADDED
package/.travis.yml
CHANGED
package/README.md
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
# gulp-preprocess [](https://www.npmjs.com/package/gulp-preprocess) [](https://travis-ci.org/pioug/gulp-preprocess) [](https://david-dm.org/pioug/gulp-preprocess)
|
|
2
2
|
|
|
3
3
|
> [Gulp](http://gulpjs.com) plugin to preprocess HTML, JavaScript, and other files based on custom context or environment configuration
|
|
4
4
|
|
|
5
|
-
|
|
6
5
|
## Information
|
|
7
6
|
|
|
8
7
|
<table>
|
|
@@ -11,7 +10,7 @@
|
|
|
11
10
|
</tr>
|
|
12
11
|
<tr>
|
|
13
12
|
<td>Node Version</td>
|
|
14
|
-
<td>>=
|
|
13
|
+
<td>>= 6.14.3</td>
|
|
15
14
|
</tr>
|
|
16
15
|
<tr>
|
|
17
16
|
<td>Gulp Version</td>
|
|
@@ -19,7 +18,6 @@
|
|
|
19
18
|
</tr>
|
|
20
19
|
</table>
|
|
21
20
|
|
|
22
|
-
|
|
23
21
|
## Usage
|
|
24
22
|
|
|
25
23
|
### Install
|
|
@@ -28,24 +26,25 @@
|
|
|
28
26
|
npm install gulp-preprocess --save-dev
|
|
29
27
|
```
|
|
30
28
|
|
|
31
|
-
|
|
32
29
|
## Examples
|
|
33
30
|
|
|
34
31
|
#### Gulpfile
|
|
35
32
|
|
|
36
33
|
```js
|
|
37
|
-
var preprocess = require(
|
|
34
|
+
var preprocess = require("gulp-preprocess");
|
|
38
35
|
|
|
39
|
-
gulp.task(
|
|
40
|
-
gulp
|
|
41
|
-
.
|
|
42
|
-
.pipe(
|
|
36
|
+
gulp.task("html", function() {
|
|
37
|
+
gulp
|
|
38
|
+
.src("./app/*.html")
|
|
39
|
+
.pipe(preprocess({ context: { NODE_ENV: "production", DEBUG: true } })) // To set environment variables in-line
|
|
40
|
+
.pipe(gulp.dest("./dist/"));
|
|
43
41
|
});
|
|
44
42
|
|
|
45
|
-
gulp.task(
|
|
46
|
-
gulp
|
|
43
|
+
gulp.task("scripts", function() {
|
|
44
|
+
gulp
|
|
45
|
+
.src(["./app/*.js"])
|
|
47
46
|
.pipe(preprocess())
|
|
48
|
-
.pipe(gulp.dest(
|
|
47
|
+
.pipe(gulp.dest("./dist/"));
|
|
49
48
|
});
|
|
50
49
|
```
|
|
51
50
|
|
|
@@ -73,37 +72,38 @@ gulp.task('scripts', function() {
|
|
|
73
72
|
#### Example JavaScript File
|
|
74
73
|
|
|
75
74
|
```js
|
|
76
|
-
var configValue =
|
|
75
|
+
var configValue = "/* @echo FOO */" || "default value";
|
|
77
76
|
|
|
78
77
|
// @ifdef DEBUG
|
|
79
|
-
someDebuggingCall()
|
|
78
|
+
someDebuggingCall();
|
|
80
79
|
// @endif
|
|
81
80
|
```
|
|
82
81
|
|
|
83
82
|
CoffeeScript files are also supported.
|
|
84
83
|
|
|
85
|
-
|
|
86
84
|
#### More Examples
|
|
87
85
|
|
|
88
86
|
`gulp-preprocess` uses [preprocess](https://github.com/jsoverson/preprocess#directive-syntax). More examples can be found in its [README](https://github.com/jsoverson/preprocess#directive-syntax).
|
|
89
87
|
|
|
90
|
-
|
|
91
88
|
## API
|
|
92
89
|
|
|
93
90
|
### preprocess(options)
|
|
94
91
|
|
|
95
92
|
#### options.context
|
|
93
|
+
|
|
96
94
|
Type: `Object`
|
|
97
95
|
Default: `{}`
|
|
98
96
|
|
|
99
97
|
Context for directives used in your preprocessed files. The default context consists of the current user environment variables. Custom context is merged with `process.env`.
|
|
100
98
|
|
|
101
99
|
#### options.includeBase
|
|
100
|
+
|
|
102
101
|
Type: `String`
|
|
103
102
|
|
|
104
103
|
Base directory for included files. By default, the path to included files is relative to the file currently being processed.
|
|
105
104
|
|
|
106
105
|
#### options.extension
|
|
106
|
+
|
|
107
107
|
Type: `String`
|
|
108
108
|
|
|
109
109
|
Override the file extension. This determines what [regular expressions are used for comments](https://github.com/jsoverson/preprocess/blob/master/lib/regexrules.js). You may wish to do this if you are using a custom extension or need to force a particular comment syntax (for example, to allow HTML-style comments in `.php` files).
|
package/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
var _
|
|
2
|
-
var map
|
|
3
|
-
var pp
|
|
4
|
-
var path
|
|
1
|
+
var _ = require("lodash");
|
|
2
|
+
var map = require("map-stream");
|
|
3
|
+
var pp = require("preprocess");
|
|
4
|
+
var path = require("path");
|
|
5
5
|
|
|
6
|
-
module.exports = function
|
|
7
|
-
var opts
|
|
6
|
+
module.exports = function(options) {
|
|
7
|
+
var opts = _.merge({}, options);
|
|
8
8
|
var context = _.merge({}, process.env, opts.context);
|
|
9
9
|
|
|
10
10
|
function ppStream(file, callback) {
|
|
@@ -12,17 +12,20 @@ module.exports = function (options) {
|
|
|
12
12
|
|
|
13
13
|
// TODO: support streaming files
|
|
14
14
|
if (file.isNull()) return callback(null, file); // pass along
|
|
15
|
-
if (file.isStream())
|
|
15
|
+
if (file.isStream())
|
|
16
|
+
return callback(new Error("gulp-preprocess: Streaming not supported"));
|
|
16
17
|
|
|
17
18
|
context.src = file.path;
|
|
18
19
|
context.srcDir = opts.includeBase || path.dirname(file.path);
|
|
19
|
-
context.NODE_ENV = context.NODE_ENV ||
|
|
20
|
+
context.NODE_ENV = context.NODE_ENV || "development";
|
|
20
21
|
|
|
21
|
-
extension = _.isEmpty(opts.extension)
|
|
22
|
+
extension = _.isEmpty(opts.extension)
|
|
23
|
+
? getExtension(context.src)
|
|
24
|
+
: opts.extension;
|
|
22
25
|
|
|
23
|
-
contents = file.contents.toString(
|
|
26
|
+
contents = file.contents.toString("utf8");
|
|
24
27
|
contents = pp.preprocess(contents, context, extension);
|
|
25
|
-
file.contents =
|
|
28
|
+
file.contents = Buffer.from(contents);
|
|
26
29
|
|
|
27
30
|
callback(null, file);
|
|
28
31
|
}
|
|
@@ -31,6 +34,6 @@ module.exports = function (options) {
|
|
|
31
34
|
};
|
|
32
35
|
|
|
33
36
|
function getExtension(filename) {
|
|
34
|
-
var ext = path.extname(filename||
|
|
37
|
+
var ext = path.extname(filename || "").split(".");
|
|
35
38
|
return ext[ext.length - 1];
|
|
36
39
|
}
|
package/package.json
CHANGED
|
@@ -1,32 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gulp-preprocess",
|
|
3
3
|
"description": "Gulp plugin to preprocess HTML, JavaScript, and other files based on custom context or environment configuration",
|
|
4
|
-
"version": "
|
|
5
|
-
"homepage": "http://github.com/
|
|
4
|
+
"version": "3.0.2",
|
|
5
|
+
"homepage": "http://github.com/pioug/gulp-preprocess",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
|
-
"url": "http://github.com/
|
|
8
|
+
"url": "http://github.com/pioug/gulp-preprocess.git"
|
|
9
9
|
},
|
|
10
10
|
"author": "Jason Sandmeyer",
|
|
11
11
|
"main": "./index.js",
|
|
12
12
|
"dependencies": {
|
|
13
|
-
"lodash": "
|
|
14
|
-
"map-stream": "0.1.x",
|
|
15
|
-
"preprocess": "
|
|
13
|
+
"lodash": "^4.17.11",
|
|
14
|
+
"map-stream": "^0.1.x",
|
|
15
|
+
"preprocess": "^3.0.0"
|
|
16
16
|
},
|
|
17
17
|
"devDependencies": {
|
|
18
|
-
"
|
|
19
|
-
"mocha": "
|
|
20
|
-
"
|
|
18
|
+
"eslint": "^5.13.0",
|
|
19
|
+
"mocha": "^5.2.0",
|
|
20
|
+
"prettier": "^1.16.4",
|
|
21
|
+
"should": "^13.2.3",
|
|
22
|
+
"vinyl": "^2.2.0"
|
|
21
23
|
},
|
|
22
24
|
"engines": {
|
|
23
|
-
"node": ">=
|
|
25
|
+
"node": ">= 6.14.3"
|
|
24
26
|
},
|
|
25
27
|
"scripts": {
|
|
26
28
|
"test": "mocha --reporter spec"
|
|
27
29
|
},
|
|
28
30
|
"bugs": {
|
|
29
|
-
"url": "https://github.com/
|
|
31
|
+
"url": "https://github.com/pioug/gulp-preprocess/issues"
|
|
30
32
|
},
|
|
31
33
|
"directories": {
|
|
32
34
|
"test": "test"
|
package/test/expected/test.html
CHANGED
package/test/expected/test.js
CHANGED
package/test/main.js
CHANGED
|
@@ -1,140 +1,144 @@
|
|
|
1
|
-
var should = require(
|
|
2
|
-
var
|
|
3
|
-
var preprocess = require(
|
|
4
|
-
var fs = require(
|
|
1
|
+
var should = require("should");
|
|
2
|
+
var File = require("vinyl");
|
|
3
|
+
var preprocess = require("../");
|
|
4
|
+
var fs = require("fs");
|
|
5
5
|
|
|
6
|
-
require(
|
|
6
|
+
require("mocha");
|
|
7
7
|
|
|
8
8
|
function fixtureFile(fileName) {
|
|
9
|
-
return new
|
|
10
|
-
base:
|
|
11
|
-
cwd:
|
|
12
|
-
path:
|
|
13
|
-
contents: fs.readFileSync(
|
|
9
|
+
return new File({
|
|
10
|
+
base: "test/fixtures",
|
|
11
|
+
cwd: "test/",
|
|
12
|
+
path: "test/fixtures/" + fileName,
|
|
13
|
+
contents: fs.readFileSync("test/fixtures/" + fileName)
|
|
14
14
|
});
|
|
15
|
-
}
|
|
15
|
+
}
|
|
16
16
|
|
|
17
|
-
process.env[
|
|
17
|
+
process.env["FAKEHOME"] = "/Users/jas";
|
|
18
18
|
|
|
19
|
-
describe(
|
|
20
|
-
describe(
|
|
21
|
-
|
|
22
|
-
it('should preprocess html', function(done){
|
|
19
|
+
describe("gulp-preprocess", function() {
|
|
20
|
+
describe("preprocess()", function() {
|
|
21
|
+
it("should preprocess html", function(done) {
|
|
23
22
|
var stream = preprocess({
|
|
24
23
|
context: {
|
|
25
|
-
firstOption:
|
|
26
|
-
secondOption:
|
|
24
|
+
firstOption: "bar",
|
|
25
|
+
secondOption: "foo"
|
|
27
26
|
}
|
|
28
27
|
});
|
|
29
28
|
|
|
30
|
-
var fakeFile = fixtureFile(
|
|
29
|
+
var fakeFile = fixtureFile("test.html");
|
|
31
30
|
|
|
32
|
-
stream.once(
|
|
31
|
+
stream.once("data", function(newFile) {
|
|
33
32
|
should.exist(newFile);
|
|
34
33
|
should.exist(newFile.contents);
|
|
35
|
-
String(newFile.contents).should.equal(
|
|
34
|
+
String(newFile.contents).should.equal(
|
|
35
|
+
fs.readFileSync("test/expected/test.html", "utf8")
|
|
36
|
+
);
|
|
36
37
|
done();
|
|
37
38
|
});
|
|
38
39
|
stream.write(fakeFile);
|
|
39
|
-
|
|
40
40
|
});
|
|
41
41
|
|
|
42
|
-
it(
|
|
42
|
+
it("should preprocess javascript", function(done) {
|
|
43
43
|
var stream = preprocess({
|
|
44
44
|
context: {
|
|
45
|
-
firstOption:
|
|
46
|
-
secondOption:
|
|
45
|
+
firstOption: "bar",
|
|
46
|
+
secondOption: "foo"
|
|
47
47
|
}
|
|
48
48
|
});
|
|
49
49
|
|
|
50
|
-
var fakeFile = fixtureFile(
|
|
50
|
+
var fakeFile = fixtureFile("test.js");
|
|
51
51
|
|
|
52
|
-
stream.once(
|
|
52
|
+
stream.once("data", function(newFile) {
|
|
53
53
|
should.exist(newFile);
|
|
54
54
|
should.exist(newFile.contents);
|
|
55
|
-
String(newFile.contents).should.equal(
|
|
55
|
+
String(newFile.contents).should.equal(
|
|
56
|
+
fs.readFileSync("test/expected/test.js", "utf8")
|
|
57
|
+
);
|
|
56
58
|
done();
|
|
57
59
|
});
|
|
58
60
|
stream.write(fakeFile);
|
|
59
|
-
|
|
60
61
|
});
|
|
61
62
|
|
|
62
|
-
it(
|
|
63
|
+
it("should preprocess coffeescript", function(done) {
|
|
63
64
|
var stream = preprocess({
|
|
64
65
|
context: {
|
|
65
|
-
firstOption:
|
|
66
|
-
secondOption:
|
|
66
|
+
firstOption: "bar",
|
|
67
|
+
secondOption: "foo"
|
|
67
68
|
}
|
|
68
69
|
});
|
|
69
70
|
|
|
70
|
-
var fakeFile = fixtureFile(
|
|
71
|
+
var fakeFile = fixtureFile("test.coffee");
|
|
71
72
|
|
|
72
|
-
stream.once(
|
|
73
|
+
stream.once("data", function(newFile) {
|
|
73
74
|
should.exist(newFile);
|
|
74
75
|
should.exist(newFile.contents);
|
|
75
|
-
String(newFile.contents).should.equal(
|
|
76
|
+
String(newFile.contents).should.equal(
|
|
77
|
+
fs.readFileSync("test/expected/test.coffee", "utf8")
|
|
78
|
+
);
|
|
76
79
|
done();
|
|
77
80
|
});
|
|
78
81
|
stream.write(fakeFile);
|
|
79
|
-
|
|
80
82
|
});
|
|
81
83
|
|
|
82
|
-
it(
|
|
84
|
+
it("should preprocess without options object", function(done) {
|
|
83
85
|
var stream = preprocess();
|
|
84
86
|
|
|
85
|
-
var fakeFile = fixtureFile(
|
|
87
|
+
var fakeFile = fixtureFile("test.coffee");
|
|
86
88
|
|
|
87
|
-
stream.once(
|
|
89
|
+
stream.once("data", function(newFile) {
|
|
88
90
|
should.exist(newFile);
|
|
89
91
|
should.exist(newFile.contents);
|
|
90
|
-
String(newFile.contents).should.equal(
|
|
92
|
+
String(newFile.contents).should.equal(
|
|
93
|
+
fs.readFileSync("test/expected/test.coffee", "utf8")
|
|
94
|
+
);
|
|
91
95
|
done();
|
|
92
96
|
});
|
|
93
97
|
stream.write(fakeFile);
|
|
94
|
-
|
|
95
98
|
});
|
|
96
99
|
|
|
97
|
-
it(
|
|
100
|
+
it("should preprocess html with custom base", function(done) {
|
|
98
101
|
var stream = preprocess({
|
|
99
|
-
includeBase:
|
|
102
|
+
includeBase: "test/fixtures/base",
|
|
100
103
|
context: {
|
|
101
|
-
firstOption:
|
|
102
|
-
secondOption:
|
|
104
|
+
firstOption: "bar",
|
|
105
|
+
secondOption: "foo"
|
|
103
106
|
}
|
|
104
107
|
});
|
|
105
108
|
|
|
106
|
-
var fakeFile = fixtureFile(
|
|
109
|
+
var fakeFile = fixtureFile("test.html");
|
|
107
110
|
|
|
108
|
-
stream.once(
|
|
111
|
+
stream.once("data", function(newFile) {
|
|
109
112
|
should.exist(newFile);
|
|
110
113
|
should.exist(newFile.contents);
|
|
111
|
-
String(newFile.contents).should.equal(
|
|
114
|
+
String(newFile.contents).should.equal(
|
|
115
|
+
fs.readFileSync("test/expected/test-base.html", "utf8")
|
|
116
|
+
);
|
|
112
117
|
done();
|
|
113
118
|
});
|
|
114
119
|
stream.write(fakeFile);
|
|
115
|
-
|
|
116
120
|
});
|
|
117
121
|
|
|
118
|
-
it(
|
|
122
|
+
it("should allow custom extension", function(done) {
|
|
119
123
|
var stream = preprocess({
|
|
120
|
-
extension:
|
|
124
|
+
extension: "html",
|
|
121
125
|
context: {
|
|
122
|
-
firstOption:
|
|
123
|
-
secondOption:
|
|
126
|
+
firstOption: "bar",
|
|
127
|
+
secondOption: "foo"
|
|
124
128
|
}
|
|
125
129
|
});
|
|
126
130
|
|
|
127
|
-
var fakeFile = fixtureFile(
|
|
131
|
+
var fakeFile = fixtureFile("test.php");
|
|
128
132
|
|
|
129
|
-
stream.once(
|
|
133
|
+
stream.once("data", function(newFile) {
|
|
130
134
|
should.exist(newFile);
|
|
131
135
|
should.exist(newFile.contents);
|
|
132
|
-
String(newFile.contents).should.equal(
|
|
136
|
+
String(newFile.contents).should.equal(
|
|
137
|
+
fs.readFileSync("test/expected/test.html", "utf8")
|
|
138
|
+
);
|
|
133
139
|
done();
|
|
134
140
|
});
|
|
135
141
|
stream.write(fakeFile);
|
|
136
|
-
|
|
137
142
|
});
|
|
138
|
-
|
|
139
143
|
});
|
|
140
144
|
});
|
package/.npmignore
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
/node_modules/
|