Skip to content

Commit

Permalink
Fix content_scripts declaration adding extraneous css
Browse files Browse the repository at this point in the history
Fix #213
  • Loading branch information
cezaraugusto committed Oct 21, 2024
1 parent 96a41cc commit 470a515
Showing 1 changed file with 15 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'path'
import fs from 'fs'
import {type Compiler, Compilation, sources} from 'webpack'
import {getManifestOverrides} from '../manifest-overrides'
import {getFilename, getManifestContent} from '../../../lib/utils'
Expand Down Expand Up @@ -43,16 +44,20 @@ export class UpdateManifest {
(contentObj: {js: string[]; css: string[]}, index: number) => {
if (contentObj.js.length && !contentObj.css.length) {
const outputPath = compiler.options.output?.path || ''

// Make a .css file for every .js file in content_scripts
// so we can later reference it in the manifest.
contentObj.css = contentObj.js.map((js: string) => {
const contentCss = path.join(outputPath, js.replace('.js', '.css'))
return getFilename(
`content_scripts/content-${index}.css`,
contentCss,
{}
)
const contentScriptsPath = path.join(outputPath, 'content_scripts')

// Iterate over the content_scripts output path and for every css file found,
// create a .css file entry so we can later reference it in the manifest.
fs.readdirSync(contentScriptsPath).forEach((file) => {
if (file.endsWith('.css')) {
contentObj.css.push(
getFilename(
`content_scripts/content-${index}.css`,
path.join(contentScriptsPath, file),
{}
)
)
}
})
}

Expand Down Expand Up @@ -97,16 +102,6 @@ export class UpdateManifest {
}
}

// During production, webpack styles are bundled in a CSS file,
// and not injected in the page via <style> tag. We need to
// reference these files in the manifest.
if (compiler.options.mode === 'development') {
if (patchedManifest.content_scripts) {
patchedManifest.content_scripts =
this.applyDevOverrides(patchedManifest)
}
}

const source = JSON.stringify(patchedManifest, null, 2)
const rawSource = new sources.RawSource(source)

Expand Down

0 comments on commit 470a515

Please sign in to comment.