Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/thirty-tips-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': minor
---

Add support for sourcemaps for emitted CSS files
39 changes: 25 additions & 14 deletions packages/rollup-plugin-import-css/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ export function importCSS(options: ImportCSSOptions): Plugin {
return
}

const hash = getSourceHash(code)
const relativePath = path.relative(rootDirectory, id)
const name = path.basename(relativePath, '.module.css')

const fileName = path.join(
path.dirname(relativePath),
path.format({
name: `${name}-${hash}`,
ext: '.css',
}),
)

// When transforming CSS modules, we want to emit the generated CSS as an
// asset and include the generated file in our generated CSS Modules file
// which contains the classes. This makes sure that if the file containing
Expand All @@ -66,25 +78,24 @@ export function importCSS(options: ImportCSSOptions): Plugin {
cssModuleClasses = json
},
}),
]).process(code, {from: id})
const source = result.css
const hash = getSourceHash(source)
const relativePath = path.relative(rootDirectory, id)
const name = path.basename(relativePath, '.module.css')

const fileName = path.join(
path.dirname(relativePath),
path.format({
name: `${name}-${hash}`,
ext: '.css',
}),
)
]).process(code, {
from: id,
to: fileName,
map: {
inline: false,
},
})

this.emitFile({
type: 'asset',
source,
source: result.css,
fileName,
})
this.emitFile({
type: 'asset',
source: result.map.toString(),
fileName: `${fileName}.map`,
})

const moduleInfo = this.getModuleInfo(id)
const cssSource = `./${path.basename(fileName)}`
Expand Down