Skip to content
This repository was archived by the owner on May 7, 2021. It is now read-only.

Commit eeef189

Browse files
christianvogtjoshuawilson
authored andcommitted
fix(build): fix gulp script for watch and missing css in dist (#156)
1 parent 2ceac6f commit eeef189

File tree

4 files changed

+34
-76
lines changed

4 files changed

+34
-76
lines changed

gulpfile.js

Lines changed: 30 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,6 @@ function minifyTemplate(file) {
8383
}
8484
}
8585

86-
// Build LESS
87-
function transpileLESS(src) {
88-
return gulp.src(src)
89-
.pipe(sourcemaps.init())
90-
.pipe(lessCompiler({
91-
paths: [ path.join(__dirname, 'less', 'includes') ]
92-
}))
93-
.pipe(sourcemaps.write())
94-
.pipe(gulp.dest(function (file) {
95-
return __dirname + file.base.slice(__dirname.length);
96-
}));
97-
}
98-
9986
// Build and minify LESS separately
10087
function transpileMinifyLESS(src) {
10188
return gulp.src(src)
@@ -104,55 +91,22 @@ function transpileMinifyLESS(src) {
10491
paths: [ path.join(__dirname, 'less', 'includes') ]
10592
}))
10693
.pipe(cssmin().on('error', function(err) {
107-
console.log(err);
94+
console.error(err);
10895
}))
109-
.pipe(rename({suffix: '.min'}))
11096
.pipe(sourcemaps.write())
11197
.pipe(gulp.dest(function (file) {
112-
return __dirname + file.base.slice(__dirname.length);
98+
return libraryDist + file.base.slice(__dirname.length);
11399
}));
114100
}
115101

116102
/**
117103
* LESS
118104
*/
119105

