Skip to content

Commit 2474552

Browse files
author
nickpape-msft
authored
Merge pull request microsoft#22 from Microsoft/nickpape/remove-comments
Introduce an option which remove comments, but only from JS files (no…
2 parents 958773a + fc0a8c3 commit 2474552

File tree

7 files changed

+136
-72
lines changed

7 files changed

+136
-72
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@microsoft/gulp-core-build-typescript",
5+
"comment": "Added additional options which will remove comments and one that will turn off sourcemaps",
6+
"type": "minor"
7+
}
8+
],
9+
"email": "nickpape@microsoft.com"
10+
}

common/npm-shrinkwrap.json

Lines changed: 45 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/temp_modules/npmx-gulp-core-build-typescript/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"gulp": "~3.9.1",
1717
"gulp-cache": "~0.4.5",
1818
"gulp-changed": "~1.3.2",
19+
"gulp-decomment": "~0.1.3",
1920
"gulp-plumber": "~1.1.0",
2021
"gulp-sourcemaps": "~1.6.0",
2122
"gulp-texttojs": "~1.0.3",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"typescript.check.workspaceVersion": false
3+
}

gulp-core-build-typescript/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ Optional override of the typescript compiler. Set this to the result of require(
6262

6363
Default: `undefined`
6464

65+
### removeCommentsFromJavaScript
66+
Removes comments from all generated `.js` files. Will **not** remove comments from generated `.d.ts` files.
67+
68+
Default: `false`
69+
70+
### emitSourceMaps
71+
If true, creates sourcemap files which are useful for debugging.
72+
73+
Default: `true`
74+
6575
# TSLintTask
6676
## Usage
6777
The task for linting the TypeScript code.

gulp-core-build-typescript/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"gulp": "~3.9.1",
2323
"gulp-cache": "~0.4.5",
2424
"gulp-changed": "~1.3.2",
25+
"gulp-decomment": "~0.1.3",
2526
"gulp-plumber": "~1.1.0",
2627
"gulp-sourcemaps": "~1.6.0",
2728
"gulp-texttojs": "~1.0.3",

gulp-core-build-typescript/src/TypeScriptTask.ts

Lines changed: 66 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ export interface ITypeScriptTaskConfig {
4545
/* tslint:disable:no-any */
4646
typescript?: any;
4747
/* tslint:enable:no-any */
48+
49+
/**
50+
* Removes comments from all generated `.js` files. Will **not** remove comments from generated `.d.ts` files.
51+
* Defaults to false.
52+
*/
53+
removeCommentsFromJavaScript?: boolean;
54+
55+
/**
56+
* If true, creates sourcemap files which are useful for debugging. Defaults to true.
57+
*/
58+
emitSourceMaps?: boolean;
4859
}
4960

5061
export class TypeScriptTask extends GulpTask<ITypeScriptTaskConfig> {
@@ -82,22 +93,25 @@ export class TypeScriptTask extends GulpTask<ITypeScriptTaskConfig> {
8293
'src/**/*.js',
8394
'src/**/*.json',
8495
'src/**/*.jsx'
85-
]
96+
],
97+
removeCommentsFromJavaScript: false,
98+
emitSourceMaps: true
8699
};
87100

88101
private _tsProject: ts.Project;
89102

