Skip to content

feat(codegen): prefer backquotes over double / single quotes#8839

Merged
graphite-app[bot] merged 1 commit intomainfrom
02-02-feat_codegen_prefer_backquotes_over_double___single_quotes
Feb 3, 2025
Merged

feat(codegen): prefer backquotes over double / single quotes#8839
graphite-app[bot] merged 1 commit intomainfrom
02-02-feat_codegen_prefer_backquotes_over_double___single_quotes

Conversation

@sapphi-red
Copy link
Member

@sapphi-red sapphi-red commented Feb 2, 2025

When thinking about why #8443 made the gzip size increased, I thought it maybe because the backquotes appear more than before the PR. So I changed the codegen to prefer backquotes than double/single quotes. It seems this separate change have a positive effect with some projects.
My guess is that in most projects, back quotes does not appear in string literals than double quotes and single quotes.

The gzipped size slightly increases for bundle.min.js, victory.js, antd.js, but those will be decreased by #8443 except for bundle.min.js.

@github-actions github-actions bot added A-codegen Area - Code Generation C-enhancement Category - New feature or request labels Feb 2, 2025
Copy link
Member Author


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@sapphi-red sapphi-red marked this pull request as ready for review February 2, 2025 10:18
@codspeed-hq
Copy link

codspeed-hq bot commented Feb 2, 2025

CodSpeed Performance Report

Merging #8839 will not alter performance

Comparing 02-02-feat_codegen_prefer_backquotes_over_double___single_quotes (b4ee617) with main (84b62c7)

Summary

✅ 33 untouched benchmarks

@sapphi-red
Copy link
Member Author

sapphi-red commented Feb 2, 2025

It seems I need to add allow_backtick: false to more places for TypeScript codegen. done

@sapphi-red sapphi-red marked this pull request as draft February 2, 2025 10:22
@sapphi-red sapphi-red force-pushed the 02-02-feat_codegen_prefer_backquotes_over_double___single_quotes branch 2 times, most recently from df82694 to 3cf8a9c Compare February 3, 2025 06:24
@sapphi-red sapphi-red marked this pull request as ready for review February 3, 2025 06:28
@graphite-app graphite-app bot added the 0-merge Merge with Graphite Merge Queue label Feb 3, 2025
@graphite-app
Copy link
Contributor

graphite-app bot commented Feb 3, 2025

Merge activity

When thinking about why #8443 made the gzip size increased, I thought it maybe because the backquotes appear more than before the PR. So I changed the codegen to prefer backquotes than double/single quotes. It seems this separate change have a positive effect with some projects.
My guess is that in most projects, back quotes does not appear in string literals than double quotes and single quotes.

The gzipped size slightly increases for `bundle.min.js`, `victory.js`, `antd.js`, but those will be decreased by #8443 except for `bundle.min.js`.
@graphite-app graphite-app bot force-pushed the 02-02-feat_codegen_prefer_backquotes_over_double___single_quotes branch from 3cf8a9c to b4ee617 Compare February 3, 2025 07:21
graphite-app bot pushed a commit that referenced this pull request Feb 3, 2025
Compress `"".concat(a, "b", c)` into `` `${a}b${c}` ``.

~~I'm not sure if this should be merged. It works for antd but it doesn't for typescript.~~ Now with #8839, it works well for typescript as well.

**References**
- [Spec of `String::concat`](https://tc39.es/ecma262/multipage/text-processing.html#sec-string.prototype.concat)
- [Spec of template literal](https://tc39.es/ecma262/multipage/ecmascript-language-expressions.html#sec-template-literals-runtime-semantics-evaluation)
@graphite-app graphite-app bot merged commit b4ee617 into main Feb 3, 2025
25 checks passed
@graphite-app graphite-app bot deleted the 02-02-feat_codegen_prefer_backquotes_over_double___single_quotes branch February 3, 2025 07:25
@longlho
Copy link
Contributor

longlho commented Feb 26, 2026

@sapphi-red I'm getting an issue/bug with this. The gist of it is we have something like:

const pathRegex = /(['"])((\w+:(\/?\/?))?[^\s,]+)\1/;

  function getImportPath(importFn) {
    return importFn.toString().match(pathRegex)?.[2] ?? null;
  }

  async function withRetry(importFn) {
    try {
      return await importFn();
    } catch {
      const path = getImportPath(importFn);
      if (path) {
        // Retry with cache-busting to bypass stale CDN cache
        return await import(`${path}?r=${Date.now()}`);
      }
      throw new Error('Dynamic import failed');
    }
  }

  // Usage:
  withRetry(() => import("./MyComponent-abc123.js"));

Before minification, importFn.toString() returns () => import("./MyComponent-abc123.js") — the path is extractable. After Rolldown minification, it returns

() => import(`./MyComponent-abc123.js`)

— the regex no longer matches because backticks aren't quotes.

Terser and esbuild both preserve string literal quote types. The minifier should not convert string literals to template literals, as it changes observable behavior via .toString().

Is there a way to opt out of this behavior?

@sapphi-red
Copy link
Member Author

The minifier should not convert string literals to template literals, as it changes observable behavior via .toString().

No minifier preserves the output of Function.prototype.toString and Oxc won't as well.

I guess you can modify the code to handle backquotes as well.

@longlho
Copy link
Contributor

longlho commented Feb 26, 2026

OK that's fair we'll handle this on our side

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

Labels

0-merge Merge with Graphite Merge Queue A-codegen Area - Code Generation C-enhancement Category - New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants