Skip to content

Commit

Permalink
fix: parse extension jsons using vscode method
Browse files Browse the repository at this point in the history
  • Loading branch information
Loïc Mangeonjean committed Apr 22, 2023
1 parent c2d9714 commit 1bd5327
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@
"compile": "NODE_OPTIONS=--max_old_space_size=8192 rollup --config rollup/rollup.config.ts --configPlugin 'typescript={tsconfig: `tsconfig.rollup.json`}' --vscode-version ${npm_package_config_vscode_version}",
"clean": "rm -rf dist/",
"compile-webpack-loader": "tsc --target es2020 -lib es2020 --module commonjs --skipLibCheck --moduleResolution node --declaration --outDir dist src/webpack-loader.ts && mv dist/webpack-loader.js dist/webpack-loader.cjs",
"compile-rollup-vsix-plugin": "tsc --target es2020 -lib es2020 --module esnext --skipLibCheck --moduleResolution node --declaration --outDir dist src/rollup-vsix-plugin.ts",
"compile-rollup-vsix-plugin": "rollup --config rollup/rollup.vsix-extension.config.ts --configPlugin 'typescript={tsconfig: `tsconfig.rollup.json`}'",
"preprepare": "./scripts/install-vscode",
"lint": "eslint {/src/**/*.ts,./rollup/*.ts,./*.ts}",
"generate-types": "tsc --project tsconfig.types.json && rollup --config rollup/rollup.types.config.ts --configPlugin 'typescript={tsconfig: `tsconfig.rollup.json`}' && rm -rf ./dist/types",
Expand Down
35 changes: 35 additions & 0 deletions rollup/rollup.vsix-extension.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import nodeResolve from '@rollup/plugin-node-resolve'
import * as rollup from 'rollup'
import typescript from '@rollup/plugin-typescript'
import * as path from 'path'
import { fileURLToPath } from 'url'

const __dirname = path.dirname(fileURLToPath(import.meta.url))
const EXTENSIONS = ['', '.ts', '.js']
const BASE_DIR = path.resolve(__dirname, '..')
const TSCONFIG = path.resolve(BASE_DIR, 'tsconfig.json')

const config: rollup.RollupOptions = {
cache: false,
treeshake: {
preset: 'smallest'
},
external: ['@rollup/pluginutils', 'path', 'yauzl'],
output: [{
format: 'esm',
dir: 'dist',
entryFileNames: '[name].js'
}],
input: 'src/rollup-vsix-plugin.ts',
plugins: [
nodeResolve({
extensions: EXTENSIONS
}),
typescript({
noEmitOnError: true,
tsconfig: TSCONFIG
})
]
}

export default config
33 changes: 21 additions & 12 deletions src/rollup-vsix-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { createFilter, FilterPattern } from '@rollup/pluginutils'
import { createFilter, FilterPattern, dataToEsm } from '@rollup/pluginutils'
import { Plugin } from 'rollup'
import * as yauzl from 'yauzl'
import yauzl from 'yauzl'
import { Readable } from 'stream'
import * as path from 'path'
import { parse } from '../vscode/vs/base/common/json.js'

interface Options {
include?: FilterPattern
Expand Down Expand Up @@ -88,25 +89,33 @@ export default function plugin (options: Options = defaultOptions): Plugin {
}
const match = /vsix:(.*):(.*)/.exec(id)
if (match != null) {
return vsixFiles[match[1]!]![match[2]!]!.toString('utf8')
const parsed = parse(vsixFiles[match[1]!]![match[2]!]!.toString('utf8'))
return {
code: dataToEsm(parsed, {
compact: true,
namedExports: false,
preferConst: false
})
}
}

if (!filter(id)) return null

const files = await readVsix(id)
const manifest = JSON.parse(files['package.json']!.toString('utf8'))
const manifest = parse(files['package.json']!.toString('utf8'))
function getVsixPath (file: string) {
return path.relative('/', path.resolve('/', file))
}

const usedFiles = ['package.json', 'package.nls.json', ...extractPathsFromExtensionManifest(manifest.contributes)]
const usedFiles = extractPathsFromExtensionManifest(manifest.contributes).filter(file => getVsixPath(file) in files)

const allFiles = ['package.json', 'package.nls.json', ...usedFiles]
const nlsExists = files['package.nls.json'] != null

const vsixFile: Record<string, Buffer> = usedFiles.reduce((acc, usedFile) => {
const filePath = files[path.relative('/', path.resolve('/', usedFile))]
if (filePath == null) {
return acc
}
const vsixFile: Record<string, Buffer> = allFiles.reduce((acc, usedFile) => {
return ({
...acc,
[usedFile]: filePath
[usedFile]: files[getVsixPath(usedFile)]!
})
}, {} as Record<string, Buffer>)
vsixFiles[id] = vsixFile
Expand All @@ -117,7 +126,7 @@ ${nlsExists ? `import nls from 'vsix:${id}:package.nls.json'` : ''}
import { registerExtension, onExtHostInitialized } from 'vscode/extensions'
onExtHostInitialized(() => {
const { registerFile } = registerExtension(manifest${nlsExists ? ', nls' : ''})
${Object.keys(vsixFile).map((filePath) => (`
${usedFiles.map((filePath) => (`
registerFile('${filePath}', async () => (await import('vsix:${id}:${filePath}.raw')).default)`))}
})
`
Expand Down

0 comments on commit 1bd5327

Please sign in to comment.