closure-calculate-chunks

raw JSON →
3.2.2 verified Fri May 01 auth: no javascript

A utility to parse JavaScript files, resolve dependencies using Node module resolution, and produce Closure Compiler chunk and file definitions based on dynamic import split points. Version 3.2.2 is the current stable release, with irregular releases. It differs from other bundlers by directly targeting Google Closure Compiler's chunk API, supporting Closure-Library style goog.provide/goog.require dependencies, and providing a visualization mode for dependency graphs. It outputs JSON for use with closure-compiler --chunk and --js flags.

error Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/closure-calculate-chunks/index.js from /path/to/your.js not supported.
cause The package is ESM-only and cannot be required with CommonJS require().
fix
Use dynamic import('closure-calculate-chunks') or convert your project to ESM.
error TypeError: calculateChunks is not a function
cause Importing as a named export instead of default export.
fix
Use: import calculateChunks from 'closure-calculate-chunks'
error SyntaxError: Unexpected token 'export'
cause The package uses ESM syntax but your Node version is older than 12.22.
fix
Update Node to >=14.0.0.
breaking Version 3.x changed the output format of the CLI and programmatic API.
fix Upgrade to 3.2.2 and adjust code to expect the new JSON structure with 'chunk' array containing strings, not objects.
breaking Requires Node >=14.0.0 as of version 3.0.0.
fix Update Node.js to 14 or later.
breaking Switched from CommonJS to ESM-only in version 3.0.0.
fix Use import syntax instead of require(). For CommonJS projects, use dynamic import() or migrate to ESM.
deprecated The --closure-library-base-js-path flag may be deprecated in future versions as Closure Library usage declines.
fix Use --extra-deps for individual namespaces instead of relying on base.js.
npm install closure-calculate-chunks
yarn add closure-calculate-chunks
pnpm add closure-calculate-chunks

Shows building a chunk definition programmatically with all available options.

import calculateChunks from 'closure-calculate-chunks';

const result = await calculateChunks({
  entrypoint: ['./src/js/entry.js'],
  manualEntrypoints: ['chunkName:./src/js/extra.js'],
  closureLibraryBaseJsPath: './node_modules/google-closure-library/closure/goog/base.js',
  depsFiles: ['./deps.js'],
  extraDeps: ['my.namespace:./src/ns.js'],
  packageJsonEntryNames: ['module', 'main'],
  namingStyle: 'entrypoint',
  namePrefix: ''
});

console.log(JSON.stringify(result));