Skip to content

Commit 623fcab

Browse files
committed
Refactor.
1 parent 2440768 commit 623fcab

2 files changed

Lines changed: 45 additions & 37 deletions

File tree

core-build/gulp-core-build-sass/src/CSSModules.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,15 @@ import * as cssModules from 'postcss-modules';
55
import * as crypto from 'crypto';
66

77
export interface ICSSModules {
8+
/**
9+
* Return a configured postcss plugin that will map class names to a
10+
* consistently generated scoped name.
11+
*/
812
getPlugin: () => postcss.AcceptedPlugin;
13+
14+
/**
15+
* Return the CSS class map that is stored after postcss-modules runs.
16+
*/
917
getCssJSON: () => Object;
1018
}
1119

core-build/gulp-core-build-sass/src/SassTask.ts

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -212,56 +212,22 @@ export class SassTask extends GulpTask<ISassTaskConfig> {
212212

213213
const scssTsOutputPath: string = `${filePath}.ts`;
214214
const classMap: Object = cssModules.getCssJSON();
215-
let exportClassNames: string = '';
215+
const stylesExportString: string = this._getStylesExportString(classMap);
216216
const content: string | undefined = result.styles;
217217

218-
if (classMap) {
219-
const classKeys: string[] = Object.keys(classMap);
220-
const styleLines: string[] = [];
221-
classKeys.forEach((key: string) => {
222-
const value: string = classMap[key];
223-
if (key.indexOf('-') !== -1) {
224-
const message: string = `The local CSS class '${key}' is not ` +
225-
`camelCase and will not be type-safe.`;
226-
if (this.taskConfig.warnOnCssInvalidPropertyName) {
227-
this.logWarning(message);
228-
} else {
229-
this.logVerbose(message);
230-
}
231-
}
232-
styleLines.push(` ${key}: '${value}'`);
233-
});
234-
235-
let exportString: string = 'export default styles;';
236-
237-
if (this.taskConfig.moduleExportName === '') {
238-
exportString = 'export = styles;';
239-
} else if (!!this.taskConfig.moduleExportName) {
240-
// exportString = `export const ${this.taskConfig.moduleExportName} = styles;`;
241-
}
242-
243-
exportClassNames = [
244-
'const styles = {',
245-
styleLines.join(`,${EOL}`),
246-
'};',
247-
'',
248-
exportString
249-
].join(EOL);
250-
}
251-
252218
let lines: string[] = [];
253219
lines.push(this.taskConfig.preamble || '');
254220

255221
if (cssOutputPathAbsolute) {
256222
lines = lines.concat([
257223
`require(${JSON.stringify(`./${path.basename(cssOutputPathAbsolute)}`)});`,
258-
exportClassNames
224+
stylesExportString
259225
]);
260226
} else if (!!content) {
261227
lines = lines.concat([
262228
'import { loadStyles } from \'@microsoft/load-themed-styles\';',
263229
'',
264-
exportClassNames,
230+
stylesExportString,
265231
'',
266232
`loadStyles(${JSON.stringify(splitStyles(content))});`
267233
]);
@@ -308,4 +274,38 @@ export class SassTask extends GulpTask<ISassTaskConfig> {
308274

309275
return url;
310276
}
277+
278+
private _getStylesExportString(classMap: Object): string {
279+
const classKeys: string[] = Object.keys(classMap);
280+
const styleLines: string[] = [];
281+
classKeys.forEach((key: string) => {
282+
const value: string = classMap[key];
283+
if (key.indexOf('-') !== -1) {
284+
const message: string = `The local CSS class '${key}' is not ` +
285+
`camelCase and will not be type-safe.`;
286+
if (this.taskConfig.warnOnCssInvalidPropertyName) {
287+
this.logWarning(message);
288+
} else {
289+
this.logVerbose(message);
290+
}
291+
}
292+
styleLines.push(` ${key}: '${value}'`);
293+
});
294+
295+
let exportString: string = 'export default styles;';
296+
297+
if (this.taskConfig.moduleExportName === '') {
298+
exportString = 'export = styles;';
299+
} else if (!!this.taskConfig.moduleExportName) {
300+
// exportString = `export const ${this.taskConfig.moduleExportName} = styles;`;
301+
}
302+
303+
return [
304+
'const styles = {',
305+
styleLines.join(`,${EOL}`),
306+
'};',
307+
'',
308+
exportString
309+
].join(EOL);
310+
}
311311
}

0 commit comments

Comments
 (0)