gulp-jlto 1.3.0 → 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/README.md CHANGED
@@ -29,3 +29,15 @@ gulp.task('jlto', () => {
29
29
  .pipe(gulp.dest('build'));
30
30
  });
31
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gulp-jlto",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "description": "Gulp tool for optimizing Jinja like templates",
5
5
  "keywords": [
6
6
  "jinja",
@@ -37,7 +37,7 @@
37
37
  },
38
38
  "license": "MIT",
39
39
  "dependencies": {
40
- "jlto": "^1.5.0"
40
+ "jlto": "^1.5.4"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@eslint/js": "^9.39.3",
@@ -57,21 +57,34 @@ let runTest = async (name, fn) => {
57
57
  }
58
58
  };
59
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
+
60
74
  let testOptimizedOutput = async () => {
61
75
  await withTempDirectory('gulp-jlto-', async (tempRoot) => {
62
- let sourceDir = path.join(tempRoot, 'src');
63
- let outputDir = path.join(tempRoot, 'dist');
64
- let sourceFilePath = path.join(sourceDir, 'template.html');
65
- let outputFilePath = path.join(outputDir, 'template.html');
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);
66
79
 
67
80
  await fsp.mkdir(sourceDir, {recursive: true});
68
- await fsp.writeFile(sourceFilePath, 'SOURCE', 'utf8');
81
+ await fsp.writeFile(sourceFilePath, sourceString, 'utf8');
69
82
 
70
83
  jlto.optimizeString = async (source, options) => {
71
- assert.strictEqual(source, 'SOURCE');
84
+ assert.strictEqual(source, sourceString);
72
85
  assert.deepStrictEqual(options, {strip: true});
73
86
 
74
- return 'OPTIMIZED';
87
+ return expectedString;
75
88
  };
76
89
 
77
90
  let optimizeTask = () => {
@@ -87,21 +100,23 @@ let testOptimizedOutput = async () => {
87
100
 
88
101
  let output = await fsp.readFile(outputFilePath, 'utf8');
89
102
 
90
- assert.strictEqual(output, 'OPTIMIZED');
103
+ assert.strictEqual(output, expectedString);
91
104
  });
92
105
  };
93
106
 
94
107
  let testFallbackOnOptimizeFailure = async () => {
95
108
  await withTempDirectory('gulp-jlto-', async (tempRoot) => {
96
- let sourceDir = path.join(tempRoot, 'src');
97
- let outputDir = path.join(tempRoot, 'dist');
98
- let sourceFilePath = path.join(sourceDir, 'template.html');
99
- let outputFilePath = path.join(outputDir, 'template.html');
109
+ let sourceString = '{% if user %}\n <div> Hello </div>\n{% endif %}';
110
+ let called = false;
111
+ let {sourceDir, outputDir, sourceFilePath, outputFilePath} = buildPaths(tempRoot);
100
112
 
101
113
  await fsp.mkdir(sourceDir, {recursive: true});
102
- await fsp.writeFile(sourceFilePath, 'SOURCE', 'utf8');
114
+ await fsp.writeFile(sourceFilePath, sourceString, 'utf8');
103
115
 
104
- jlto.optimizeString = async () => {
116
+ jlto.optimizeString = async (source, options) => {
117
+ called = true;
118
+ assert.strictEqual(source, sourceString);
119
+ assert.strictEqual(options, undefined);
105
120
  throw new Error('optimizer failed');
106
121
  };
107
122
 
@@ -118,7 +133,8 @@ let testFallbackOnOptimizeFailure = async () => {
118
133
 
119
134
  let output = await fsp.readFile(outputFilePath, 'utf8');
120
135
 
121
- assert.strictEqual(output, 'SOURCE');
136
+ assert.strictEqual(called, true);
137
+ assert.strictEqual(output, sourceString);
122
138
  });
123
139
  };
124
140
 
package/test/main.test.js CHANGED
@@ -17,6 +17,13 @@ let runPlugin = (file, options) => {
17
17
  });
18
18
  };
19
19
 
20
+ let createFile = (contents) => {
21
+ return {
22
+ isNull: () => false,
23
+ contents: Buffer.from(contents),
24
+ };
25
+ };
26
+
20
27
  let testCases = [
21
28
  {
22
29
  name: 'passes null files through unchanged',
@@ -43,35 +50,38 @@ let testCases = [
43
50
  {
44
51
  name: 'optimizes non-null files when optimizer resolves',
45
52
  run: async () => {
53
+ let sourceString = '{% if user %}\n <div> Hello </div>\n{% endif %}';
54
+ let expectedString = '{% if user %}<div>Hello</div>{% endif %}';
55
+
46
56
  jlto.optimizeString = async (source, options) => {
47
- assert.strictEqual(source, 'source');
57
+ assert.strictEqual(source, sourceString);
48
58
  assert.deepStrictEqual(options, {keepComments: false});
49
59
 
50
- return 'optimized';
60
+ return expectedString;
51
61
  };
52
62
 
53
- let file = {
54
- isNull: () => false,
55
- contents: Buffer.from('source'),
56
- };
63
+ let file = createFile(sourceString);
57
64
  let result = await runPlugin(file, {keepComments: false});
58
65
 
59
- assert.strictEqual(result.contents.toString(), 'optimized');
66
+ assert.strictEqual(result, file);
67
+ assert.strictEqual(result.contents.toString(), expectedString);
60
68
  },
61
69
  },
62
70
  {
63
71
  name: 'keeps original contents when optimizer throws',
64
72
  run: async () => {
73
+ let called = false;
74
+
65
75
  jlto.optimizeString = async () => {
76
+ called = true;
66
77
  throw new Error('boom');
67
78
  };
68
79
 
69
- let file = {
70
- isNull: () => false,
71
- contents: Buffer.from('source'),
72
- };
80
+ let file = createFile('source');
73
81
  let result = await runPlugin(file);
74
82
 
83
+ assert.strictEqual(result, file);
84
+ assert.strictEqual(called, true);
75
85
  assert.strictEqual(result.contents.toString(), 'source');
76
86
  },
77
87
  },