Skip to content

Commit e71cba7

Browse files
committed
Merge pull request #952 from hank-ehly/feature/enable-scss
feature/enable-scss - issue #895
2 parents 8618287 + 471eca2 commit e71cba7

9 files changed

+119
-32
lines changed

karma.conf.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ module.exports = function(config) {
4040
{ pattern: 'node_modules/@angular/**/*.js', included: false, watched: true },
4141

4242
{ pattern: 'dist/dev/**/*.js', included: false, watched: true },
43+
{ pattern: 'dist/dev/**/*.html', included: false, watched: true, served: true },
44+
{ pattern: 'dist/dev/**/*.css', included: false, watched: true, served: true },
4345
{ pattern: 'node_modules/systemjs/dist/system-polyfills.js', included: false, watched: false }, // PhantomJS2 (and possibly others) might require it
4446

4547
// suppress annoying 404 warnings for resources, images, etc.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,11 @@
6262
"gulp-load-plugins": "^1.2.2",
6363
"gulp-plumber": "~1.1.0",
6464
"gulp-postcss": "^6.1.1",
65+
"gulp-progeny": "^0.2.1",
6566
"gulp-protractor": "^2.3.0",
6667
"gulp-replace": "^0.5.4",
68+
"gulp-sass": "^2.3.1",
69+
"gulp-sass-lint": "^1.1.1",
6770
"gulp-shell": "~0.5.2",
6871
"gulp-sourcemaps": "2.0.0-alpha",
6972
"gulp-template": "^4.0.0",

tools/config/seed.config.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ export class SeedConfig {
210210
* The name of the bundle file to includes all CSS files.
211211
* @type {string}
212212
*/
213-
CSS_PROD_BUNDLE = 'all.css';
213+
CSS_PROD_BUNDLE = 'main.css';
214214

215215
/**
216216
* The name of the bundle file to include all JavaScript shims.
@@ -241,6 +241,13 @@ export class SeedConfig {
241241
*/
242242
CODELYZER_RULES = customRules();
243243

244+
/**
245+
* The flag to enable handling of SCSS files
246+
* The default value is false. Override with the '--scss' flag.
247+
* @type {boolean}
248+
*/
249+
ENABLE_SCSS = argv['scss'] || false;
250+
244251
/**
245252
* The list of NPM dependcies to be injected in the `index.html`.
246253
* @type {InjectableDependency[]}
@@ -258,7 +265,7 @@ export class SeedConfig {
258265
* @type {InjectableDependency[]}
259266
*/
260267
APP_ASSETS: InjectableDependency[] = [
261-
{ src: `${this.CSS_SRC}/main.css`, inject: true, vendor: false }
268+
{ src: `${this.CSS_SRC}/main.${ this.getInjectableStyleExtension() }`, inject: true, vendor: false },
262269
];
263270

264271
/**
@@ -430,6 +437,10 @@ export class SeedConfig {
430437
return null;
431438
}
432439

440+
getInjectableStyleExtension() {
441+
return this.ENV === ENVIRONMENTS.PRODUCTION && this.ENABLE_SCSS ? 'scss' : 'css';
442+
}
443+
433444
}
434445

435446
/**

tools/tasks/seed/build.assets.dev.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import { APP_DEST, APP_SRC, TEMP_FILES } from '../../config';
1010
export = () => {
1111
let paths: string[] = [
1212
join(APP_SRC, '**'),
13-
'!' + join(APP_SRC, '**', '*.ts')
13+
'!' + join(APP_SRC, '**', '*.ts'),
14+
'!' + join(APP_SRC, '**', '*.scss')
1415
].concat(TEMP_FILES.map((p) => { return '!' + p; }));
1516

1617
return gulp.src(paths)

tools/tasks/seed/build.assets.prod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export = () => {
2525
'!' + join(APP_SRC, '**', '*.ts'),
2626
'!' + join(APP_SRC, '**', '*.css'),
2727
'!' + join(APP_SRC, '**', '*.html'),
28+
'!' + join(APP_SRC, '**', '*.scss'),
2829
'!' + join(ASSETS_SRC, '**', '*.js')
2930
].concat(TEMP_FILES.map((p) => { return '!' + p; })))
3031
.pipe(onlyDirs(es))

tools/tasks/seed/build.html_css.ts

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,18 @@ import * as gulpLoadPlugins from 'gulp-load-plugins';
55
import * as merge from 'merge-stream';
66
import { join } from 'path';
77

8-
import { APP_DEST, APP_SRC, BROWSER_LIST, CSS_DEST, CSS_PROD_BUNDLE, DEPENDENCIES, ENV, getPluginConfig, TMP_DIR } from '../../config';
8+
import {
9+
APP_DEST,
10+
APP_SRC,
11+
BROWSER_LIST,
12+
CSS_DEST,
13+
CSS_PROD_BUNDLE,
14+
DEPENDENCIES,
15+
ENV,
16+
getPluginConfig,
17+
TMP_DIR,
18+
ENABLE_SCSS
19+
} from '../../config';
920

1021
const plugins = <any>gulpLoadPlugins();
1122
const cleanCss = require('gulp-clean-css');
@@ -37,6 +48,55 @@ function prepareTemplates() {
3748
.pipe(gulp.dest(TMP_DIR));
3849
}
3950

51+
/**
52+
* Execute the appropriate component-stylesheet processing method based on presence of --scss flag.
53+
*/
54+
function processComponentStylesheets() {
55+
return ENABLE_SCSS ? processScss('component') : processComponentCss();
56+
}
57+
58+
/**
59+
* Processes the SCSS files for the specified origin.
60+
* @param {string} origin The origin of the SCSS files being handled. Must be either 'component' or 'external.'
61+
*/
62+
function processScss(origin: 'component'|'external') {
63+
let scssSrc = getScssForOrigin(origin);
64+
let scssDest = getScssDestForOrigin(origin);
65+
return gulp.src(scssSrc)
66+
.pipe(isProd ? plugins.cached(`process-${origin}-scss`) : plugins.util.noop())
67+
.pipe(isProd ? plugins.progeny() : plugins.util.noop())
68+
.pipe(plugins.sourcemaps.init())
69+
.pipe(plugins.sass({includePaths: ['./node_modules/']}).on('error', plugins.sass.logError))
70+
.pipe(plugins.postcss(processors))
71+
.pipe(plugins.sourcemaps.write(isProd ? '.' : ''))
72+
.pipe(gulp.dest(scssDest));
73+
}
74+
75+
/**
76+
* Get an array of SCSS files belonging to the specified origin.
77+
* @param {string} origin The origin of the SCSS files being handled. Must be either 'component' or 'external.'
78+
*/
79+
function getScssForOrigin(origin: 'component'|'external') {
80+
if (origin === 'component') return [join(APP_SRC, '**', '*.scss')]; else {
81+
return getExternalStylesheets().map(r => r.src);
82+
}
83+
}
84+
85+
function getExternalStylesheets() {
86+
let stylesheet = ENABLE_SCSS ? 'scss' : 'css';
87+
return DEPENDENCIES.filter(d => new RegExp(`\.${stylesheet}$`).test(d.src));
88+
}
89+
90+
/**
91+
* Get the destination of the processed SCSS files belonging to the specified origin.
92+
* @param {string} origin The origin of the SCSS files being handled. Must be either 'component' or 'external.'
93+
*/
94+
function getScssDestForOrigin(origin: 'component'|'external') {
95+
if (origin === 'component') return (isProd ? TMP_DIR : APP_DEST); else {
96+
return CSS_DEST;
97+
}
98+
}
99+
40100
/**
41101
* Processes the CSS files within `src/client` excluding those in `src/client/assets` using `postcss` with the
42102
* configured processors.
@@ -48,7 +108,14 @@ function processComponentCss() {
48108
])
49109
.pipe(isProd ? plugins.cached('process-component-css') : plugins.util.noop())
50110
.pipe(plugins.postcss(processors))
51-
.pipe(gulp.dest(isProd ? TMP_DIR: APP_DEST));
111+
.pipe(gulp.dest(isProd ? TMP_DIR : APP_DEST));
112+
}
113+
114+
/**
115+
* Execute external-stylesheet processing method based on presence of --scss flag.
116+
*/
117+
function processExternalStylesheets() {
118+
return ENABLE_SCSS ? processScss('external') : processExternalCss();
52119
}
53120

54121
/**
@@ -73,4 +140,4 @@ function getExternalCss() {
73140
/**
74141
* Executes the build process, processing the HTML and CSS files.
75142
*/
76-
export = () => merge(processComponentCss(), prepareTemplates(), processExternalCss());
143+
export = () => merge(processComponentStylesheets(), prepareTemplates(), processExternalStylesheets());

tools/tasks/seed/build.index.dev.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as gulpLoadPlugins from 'gulp-load-plugins';
33
import { join } from 'path';
44
import * as slash from 'slash';
55

6-
import { APP_BASE, APP_DEST, APP_SRC, DEPENDENCIES } from '../../config';
6+
import { APP_BASE, APP_DEST, APP_SRC, DEPENDENCIES, CSS_DEST, ASSETS_SRC } from '../../config';
77
import { templateLocals } from '../../utils';
88

99
const plugins = <any>gulpLoadPlugins();
@@ -48,8 +48,10 @@ function getInjectablesDependenciesRef(name?: string) {
4848
*/
4949
function mapPath(dep: any) {
5050
let envPath = dep.src;
51-
if (envPath.startsWith(APP_SRC)) {
52-
envPath = join(APP_DEST, dep.src.replace(APP_SRC, ''));
51+
if (envPath.startsWith(APP_SRC) && !envPath.endsWith('.scss')) {
52+
envPath = join(APP_DEST, envPath.replace(APP_SRC, ''));
53+
} else if (envPath.startsWith(APP_SRC) && envPath.endsWith('.scss')) {
54+
envPath = envPath.replace(ASSETS_SRC, CSS_DEST).replace('.scss', '.css');
5355
}
5456
return envPath;
5557
}

tools/tasks/seed/css-lint.ts

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import * as gulpLoadPlugins from 'gulp-load-plugins';
55
import * as merge from 'merge-stream';
66
import * as reporter from 'postcss-reporter';
77
import * as stylelint from 'stylelint';
8-
import { join} from 'path';
8+
import { join } from 'path';
99

10-
import { APP_ASSETS, APP_SRC, BROWSER_LIST, CSS_SRC, ENV } from '../../config';
10+
import { APP_ASSETS, APP_SRC, BROWSER_LIST, CSS_SRC, ENV, DEPENDENCIES, ENABLE_SCSS } from '../../config';
1111

1212
const plugins = <any>gulpLoadPlugins();
1313

1414
const isProd = ENV === 'prod';
15+
var stylesheetType = ENABLE_SCSS ? 'scss' : 'css';
1516

1617
const processors = [
1718
doiuse({
@@ -22,35 +23,33 @@ const processors = [
2223
reporter({clearMessages: true})
2324
];
2425

25-
/**
26-
* Lints the component CSS files.
27-
*/
28-
function lintComponentCss() {
26+
function lintComponentStylesheets() {
2927
return gulp.src([
30-
join(APP_SRC, '**', '*.css'),
31-
'!' + join(CSS_SRC, '**', '*.css')
32-
])
33-
.pipe(isProd ? plugins.cached('css-lint') : plugins.util.noop())
34-
.pipe(plugins.postcss(processors));
28+
join(APP_SRC, '**', `*.${stylesheetType}`),
29+
`!${join(APP_SRC, 'assets', '**', '*.scss')}`,
30+
`!${join(CSS_SRC, '**', '*.css')}`
31+
]).pipe(isProd ? plugins.cached('css-lint') : plugins.util.noop())
32+
.pipe(ENABLE_SCSS ? plugins.sassLint() : plugins.postcss(processors))
33+
.pipe(ENABLE_SCSS ? plugins.sassLint.format() : plugins.util.noop())
34+
.pipe(ENABLE_SCSS ? plugins.sassLint.failOnError() : plugins.util.noop());
3535
}
3636

37-
/**
38-
* Lints the external CSS files.
39-
*/
40-
function lintExternalCss() {
41-
return gulp.src(getExternalCss().map(r => r.src))
37+
function lintExternalStylesheets() {
38+
return gulp.src(getExternalStylesheets().map(r => r.src))
4239
.pipe(isProd ? plugins.cached('css-lint') : plugins.util.noop())
43-
.pipe(plugins.postcss(processors));
40+
.pipe(ENABLE_SCSS ? plugins.sassLint() : plugins.postcss(processors))
41+
.pipe(ENABLE_SCSS ? plugins.sassLint.format() : plugins.util.noop())
42+
.pipe(ENABLE_SCSS ? plugins.sassLint.failOnError() : plugins.util.noop());
4443
}
4544

46-
/**
47-
* Returns the array of external CSS files.
48-
*/
49-
function getExternalCss() {
50-
return APP_ASSETS.filter(d => /\.css$/.test(d.src) && !d.vendor);
45+
function getExternalStylesheets() {
46+
let stylesheets = ENABLE_SCSS ? DEPENDENCIES : APP_ASSETS;
47+
return stylesheets
48+
.filter(d => new RegExp(`\.${stylesheetType}$`)
49+
.test(d.src) && !d.vendor);
5150
}
5251

5352
/**
5453
* Executes the build process, linting the component and external CSS files using `stylelint`.
5554
*/
56-
export = () => merge(lintComponentCss(), lintExternalCss());
55+
export = () => merge(lintComponentStylesheets(), lintExternalStylesheets());

typings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"gulp": "github:DefinitelyTyped/DefinitelyTyped/gulp/gulp.d.ts#5c3e47967affa3c4128a3875d1664ba206ae1b0f",
1717
"gulp-load-plugins": "github:DefinitelyTyped/DefinitelyTyped/gulp-load-plugins/gulp-load-plugins.d.ts#e081148d88b857d66509e3b46edbd08b3f75f96a",
1818
"gulp-protractor": "registry:dt/gulp-protractor#1.0.0+20160316155526",
19+
"gulp-sass": "github:DefinitelyTyped/DefinitelyTyped/gulp-sass/gulp-sass.d.ts#56295f5058cac7ae458540423c50ac2dcf9fc711",
1920
"gulp-shell": "github:DefinitelyTyped/DefinitelyTyped/gulp-shell/gulp-shell.d.ts#5c3e47967affa3c4128a3875d1664ba206ae1b0f",
2021
"gulp-util": "github:DefinitelyTyped/DefinitelyTyped/gulp-util/gulp-util.d.ts#5a8fc5ee71701431e4fdbb80c506e3c13f85a9ff",
2122
"jasmine": "github:DefinitelyTyped/DefinitelyTyped/jasmine/jasmine.d.ts#26c98c8a9530c44f8c801ccc3b2057e2101187ee",

0 commit comments

Comments
 (0)