Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module Code Splitting (--chunk / --module flag) #61

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,38 @@ grunt.initConfig({
});
```

## Module Code Splitting

Closure Compiler can split the output of a compilation into multiple files to support dynamic loading. This process uses the `--chunk` flag (before June 2018 it was named `--module`). See [how do I split my javascript into modules](https://stackoverflow.com/questions/10395810/how-do-i-split-my-javascript-into-modules-using-googles-closure-compiler/10401030#10401030)

Modules can be stitched together without a module loading logic enabling to achieve optimal compression and optimization with the ability to load modules selectively.

Source: https://github.com/google/closure-compiler/wiki/JS-Modules#code-splitting-output-modules

```javascript
grunt.initConfig({
'closure-compiler': {
frontend: {
closurePath: '/src/to/closure-compiler',
modules: {
"module-name": {
src: ['js/source.js', 'js/other-source.js']
},
"extra-module": {
src: ['js/extra-module.js'],
dep: ['module-name']
}
},
moduleOutputPath: 'static/js/',
maxBuffer: 500,
options: {
compilation_level: 'ADVANCED_OPTIMIZATIONS',
language_in: 'ECMASCRIPT5_STRICT'
}
}
}
});
```

## Note

Expand Down
71 changes: 53 additions & 18 deletions tasks/closure-compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,34 @@ module.exports = function(grunt) {
var command = 'java -jar "' + closurePath + '/build/compiler.jar"';

data.cwd = data.cwd || './';

var output_mode;

// --module mode
if ("modules" in data) {
output_mode = 'modules';
} else { // --js mode
output_mode = 'js';

data.js = grunt.file.expand({cwd: data.cwd}, data.js);

// Sanitize options passed.
if (!data.js.length) {
// This task requires a minima an input file.
grunt.warn('Missing js property.');
return false;
}

data.js = grunt.file.expand({cwd: data.cwd}, data.js);

// Sanitize options passed.
if (!data.js.length) {
// This task requires a minima an input file.
grunt.warn('Missing js property.');
return false;
}

// Build command line.
command += ' --js "' + data.js.join('" --js "') + '"';
// Build command line.
command += ' --js "' + data.js.join('" --js "') + '"';

if (data.jsOutputFile) {
if (!grunt.file.isPathAbsolute(data.jsOutputFile)) {
data.jsOutputFile = path.resolve('./') + '/' + data.jsOutputFile;
if (data.jsOutputFile) {
if (!grunt.file.isPathAbsolute(data.jsOutputFile)) {
data.jsOutputFile = path.resolve('./') + '/' + data.jsOutputFile;
}
command += ' --js_output_file "' + data.jsOutputFile + '"';
reportFile = data.reportFile || data.jsOutputFile + '.report.txt';
}
command += ' --js_output_file "' + data.jsOutputFile + '"';
reportFile = data.reportFile || data.jsOutputFile + '.report.txt';
}

if (data.externs) {
Expand Down Expand Up @@ -84,8 +93,34 @@ module.exports = function(grunt) {
}
}

// because closure compiler does not create dirs.
grunt.file.write(data.jsOutputFile, '');
if (output_mode === 'js') {
// because closure compiler does not create dirs.
grunt.file.write(data.jsOutputFile, '');
} else { // modules

for (var module in data.modules) {

// @todo add --define option for modules
//command += '" --define="DEBUG=false"';

data.modules[module].src.forEach(function(src) {
command += ' --js "' + src + '"';
});

command += ' --chunk "' + module + ':' + data.modules[module].src.length + ':';

// add dependency
if (data.modules[module].dep) {
command += data.modules[module].dep.join(',');
command += ':';
}
command += '"';
}

// module output directory
command += ' --module_output_path_prefix ' + data.moduleOutputPath;

}

// Minify WebGraph class.
exec(command, { maxBuffer: data.maxBuffer * 1024, cwd: data.cwd }, function(err, stdout, stderr) {
Expand Down