Skip to content

Commit 219e019

Browse files
CLI: Emit comment when source maps are saved to files (#19447)
See #19362 (comment)
1 parent 7fcdd84 commit 219e019

File tree

4 files changed

+26
-12
lines changed

4 files changed

+26
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Do not wrap `color-mix` in a `@supports` rule if one already exists ([#19450](https://github.com/tailwindlabs/tailwindcss/pull/19450))
1313
- Allow whitespace around `@source inline()` argument ([#19461](https://github.com/tailwindlabs/tailwindcss/pull/19461))
14+
- CLI: Emit comment when source maps are saved to files ([#19447](https://github.com/tailwindlabs/tailwindcss/pull/19447))
1415

1516
### Added
1617

integrations/cli/index.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ describe.each([
752752
expect(map.at(4, 0)).toMatchObject({
753753
source: null,
754754
original: '(none)',
755-
generated: '}...',
755+
generated: '}\n\n/*# sou...',
756756
})
757757
},
758758
)
@@ -815,7 +815,7 @@ describe.each([
815815
expect(map.at(4, 0)).toMatchObject({
816816
source: null,
817817
original: '(none)',
818-
generated: '}...',
818+
generated: '}\n\n/*# sou...',
819819
})
820820
},
821821
)
@@ -1118,7 +1118,7 @@ describe.each([
11181118
expect(map.at(4, 0)).toMatchObject({
11191119
source: null,
11201120
original: '(none)',
1121-
generated: '}...',
1121+
generated: '}\n\n/*# sou...',
11221122
})
11231123

11241124
// Write to project source files
@@ -1190,7 +1190,7 @@ describe.each([
11901190
expect(map.at(7, 0)).toMatchObject({
11911191
source: null,
11921192
original: '(none)',
1193-
generated: '}...',
1193+
generated: '}\n\n/*# sou...',
11941194
})
11951195

11961196
// Write to the main CSS file
@@ -1290,7 +1290,7 @@ describe.each([
12901290
expect(map.at(10, 0)).toMatchObject({
12911291
source: null,
12921292
original: '(none)',
1293-
generated: '}...',
1293+
generated: '}\n\n/*# sou...',
12941294
})
12951295
},
12961296
)

packages/@tailwindcss-cli/src/commands/build/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,18 @@ export async function handle(args: Result<ReturnType<typeof options>>) {
182182
output += `\n`
183183
output += map.inline
184184
} else if (typeof args['--map'] === 'string') {
185+
let basePath =
186+
args['--output'] && args['--output'] !== '-'
187+
? path.dirname(path.resolve(args['--output']))
188+
: process.cwd()
189+
190+
let mapPath = path.resolve(args['--map'])
191+
192+
let relativePath = path.relative(basePath, mapPath)
193+
194+
output += `\n`
195+
output += map.comment(relativePath)
196+
185197
DEBUG && I.start('Write source map')
186198
await outputFile(args['--map'], map.raw)
187199
DEBUG && I.end('Write source map')

packages/@tailwindcss-node/src/source-maps.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export type { DecodedSource, DecodedSourceMap }
66
export interface SourceMap {
77
readonly raw: string
88
readonly inline: string
9+
comment(url: string): string
910
}
1011

1112
function serializeSourceMap(map: DecodedSourceMap): string {
@@ -44,16 +45,16 @@ function serializeSourceMap(map: DecodedSourceMap): string {
4445
export function toSourceMap(map: DecodedSourceMap | string): SourceMap {
4546
let raw = typeof map === 'string' ? map : serializeSourceMap(map)
4647

48+
function comment(url: string) {
49+
return `/*# sourceMappingURL=${url} */\n`
50+
}
51+
4752
return {
4853
raw,
4954
get inline() {
50-
let tmp = ''
51-
52-
tmp += '/*# sourceMappingURL=data:application/json;base64,'
53-
tmp += Buffer.from(raw, 'utf-8').toString('base64')
54-
tmp += ' */\n'
55-
56-
return tmp
55+
let inlined = Buffer.from(raw, 'utf-8').toString('base64')
56+
return comment(`data:application/json;base64,${inlined}`)
5757
},
58+
comment,
5859
}
5960
}

0 commit comments

Comments
 (0)