Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@rushstack/heft-sass-plugin",
"comment": "Add `preserveIcssExports` option to keep the ICSS `:export` block in emitted CSS output, required when downstream webpack loaders (e.g. `css-loader` icssParser) need to extract `:export` values at bundle time.",
"type": "minor"
}
],
"packageName": "@rushstack/heft-sass-plugin"
}
3 changes: 3 additions & 0 deletions common/config/subspaces/default/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions heft-plugins/heft-sass-plugin/config/jest.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "local-node-rig/profiles/default/config/jest.config.json"
}
1 change: 1 addition & 0 deletions heft-plugins/heft-sass-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"devDependencies": {
"@microsoft/api-extractor": "workspace:*",
"@rushstack/heft": "workspace:*",
"@rushstack/terminal": "workspace:*",
"eslint": "~9.37.0",
"local-node-rig": "workspace:*"
},
Expand Down
5 changes: 4 additions & 1 deletion heft-plugins/heft-sass-plugin/src/SassPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface ISassConfigurationJson {
silenceDeprecations?: string[];
excludeFiles?: string[];
doNotTrimOriginalFileExtension?: boolean;
preserveIcssExports?: boolean;
}

const SASS_CONFIGURATION_LOCATION: string = 'config/sass.json';
Expand Down Expand Up @@ -98,7 +99,8 @@ export default class SassPlugin implements IHeftPlugin {
nonModuleFileExtensions,
silenceDeprecations,
excludeFiles,
doNotTrimOriginalFileExtension
doNotTrimOriginalFileExtension,
preserveIcssExports
} = sassConfigurationJson || {};

function resolveFolder(folder: string): string {
Expand Down Expand Up @@ -126,6 +128,7 @@ export default class SassPlugin implements IHeftPlugin {
}),
silenceDeprecations,
doNotTrimOriginalFileExtension,
preserveIcssExports,
postProcessCssAsync: hooks.postProcessCss.isUsed()
? async (cssText: string) => hooks.postProcessCss.promise(cssText)
: undefined
Expand Down
21 changes: 19 additions & 2 deletions heft-plugins/heft-sass-plugin/src/SassProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ export interface ISassProcessorOptions {
*/
doNotTrimOriginalFileExtension?: boolean;

/**
* If true, the ICSS `:export` block will be preserved in the emitted CSS output. This is necessary
* when the CSS is consumed by a webpack loader (e.g. css-loader's icssParser) that extracts `:export`
* values at bundle time to generate JavaScript exports.
*
* Defaults to false.
*/
preserveIcssExports?: boolean;

/**
* A callback to further modify the raw CSS text after it has been generated. Only relevant if emitting CSS files.
*/
Expand Down Expand Up @@ -751,7 +760,8 @@ export class SassProcessor {
srcFolder,
exportAsDefault,
doNotTrimOriginalFileExtension,
postProcessCssAsync
postProcessCssAsync,
preserveIcssExports
} = this._options;

// Handle CSS modules
Expand All @@ -769,7 +779,14 @@ export class SassProcessor {
const postCssResult: postcss.Result = await postcss
.default([postCssModules])
.process(css, { from: sourceFilePath });
css = postCssResult.css;

if (!preserveIcssExports) {
// Default behavior: use the transformed CSS output, which has the :export block stripped.
css = postCssResult.css;
}
// If preserveIcssExports is true, we discard the transformed CSS and keep the original so
// that the :export block remains in the output for downstream webpack loaders (e.g.
// css-loader's icssParser) that extract :export values at bundle time.
}

if (postProcessCssAsync) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@
"doNotTrimOriginalFileExtension": {
"type": "boolean",
"description": "If true, the original file extension will not be trimmed when generating the output CSS file. The generated CSS file will retain its original extension. For example, \"styles.scss\" will generate \"styles.scss.css\" instead of \"styles.css\"."
},

"preserveIcssExports": {
"type": "boolean",
"description": "If true, the ICSS `:export` block will be preserved in the emitted CSS output. This is necessary when the CSS is consumed by a webpack loader (e.g. css-loader's icssParser) that extracts `:export` values at bundle time to generate JavaScript exports. Defaults to false."
}
}
}
Loading