Background: Our project currently uses closure-calculate-chunks
to chunk the output into the main chunk ("blockly") and several other chunks ("blocks", and "javascript", "python", "php", etc., the latter collectively known as the "generators").
At the moment the second and subsequent chunks depend only on the first chunk, but I experimentally added an import
statement that caused the "javascript" chunk to depend on the "blocks" chunk (specifically, by import
ing the entry point of the "blocks" chunk) as well as the "blockly" chunk.
After updating our deps.js
using closure-make-deps
, running our usual c-c-c command:
closure-calculate-chunks --closure-library-base-js-path ./closure/goog/base_minimal.js --deps-file ./tests/deps.js \
--entrypoint 'core/blockly.js' --entrypoint 'blocks/blocks.js' --entrypoint 'generators/javascript/all.js' \
--entrypoint 'generators/python/all.js' # ... etc.
resulted in the error message:
Error: Invalid chunk definitions:
Chunk entrypoint blocks/blocks.js not found in chunk sources. Ensure that all imports of blocks/blocks.js are dynamic.
This was pretty confusing, because it appears to be factually incorrect: blocks/blocks.js
appears both in deps.js
and as an --entrypoint
. After adding some console.log
messages to lib/chunk-graph.js
I was able to figure out that the problem was that all of the files for the "blocks" chunk were being hoisted into the "blockly" chunk, leaving no files left.
I was aware of c-c-c's hoisting ability, but had (perhaps naïvely!) expected it to detect that the "javascript" chunk depended on the "blocks" chunk.
Now I am trying to figure out how to get closure-calculate-deps to deal with inter-chunk dependencies not involving the first chunk. After reading the documentation, I noted that the --manual-entrypoint
switch might be relevant; I tried:
closure-calculate-chunks --closure-library-base-js-path ./closure/goog/base_minimal.js --deps-file ./tests/deps.js \
--entrypoint 'core/blockly.js' --entrypoint 'blocks/blocks.js' --manual-entrypoint 'blocks/blocks.js:generators/javascript/all.js' \
--entrypoint 'generators/python/all.js' # ... etc.
And this did seem to have the desired effect. However:
- The documentation for
--manual-entrypoint
just says "Add a custom entrypoint for code that is not discoverable", which wasn't too helpful, because:
- this doesn't exactly stand out as being the way to specify dependency, and
it's not clear how one would specify dependency upon more than one previous chunk—e.g., a diamond dependency, where b
and c
depend on a
, and then d
depends upon b
and c
, and [edit: I see that Closure Compiler doesn't actually support this.]
- I wasn't aware that any entrypoint could be "discoverable" (though I guess this might have something to do with
--package-json-entry-names
, which I also don't understand).
- This caused c-c-c to reorder the chunks in its output, compared to its input.
- Since all of our generators have an entrypoint named
generators/<lang>/all.js
, c-c-c ends up naming them all
, all1
, all2
, etc., which is not very helpful.
- I had therefore written some code to rename the chunks generated by c-c-c to correspond to our desired chunk names before passing them to Closure Compiler.
- Previously I relied on them coming out in the same order they went in, but now I'm not sure what to do; it looks like I will have to look through the contents of each chunk and try to match the (now absolute!) pathnames against one of the entrypoints.
- I even tried
--naming-style=numbered
in the hopes that they might come out in the order 0, 3, 4, 5, 1, 2 (and I could just index my original array of chunk entrypoints) but alas no.
I feel like I'm fighting the tool.
Can the documentation and/or code be improved to make it easier to do this (or at least easier to understand what one is doing wrong)?