Skip to content

Commit 8e6df29

Browse files
authored
Merge pull request #3489 from rtritto/replace-lodash
2 parents cc53141 + 4ce95f0 commit 8e6df29

5 files changed

Lines changed: 132 additions & 93 deletions

File tree

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
"@types/eslint": "^8.56.10",
2424
"@types/estree": "^1.0.5",
2525
"@types/fs-extra": "^9.0.13",
26-
"@types/lodash": "^4.17.0",
2726
"@types/node": "^20.14.8",
2827
"@types/stringify-object": "^3.3.1",
2928
"@typescript-eslint/eslint-plugin": "^5.62.0",

packages/workbox-build/package-lock.json

Lines changed: 18 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/workbox-build/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@
3434
"@trickfilm400/rollup-plugin-off-main-thread": "^3.0.0-pre1",
3535
"ajv": "^8.6.0",
3636
"common-tags": "^1.8.0",
37+
"eta": "^4.5.1",
3738
"fast-json-stable-stringify": "^2.1.0",
3839
"fs-extra": "^9.0.1",
3940
"glob": "^11.0.1",
40-
"lodash": "^4.17.20",
4141
"pretty-bytes": "^5.3.0",
4242
"rollup": "^4.53.3",
4343
"source-map": "^0.8.0-beta.0",

packages/workbox-build/src/lib/populate-sw-template.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
https://opensource.org/licenses/MIT.
77
*/
88

9-
import template from 'lodash/template';
9+
import {Eta} from 'eta';
1010

1111
import {errors} from './errors';
1212
import {GeneratePartial, ManifestEntry} from '../types';
@@ -15,6 +15,11 @@ import {runtimeCachingConverter} from './runtime-caching-converter';
1515
import {stringifyWithoutComments} from './stringify-without-comments';
1616
import {swTemplate} from '../templates/sw-template';
1717

