gulp-jlto 1.2.1 β 1.3.1
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/.nvmrc +1 -0
- package/LICENSE +1 -1
- package/README.md +12 -10
- package/index.js +7 -6
- package/package.json +11 -4
- package/test/gulp-task.test.js +153 -0
- package/test/main.test.js +115 -0
package/.nvmrc
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
v24.13.1
|
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,13 +1,3 @@
|
|
|
1
|
-
# Support Ukraine πΊπ¦
|
|
2
|
-
|
|
3
|
-
- Via United24 platform (the initiative of the President of Ukraine):
|
|
4
|
-
- [One click donation (credit card, bank transfer or crypto)](https://u24.gov.ua/)
|
|
5
|
-
- Via National Bank of Ukraine:
|
|
6
|
-
- [Ukrainian army](https://bank.gov.ua/en/about/support-the-armed-forces)
|
|
7
|
-
- [Humanitarian aid to Ukraine](https://bank.gov.ua/en/about/humanitarian-aid-to-ukraine)
|
|
8
|
-
|
|
9
|
-
[#StandWithUkraine](https://twitter.com/hashtag/StandWithUkraine)
|
|
10
|
-
|
|
11
1
|
# gulp-jlto
|
|
12
2
|
|
|
13
3
|
[](https://nodei.co/npm/gulp-jlto/)
|
|
@@ -39,3 +29,15 @@ gulp.task('jlto', () => {
|
|
|
39
29
|
.pipe(gulp.dest('build'));
|
|
40
30
|
});
|
|
41
31
|
```
|
|
32
|
+
|
|
33
|
+
## Tests
|
|
34
|
+
|
|
35
|
+
Tests are written using Node's `assert` module. To run them, invoke `npm test`.
|
|
36
|
+
|
|
37
|
+
## Further Reading
|
|
38
|
+
|
|
39
|
+
[Why You Should Write Open Source Code and How It Helps Your Career](https://explainme.online/article/why-you-should-write-open-source-code-and-how-it-helps-your-career)
|
|
40
|
+
|
|
41
|
+
## License
|
|
42
|
+
|
|
43
|
+
JLTO is available under the [MIT license](https://opensource.org/licenses/MIT), see the LICENSE file for more information.
|
package/index.js
CHANGED
|
@@ -12,12 +12,13 @@ module.exports = (options) => {
|
|
|
12
12
|
}
|
|
13
13
|
let source = String(file.contents);
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
15
|
+
Promise.resolve()
|
|
16
|
+
.then(() => jlto.optimizeString(source, options))
|
|
17
|
+
.then((result) => {
|
|
18
|
+
file.contents = Buffer.from(result);
|
|
19
|
+
})
|
|
20
|
+
.catch(() => {})
|
|
21
|
+
.finally(() => callback(null, file));
|
|
21
22
|
};
|
|
22
23
|
|
|
23
24
|
return transform;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gulp-jlto",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.1",
|
|
4
4
|
"description": "Gulp tool for optimizing Jinja like templates",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"jinja",
|
|
@@ -31,11 +31,18 @@
|
|
|
31
31
|
"url": "git://github.com/dmytro-krekota/gulp-jlto.git"
|
|
32
32
|
},
|
|
33
33
|
"main": "index.js",
|
|
34
|
-
"
|
|
35
|
-
"
|
|
34
|
+
"scripts": {
|
|
35
|
+
"lint": "eslint .",
|
|
36
|
+
"test": "node test/main.test.js && node test/gulp-task.test.js"
|
|
36
37
|
},
|
|
37
38
|
"license": "MIT",
|
|
38
39
|
"dependencies": {
|
|
39
|
-
"jlto": "^1.4
|
|
40
|
+
"jlto": "^1.5.4"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@eslint/js": "^9.39.3",
|
|
44
|
+
"eslint": "^9.39.3",
|
|
45
|
+
"globals": "^17.3.0",
|
|
46
|
+
"gulp": "^5.0.1"
|
|
40
47
|
}
|
|
41
48
|
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
let assert = require('assert');
|
|
2
|
+
let fs = require('fs');
|
|
3
|
+
let os = require('os');
|
|
4
|
+
let path = require('path');
|
|
5
|
+
let stream = require('stream');
|
|
6
|
+
let util = require('util');
|
|
7
|
+
let gulp = require('gulp');
|
|
8
|
+
let jlto = require('jlto');
|
|
9
|
+
let createPlugin = require('../index');
|
|
10
|
+
|
|
11
|
+
let fsp = fs.promises;
|
|
12
|
+
let finished = util.promisify(stream.finished);
|
|
13
|
+
let originalOptimizeString = jlto.optimizeString;
|
|
14
|
+
|
|
15
|
+
let removeDirectory = async (dirPath) => {
|
|
16
|
+
if (fsp.rm) {
|
|
17
|
+
await fsp.rm(dirPath, {recursive: true, force: true});
|
|
18
|
+
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
await fsp.rmdir(dirPath, {recursive: true});
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
let withTempDirectory = async (prefix, fn) => {
|
|
26
|
+
let tempRoot = await fsp.mkdtemp(path.join(os.tmpdir(), prefix));
|
|
27
|
+
|
|
28
|
+
try {
|
|
29
|
+
await fn(tempRoot);
|
|
30
|
+
} finally {
|
|
31
|
+
await removeDirectory(tempRoot);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
let runTask = (taskFn) => {
|
|
36
|
+
return new Promise((resolve, reject) => {
|
|
37
|
+
gulp.series(taskFn)((error) => {
|
|
38
|
+
if (error) {
|
|
39
|
+
return reject(error);
|
|
40
|
+
}
|
|
41
|
+
resolve();
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
let runTest = async (name, fn) => {
|
|
47
|
+
try {
|
|
48
|
+
jlto.optimizeString = originalOptimizeString;
|
|
49
|
+
await fn();
|
|
50
|
+
console.log('ok - ' + name);
|
|
51
|
+
} catch (error) {
|
|
52
|
+
console.error('not ok - ' + name);
|
|
53
|
+
console.error(error && error.stack ? error.stack : error);
|
|
54
|
+
process.exitCode = 1;
|
|
55
|
+
} finally {
|
|
56
|
+
jlto.optimizeString = originalOptimizeString;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
let buildPaths = (tempRoot) => {
|
|
61
|
+
let sourceDir = path.join(tempRoot, 'src');
|
|
62
|
+
let outputDir = path.join(tempRoot, 'dist');
|
|
63
|
+
let sourceFilePath = path.join(sourceDir, 'template.html');
|
|
64
|
+
let outputFilePath = path.join(outputDir, 'template.html');
|
|
65
|
+
|
|
66
|
+
return {
|
|
67
|
+
sourceDir,
|
|
68
|
+
outputDir,
|
|
69
|
+
sourceFilePath,
|
|
70
|
+
outputFilePath,
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
let testOptimizedOutput = async () => {
|
|
75
|
+
await withTempDirectory('gulp-jlto-', async (tempRoot) => {
|
|
76
|
+
let sourceString = '{% if user %}\n <div> Hello </div>\n{% endif %}';
|
|
77
|
+
let expectedString = '{% if user %}<div>Hello</div>{% endif %}';
|
|
78
|
+
let {sourceDir, outputDir, sourceFilePath, outputFilePath} = buildPaths(tempRoot);
|
|
79
|
+
|
|
80
|
+
await fsp.mkdir(sourceDir, {recursive: true});
|
|
81
|
+
await fsp.writeFile(sourceFilePath, sourceString, 'utf8');
|
|
82
|
+
|
|
83
|
+
jlto.optimizeString = async (source, options) => {
|
|
84
|
+
assert.strictEqual(source, sourceString);
|
|
85
|
+
assert.deepStrictEqual(options, {strip: true});
|
|
86
|
+
|
|
87
|
+
return expectedString;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
let optimizeTask = () => {
|
|
91
|
+
let vinylStream = gulp
|
|
92
|
+
.src(path.join(sourceDir, '*.html'))
|
|
93
|
+
.pipe(createPlugin({strip: true}))
|
|
94
|
+
.pipe(gulp.dest(outputDir));
|
|
95
|
+
|
|
96
|
+
return finished(vinylStream);
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
await runTask(optimizeTask);
|
|
100
|
+
|
|
101
|
+
let output = await fsp.readFile(outputFilePath, 'utf8');
|
|
102
|
+
|
|
103
|
+
assert.strictEqual(output, expectedString);
|
|
104
|
+
});
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
let testFallbackOnOptimizeFailure = async () => {
|
|
108
|
+
await withTempDirectory('gulp-jlto-', async (tempRoot) => {
|
|
109
|
+
let sourceString = '{% if user %}\n <div> Hello </div>\n{% endif %}';
|
|
110
|
+
let called = false;
|
|
111
|
+
let {sourceDir, outputDir, sourceFilePath, outputFilePath} = buildPaths(tempRoot);
|
|
112
|
+
|
|
113
|
+
await fsp.mkdir(sourceDir, {recursive: true});
|
|
114
|
+
await fsp.writeFile(sourceFilePath, sourceString, 'utf8');
|
|
115
|
+
|
|
116
|
+
jlto.optimizeString = async (source, options) => {
|
|
117
|
+
called = true;
|
|
118
|
+
assert.strictEqual(source, sourceString);
|
|
119
|
+
assert.strictEqual(options, undefined);
|
|
120
|
+
throw new Error('optimizer failed');
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
let optimizeTask = () => {
|
|
124
|
+
let vinylStream = gulp
|
|
125
|
+
.src(path.join(sourceDir, '*.html'))
|
|
126
|
+
.pipe(createPlugin())
|
|
127
|
+
.pipe(gulp.dest(outputDir));
|
|
128
|
+
|
|
129
|
+
return finished(vinylStream);
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
await runTask(optimizeTask);
|
|
133
|
+
|
|
134
|
+
let output = await fsp.readFile(outputFilePath, 'utf8');
|
|
135
|
+
|
|
136
|
+
assert.strictEqual(called, true);
|
|
137
|
+
assert.strictEqual(output, sourceString);
|
|
138
|
+
});
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
let run = async () => {
|
|
142
|
+
await runTest('runs plugin in a gulp task and writes optimized output', testOptimizedOutput);
|
|
143
|
+
await runTest('keeps original output in gulp task when optimizer fails', testFallbackOnOptimizeFailure);
|
|
144
|
+
|
|
145
|
+
if (process.exitCode && process.exitCode !== 0) {
|
|
146
|
+
process.exit(process.exitCode);
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
run().catch((error) => {
|
|
151
|
+
console.error(error && error.stack ? error.stack : error);
|
|
152
|
+
process.exit(1);
|
|
153
|
+
});
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
let assert = require('assert');
|
|
2
|
+
let jlto = require('jlto');
|
|
3
|
+
let createPlugin = require('../index');
|
|
4
|
+
|
|
5
|
+
let originalOptimizeString = jlto.optimizeString;
|
|
6
|
+
|
|
7
|
+
let runPlugin = (file, options) => {
|
|
8
|
+
let plugin = createPlugin(options);
|
|
9
|
+
|
|
10
|
+
return new Promise((resolve, reject) => {
|
|
11
|
+
plugin._transform(file, 'utf8', (error, outputFile) => {
|
|
12
|
+
if (error) {
|
|
13
|
+
return reject(error);
|
|
14
|
+
}
|
|
15
|
+
resolve(outputFile);
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
let createFile = (contents) => {
|
|
21
|
+
return {
|
|
22
|
+
isNull: () => false,
|
|
23
|
+
contents: Buffer.from(contents),
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
let testCases = [
|
|
28
|
+
{
|
|
29
|
+
name: 'passes null files through unchanged',
|
|
30
|
+
run: async () => {
|
|
31
|
+
let called = false;
|
|
32
|
+
|
|
33
|
+
jlto.optimizeString = async () => {
|
|
34
|
+
called = true;
|
|
35
|
+
|
|
36
|
+
return 'ignored';
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
let file = {
|
|
40
|
+
isNull: () => true,
|
|
41
|
+
contents: Buffer.from('source'),
|
|
42
|
+
};
|
|
43
|
+
let result = await runPlugin(file);
|
|
44
|
+
|
|
45
|
+
assert.strictEqual(result, file);
|
|
46
|
+
assert.strictEqual(result.contents.toString(), 'source');
|
|
47
|
+
assert.strictEqual(called, false);
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
name: 'optimizes non-null files when optimizer resolves',
|
|
52
|
+
run: async () => {
|
|
53
|
+
let sourceString = '{% if user %}\n <div> Hello </div>\n{% endif %}';
|
|
54
|
+
let expectedString = '{% if user %}<div>Hello</div>{% endif %}';
|
|
55
|
+
|
|
56
|
+
jlto.optimizeString = async (source, options) => {
|
|
57
|
+
assert.strictEqual(source, sourceString);
|
|
58
|
+
assert.deepStrictEqual(options, {keepComments: false});
|
|
59
|
+
|
|
60
|
+
return expectedString;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
let file = createFile(sourceString);
|
|
64
|
+
let result = await runPlugin(file, {keepComments: false});
|
|
65
|
+
|
|
66
|
+
assert.strictEqual(result, file);
|
|
67
|
+
assert.strictEqual(result.contents.toString(), expectedString);
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
name: 'keeps original contents when optimizer throws',
|
|
72
|
+
run: async () => {
|
|
73
|
+
let called = false;
|
|
74
|
+
|
|
75
|
+
jlto.optimizeString = async () => {
|
|
76
|
+
called = true;
|
|
77
|
+
throw new Error('boom');
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
let file = createFile('source');
|
|
81
|
+
let result = await runPlugin(file);
|
|
82
|
+
|
|
83
|
+
assert.strictEqual(result, file);
|
|
84
|
+
assert.strictEqual(called, true);
|
|
85
|
+
assert.strictEqual(result.contents.toString(), 'source');
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
];
|
|
89
|
+
|
|
90
|
+
let run = async () => {
|
|
91
|
+
let failures = 0;
|
|
92
|
+
|
|
93
|
+
for (let testCase of testCases) {
|
|
94
|
+
try {
|
|
95
|
+
jlto.optimizeString = originalOptimizeString;
|
|
96
|
+
await testCase.run();
|
|
97
|
+
console.log('ok - ' + testCase.name);
|
|
98
|
+
} catch (error) {
|
|
99
|
+
failures += 1;
|
|
100
|
+
console.error('not ok - ' + testCase.name);
|
|
101
|
+
console.error(error && error.stack ? error.stack : error);
|
|
102
|
+
} finally {
|
|
103
|
+
jlto.optimizeString = originalOptimizeString;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (failures > 0) {
|
|
108
|
+
process.exit(1);
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
run().catch((error) => {
|
|
113
|
+
console.error(error && error.stack ? error.stack : error);
|
|
114
|
+
process.exit(1);
|
|
115
|
+
});
|