-
-
Notifications
You must be signed in to change notification settings - Fork 76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for configuring Shared Sass Resources for using global mixins, variables, etc. #300
base: main
Are you sure you want to change the base?
Changes from 2 commits
83f667f
ff53d5c
20f6c75
390d1a8
8bb5e3a
bb2dd4a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
$limeGreen: #32cd32; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
@mixin desktop { | ||
@media only screen and (min-width: 900px) { | ||
@content; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export const CACHE_FOLDER = './node_modules/.cache/jest-preview'; | ||
export const CACHE_SUB_FOLDER = '.cache/jest-preview'; | ||
export const CACHE_FOLDER = `./node_modules/${CACHE_SUB_FOLDER}`; | ||
export const SASS_LOAD_PATHS_CONFIG = 'cache-sass-load-paths.config'; | ||
export const SASS_SHARED_RESOURCES_CONCAT = '_sass-resources-concat.scss'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ import { pathToFileURL } from 'url'; | |
import camelcase from 'camelcase'; | ||
import slash from 'slash'; | ||
import { transform } from '@svgr/core'; | ||
import { CACHE_FOLDER, SASS_LOAD_PATHS_CONFIG } from './constants'; | ||
import { CACHE_FOLDER, CACHE_SUB_FOLDER, SASS_LOAD_PATHS_CONFIG, SASS_SHARED_RESOURCES_CONCAT } from './constants'; | ||
import { createCacheFolderIfNeeded } from './utils'; | ||
|
||
// https://github.com/vitejs/vite/blob/c29613013ca1c6d9c77b97e2253ed1f07e40a544/packages/vite/src/node/plugins/css.ts#L97-L98 | ||
|
@@ -372,6 +372,10 @@ module.exports = ${output.cssModulesExportedTokens || '{}'}`, | |
}; | ||
} | ||
|
||
type BuildCssOptions = { | ||
prependUseShared: boolean; | ||
}; | ||
|
||
function processSass(filename: string): string { | ||
let sass; | ||
|
||
|
@@ -397,6 +401,20 @@ function processSass(filename: string): string { | |
sassLoadPathsConfig = []; | ||
} | ||
|
||
// Workaround for ?bug? with sass.compileString that the location of the source file is not automatically included in load paths | ||
sassLoadPathsConfig.push(path.parse(filename).dir); | ||
|
||
const sharedSassResourcesPath = path.join(CACHE_FOLDER, SASS_SHARED_RESOURCES_CONCAT); | ||
const buildOptions: BuildCssOptions = { prependUseShared: false}; | ||
|
||
if (fs.existsSync(sharedSassResourcesPath)) { | ||
buildOptions.prependUseShared = true; | ||
} | ||
|
||
return buildCssResult(sass, sassLoadPathsConfig, filename, buildOptions); | ||
} | ||
|
||
function buildCssResult(sass: any, sassLoadPathsConfig: string[], filename: string, options: BuildCssOptions): string { | ||
let cssResult; | ||
|
||
// An importer that redirects relative URLs starting with "~" to `node_modules` | ||
|
@@ -412,17 +430,32 @@ function processSass(filename: string): string { | |
); | ||
}; | ||
|
||
if (sass.compile) { | ||
cssResult = sass.compile(filename, { | ||
loadPaths: sassLoadPathsConfig, | ||
importers: [ | ||
{ | ||
findFileUrl(url: string) { | ||
return tildeImporter(url); | ||
}, | ||
if (options.prependUseShared && !sass.compileString) { | ||
console.warn("WARNING: Config specifies sharedSassResources, but your version of Sass doesn't support it - consider updating to v1.45.0 or higher.") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should add a semicolon to the end of this line |
||
} | ||
|
||
const compileOptions = { | ||
loadPaths: sassLoadPathsConfig, | ||
importers: [ | ||
{ | ||
findFileUrl(url: string) { | ||
return tildeImporter(url); | ||
}, | ||
], | ||
}).css; | ||
}, | ||
], | ||
} | ||
|
||
if (options.prependUseShared && sass.compileString) { | ||
// Transform the filename for use in "use" statement | ||
const sharedResourcesFilenameForUse = SASS_SHARED_RESOURCES_CONCAT.replace(/^_|\.scss$/g, ''); | ||
// Prepend a "use" statement so shared sass resources are accessible from all source files | ||
const useSharedStatement = `@use '~${CACHE_SUB_FOLDER}/${sharedResourcesFilenameForUse}' as *;`; | ||
const sassContent = fs.readFileSync(filename, 'utf8'); | ||
const sassPrefixedWithSharedResources = `${useSharedStatement}\n\n${sassContent}`; | ||
|
||
cssResult = sass.compileString(sassPrefixedWithSharedResources, compileOptions).css; | ||
} else if (sass.compile) { | ||
cssResult = sass.compile(filename, compileOptions).css; | ||
} | ||
// Because sass.compile is only introduced since sass version 1.45.0 | ||
// For older versions, we have to use the legacy API: renderSync | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,3 +59,9 @@ Default: `false` | |
Automatically preview the UI in the external browser when the test fails. You don't need to invoke `preview.debug()` by yourself anymore. | ||
|
||
Set to `false` if you experience any error or just want to opt out. | ||
|
||
## sharedSassResources: string[] | ||
|
||
Default: `undefined` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The default value should be [], just for consistency with sassLoadPaths, and no need to check that |
||
|
||
Optional list of paths to SASS files that define shared resources (e.g. variables, mixins, etc). The paths are relative to the root of the project. Requires SASS v1.45.0 or higher. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mikeb-meq I think we can move the logic to create
sassPrefixedWithSharedResources
to be here, the reasons should be:sassLoadPathsConfig
. We might update thebuildCssResult
arguments to `buildCssResult(sass: any, sassLoadPathsConfig: string[], sassPrefixedWithSharedResources: string, filename: string)