18+
const eta = new Eta({
19+
useWith: true,
20+
autoEscape: false,
21+
})
22+
1823
export function populateSWTemplate({
1924
cacheId,
2025
cleanupOutdatedCaches,
@@ -72,7 +77,7 @@ export function populateSWTemplate({
7277
const moduleRegistry = new ModuleRegistry();
7378

7479
try {
75-
const populatedTemplate = template(swTemplate)({
80+
const populatedTemplate = eta.renderString(swTemplate, {
7681
cacheId,
7782
cleanupOutdatedCaches,
7883
clientsClaim,

test/workbox-build/node/lib/populate-sw-template.js

Lines changed: 106 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@ describe(`[workbox-build] lib/populate-sw-template.js`, function () {
1919
it(`should throw an error if templating fails`, function () {
2020
const manifestEntries = ['ignored'];
2121

22+
// Mock the Eta class and its renderString method to throw an error
2223
const {populateSWTemplate} = proxyquire(MODULE_PATH, {
23-
'lodash/template': () => {
24-
throw new Error();
24+
'eta': {
25+
Eta: class {
26+
renderString() {
27+
throw new Error();
28+
}
29+
},
2530
},
2631
});
2732

@@ -35,7 +40,11 @@ describe(`[workbox-build] lib/populate-sw-template.js`, function () {
3540

3641
it(`should throw an error if both manifestEntries and runtimeCaching are empty`, function () {
3742
const {populateSWTemplate} = proxyquire(MODULE_PATH, {
38-
'lodash/template': () => {},
43+
'eta': {
44+
Eta: class {
45+
renderString() {}
46+
},
47+
},
3948
});
4049

4150
try {
@@ -54,10 +63,16 @@ describe(`[workbox-build] lib/populate-sw-template.js`, function () {
5463
const precacheOptionsString = '{}';
5564
const manifestEntries = ['ignored'];
5665

57-
const innerStub = sinon.stub().returns('');
58-
const outerStub = sinon.stub().returns(innerStub);
66+
// Create a single stub to simulate renderString
67+
const renderStringStub = sinon.stub().returns('');
5968
const {populateSWTemplate} = proxyquire(MODULE_PATH, {
60-
'lodash/template': outerStub,
69+
'eta': {
70+
Eta: class {
71+
constructor() {
72+
this.renderString = renderStringStub;
73+
}
74+
},
75+
},
6176
'./runtime-caching-converter': {
6277
runtimeCachingConverter: () => runtimeCachingPlaceholder,
6378
},
@@ -66,30 +81,30 @@ describe(`[workbox-build] lib/populate-sw-template.js`, function () {
6681

6782
populateSWTemplate({manifestEntries});
6883

69-
expect(outerStub.alwaysCalledWith(swTemplate)).to.be.true;
70-
71-
// Doing a strict comparison with functions isn't easy.
72-
expect(innerStub.args[0][0].use).to.be.a('function');
73-
delete innerStub.args[0][0].use;
74-
75-
expect(innerStub.args[0]).to.eql([
76-
{
77-
manifestEntries,
78-
cacheId: undefined,
79-
cleanupOutdatedCaches: undefined,
80-
clientsClaim: undefined,
81-
disableDevLogs: undefined,
82-
importScripts: undefined,
83-
navigateFallback: undefined,
84-
navigateFallbackDenylist: undefined,
85-
navigateFallbackAllowlist: undefined,
86-
navigationPreload: undefined,
87-
offlineAnalyticsConfigString: undefined,
88-
precacheOptionsString,
89-
runtimeCaching: runtimeCachingPlaceholder,
90-
skipWaiting: undefined,
91-
},
92-
]);
84+
// Eta receives the template as the first argument: args[0][0]
85+
expect(renderStringStub.args[0][0]).to.equal(swTemplate);
86+
87+
// The data is passed as the second argument: args[0][1]
88+
expect(renderStringStub.args[0][1].use).to.be.a('function');
89+
delete renderStringStub.args[0][1].use;
90+
91+
// Compare the data object directly
92+
expect(renderStringStub.args[0][1]).to.eql({
93+
manifestEntries,
94+
cacheId: undefined,
95+
cleanupOutdatedCaches: undefined,
96+
clientsClaim: undefined,
97+
disableDevLogs: undefined,
98+
importScripts: undefined,
99+
navigateFallback: undefined,
100+
navigateFallbackDenylist: undefined,
101+
navigateFallbackAllowlist: undefined,
102+
navigationPreload: undefined,
103+
offlineAnalyticsConfigString: undefined,
104+
precacheOptionsString,
105+
runtimeCaching: runtimeCachingPlaceholder,
106+
skipWaiting: undefined,
107+
});
93108
});
94109

95110
it(`should pass the expected options to the template`, function () {
@@ -115,14 +130,15 @@ describe(`[workbox-build] lib/populate-sw-template.js`, function () {
115130
const precacheOptionsString =
116131
'{\n "directoryIndex": "index.html",\n "ignoreURLParametersMatching": [/a/, /b/]\n}';
117132

118-
// There are two stages in templating: creating the active template function
119-
// from an initial string, and passing variables to that template function
120-
// to get back a final, populated template string.
121-
// We need to stub out both of those steps to test the full flow.
122-
const templatePopulationStub = sinon.stub().returns('');
123-
const templateCreationStub = sinon.stub().returns(templatePopulationStub);
133+
const renderStringStub = sinon.stub().returns('');
124134
const {populateSWTemplate} = proxyquire(MODULE_PATH, {
125-
'lodash/template': templateCreationStub,
135+
'eta': {
136+
Eta: class {
137+
constructor() {
138+
this.renderString = renderStringStub;
139+
}
140+
},
141+
},
126142
'./runtime-caching-converter': {
127143
runtimeCachingConverter: () => runtimeCachingPlaceholder,
128144
},
@@ -148,30 +164,30 @@ describe(`[workbox-build] lib/populate-sw-template.js`, function () {
148164
skipWaiting,
149165
});
150166

151-
expect(templateCreationStub.alwaysCalledWith(swTemplate)).to.be.true;
152-
153-
// Doing a strict comparison with functions isn't easy.
154-
expect(templatePopulationStub.args[0][0].use).to.be.a('function');
155-
delete templatePopulationStub.args[0][0].use;
156-
157-
expect(templatePopulationStub.args[0]).to.eql([
158-
{
159-
cacheId,
160-
cleanupOutdatedCaches,
161-
clientsClaim,
162-
disableDevLogs,
163-
importScripts,
164-
manifestEntries,
165-
navigateFallback,
166-
navigateFallbackDenylist,
167-
navigateFallbackAllowlist,
168-
navigationPreload,
169-
offlineAnalyticsConfigString,
170-
runtimeCaching: runtimeCachingPlaceholder,
171-
precacheOptionsString,
172-
skipWaiting,
173-
},
174-
]);
167+
// Eta receives the template as the first argument: args[0][0]
168+
expect(renderStringStub.args[0][0]).to.equal(swTemplate);
169+
170+
// The data is passed as the second argument: args[0][1]
171+
expect(renderStringStub.args[0][1].use).to.be.a('function');
172+
delete renderStringStub.args[0][1].use;
173+
174+
// Compare the data object directly
175+
expect(renderStringStub.args[0][1]).to.eql({
176+
cacheId,
177+
cleanupOutdatedCaches,
178+
clientsClaim,
179+
disableDevLogs,
180+
importScripts,
181+
manifestEntries,
182+
navigateFallback,
183+
navigateFallbackDenylist,
184+
navigateFallbackAllowlist,
185+
navigationPreload,
186+
offlineAnalyticsConfigString,
187+
runtimeCaching: runtimeCachingPlaceholder,
188+
precacheOptionsString,
189+
skipWaiting,
190+
});
175191
});
176192

177193
it(`should handle a complex offlineGoogleAnalytics value when populating the template`, function () {
@@ -190,10 +206,15 @@ describe(`[workbox-build] lib/populate-sw-template.js`, function () {
190206
const offlineAnalyticsConfigString = `{\n\tparameterOverrides: {\n\t\tcd1: 'offline'\n\t},\n\thitFilter: (params) => {\n \n params.set('cm1', params.get('qt'));\n }\n}`;
191207
const manifestEntries = ['ignored'];
192208

193-
const innerStub = sinon.stub().returns('');
194-
const outerStub = sinon.stub().returns(innerStub);
209+
const renderStringStub = sinon.stub().returns('');
195210
const {populateSWTemplate} = proxyquire(MODULE_PATH, {
196-
'lodash/template': outerStub,
211+
'eta': {
212+
Eta: class {
213+
constructor() {
214+
this.renderString = renderStringStub;
215+
}
216+
},
217+
},
197218
'./runtime-caching-converter': {
198219
runtimeCachingConverter: () => runtimeCachingPlaceholder,
199220
},
@@ -202,29 +223,26 @@ describe(`[workbox-build] lib/populate-sw-template.js`, function () {
202223

203224
populateSWTemplate({manifestEntries, offlineGoogleAnalytics});
204225

205-
expect(outerStub.alwaysCalledWith(swTemplate)).to.be.true;
206-
207-
// Doing a strict comparison with functions isn't easy.
208-
expect(innerStub.args[0][0].use).to.be.a('function');
209-
delete innerStub.args[0][0].use;
210-
211-
expect(innerStub.args[0]).to.eql([
212-
{
213-
manifestEntries,
214-
cacheId: undefined,
215-
cleanupOutdatedCaches: undefined,
216-
clientsClaim: undefined,
217-
disableDevLogs: undefined,
218-
importScripts: undefined,
219-
navigateFallback: undefined,
220-
navigateFallbackDenylist: undefined,
221-
navigateFallbackAllowlist: undefined,
222-
navigationPreload: undefined,
223-
offlineAnalyticsConfigString,
224-
precacheOptionsString,
225-
runtimeCaching: runtimeCachingPlaceholder,
226-
skipWaiting: undefined,
227-
},
228-
]);
226+
expect(renderStringStub.args[0][0]).to.equal(swTemplate);
227+
228+
expect(renderStringStub.args[0][1].use).to.be.a('function');
229+
delete renderStringStub.args[0][1].use;
230+
231+
expect(renderStringStub.args[0][1]).to.eql({
232+
manifestEntries,
233+
cacheId: undefined,
234+
cleanupOutdatedCaches: undefined,
235+
clientsClaim: undefined,
236+
disableDevLogs: undefined,
237+
importScripts: undefined,
238+
navigateFallback: undefined,
239+
navigateFallbackDenylist: undefined,
240+
navigateFallbackAllowlist: undefined,
241+
navigationPreload: undefined,
242+
offlineAnalyticsConfigString,
243+
precacheOptionsString,
244+
runtimeCaching: runtimeCachingPlaceholder,
245+
skipWaiting: undefined,
246+
});
229247
});
230248
});

0 commit comments

Comments
 (0)