iTranslated by AI
The Challenges of Using pnpm Workspaces for VS Code Extension Development
In the extension I'm developing called ts-type-expand, I previously used a monorepo configuration with yarn workspaces, but this time I decided to migrate to pnpm workspaces.
There were many sticking points, so I will share my findings.
Basic Migration Method
To migrate the lockfile, use the following command:
$ pnpm import
This allows you to convert the contents of yarn.lock into pnpm-lock.yaml.
After that:
- Delete yarn configuration files
- Create pnpm-workspace.yaml and move the workspace specifications
Doing these steps completes the migration for package dependency management.
pnpm is stricter about module resolution than other package managers, so there might be cases where issues arise, but since that deviates from the main topic of this entry, I'll leave that to other articles.
Limitation: pnpm is not supported by the vsce command
To package an extension, you need to use the vsce command, but it only supports npm/yarn and does not support pnpm.
This is being discussed in the issue above.
Specifically, when you run:
$ vsce package
an error occurs because the dependencies listed in dependencies do not exist under node_modules in the npm format.
Following the issue, it seems that official pnpm support will not be implemented.
As a workaround, a method is introduced to bundle dependencies using a bundler and use the --no-dependencies option of vsce package.
By the way, if you specify the --no-dependencies option, node_modules will not be packaged regardless of the .vscodeignore settings, so bundling becomes mandatory.
Bundling the Package
You can use esbuild for bundling, but I recommend tsup. For a single package, type definitions aren't necessary, but for multiple packages, you might have cases where you want to output them. With tsup, you can output type definitions with just one command.
By the way,
As discussed in this issue, VSCode extensions do not currently support ESM and must be prepared as "type": "commonjs" packages. Therefore, we set the output format to commonjs.
$ pnpm add -D tsup
import { defineConfig } from 'tsup'
export default defineConfig({
entry: ['./src/extension.ts'],
dts: false, // Not needed as it's not referenced from elsewhere
sourcemap: 'inline',
target: 'node16',
format: ['cjs'],
tsconfig: 'tsconfig.json',
external: ['vscode'],
outDir: 'dist',
clean: true,
minify: true,
})
node_modules Were Necessary for the Language Service Plugin
In ts-type-expand, we use a Language Service Plugin to receive TypeScript type information from the built-in tsserver. While basic module references can be handled with the bundling mentioned above, Language Service Plugins work by loading the package from node_modules based on a specification in package.json like the following:
{
"contributes": {
"typescriptServerPlugins": [
{
"name": "ts-type-expand-plugin",
"enableForWorkspaceTypeScriptVersions": true
}
]
}
}
In such cases, bundling alone is not enough.
Therefore, as a rather brute-force approach, I managed it by creating a temporary package that is a copy of the extension and rewriting the dependencies to include only ts-type-expand-plugin.
The specific process is as follows:
#!/usr/bin/env bash
set -eux
REPOSITORY_DIR=$(git rev-parse --show-toplevel)
EXTENSION_TMP_DIR=${REPOSITORY_DIR}/extension-tmp # Temporary directory for vsce package
VERSION=$1
cd ${REPOSITORY_DIR}
# Build everything
pnpm build
# Pack the local ts-type-expand-plugin so it can be installed in a temporary package that doesn't belong to the workspace
pushd ./packages/ts-type-expand-plugin
ts_type_expand_plugin_packed=$(pnpm pack --pack-destination ${EXTENSION_TMP_DIR})
popd
# Since having other dependencies causes an error when running vsce package,
# prepare a copy of package.json with the dependencies and devDependencies sections emptied.
node -e "console.log(JSON.stringify({...require('./packages/ts-type-expand/package.json'), scripts: {}, dependencies: {}, devDependencies: {}, version: '${VERSION}'}, null, 2))"\
>> ${EXTENSION_TMP_DIR}/package.json
# Copy only the resources necessary for packaging
cp ./packages/ts-type-expand/.vscodeignore ${EXTENSION_TMP_DIR}
cp ./packages/ts-type-expand/LICENSE ${EXTENSION_TMP_DIR}
cp -r ./packages/ts-type-expand/dist ${EXTENSION_TMP_DIR}
# Install the packed ts-type-expand-plugin locally
pushd ${EXTENSION_TMP_DIR}
npm i $ts_type_expand_plugin_packed
pnpm vsce package --no-yarn
While I have omitted some parts, the key points are as follows:
- Pack
ts-type-expand-pluginas well, so it can be installed in a temporary package that does not belong to the pnpm workspace. - Copy
package.jsonfrom the extension package, empty thedependenciesanddevDependenciessections, and thennpm installthe packed plugin. - By using
pnpm vsce package --no-yarn, you can package it such that onlyts-type-expand-pluginhas anode_modulesdirectory, while other dependencies are bundled.
- dist
- extension.js <-- Dependencies other than the Language Service Plugin are bundled
- node_modules
- ts-type-expand-plugin <-- The only required package
I was able to handle it this way.
If you have other modules that are difficult to bundle, you could probably handle them similarly. However, as you can see, it is quite a lot of work. Unless you have a strong preference, I recommend using a package manager other than pnpm if node_modules are required.
(As for why I'm using it... well, it's because I had that strong preference...)
Summary
I've introduced how to use pnpm in VSCode extension development!
- Although there is no official support, it basically works just by bundling, except for special cases.
- If you use packages that are difficult to bundle or use a Language Service Plugin, it requires a brute-force approach and can be quite a lot of work, but it can be handled by creating a temporary package (though I don't necessarily recommend it).
As you can see, although it takes some effort, you can use pnpm for VSCode extension development!
pnpm is wonderful, so if you're someone who wants to use it even if it costs a bit more, by all means, try migrating!
That's all for now.
Discussion