Skip to content

Commit

Permalink
feat: localize extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Loïc Mangeonjean committed Apr 22, 2023
1 parent 494affb commit 268233d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
10 changes: 7 additions & 3 deletions rollup/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,15 +370,18 @@ export default (args: Record<string, string>): rollup.RollupOptions[] => {
// load extension directory as a module that loads the extension
if (path.dirname(id) === DEFAULT_EXTENSIONS_PATH) {
const manifestPath = path.resolve(id, 'package.json')
const manifestNlsPath = path.resolve(id, 'package.nls.json')
const manifest = JSON.parse((await fsPromise.readFile(manifestPath)).toString('utf8'))
const nlsExists = fs.existsSync(manifestNlsPath)
try {
const filePaths = extractPathsFromExtensionManifest(manifest.contributes)
return `
import manifest from '${manifestPath}'
${nlsExists ? `import nls from '${manifestNlsPath}'` : ''}
import { registerExtension } from '../src/extensions'
import { onExtHostInitialized } from '../src/vscode-services/extHost'
onExtHostInitialized(() => {
const { registerFile } = registerExtension(manifest)
const { registerFile } = registerExtension(manifest${nlsExists ? ', nls' : ''})
${filePaths.map(filePath => (`
registerFile('${filePath}', async () => await import('${path.resolve(id, filePath)}'))`))}
})
Expand All @@ -392,8 +395,9 @@ ${filePaths.map(filePath => (`
},
transform (code, id) {
if (path.dirname(id).startsWith(DEFAULT_EXTENSIONS_PATH + '/')) {
if (path.basename(id) === 'package.json') {
// Load extension package.json as a json
const basename = path.basename(id)
if (['package.json', 'package.nls.json'].includes(basename)) {
// Load extension package.json and package.nls.json as a json
const parsed = parse(code)
return {
code: dataToEsm(parsed, {
Expand Down
11 changes: 8 additions & 3 deletions src/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Severity from 'vs/base/common/severity'
import { localize } from 'vs/nls'
import { Registry } from 'vs/platform/registry/common/platform'
import { ConfigurationScope, IConfigurationRegistry, Extensions as ConfigurationExtensions } from 'vs/platform/configuration/common/configurationRegistry'
import { ITranslations, localizeManifest } from 'vs/platform/extensionManagement/common/extensionNls'
import * as api from './api'
import { registerExtensionFile } from './service-override/files'
import createLanguagesApi from './vscode-services/languages'
Expand Down Expand Up @@ -133,16 +134,19 @@ interface RegisterExtensionResult extends IDisposable {
registerFile: (path: string, getContent: () => Promise<string>) => IDisposable
dispose (): void
}
export function registerExtension (manifest: IExtensionManifest): RegisterExtensionResult {

export function registerExtension (manifest: IExtensionManifest, defaultNLS?: ITranslations): RegisterExtensionResult {
const uuid = generateUuid()
const location = URI.from({ scheme: 'extension', path: `/${uuid}` })

const localizedManifest = defaultNLS != null ? localizeManifest(manifest, defaultNLS) : manifest

const extension: IExtension = {
manifest,
manifest: localizedManifest,
type: ExtensionType.User,
isBuiltin: false,
identifier: {
id: getExtensionId(manifest.publisher, manifest.name),
id: getExtensionId(localizedManifest.publisher, localizedManifest.name),
uuid
},
location,
Expand All @@ -167,6 +171,7 @@ export function registerExtension (manifest: IExtensionManifest): RegisterExtens

export {
IExtensionManifest,
ITranslations,
IExtensionContributions,
onExtHostInitialized
}
6 changes: 4 additions & 2 deletions src/rollup-vsix-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ export default function plugin (options: Options = defaultOptions): Plugin {
const files = await readVsix(id)
const manifest = JSON.parse(files['package.json']!.toString('utf8'))

const usedFiles = ['package.json', ...extractPathsFromExtensionManifest(manifest.contributes)]
const usedFiles = ['package.json', 'package.nls.json', ...extractPathsFromExtensionManifest(manifest.contributes)]
const nlsExists = files['package.nls.json'] != null

const vsixFile: Record<string, Buffer> = usedFiles.reduce((acc, usedFile) => {
const filePath = files[path.relative('/', path.resolve('/', usedFile))]
Expand All @@ -112,9 +113,10 @@ export default function plugin (options: Options = defaultOptions): Plugin {

return `
import manifest from 'vsix:${id}:package.json'
${nlsExists ? `import nls from 'vsix:${id}:package.nls.json'` : ''}
import { registerExtension, onExtHostInitialized } from 'vscode/extensions'
onExtHostInitialized(() => {
const { registerFile } = registerExtension(manifest)
const { registerFile } = registerExtension(manifest${nlsExists ? ', nls' : ''})
${Object.keys(vsixFile).map((filePath) => (`
registerFile('${filePath}', async () => (await import('vsix:${id}:${filePath}.raw')).default)`))}
})
Expand Down

0 comments on commit 268233d

Please sign in to comment.