google / google/closure-compiler
[FEATURE] Support rollup-style bundling
- Dominant language
- JavaScript
- Stars
- 7.7k
- Forks
- 1.2k
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 6
Description
Similar to the [rollup](https://github.com/rollup/rollup) this feature would combine all es6 modules with very minor modifications into a single module. In other words it'd flatten the modules. As with the [rollup](https://github.com/rollup/rollup) tool itself, this would make it easier to create export targets for sharing closure code between projects.
Consider the following set of modules:
File: parent.js
```
export class Parent {
/**
* @param {./utils/child.Child} child
*/
constructor(child) {
/** @const */
this.child = child;
}
}
```
File: utils/child.js
```
export class Child {
}
```
File: exports.js
```
import {Parent} from './src/parent.js';
import {Child} from './src/utils/child.js';
export {
Parent,
Child,
}
```
The `closure_rollup` tool with the `exports.js` as an input will produce the following single output module:
```
class Child {
}
class Parent {
/**
* @param {Child} child
*/
constructor(child) {
/** @const */
this.child = child;
}
}
export {
Parent,
Child,
}
```
In a nutshell, the `closure_rollup --in exports.js --out out.js` would do the following:
1. Read all modules, analyzes imports, exports and closure type annotations. Construct the dependency graph.
2. Order modules based on the dependency graph.
3. Remove all `import` and `export` statements from all modules.
4. Rewrite all types to be flat. E.g. `{./utils/child.Child}` -> `{Child}`.
5. Keep all other whitespace/comments as intact as possible.
6. Disambiguate duplicate names for top-level functions/classes.
7. Possibly do a very minor tree shaking: any top-level function, class or type not referenced anywhere could be removed.
8. Output all rewritten modules in order into a single module.
9. Complete the output module with the exports from the provided `exports.js`.
This kind of tool could allow a single well-used Closure project to create numerous export targets that would be easily shareable in the Closure form for other Closure projects to use.
Contributor guide
Assessment
This issue has not been assessed yet.