You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Current versions of VSCE interpret the files property in package.json as a list of files which should be included in the VSIX itself, overriding the default rules for what to include. This is incompatible with using that same property to select which files get included in the private extension package that is published to an NPM repository, since for that we want to only include the .vsix file.
One possible workaround to this is as follows:
Remove "files": [...] from package.json
Add "private": true to package.json to make sure it can't accidentally be published
Create a separate directory with its own package.json which will be used to publish the package to the NPM repository. The publish package.json should be identical to the original except:
Add back in the files field that was removed from the main package.json.
Remove the private field to allow it to be published.
Fields such as scripts, dependencies, and devDependencies can optionally be removed.
Copy the .vsix file and any resources such as README, LICENSE, and CHANGELOG files to the publish directory.
Publish the NPM package from the publish directory.
Here is a script that could be used as a starting point for automating this:
prepublish.mjs
/** * The VSCE tool uses package.json's "files" field to determine which files to * include in the .vsix package, but we want to use that to control which files * get included in the npm package instead. * * This script copies the files to publish into a "publish" directory and * creates a new package.json there with the "files" field set. */// @ts-checkimport*asfsfrom'node:fs/promises';import*aspathfrom'node:path';import*asprocessfrom'node:process';import{parseArgs}from'node:util';constRESOURCES=['CHANGELOG.md','README.md','LICENSE',];/** * @param {string} packagePath Path to the directory containing the package.json. * @param {string} publishPath Path to the directory to publish to. * @returns {Promise<string[]>} List of copied files. */asyncfunctioncopyResources(packagePath,publishPath){constresources=[];for(constresourceofRESOURCES){try{awaitfs.copyFile(path.join(packagePath,resource),path.join(publishPath,resource));resources.push(resource);}catch(err){if(err.code!=='ENOENT'){throwerr;}}}returnresources;}/** * @param {string} packagePath Path to the directory containing the package.json. */asyncfunctionpreparePublishPackage(packagePath){constpublishPath=path.join(packagePath,'publish');awaitfs.rm(publishPath,{recursive: true,force: true});awaitfs.mkdir(publishPath);constfiles=awaitcopyResources(packagePath,publishPath);constpackageJson=JSON.parse(awaitfs.readFile(path.join(packagePath,'package.json'),'utf8'));deletepackageJson['dependencies'];deletepackageJson['devDependencies'];deletepackageJson['private'];deletepackageJson['scripts'];packageJson['files']=['extension.vsix', ...files];awaitfs.writeFile(path.join(publishPath,'package.json'),JSON.stringify(packageJson,null,2));}const{ values, positionals }=parseArgs({allowPositionals: true,options: {help: {type: 'boolean',alias: 'h'},},});if(values.help||positionals.length>1){console.log('Usage: node prepublish.mjs [path]');console.log();console.log('Creates a "publish" directory with the files to publish an extension.');console.log('After running this, package the extension with "vsce package -o publish/extension.vsix",');console.log('then move to the "publish" directory before running "npm publish".');process.exit(0);}awaitpreparePublishPackage(positionals[0]??process.cwd());
The text was updated successfully, but these errors were encountered:
joelspadin-garmin
changed the title
Publishing to NPM repository is incompatible with new versions of VSCE
Publishing to NPM repository is incompatible with new VSCE
Nov 4, 2024
Current versions of VSCE interpret the
files
property in package.json as a list of files which should be included in the VSIX itself, overriding the default rules for what to include. This is incompatible with using that same property to select which files get included in the private extension package that is published to an NPM repository, since for that we want to only include the .vsix file.One possible workaround to this is as follows:
"files": [...]
from package.json"private": true
to package.json to make sure it can't accidentally be publishedfiles
field that was removed from the main package.json.private
field to allow it to be published.scripts
,dependencies
, anddevDependencies
can optionally be removed.Here is a script that could be used as a starting point for automating this:
prepublish.mjs
The text was updated successfully, but these errors were encountered: