Skip to content

Commit

Permalink
feat: add way to transform extension manifests
Browse files Browse the repository at this point in the history
  • Loading branch information
Loïc Mangeonjean committed Jun 15, 2023
1 parent f4bffc4 commit 97c649d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
16 changes: 12 additions & 4 deletions src/rollup-extension-directory-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,18 @@ interface Options {
exclude?: FilterPattern
isDefaultExtension?: boolean
withCode?: (extensionPath: string) => boolean
rollupPlugins: InputPluginOption[]
rollupPlugins?: InputPluginOption[]
transformManifest?: (manifest: IExtensionManifest) => IExtensionManifest
}

export default function plugin ({ include, exclude, isDefaultExtension = false, withCode = () => true, rollupPlugins }: Options): Plugin {
export default function plugin ({
include,
exclude,
isDefaultExtension = false,
withCode = () => true,
rollupPlugins = [],
transformManifest = manifest => manifest
}: Options): Plugin {
const filter = createFilter(include, exclude)

return {
Expand All @@ -43,7 +51,7 @@ export default function plugin ({ include, exclude, isDefaultExtension = false,
parsed = localizeManifest(parsed, parseJson(id, (await fsPromise.readFile(nlsFile)).toString()))
}
return {
code: dataToEsm(parsed, {
code: dataToEsm(transformManifest(parsed), {
compact: true,
namedExports: false,
preferConst: false
Expand Down Expand Up @@ -80,7 +88,7 @@ export default function plugin ({ include, exclude, isDefaultExtension = false,
if (stat.isDirectory()) {
// Load the extension directory as a module importing the required files and registering the extension
const manifestPath = path.resolve(id, 'package.json')
const manifest = JSON.parse((await fsPromise.readFile(manifestPath)).toString('utf8'))
const manifest = transformManifest(parseJson<IExtensionManifest>(id, (await fsPromise.readFile(manifestPath)).toString('utf8')))
try {
const extensionResources = await extractResourcesFromExtensionManifest(manifest, async (resourcePath) => {
return (await fsPromise.readFile(path.join(id, resourcePath)))
Expand Down
18 changes: 11 additions & 7 deletions src/rollup-vsix-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ interface Options {
include?: FilterPattern
exclude?: FilterPattern
withCode?: (extensionPath: string) => boolean
rollupPlugins: InputPluginOption[]
rollupPlugins?: InputPluginOption[]
transformManifest?: (manifest: IExtensionManifest) => IExtensionManifest
}

function read (stream: Readable): Promise<Buffer> {
Expand Down Expand Up @@ -61,8 +62,9 @@ function getVsixPath (file: string) {
export default function plugin ({
include = '**/*.vsix',
exclude,
rollupPlugins,
withCode = () => true
rollupPlugins = [],
withCode = () => true,
transformManifest = manifest => manifest
}: Options): Plugin {
const filter = createFilter(include, exclude)
const vsixFiles: Record<string, Record<string, Buffer>> = {}
Expand Down Expand Up @@ -110,11 +112,13 @@ export default function plugin ({
const file = match[2]!
const vsixFile = vsixFiles[match[1]!]!
let parsed = parseJson<IExtensionManifest>(id, vsixFile[file]!.toString('utf8'))
if (file === 'package.json' && 'package.nls.json' in vsixFile) {
parsed = localizeManifest(parsed, parseJson(id, vsixFile['package.nls.json']!.toString()))
if (file === 'package.json') {
if ('package.nls.json' in vsixFile) {
parsed = localizeManifest(parsed, parseJson(id, vsixFile['package.nls.json']!.toString()))
}
}
return {
code: dataToEsm(parsed, {
code: dataToEsm(transformManifest(parsed), {
compact: true,
namedExports: false,
preferConst: false
Expand All @@ -125,7 +129,7 @@ export default function plugin ({
if (!filter(id)) return null

const files = await readVsix(id)
const manifest = parseJson<IExtensionManifest>(id, files['package.json']!.toString('utf8'))
const manifest = transformManifest(parseJson<IExtensionManifest>(id, files['package.json']!.toString('utf8')))

const extensionResources = (await extractResourcesFromExtensionManifest(manifest, async path => {
return files[getVsixPath(path)]!
Expand Down

0 comments on commit 97c649d

Please sign in to comment.