120-
// Copy asset LESS to dist/less and replace relative paths for flattened directory
121-
function copyAssetsLess() {
122-
return gulp.src(['./src/assets/stylesheets/*.less'])
123-
.pipe(replace(/\.\.\/.\.\/.\.\//g, function () {
124-
return '../../../../';
125-
}))
126-
.pipe(replace(/@import '\.\.\/\.\.\/.*\//g, function () {
127-
return '@import \'';
128-
}))
129-
.pipe(rename({dirname: ''}))
130-
.pipe(gulp.dest(libraryDist + '/dist/less'));
131-
}
132-
133-
// Copy component LESS to dist/less in a flattened directory
134-
function copyLess() {
135-
return gulp.src(['./src/app/**/*.less'].concat(globalExcludes))
136-
.pipe(rename({dirname: ''}))
137-
.pipe(gulp.dest(libraryDist + '/dist/less'));
138-
}
139-
140-
/**
141-
* CSS
142-
*/
143-
144-
// Copy CSS to dist/css
145-
function copyCss() {
146-
return gulp.src(['./src/assets/stylesheets/*.css'], {base: './src/assets/stylesheets'})
147-
.pipe(gulp.dest(function (file) {
148-
return libraryDist + '/dist/css' + file.base.slice(__dirname.length); // save directly to dist
149-
}));
150-
}
151-
152106
// Stylelint
153107
function lintCss() {
154108
return gulp
155-
.src(['./src/assets/stylesheets/*.less', './src/app/**/*.less'])
109+
.src([appSrc + '/**/*.less'])
156110
.pipe(stylelint({
157111
failAfterError: true,
158112
reporters: [
@@ -162,13 +116,21 @@ function lintCss() {
162116
}
163117

164118
// Less compilation and minifiction
165-
function minCss() {
166-
return transpileMinifyLESS(appSrc + '/assets/stylesheets/*.less');
119+
function transpileLess() {
120+
return transpileMinifyLESS(appSrc + '/**/*.less');
167121
}
168122

169-
// Less compilation
170-
function transpileLess() {
171-
return transpileLESS(appSrc + '/assets/stylesheets/*.less');
123+
// update templates styleUrls
124+
function postTranspile() {
125+
return gulp.src(['dist/**/*.js'])
126+
.pipe(replace(/templateUrl:\s/g, "template: require("))
127+
.pipe(replace(/\.html',/g, ".html'),"))
128+
.pipe(replace(/\.html'/g, ".html')"))
129+
.pipe(replace(/styleUrls: \[/g, "styles: [require("))
130+
.pipe(replace(/\.less']/g, ".css').toString()]"))
131+
.pipe(gulp.dest(function (file) {
132+
return file.base; // because of Angular's encapsulation, it's natural to save the css where the less-file was
133+
}));
172134
}
173135

174136
/**
@@ -177,7 +139,7 @@ function transpileLess() {
177139

178140
// Inline HTML templates in component classes
179141
function inlineTemplate() {
180-
return gulp.src(['./src/app/**/*.ts'].concat(globalExcludes), {base: './'})
142+
return gulp.src([appSrc + '/app/**/*.ts'].concat(globalExcludes), {base: './'})
181143
.pipe(replace(/templateUrl.*\'/g, function (matched) {
182144
let fileName = matched.match(/\/.*html/g).toString();
183145
let dirName = this.file.relative.substring(0, this.file.relative.lastIndexOf('/'));
@@ -223,18 +185,17 @@ function transpileAot() {
223185

224186
// Watch source
225187
function watch() {
226-
gulp.watch([appSrc + '/app/**/*.ts', '!' + appSrc + '/app/**/*.spec.ts']).on('change', function (e) {
227-
console.log('TypeScript file ' + e.path + ' has been changed. Compiling.');
188+
gulp.watch([appSrc + '/app/**/*.ts', '!' + appSrc + '/app/**/*.spec.ts']).on('change', function (path) {
189+
console.log('TypeScript file ' + path + ' has been changed. Compiling.');
190+
gulp.series(inlineTemplate, transpile, updateWatchDist)();
228191
});
229-
gulp.watch([appSrc + '/app/**/*.less']).on('change', function (e) {
230-
console.log(e.path + ' has been changed. Updating.');
231-
transpileLESS(e.path);
232-
updateWatchDist();
192+
gulp.watch([appSrc + '/app/**/*.less', appSrc + '/assets/**/*.less']).on('change', function (path) {
193+
console.log(path + ' has been changed. Updating.');
194+
gulp.series(() => transpileMinifyLESS(path), updateWatchDist)();
233195
});
234-
gulp.watch([appSrc + '/app/**/*.html']).on('change', function (e) {
235-
console.log(e.path + ' has been changed. Updating.');
236-
copyToDist(e.path);
237-
updateWatchDist();
196+
gulp.watch([appSrc + '/app/**/*.html']).on('change', function (path) {
197+
console.log(path + ' has been changed. Updating.');
198+
gulp.series(inlineTemplate, transpile, updateWatchDist)();
238199
});
239200
}
240201

@@ -249,21 +210,19 @@ function updateWatchDist() {
249210
/**
250211
* Tasks
251212
*/
252-
253-
const buildLessSeries = gulp.series(copyAssetsLess, copyLess);
254-
const buildCssSeries = gulp.series(lintCss, transpileLess, minCss, copyCss);
255-
const buildAotSeries = gulp.series(inlineTemplate, transpileAot);
213+
const buildCssSeries = gulp.series(lintCss, transpileLess);
214+
const buildAotSeries = gulp.series(inlineTemplate, transpileAot, postTranspile);
215+
const transpileSeries = gulp.series(inlineTemplate, transpile, postTranspile);
256216
const copyExamplesSeries = gulp.series(copyExamples);
257217
const copyPkgFilesSeries = gulp.series(copyPkgFiles);
258218

259-
const buildSeries = gulp.series(inlineTemplate, transpile, buildCssSeries, buildLessSeries, copyPkgFilesSeries);
219+
const buildSeries = gulp.series(transpileSeries, buildCssSeries, copyPkgFilesSeries);
260220
const updateWatchDistSeries = gulp.series(buildSeries, updateWatchDist);
261221
const watchSeries = gulp.series(updateWatchDistSeries, watch);
262222

263223
gulp.task('build', buildSeries);
264224
gulp.task('build-aot', buildAotSeries);
265225
gulp.task('build-css', buildCssSeries);
266-
gulp.task('build-less', buildLessSeries);
267226
gulp.task('copy-examples', copyExamplesSeries);
268227
gulp.task('copy-pkg-files', copyPkgFilesSeries);
269228
gulp.task('watch', watchSeries);

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
"name": "ngx-widgets",
33
"version": "0.0.0-development",
44
"description": "A collection of Angular components and other useful things to be shared.",
5-
"main": "bundles/ngx-widgets.umd.js",
6-
"module": "index.js",
7-
"typings": "index.d.ts",
5+
"module": "src/app/index.js",
6+
"typings": "src/app/index.d.ts",
87
"scripts": {
98
"build": "npm-run-all --serial lint:less lint:ts build:aot build:bundle",
109
"build:aot": "npm run rimraf -- build build-aot && gulp build-aot",

tsconfig-aot.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"@angular/router": ["node_modules/@angular/router"],
3131
"rxjs/*": ["node_modules/rxjs/*"]
3232
},
33-
"rootDir": "build/src/app",
33+
"rootDir": "build",
3434
"skipLibCheck": true,
3535
"sourceMap": true,
3636
"strictNullChecks": false,

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"@angular/router": ["node_modules/@angular/router"],
2929
"rxjs/*": ["node_modules/rxjs/*"]
3030
},
31-
"rootDir": "build/src/app",
31+
"rootDir": "build",
3232
"skipLibCheck": true,
3333
"sourceMap": true,
3434
"strictNullChecks": false,

0 commit comments

Comments
 (0)