90103
public executeTask(gulp: gulpType.Gulp, completeCallback: (result?: string) => void): void {
91104
/* tslint:disable:typedef */
92-
const plumber = require('gulp-plumber');
93-
const sourcemaps = require('gulp-sourcemaps');
94105
const assign = require('object-assign');
95106
const merge = require('merge2');
96107
/* tslint:enable:typedef */
97108

98-
let errorCount: number = 0;
99109
const allStreams: NodeJS.ReadWriteStream[] = [];
100110

111+
const result: { errorCount: number } = {
112+
errorCount: 0
113+
};
114+
101115
/* tslint:disable:no-any */
102116
let tsConfig: any = this.readJSONSync('tsconfig.json');
103117
/* tslint:enable:no-any */
@@ -121,65 +135,32 @@ export class TypeScriptTask extends GulpTask<ITypeScriptTaskConfig> {
121135
this.log(`Using custom version: ${this.taskConfig.typescript.version}`);
122136
}
123137

124-
const tsCompilerOptions: ts.Project = assign({}, tsConfig.compilerOptions, {
138+
const tsCompilerOptions: ts.Settings = assign({}, tsConfig.compilerOptions, {
125139
module: 'commonjs',
126140
typescript: this.taskConfig.typescript
127141
});
128142

129-
const tsProject: ts.Project = this._tsProject = this._tsProject || ts.createProject(tsCompilerOptions);
130-
131-
/* tslint:disable:typedef */
132-
const { libFolder, libAMDFolder } = this.buildConfig;
133-
/* tslint:enable:typedef */
134-
let tsResult: ts.CompileStream = gulp.src(this.taskConfig.sourceMatch)
135-
.pipe(plumber({
136-
errorHandler: (): void => {
137-
errorCount++;
138-
}
139-
}))
140-
.pipe(sourcemaps.init())
141-
.pipe(ts(tsProject, undefined, this.taskConfig.reporter));
142-
143-
allStreams.push(tsResult.js
144-
.pipe(sourcemaps.write('.', { sourceRoot: this._resolveSourceMapRoot }))
145-
.pipe(gulp.dest(libFolder)));
146-
147-
allStreams.push(tsResult.dts.pipe(gulp.dest(libFolder)));
143+
this._compileProject(gulp, tsCompilerOptions, this.buildConfig.libFolder, allStreams, result);
148144

149145
// Static passthrough files.
150146
const staticSrc: NodeJS.ReadWriteStream = gulp.src(this.taskConfig.staticMatch);
151147

152148
allStreams.push(
153-
staticSrc.pipe(gulp.dest(libFolder)));
149+
staticSrc.pipe(gulp.dest(this.buildConfig.libFolder)));
154150

155151
// If AMD modules are required, also build that.
156-
if (libAMDFolder) {
152+
if (this.buildConfig.libAMDFolder) {
157153
allStreams.push(
158-
staticSrc.pipe(gulp.dest(libAMDFolder)));
154+
staticSrc.pipe(gulp.dest(this.buildConfig.libAMDFolder)));
159155

160156
const tsAMDProject: ts.Project = ts.createProject(assign({}, tsCompilerOptions, { module: 'amd' }));
161-
162-
tsResult = gulp.src(this.taskConfig.sourceMatch)
163-
.pipe(plumber({
164-
errorHandler: (): void => {
165-
errorCount++;
166-
}
167-
}))
168-
.pipe(sourcemaps.write({ sourceRoot: this._resolveSourceMapRoot }))
169-
.pipe(ts(tsAMDProject, undefined, this.taskConfig.reporter));
170-
171-
allStreams.push(
172-
tsResult.js
173-
.pipe(sourcemaps.write('.', { sourceRoot: this._resolveSourceMapRoot }))
174-
.pipe(gulp.dest(libAMDFolder)));
175-
176-
allStreams.push(tsResult.dts.pipe(gulp.dest(libAMDFolder)));
157+
this._compileProject(gulp, tsAMDProject, this.buildConfig.libAMDFolder, allStreams, result);
177158
}
178159

179160
// Listen for pass/fail, and ensure that the task passes/fails appropriately.
180161
merge(allStreams)
181162
.on('queueDrain', () => {
182-
if (this.taskConfig.failBuildOnErrors && errorCount) {
163+
if (this.taskConfig.failBuildOnErrors && result.errorCount) {
183164
completeCallback('TypeScript error(s) occurred.');
184165
} else {
185166
completeCallback();
@@ -193,6 +174,47 @@ export class TypeScriptTask extends GulpTask<ITypeScriptTaskConfig> {
193174
throw 'Do not use mergeConfig with gulp-core-build-typescript';
194175
}
195176

177+
private _compileProject(gulp: gulpType.Gulp, tsCompilerOptions: ts.Settings, destDir: string,
178+
allStreams: NodeJS.ReadWriteStream[], result: { errorCount: number }): void {
179+
/* tslint:disable:typedef */
180+
const plumber = require('gulp-plumber');
181+
const sourcemaps = require('gulp-sourcemaps');
182+
/* tslint:enable:typedef */
183+
184+
const tsProject: ts.Project = this._tsProject = this._tsProject || ts.createProject(tsCompilerOptions);
185+
186+
// tslint:disable-next-line:no-any
187+
let tsResult: any = gulp.src(this.taskConfig.sourceMatch)
188+
.pipe(plumber({
189+
errorHandler: (): void => {
190+
result.errorCount++;
191+
}
192+
}));
193+
194+
if (this.taskConfig.emitSourceMaps) {
195+
tsResult = tsResult.pipe(sourcemaps.init());
196+
}
197+
198+
tsResult = tsResult
199+
.pipe(ts(tsProject, undefined, this.taskConfig.reporter));
200+
201+
// tslint:disable-next-line:typedef
202+
let jsResult = (this.taskConfig.removeCommentsFromJavaScript
203+
? tsResult.js.pipe(require('gulp-decomment')({
204+
space: !!this.taskConfig.emitSourceMaps /* turn comments into spaces to preserve sourcemaps */
205+
}))
206+
: tsResult.js);
207+
208+
if (this.taskConfig.emitSourceMaps) {
209+
jsResult = jsResult.pipe(sourcemaps.write('.', { sourceRoot: this._resolveSourceMapRoot }));
210+
}
211+
212+
allStreams.push(jsResult
213+
.pipe(gulp.dest(destDir)));
214+
215+
allStreams.push(tsResult.dts.pipe(gulp.dest(destDir)));
216+
}
217+
196218
private _resolveSourceMapRoot(file: { relative: string, cwd: string }): string {
197219
return path.relative(file.relative, path.join(file.cwd, 'src'));
198220
}

0 commit comments

Comments
 (0)