Search code examples
visual-studio-codevscode-extensions

Include or Exclude node_modules with esbuild


I am using esbuild to build a vscode extension. The build gives a warning like:

✨  Done in 1.28s.
This extension consists of 7026 files, out of which 5430 are JavaScript files. For performance reasons, you should bundle your extension: https://aka.ms/vscode-bundle-extension . You should also exclude unnecessary files by adding them to your .vscodeignore: https://aka.ms/vscode-vscodeignore
 DONE  Packaged: /my-vscode/my-vscode-0.0.2.vsix (7026 files, 8.43MB)

I am not sure I node_modules need to be added to .vscodeignore ? when I tried doing it and installed the plugin, I get an error that my commands are not found ..

Any thoughts ?


Solution

  • You can pass the external keyword in your esbuild config to exclude certain modules (or all of them using an asterisk *). For example, the following will exclude the pg, sqlite3, tedious, pg-hstore modules from your build process:

    const { nodeExternalsPlugin } = require("esbuild-node-externals");
    
    esbuild
        .build({
          entryPoints: [entryFile],
          outfile: outFile,
          minify: true,
          bundle: true,
          target: TARGET,
          plugins: [copyPlugin, nodeExternalsPlugin()],
          sourcemap: true,
          platform: "node",
          define,
          external: ["pg", "sqlite3", "tedious", "pg-hstore"],
        })
        .then((r) => {
          console.log(`Build ${entryFile} to ${outFile} succeeded.`);
        })
        .catch((e) => {
          console.log("Error building:", e.message);
          process.exit(1);
        });