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

Fix content_scripts declaration adding extraneous css #215

Merged
merged 1 commit into from
Oct 28, 2024
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
53 changes: 49 additions & 4 deletions programs/develop/build.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
DEFAULT_TEMPLATE,
SUPPORTED_BROWSERS
} from '../../examples/data'
import {extensionBuild} from './dist/module'
import {extensionBuild, Manifest} from './dist/module'

async function removeDir(dirPath: string) {
if (fs.existsSync(dirPath)) {
Expand Down Expand Up @@ -48,6 +48,7 @@ function distFileExists(
)

if (filePath) {
console.log('3iuteghkgewktgdtqwegdhiq3thegd', path.join(templatePath, filePath))
return fs.existsSync(path.join(templatePath, filePath))
} else {
// Check if any HTML file exists in the directory
Expand Down Expand Up @@ -86,11 +87,55 @@ describe('extension build', () => {
browser: SUPPORTED_BROWSERS[0] as 'chrome'
})

expect.assertions(1)

expect(
path.join(templatePath, SUPPORTED_BROWSERS[0], 'manifest.json')
path.join(
templatePath,
'dist',
SUPPORTED_BROWSERS[0],
'manifest.json'
)
).toBeTruthy()

const manifestText = fs.readFileSync(
path.join(
templatePath,
'dist',
SUPPORTED_BROWSERS[0],
'manifest.json'
),
'utf-8'
)

const manifest: Manifest = JSON.parse(manifestText)
expect(manifest.name).toBeTruthy
expect(manifest.version).toBeTruthy
expect(manifest.manifest_version).toBeTruthy

if (template.name.includes('content')) {
expect(manifest.content_scripts![0].css![0]).toEqual(
'content_scripts/content-0.css'
)

expect(manifest.content_scripts![0].js![0]).toEqual(
'content_scripts/content-0.js'
)

expect(
distFileExists(
template.name,
SUPPORTED_BROWSERS[0],
'content_scripts/content-0.css'
)
).toBeTruthy()

expect(
distFileExists(
template.name,
SUPPORTED_BROWSERS[0],
'content_scripts/content-0.js'
)
).toBeTruthy()
}
},
80000
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import path from 'path'
import {type Compiler, Compilation, sources} from 'webpack'
import {Compiler, Compilation, sources} from 'webpack'
import {getManifestOverrides} from '../manifest-overrides'
import {getFilename, getManifestContent} from '../../../lib/utils'
import {
type FilepathList,
type PluginInterface,
type Manifest
} from '../../../webpack-types'
import {FilepathList, PluginInterface, Manifest} from '../../../webpack-types'

export class UpdateManifest {
public readonly manifestPath: string
Expand Down Expand Up @@ -34,31 +30,30 @@ export class UpdateManifest {
}

private applyProdOverrides(
compiler: Compiler,
compilation: Compilation,
overrides: Record<string, any>
) {
if (!overrides.content_scripts) return {}

return overrides.content_scripts.map(
(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,
{}
)
})
}

return contentObj
}
)
const outputPath = compilation.options.output?.path || ''

// Collect all CSS assets in `content_scripts` for use in the manifest
const contentScriptsCss = compilation
.getAssets()
.filter(
(asset) =>
asset.name.includes('content_scripts') && asset.name.endsWith('.css')
)
.map((asset) => path.join(outputPath, asset.name))

// Assign the collected CSS files to each `content_scripts` entry
for (const contentObj of overrides.content_scripts) {
contentObj.css = contentScriptsCss.map((cssFilePath, index) =>
getFilename(`content_scripts/content-${index}.css`, cssFilePath, {})
)
}

return overrides.content_scripts
}

apply(compiler: Compiler) {
Expand Down Expand Up @@ -97,16 +92,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 All @@ -119,7 +104,7 @@ export class UpdateManifest {
// in a content_scripts CSS file, so we need to reference it
// in the manifest.
if (compiler.options.mode === 'production') {
compilation.hooks.afterProcessAssets.tap(
compilation.hooks.afterOptimizeAssets.tap(
'manifest:update-manifest',
() => {
if (compilation.errors.length > 0) return
Expand All @@ -143,7 +128,7 @@ export class UpdateManifest {

if (patchedManifest.content_scripts) {
patchedManifest.content_scripts = this.applyProdOverrides(
compiler,
compilation,
patchedManifest
)
}
Expand Down