Skip to content

refactor(workbox-build): remove useWith from Eta template configuration#3513

Open
rtritto wants to merge 1 commit into
GoogleChrome:v7from
rtritto:remove-use-with
Open

refactor(workbox-build): remove useWith from Eta template configuration#3513
rtritto wants to merge 1 commit into
GoogleChrome:v7from
rtritto:remove-use-with

Conversation

@rtritto
Copy link
Copy Markdown
Contributor

@rtritto rtritto commented May 8, 2026

This PR refactors the Eta template rendering configuration within workbox-build by removing the useWith: true option.

Using with () { ... } blocks (which useWith relies on) is generally considered bad practice in modern JavaScript due to performance implications, potential scoping issues, and incompatibility with Strict Mode.

To accommodate this change, the service worker template (sw-template.ts) has been updated to explicitly reference injected variables using the default it. object property, ensuring compatibility and cleaner variable resolution during template compilation.

Changes

  • packages/workbox-build/src/lib/populate-sw-template.ts: Removed useWith: true from the Eta initialization options.
  • packages/workbox-build/src/templates/sw-template.ts: Updated all template variable interpolations (e.g., <%= importScripts %>, <%= use(...) %>) to use the explicit it. prefix (e.g., <%= it.importScripts %>, <%= it.use(...) %>).

Reference

with () {} in JavaScript slows down execution and can cause confusing bugs from https://dev.to/bgub/i-built-a-js-template-engine-3x-faster-than-ejs-lj8

@google-cla
Copy link
Copy Markdown

google-cla Bot commented May 8, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@rtritto
Copy link
Copy Markdown
Contributor Author

rtritto commented May 8, 2026

FYI @jayaddison

@rtritto rtritto force-pushed the remove-use-with branch from 4ff59de to 60ec7c9 Compare May 8, 2026 11:42
@jayaddison
Copy link
Copy Markdown

Thanks @rtritto. Could we enable strict mode for the template evaluation?

@rtritto
Copy link
Copy Markdown
Contributor Author

rtritto commented May 8, 2026

Nice catch, I did the commit

@rtritto rtritto force-pushed the remove-use-with branch from b38e680 to 60ec7c9 Compare May 8, 2026 12:09
@rtritto
Copy link
Copy Markdown
Contributor Author

rtritto commented May 8, 2026

Removed the commit.

The reason useStrict is not a valid configuration option in modern versions of Eta is that simply removing useWith: true already solves the core issue.

Without useWith: true, Eta no longer wraps the template data in a with(it) { ... } block. Instead, it compiles the template into a standard, clean JavaScript function.

Since modern Node.js and TypeScript project environments (especially those using ES Modules or transpilers) run in Strict > Mode by default, the generated template function will naturally inherit this Strict Mode. This automatically provides the expected performance optimizations and security benefits.

If you absolutely need to explicitly enforce the "use strict"; directive inside the compiled function body, the supported way to do this in Eta is to add it directly at the very top of your template file sw-template.ts:

<% "use strict"; %>
/**
 * Welcome to your Workbox-powered service worker!
 ...

However, in most modern codebases, just removing useWith: true and explicitly referencing variables with it. is the correct and sufficient approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants