-
Notifications
You must be signed in to change notification settings - Fork 698
Expand file tree
/
Copy pathSassPlugin.ts
More file actions
208 lines (180 loc) · 6.72 KB
/
Copy pathSassPlugin.ts
File metadata and controls
208 lines (180 loc) · 6.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import path from 'node:path';
import { AsyncSeriesWaterfallHook } from 'tapable';
import type {
HeftConfiguration,
IHeftTaskSession,
IHeftPlugin,
IHeftTaskRunHookOptions,
IHeftTaskRunIncrementalHookOptions,
IWatchedFileState,
ConfigurationFile
} from '@rushstack/heft';
import { PLUGIN_NAME } from './constants';
import { type ICssOutputFolder, type ISassProcessorOptions, SassProcessor } from './SassProcessor';
import sassConfigSchema from './schemas/heft-sass-plugin.schema.json';
export interface ISassConfigurationJson {
srcFolder?: string;
generatedTsFolder?: string;
cssOutputFolders?: (string | ICssOutputFolder)[];
secondaryGeneratedTsFolders?: string[];
exportAsDefault?: boolean;
fileExtensions?: string[];
nonModuleFileExtensions?: string[];
silenceDeprecations?: string[];
excludeFiles?: string[];
doNotTrimOriginalFileExtension?: boolean;
preserveIcssExports?: boolean;
sourceMap?: boolean;
}
const SASS_CONFIGURATION_LOCATION: string = 'config/sass.json';
const SASS_CONFIGURATION_FILE_SPECIFICATION: ConfigurationFile.IProjectConfigurationFileSpecification<ISassConfigurationJson> =
{
projectRelativeFilePath: SASS_CONFIGURATION_LOCATION,
jsonSchemaObject: sassConfigSchema
};
/**
* @public
*/
export interface ISassPluginAccessor {
readonly hooks: ISassPluginAccessorHooks;
}
/**
* @public
*/
export interface ISassPluginAccessorHooks {
/**
* Hook that will be invoked after the CSS is generated but before it is written to a file.
*/
readonly postProcessCss: AsyncSeriesWaterfallHook<string>;
}
export default class SassPlugin implements IHeftPlugin {
public accessor: ISassPluginAccessor = {
hooks: {
postProcessCss: new AsyncSeriesWaterfallHook<string>(['cssText'])
}
};
/**
* Generate typings for Sass files before TypeScript compilation.
*/
public apply(taskSession: IHeftTaskSession, heftConfiguration: HeftConfiguration): void {
const { numberOfCores, slashNormalizedBuildFolderPath } = heftConfiguration;
const { logger, tempFolderPath } = taskSession;
const { terminal } = logger;
const {
accessor: { hooks }
} = this;
let sassProcessorPromise: Promise<SassProcessor> | undefined;
function initializeSassProcessorAsync(): Promise<SassProcessor> {
if (sassProcessorPromise) {
return sassProcessorPromise;
}
return (sassProcessorPromise = (async (): Promise<SassProcessor> => {
const sassConfigurationJson: ISassConfigurationJson | undefined =
await heftConfiguration.tryLoadProjectConfigurationFileAsync(
SASS_CONFIGURATION_FILE_SPECIFICATION,
terminal
);
const {
generatedTsFolder = 'temp/sass-ts',
srcFolder = 'src',
cssOutputFolders,
secondaryGeneratedTsFolders,
exportAsDefault = true,
fileExtensions,
nonModuleFileExtensions,
silenceDeprecations,
excludeFiles,
doNotTrimOriginalFileExtension,
preserveIcssExports,
sourceMap
} = sassConfigurationJson || {};
function resolveFolder(folder: string): string {
return path.resolve(slashNormalizedBuildFolderPath, folder);
}
const sassProcessorOptions: ISassProcessorOptions = {
buildFolder: slashNormalizedBuildFolderPath,
concurrency: numberOfCores,
dtsOutputFolders: [generatedTsFolder, ...(secondaryGeneratedTsFolders || [])].map(resolveFolder),
logger,
exportAsDefault,
srcFolder: resolveFolder(srcFolder),
excludeFiles,
fileExtensions,
nonModuleFileExtensions,
cssOutputFolders: cssOutputFolders?.map((folder: string | ICssOutputFolder) => {
const folderPath: string = typeof folder === 'string' ? folder : folder.folder;
const shimModuleFormat: 'commonjs' | 'esnext' | undefined =
typeof folder === 'string' ? undefined : folder.shimModuleFormat;
return {
folder: resolveFolder(folderPath),
shimModuleFormat
};
}),
silenceDeprecations,
doNotTrimOriginalFileExtension,
preserveIcssExports,
sourceMap,
postProcessCssAsync: hooks.postProcessCss.isUsed()
? async (cssText: string) => hooks.postProcessCss.promise(cssText)
: undefined
};
const sassProcessor: SassProcessor = new SassProcessor(sassProcessorOptions);
await sassProcessor.loadCacheAsync(tempFolderPath);
return sassProcessor;
})());
}
const compileFilesAsync = async (
sassProcessor: SassProcessor,
files: Set<string>,
changed: boolean
): Promise<void> => {
if (files.size === 0) {
terminal.writeLine(`No SCSS files to process.`);
return;
}
await sassProcessor.compileFilesAsync(files);
terminal.writeLine(`Finished compiling.`);
};
taskSession.hooks.run.tapPromise(PLUGIN_NAME, async (runOptions: IHeftTaskRunHookOptions) => {
terminal.writeLine(`Starting...`);
const sassProcessor: SassProcessor = await initializeSassProcessorAsync();
terminal.writeVerboseLine(`Scanning for SCSS files...`);
const files: string[] = await runOptions.globAsync(sassProcessor.inputFileGlob, {
absolute: true,
ignore: sassProcessor.ignoredFileGlobs,
cwd: sassProcessor.sourceFolderPath
});
const fileSet: Set<string> = new Set();
for (const file of files) {
// Using path.resolve to normalize slashes
fileSet.add(path.resolve(file));
}
await compileFilesAsync(sassProcessor, fileSet, false);
});
taskSession.hooks.runIncremental.tapPromise(
PLUGIN_NAME,
async (runOptions: IHeftTaskRunIncrementalHookOptions) => {
terminal.writeLine(`Starting...`);
const sassProcessor: SassProcessor = await initializeSassProcessorAsync();
terminal.writeVerboseLine(`Scanning for changed SCSS files...`);
const changedFiles: Map<string, IWatchedFileState> = await runOptions.watchGlobAsync(
sassProcessor.inputFileGlob,
{
absolute: true,
cwd: sassProcessor.sourceFolderPath,
ignore: sassProcessor.ignoredFileGlobs
}
);
const modifiedFiles: Set<string> = new Set();
for (const [file, { changed }] of changedFiles) {
if (changed) {
modifiedFiles.add(file);
}
}
await compileFilesAsync(sassProcessor, modifiedFiles, true);
}
);
}
}