Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: add footer and banner for css file #775

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions cmd/esbuild/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,18 @@ var helpText = func(colors logger.Colors) string {
--watch Watch mode: rebuild on file system changes

` + colors.Bold + `Advanced options:` + colors.Default + `
--banner=... Text to be prepended to each output file
--banner=... Text to be prepended to each output JS file
--charset=utf8 Do not escape UTF-8 code points
--color=... Force use of color terminal escapes (true | false)
--error-limit=... Maximum error count or 0 to disable (default 10)
--footer=... Text to be appended to each output file
--footer=... Text to be appended to each output JS file
--inject:F Import the file F into all input files and
automatically replace matching globals with imports
--keep-names Preserve "name" on functions and classes
--log-level=... Disable logging (info | warning | error | silent,
default info)
--css-banner=... Text to be prepended to each output CSS file
--css-footer=... Text to be appended to each output CSS file
--main-fields=... Override the main file order in package.json
(default "browser,module,main" when platform is
browser and "main,module" when platform is node)
Expand Down
14 changes: 13 additions & 1 deletion internal/bundler/linker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4100,6 +4100,7 @@ func (repr *chunkReprCSS) generate(c *linkerContext, chunk *chunkInfo) func(gene
return func(continueData generateContinue) []OutputFile {
waitGroup.Wait()
j := js_printer.Joiner{}
prevOffset := lineColumnOffset{}
newlineBeforeComment := false

// Generate any prefix rules now
Expand Down Expand Up @@ -4134,8 +4135,15 @@ func (repr *chunkReprCSS) generate(c *linkerContext, chunk *chunkInfo) func(gene
}
}

// Start the metadata
jMeta := js_printer.Joiner{}
if len(c.options.CSSBanner) > 0 {
prevOffset.advanceString(c.options.CSSBanner)
prevOffset.advanceString("\n")
j.AddString(c.options.CSSBanner)
j.AddString("\n")
}

// Start the metadata
if c.options.AbsMetadataFile != "" {
isFirstMeta := true
jMeta.AddString("{\n \"imports\": [")
Expand Down Expand Up @@ -4182,6 +4190,10 @@ func (repr *chunkReprCSS) generate(c *linkerContext, chunk *chunkInfo) func(gene
}
}

if len(c.options.CSSFooter) > 0 {
j.AddString(c.options.CSSFooter)
}

// Make sure the file ends with a newline
if j.Length() > 0 && j.LastByte() != '\n' {
j.AddString("\n")
Expand Down
2 changes: 2 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ type Options struct {
InjectedFiles []InjectedFile
Banner string
Footer string
CSSBanner string
CSSFooter string

Plugins []Plugin

Expand Down
4 changes: 4 additions & 0 deletions lib/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ function pushCommonFlags(flags: string[], options: CommonOptions, keys: OptionKe
let keepNames = getFlag(options, keys, 'keepNames', mustBeBoolean);
let banner = getFlag(options, keys, 'banner', mustBeString);
let footer = getFlag(options, keys, 'footer', mustBeString);
let cssBanner = getFlag(options, keys, 'cssBanner', mustBeString);
let cssFooter = getFlag(options, keys, 'cssFooter', mustBeString);

if (sourcesContent !== void 0) flags.push(`--sources-content=${sourcesContent}`);
if (target) {
Expand Down Expand Up @@ -142,6 +144,8 @@ function pushCommonFlags(flags: string[], options: CommonOptions, keys: OptionKe

if (banner) flags.push(`--banner=${banner}`);
if (footer) flags.push(`--footer=${footer}`);
if (cssBanner) flags.push(`--css-banner=${cssBanner}`);
if (cssFooter) flags.push(`--css-footer=${cssFooter}`);
}

function flagsForBuildOptions(
Expand Down
2 changes: 2 additions & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ interface CommonOptions {
keepNames?: boolean;
banner?: string;
footer?: string;
cssBanner?: string;
cssFooter?: string;

color?: boolean;
logLevel?: LogLevel;
Expand Down
4 changes: 4 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ type BuildOptions struct {
Inject []string
Banner string
Footer string
CSSBanner string
CSSFooter string
NodePaths []string // The "NODE_PATH" variable from Node.js

EntryPoints []string
Expand Down Expand Up @@ -326,6 +328,8 @@ type TransformOptions struct {
TsconfigRaw string
Footer string
Banner string
CSSFooter string
CSSBanner string

Define map[string]string
Pure []string
Expand Down
2 changes: 2 additions & 0 deletions pkg/api/api_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,8 @@ func rebuildImpl(
AbsNodePaths: make([]string, len(buildOpts.NodePaths)),
Banner: buildOpts.Banner,
Footer: buildOpts.Footer,
CSSBanner: buildOpts.CSSBanner,
CSSFooter: buildOpts.CSSFooter,
WatchMode: buildOpts.Watch != nil,
Plugins: plugins,
}
Expand Down
16 changes: 16 additions & 0 deletions pkg/cli/cli_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,22 @@ func parseOptionsImpl(osArgs []string, buildOpts *api.BuildOptions, transformOpt
transformOpts.Footer = value
}

case strings.HasPrefix(arg, "--css-banner="):
value := arg[len("--css-banner="):]
if buildOpts != nil {
buildOpts.CSSBanner = value
} else {
transformOpts.CSSBanner = value
}

case strings.HasPrefix(arg, "--css-footer="):
value := arg[len("--css-footer="):]
if buildOpts != nil {
buildOpts.CSSFooter = value
} else {
transformOpts.CSSFooter = value
}

case strings.HasPrefix(arg, "--error-limit="):
value := arg[len("--error-limit="):]
limit, err := strconv.Atoi(value)
Expand Down