Skip to content

Commit

Permalink
fix: Fix name conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
ntt261298 committed May 4, 2022
1 parent 14d2b2d commit c8e19fa
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
15 changes: 9 additions & 6 deletions src/configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface JestPreviewConfigOptions {
export async function jestPreviewConfigure(
{
externalCss = [],
sassLoadPaths = [],
autoPreview = false,
publicFolder,
}: JestPreviewConfigOptions = {
Expand All @@ -34,16 +35,18 @@ export async function jestPreviewConfigure(
});
}

let sassLoadPaths: string[] = [];
// Save sassLoadPaths to cache, so we can use it in the transformer
let sassLoadPathsConfig: string[] = [];
// Save sassLoadPathsConfig to cache, so we can use it in the transformer
if (sassLoadPaths) {
sassLoadPaths = sassLoadPaths.map((path) => `${process.cwd()}/${path}`);
sassLoadPathsConfig = sassLoadPaths.map(
(path) => `${process.cwd()}/${path}`,
);

createCacheFolderIfNeeded();

fs.writeFileSync(
path.join(CACHE_FOLDER, SASS_LOAD_PATHS_CONFIG),
JSON.stringify(sassLoadPaths),
JSON.stringify(sassLoadPathsConfig),
);
}

Expand All @@ -63,7 +66,7 @@ export async function jestPreviewConfigure(
'.css',
);

const sassLoadPathsConfig = sassLoadPaths.reduce(
const sassLoadPathsCLIConfig = sassLoadPathsConfig.reduce(
(currentConfig, nextLoadPath) =>
`${currentConfig} --load-path ${nextLoadPath}`,
'',
Expand All @@ -77,7 +80,7 @@ export async function jestPreviewConfigure(
// Todo: Support import ~ for configured scss
// Currently, we cannot find the option to pass `importer` to sass CLI: https://sass-lang.com/documentation/cli/dart-sass#options
exec(
`./node_modules/.bin/sass ${cssFile} ${cssDestinationFile} --no-source-map ${sassLoadPathsConfig}`,
`./node_modules/.bin/sass ${cssFile} ${cssDestinationFile} --no-source-map ${sassLoadPathsCLIConfig}`,
(err: any) => {
if (err) {
console.log(err);
Expand Down
8 changes: 4 additions & 4 deletions src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,18 @@ function processSass(src: string, filename: string): string {
SASS_LOAD_PATHS_CONFIG,
);

let sassLoadPaths: string[];
let sassLoadPathsConfig: string[];
if (fs.existsSync(sassLoadPathsConfigPath)) {
const sassLoadPathsString = fs
.readFileSync(path.join(CACHE_FOLDER, SASS_LOAD_PATHS_CONFIG), 'utf8')
.trim();
sassLoadPaths = JSON.parse(sassLoadPathsString);
sassLoadPathsConfig = JSON.parse(sassLoadPathsString);
} else {
sassLoadPaths = [];
sassLoadPathsConfig = [];
}

const cssResult = sass.compile(filename, {
loadPaths: sassLoadPaths,
loadPaths: sassLoadPathsConfig,
importers: [
{
// An importer that redirects relative URLs starting with "~" to `node_modules`
Expand Down

0 comments on commit c8e19fa

Please sign in to comment.