Skip to content
This repository was archived by the owner on Jun 14, 2024. It is now read-only.

Commit d8812c6

Browse files
committed
updates to showcase Gulp/Webpack
Change-Id: Ic403000089fad2187fd9c9542b60d854c4ebcb00
1 parent deb0a3f commit d8812c6

File tree

1 file changed

+72
-9
lines changed

1 file changed

+72
-9
lines changed

README.md

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,100 @@ Any bugs not related to the plugins themselves should be reported to the [main r
77

88
Unlike other packages, this allows Closure Compiler to run entirely in JS.
99
*Java is not required.*
10-
However, this has a few tradeoffs- some features are not available and performance is not on-par with the Java implementation.
10+
11+
This is an experimental release- some features are not available and performance may not be on-par with the Java implementation.
1112

1213
## Usage
1314

14-
### Installation
15+
First, install the latest version-
1516

1617
```bash
1718
npm install --save google-closure-compiler-js
1819
```
1920

20-
### Grunt/Gulp
21-
22-
As of July 2016, neither a Grunt Task nor a Gulp Plugin is available.
23-
Contributions are welcome.
24-
2521
### Native Node Usage
2622

2723
The module provides `compile` as a low-level method to compile JavaScript.
2824
By default, this compiles ES6 to ES5 and includes the default set of ECMAScript externs files.
29-
For now, the flags are [listed in source](https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/gwt/client/GwtRunner.java#L73).
25+
For example-
3026

3127
```js
3228
const compile = require('google-closure-compiler-js').compile;
3329

3430
const flags = {
35-
jsCode: [{source: 'const x = 1 + 2;'}],
31+
jsCode: [{src: 'const x = 1 + 2;'}],
3632
};
3733
const out = compile(flags);
3834
console.info(out.compiledCode); // will print 'var x = 3;\n'
3935
```
4036

37+
### Build Systems
38+
39+
#### Webpack
40+
41+
Your `webpack.config.js` should look like this-
42+
43+
```js
44+
const ClosureCompiler = require('google-closure-compiler-js').webpack;
45+
const path = require('path');
46+
47+
module.exports = {
48+
entry: [
49+
path.join(__dirname, 'app.js')
50+
],
51+
output: {
52+
path: path.join(__dirname, 'dist'),
53+
filename: 'app.min.js'
54+
},
55+
plugins: [
56+
new ClosureCompiler({
57+
options: {
58+
languageIn: 'ECMASCRIPT6',
59+
languageOut: 'ECMASCRIPT5',
60+
compilationLevel: 'ADVANCED',
61+
warningLevel: 'VERBOSE',
62+
},
63+
})
64+
]
65+
};
66+
```
67+
68+
#### Gulp
69+
70+
Your `gulpfile.js` should contain a task like this-
71+
72+
```js
73+
const compiler = require('google-closure-compiler-js').gulp();
74+
75+
gulp.task('script', function() {
76+
return gulp.src('./path/to/src.js', {base: './'})
77+
// your other steps here
78+
.pipe(compiler({
79+
compilationLevel: 'SIMPLE',
80+
warningLevel: 'VERBOSE',
81+
outputWrapper: '(function(){\n%output%\n}).call(this)',
82+
jsOutputFile: 'output.min.js', // outputs single file
83+
createSourceMap: true,
84+
}))
85+
.pipe(gulp.dest('./dist'));
86+
});
87+
```
88+
89+
As of release v20160822, commonJS imports may be broken: we recommend that you compile a single file only (e.g. via Browserify or other tools), or use ES6 imports.
90+
91+
### Flags
92+
93+
The Closure Compiler in JS supports many of the flags supported by the Java-based Closure Compiler.
94+
For now, the supported flags are [listed in source](https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/gwt/client/GwtRunner.java#L93).
95+
96+
Notably, unless you're using a build system, you have to specify code via flags.
97+
Both `jsCode` and `externs` accept an array containing objects with `src`, `path`, and `sourceMap` properties.
98+
For those of you familiar with [Closure syntax](https://developers.google.com/closure/compiler/docs/js-for-compiler), that's `Array<{src: string, path: string, sourceMap: string}`.
99+
100+
<!--
101+
Using `path`, you can construct a virtual filesystem for use with ES6 imports or CommonJS imports (although don't forget to specify `processCommonJsModules: true`).
102+
-->
103+
41104
## License
42105

43106
Copyright 2016 The Closure Compiler Authors

0 commit comments

Comments
 (0)