Skip to content

Commit 7c1e87c

Browse files
committed
Provide a flag to auto-prefix local css classes when minified
1 parent d34e79e commit 7c1e87c

File tree

8 files changed

+33
-2
lines changed

8 files changed

+33
-2
lines changed

cmd/esbuild/main.go

+2
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ var helpText = func(colors logger.Colors) string {
9393
eof | linked | external, default eof when bundling
9494
and inline otherwise)
9595
--line-limit=... Lines longer than this will be wrap onto a new line
96+
--local-css-prefix=... Add a prefix to local-css classes if
97+
minify-identifiers is enabled
9698
--log-level=... Disable logging (verbose | debug | info | warning |
9799
error | silent, default info)
98100
--log-limit=... Maximum message count or 0 to disable (default 6)

internal/config/config.go

+1
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ type Options struct {
474474
TreeShaking bool
475475
DropDebugger bool
476476
MangleQuoted bool
477+
LocalCSSPrefix string
477478
Platform Platform
478479
OutputFormat Format
479480
NeedsMetafile bool

internal/linker/linker.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -493,10 +493,10 @@ func (c *linkerContext) mangleLocalCSS(usedLocalNames map[string]bool) {
493493
nextName := 0
494494

495495
for _, symbolCount := range sorted {
496-
name := minifier.NumberToMinifiedName(nextName)
496+
name := c.options.LocalCSSPrefix + minifier.NumberToMinifiedName(nextName)
497497
for globalNames[name] || usedLocalNames[name] {
498498
nextName++
499-
name = minifier.NumberToMinifiedName(nextName)
499+
name = c.options.LocalCSSPrefix + minifier.NumberToMinifiedName(nextName)
500500
}
501501

502502
// Turn this local name into a global one

lib/shared/common.ts

+2
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ function flagsForBuildOptions(
277277
let write = getFlag(options, keys, 'write', mustBeBoolean) ?? writeDefault; // Default to true if not specified
278278
let allowOverwrite = getFlag(options, keys, 'allowOverwrite', mustBeBoolean)
279279
let mangleCache = getFlag(options, keys, 'mangleCache', mustBeObject)
280+
let localCSSPrefix = getFlag(options, keys, 'localCSSPrefix', mustBeString)
280281
keys.plugins = true; // "plugins" has already been read earlier
281282
checkForInvalidFlags(options, keys, `in ${callName}() call`)
282283

@@ -304,6 +305,7 @@ function flagsForBuildOptions(
304305
if (entryNames) flags.push(`--entry-names=${entryNames}`)
305306
if (chunkNames) flags.push(`--chunk-names=${chunkNames}`)
306307
if (assetNames) flags.push(`--asset-names=${assetNames}`)
308+
if (localCSSPrefix) flags.push(`--local-css-prefix=${localCSSPrefix}`)
307309
if (mainFields) {
308310
let values: string[] = []
309311
for (let value of mainFields) {

lib/shared/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ export interface BuildOptions extends CommonOptions {
168168
absWorkingDir?: string
169169
/** Documentation: https://esbuild.github.io/api/#node-paths */
170170
nodePaths?: string[]; // The "NODE_PATH" variable from Node.js
171+
/** Documentation: https://esbuild.github.io/api/#local-css-prefix */
172+
localCSSPrefix?: string;
171173
}
172174

173175
export interface StdinOptions {

pkg/api/api.go

+1
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ type BuildOptions struct {
296296
TreeShaking TreeShaking // Documentation: https://esbuild.github.io/api/#tree-shaking
297297
IgnoreAnnotations bool // Documentation: https://esbuild.github.io/api/#ignore-annotations
298298
LegalComments LegalComments // Documentation: https://esbuild.github.io/api/#legal-comments
299+
LocalCSSPrefix string // Documentation: https://esbuild.github.io/api/#local-css-prefix
299300

300301
JSX JSX // Documentation: https://esbuild.github.io/api/#jsx-mode
301302
JSXFactory string // Documentation: https://esbuild.github.io/api/#jsx-factory

pkg/api/api_impl.go

+19
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/evanw/esbuild/internal/compat"
2929
"github.com/evanw/esbuild/internal/config"
3030
"github.com/evanw/esbuild/internal/css_ast"
31+
"github.com/evanw/esbuild/internal/css_lexer"
3132
"github.com/evanw/esbuild/internal/fs"
3233
"github.com/evanw/esbuild/internal/graph"
3334
"github.com/evanw/esbuild/internal/helpers"
@@ -524,6 +525,23 @@ func validateJSXExpr(log logger.Log, text string, name string) config.DefineExpr
524525
return config.DefineExpr{}
525526
}
526527

528+
func validateCSSIdentifier(log logger.Log, ident string) string {
529+
for i, c := range ident {
530+
if i == 0 {
531+
if !css_lexer.IsNameStart(c) {
532+
log.AddError(nil, logger.Range{}, fmt.Sprintf("Invalid CSS prefix: %q", ident))
533+
return ""
534+
}
535+
} else {
536+
if !css_lexer.IsNameContinue(c) {
537+
log.AddError(nil, logger.Range{}, fmt.Sprintf("Invalid CSS prefix: %q", ident))
538+
return ""
539+
}
540+
}
541+
}
542+
return ident
543+
}
544+
527545
func validateDefines(
528546
log logger.Log,
529547
defines map[string]string,
@@ -1257,6 +1275,7 @@ func validateBuildOptions(
12571275
MangleProps: validateRegex(log, "mangle props", buildOpts.MangleProps),
12581276
ReserveProps: validateRegex(log, "reserve props", buildOpts.ReserveProps),
12591277
MangleQuoted: buildOpts.MangleQuoted == MangleQuotedTrue,
1278+
LocalCSSPrefix: validateCSSIdentifier(log, buildOpts.LocalCSSPrefix),
12601279
DropLabels: append([]string{}, buildOpts.DropLabels...),
12611280
DropDebugger: (buildOpts.Drop & DropDebugger) != 0,
12621281
AllowOverwrite: buildOpts.AllowOverwrite,

pkg/cli/cli_impl.go

+4
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,10 @@ func parseOptionsImpl(
204204
value := arg[len("--mangle-cache="):]
205205
extras.mangleCache = &value
206206

207+
case strings.HasPrefix(arg, "--local-css-prefix=") && buildOpts != nil:
208+
value := arg[len("--local-css-prefix="):]
209+
buildOpts.LocalCSSPrefix = value
210+
207211
case strings.HasPrefix(arg, "--drop:"):
208212
value := arg[len("--drop:"):]
209213
switch value {

0 commit comments

Comments
 (0)