Skip to content

Commit

Permalink
Fix public/ output path not working consistently
Browse files Browse the repository at this point in the history
  • Loading branch information
cezaraugusto committed Aug 26, 2024
1 parent 86fa0a9 commit 9d7371c
Show file tree
Hide file tree
Showing 31 changed files with 172 additions and 166 deletions.
6 changes: 1 addition & 5 deletions programs/create/create.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,7 @@ describe('extension create', () => {
it.each(ALL_TEMPLATES)(
`creates the "$name" extension template`,
async (template) => {
const templatePath = path.join(
__dirname,
'dist',
template.name
)
const templatePath = path.join(__dirname, 'dist', template.name)

await extensionCreate(templatePath, {
template: template.name,
Expand Down
8 changes: 8 additions & 0 deletions programs/develop/webpack/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,11 @@ export const CERTIFICATE_DESTINATION_PATH = path.join(
process.cwd(),
'node_modules/extension-develop/dist/certs'
)

export const CHROMIUM_BASED_BROWSERS = ['chrome', 'edge']
export const GECKO_BASED_BROWSERS = ['firefox']

export const SUPPORTED_BROWSERS = [
...CHROMIUM_BASED_BROWSERS,
...GECKO_BASED_BROWSERS
]
34 changes: 20 additions & 14 deletions programs/develop/webpack/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ export function isFromFilepathList(

export function getFilename(
feature: string,
filepath: string,
exclude: string[]
filePath: string,
excludeList: FilepathList
) {
const entryExt = path.extname(filepath)
const entryExt = path.extname(filePath)

// Do not attempt to rewrite the asset path if it's in the exclude list.
const shouldSkipRewrite = shouldExclude(filepath, {exclude})
const skipPathResolve = shouldExclude(filePath, excludeList)

let fileOutputpath = shouldSkipRewrite ? path.normalize(filepath) : feature
let fileOutputpath = skipPathResolve ? path.normalize(filePath) : feature

if (['.js', '.jsx', '.tsx', '.ts'].includes(entryExt)) {
fileOutputpath = fileOutputpath.replace(entryExt, '.js')
Expand All @@ -61,8 +61,8 @@ export function getFilename(
/**
* Change the path from win style to unix style
*/
export function unixify(filepath: string) {
return filepath.replace(/\\/g, '/')
export function unixify(filePath: string) {
return filePath.replace(/\\/g, '/')
}

export function shouldExclude(
Expand All @@ -74,17 +74,23 @@ export function shouldExclude(
}

const unixifiedFilePath = unixify(filePath)
return Object.values(ignorePatterns).some((pattern) => {
if (typeof pattern !== 'string') {
return false
}

const _pattern = unixify(pattern)
const isFilePathInExcludedList = Object.values(ignorePatterns).some(
(pattern) => {
if (typeof pattern !== 'string') {
return false
}

return unixifiedFilePath.endsWith(_pattern)
})
const _pattern = unixify(pattern).replace(/\/$/, '')

return unixifiedFilePath.includes(_pattern)
}
)

return isFilePathInExcludedList
}


export function getManifestContent(
compilation: Compilation,
manifestPath: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {type Manifest} from '../../../../webpack-types'

export function jsonFields(
context: string,

manifest: Manifest
): Record<string, string | undefined> {
return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {type Manifest} from '../../../../webpack-types'
import {type Manifest, type FilepathList} from '../../../../webpack-types'
import {getFilename} from '../../../../lib/utils'

export function backgroundPage(manifest: Manifest, exclude: string[]) {
export function backgroundPage(manifest: Manifest, excludeList: FilepathList) {
return (
manifest.background &&
manifest.background.page && {
Expand All @@ -11,7 +11,7 @@ export function backgroundPage(manifest: Manifest, exclude: string[]) {
page: getFilename(
'background/page.html',
manifest.background.page as string,
exclude
excludeList
)
})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
import {type Manifest} from '../../../../webpack-types'
import {type Manifest, type FilepathList} from '../../../../webpack-types'
import {getFilename} from '../../../../lib/utils'

export function chromeUrlOverrides(manifest: Manifest, exclude: string[]) {
export function chromeUrlOverrides(
manifest: Manifest,
excludeList: FilepathList
) {
return (
manifest.chrome_url_overrides && {
chrome_url_overrides: {
...(manifest.chrome_url_overrides.bookmarks && {
bookmarks: getFilename(
'chrome_url_overrides/bookmarks.html',
manifest.chrome_url_overrides.bookmarks,
exclude
excludeList
)
}),
...(manifest.chrome_url_overrides.history && {
history: getFilename(
'chrome_url_overrides/history.html',
manifest.chrome_url_overrides.history,
exclude
excludeList
)
}),
...(manifest.chrome_url_overrides.newtab && {
newtab: getFilename(
'chrome_url_overrides/newtab.html',
manifest.chrome_url_overrides.newtab,
exclude
excludeList
)
})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {type Manifest} from '../../../../webpack-types'
import {type Manifest, type FilepathList} from '../../../../webpack-types'
import {getFilename} from '../../../../lib/utils'

interface ContentObj {
js?: string[] | undefined
css?: string[] | undefined
}

export function contentScripts(manifest: Manifest, exclude: string[]) {
export function contentScripts(manifest: Manifest, excludeList: FilepathList) {
return (
manifest.content_scripts && {
content_scripts: manifest.content_scripts.map(
Expand All @@ -27,7 +27,7 @@ export function contentScripts(manifest: Manifest, exclude: string[]) {
return getFilename(
`content_scripts/content-${index}.js`,
js,
exclude
excludeList
)
})
],
Expand All @@ -36,7 +36,7 @@ export function contentScripts(manifest: Manifest, exclude: string[]) {
return getFilename(
`content_scripts/content-${index}.css`,
css,
exclude
excludeList
)
})
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import {type Manifest} from '../../../../webpack-types'
import {type Manifest, type FilepathList} from '../../../../webpack-types'
import {getFilename} from '../../../../lib/utils'

// A DevTools extension adds functionality to the Chrome DevTools.
// It can add new UI panels and sidebars, interact with the
// inspected page, get information about network requests, and more.
export function devtoolsPage(manifest: Manifest, exclude: string[]) {
export function devtoolsPage(manifest: Manifest, excludeList: FilepathList) {
return (
manifest.devtools_page && {
devtools_page: getFilename(
'devtools_page.html',
manifest.devtools_page,
exclude
excludeList
)
}
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import path from 'path'
import {type Manifest} from '../../../../webpack-types'
import {type Manifest, type FilepathList} from '../../../../webpack-types'
import {getFilename} from '../../../../lib/utils'

const getBasename = (filepath: string) => path.basename(filepath)

export function icons(manifest: Manifest, exclude: string[]) {
export function icons(manifest: Manifest, excludeList: FilepathList) {
return (
manifest.icons && {
icons: Object.fromEntries(
Object.entries(manifest.icons).map(([size, icon]) => {
return [
size,
getFilename(`icons/${getBasename(icon)}`, icon, exclude)
getFilename(`icons/${getBasename(icon)}`, icon, excludeList)
]
})
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@ import {storage} from './storage'
import {theme} from './theme'
import {userScripts} from './user_scripts'
import {webAccessibleResources} from './web_accessible_resources'
import {type Manifest} from '../../../../webpack-types'
import {type Manifest, type FilepathList} from '../../../../webpack-types'

export function manifestCommon(manifest: Manifest, exclude: string[]) {
export function manifestCommon(manifest: Manifest, excludeList: FilepathList) {
return {
...backgroundPage(manifest, exclude),
...chromeUrlOverrides(manifest, exclude),
...contentScripts(manifest, exclude),
...devtoolsPage(manifest, exclude),
...icons(manifest, exclude),
...optionsPage(manifest, exclude),
...optionsUi(manifest, exclude),
...pageAction(manifest, exclude),
...sandbox(manifest, exclude),
...sidePanel(manifest, exclude),
...sidebarAction(manifest, exclude),
...storage(manifest, exclude),
...theme(manifest, exclude),
...userScripts(manifest, exclude),
...backgroundPage(manifest, excludeList),
...chromeUrlOverrides(manifest, excludeList),
...contentScripts(manifest, excludeList),
...devtoolsPage(manifest, excludeList),
...icons(manifest, excludeList),
...optionsPage(manifest, excludeList),
...optionsUi(manifest, excludeList),
...pageAction(manifest, excludeList),
...sandbox(manifest, excludeList),
...sidePanel(manifest, excludeList),
...sidebarAction(manifest, excludeList),
...storage(manifest, excludeList),
...theme(manifest, excludeList),
...userScripts(manifest, excludeList),
...webAccessibleResources(manifest)
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {type Manifest} from '../../../../webpack-types'
import {type Manifest, type FilepathList} from '../../../../webpack-types'
import {getFilename} from '../../../../lib/utils'

export function optionsPage(manifest: Manifest, exclude: string[]) {
export function optionsPage(manifest: Manifest, excludeList: FilepathList) {
return (
manifest.options_page && {
options_page: getFilename(
'options_ui/page.html',
manifest.options_page,
exclude
excludeList
)
}
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {type Manifest} from '../../../../webpack-types'
import {type Manifest, type FilepathList} from '../../../../webpack-types'
import {getFilename} from '../../../../lib/utils'

export function optionsUi(manifest: Manifest, exclude: string[]) {
export function optionsUi(manifest: Manifest, excludeList: FilepathList) {
return (
manifest.options_ui && {
options_ui: {
Expand All @@ -10,7 +10,7 @@ export function optionsUi(manifest: Manifest, exclude: string[]) {
page: getFilename(
'options_ui/page.html',
manifest.options_ui.page,
exclude
excludeList
)
})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import path from 'path'
import {type Manifest} from '../../../../webpack-types'
import {type Manifest, type FilepathList} from '../../../../webpack-types'
import {getFilename} from '../../../../lib/utils'

const getBasename = (filepath: string) => path.basename(filepath)
export function pageAction(manifest: Manifest, exclude: string[]) {
export function pageAction(manifest: Manifest, excludeList: FilepathList) {
return (
manifest.page_action && {
page_action: {
Expand All @@ -12,7 +12,7 @@ export function pageAction(manifest: Manifest, exclude: string[]) {
default_popup: getFilename(
'page_action/default_popup.html',
manifest.page_action.default_popup as string,
exclude
excludeList
)
}),
...(manifest.page_action.default_icon && {
Expand All @@ -23,15 +23,19 @@ export function pageAction(manifest: Manifest, exclude: string[]) {
manifest.page_action.default_icon as string
)}`,
manifest.page_action.default_icon as string,
exclude
excludeList
)
: Object.fromEntries(
Object.entries(
manifest.page_action.default_icon as Record<string, string>
).map(([size, icon]) => {
return [
size,
getFilename(`icons/${getBasename(icon)}`, icon, exclude)
getFilename(
`icons/${getBasename(icon)}`,
icon,
excludeList
)
]
})
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import {type Manifest} from '../../../../webpack-types'
import {type Manifest, type FilepathList} from '../../../../webpack-types'
import {getFilename} from '../../../../lib/utils'

// Defines an collection of app or extension pages that are to be served
// in a sandboxed unique origin, and optionally a Content Security Policy
// to use with them.
export function sandbox(manifest: Manifest, exclude: string[]) {
export function sandbox(manifest: Manifest, excludeList: FilepathList) {
return (
manifest.sandbox && {
sandbox: {
...manifest.sandbox,
...(manifest.sandbox.pages && {
pages: manifest.sandbox.pages.map((page: string, index: number) => {
return getFilename(`sandbox/page-${index}.html`, page, exclude)
return getFilename(`sandbox/page-${index}.html`, page, excludeList)
})
})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import path from 'path'
import {type Manifest} from '../../../../webpack-types'
import {type Manifest, type FilepathList} from '../../../../webpack-types'
import {getFilename} from '../../../../lib/utils'

const getBasename = (filepath: string) => path.basename(filepath)
export function sidebarAction(manifest: Manifest, exclude: string[]) {
export function sidebarAction(manifest: Manifest, excludeList: FilepathList) {
return (
manifest.sidebar_action && {
sidebar_action: {
Expand All @@ -12,7 +12,7 @@ export function sidebarAction(manifest: Manifest, exclude: string[]) {
default_panel: getFilename(
`sidebar_action/default_panel.html`,
manifest.sidebar_action.default_panel as string,
exclude
excludeList
)
}),

Expand All @@ -22,7 +22,7 @@ export function sidebarAction(manifest: Manifest, exclude: string[]) {
manifest.sidebar_action.default_icon as string
)}`,
manifest.sidebar_action.default_icon as string,
exclude
excludeList
)
})
}
Expand Down
Loading

0 comments on commit 9d7371c

Please sign in to comment.