Skip to content

Commit

Permalink
PR #308 - sass-update-from-1.62, by glenn2223
Browse files Browse the repository at this point in the history
Update SASS past 1.62 by forcing import of node version
  • Loading branch information
glenn2223 authored Aug 8, 2023
2 parents c37e4e2 + ae7dbe1 commit abf29d4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 89 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@
"fdir": "^6.0.1",
"picomatch": "^2.3.1",
"postcss": "^8.4.26",
"sass": "^1.62.1"
"sass": "^1.64.2"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^25.0.3",
Expand Down
110 changes: 26 additions & 84 deletions src/SassCompileHelper.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,29 @@
import { Helper, IFormat } from "./helper";
import { OutputWindow } from "./VscodeExtensions";
import { OutputLevel } from "./OutputLevel";
import { LegacyException } from "sass";
import * as compiler from "sass";
import { workspace } from "vscode";
import { existsSync } from "fs";
import path from "path";
import { fileURLToPath, pathToFileURL } from "url";
import * as compiler from "../node_modules/sass/sass.node";

export class SassHelper {
private static parsePath<T>(
importUrl: string,
cb: (newPath: string) => T
): T | null {
private static parsePath<T>(importUrl: string, cb: (newPath: string) => T): T | null {
if (workspace.workspaceFolders) {
const normalisedUrl = importUrl.replace(/\\/g, "/"),
urlParts = normalisedUrl
.substring(1)
.split("/")
.filter((x) => x.length > 0);

if (
normalisedUrl.startsWith("~") &&
normalisedUrl.indexOf("/") > -1
) {
if (normalisedUrl.startsWith("~") && normalisedUrl.indexOf("/") > -1) {
for (let i = 0; i < workspace.workspaceFolders.length; i++) {
const workingPath = [
workspace.workspaceFolders[i].uri.fsPath,
"node_modules",
]
const workingPath = [workspace.workspaceFolders[i].uri.fsPath, "node_modules"]
.concat(...urlParts.slice(0, -1))
.join("/");

if (existsSync(workingPath)) {
return cb(
workingPath +
path.sep +
urlParts.slice(-1).join(path.sep)
);
return cb(workingPath + path.sep + urlParts.slice(-1).join(path.sep));
}
}
} else if (normalisedUrl.startsWith("/")) {
Expand All @@ -49,16 +35,9 @@ export class SassHelper {
);

if (rootIsWorkspace) {
const filePath = [
folder.uri.fsPath,
normalisedUrl.substring(1),
].join("/");

if (
existsSync(
filePath.substring(0, filePath.lastIndexOf("/"))
)
) {
const filePath = [folder.uri.fsPath, normalisedUrl.substring(1)].join("/");

if (existsSync(filePath.substring(0, filePath.lastIndexOf("/")))) {
return cb(filePath);
}
}
Expand All @@ -81,13 +60,7 @@ export class SassHelper {
OutputWindow.Show(
OutputLevel.Warning,
"Warning:",
[message].concat(
this.format(
options.span,
options.stack,
options.deprecation
)
)
[message].concat(this.format(options.span, options.stack, options.deprecation))
);
},
debug: (message: string, options: { span?: compiler.SourceSpan }) => {
Expand All @@ -102,9 +75,7 @@ export class SassHelper {
static toSassOptions<T extends boolean>(
format: IFormat,
useNew: T
): T extends true
? compiler.LegacyFileOptions<"sync">
: compiler.Options<"sync">;
): T extends true ? compiler.LegacyFileOptions<"sync"> : compiler.Options<"sync">;
static toSassOptions(
format: IFormat,
useNew: boolean
Expand All @@ -115,9 +86,7 @@ export class SassHelper {
importers: [
{
findFileUrl: (importUrl) =>
SassHelper.parsePath(importUrl, (newPath) =>
pathToFileURL(newPath)
),
SassHelper.parsePath(importUrl, (newPath) => pathToFileURL(newPath)),
},
],
logger: SassHelper.loggerProperty,
Expand Down Expand Up @@ -179,13 +148,10 @@ export class SassHelper {
const compileResult = compiler.compile(SassPath, options);

if (compileResult.sourceMap) {
compileResult.sourceMap.sources =
compileResult.sourceMap.sources.map((sourcePath) =>
path.relative(
path.join(targetCssUri, "../"),
fileURLToPath(sourcePath)
)
);
compileResult.sourceMap.sources = compileResult.sourceMap.sources.map(
(sourcePath) =>
path.relative(path.join(targetCssUri, "../"), fileURLToPath(sourcePath))
);
}

return {
Expand All @@ -208,10 +174,8 @@ export class SassHelper {
}
}

private static instanceOfSassExcpetion(
object: unknown
): object is LegacyException {
return "formatted" in (object as LegacyException);
private static instanceOfSassExcpetion(object: unknown): object is compiler.LegacyException {
return "formatted" in (object as compiler.LegacyException);
}

private static format(
Expand All @@ -226,59 +190,41 @@ export class SassHelper {
stringArray.push(stack);
}
} else {
stringArray.push(
this.charOfLength(span.start.line.toString().length, "╷")
);
stringArray.push(this.charOfLength(span.start.line.toString().length, "╷"));

let lineNumber = span.start.line;

do {
stringArray.push(
`${lineNumber} |${
span.context?.split("\n")[
lineNumber - span.start.line
] ?? span.text.split("\n")[lineNumber - span.start.line]
span.context?.split("\n")[lineNumber - span.start.line] ??
span.text.split("\n")[lineNumber - span.start.line]
}`
);

lineNumber++;
} while (lineNumber < span.end.line);

stringArray.push(
this.charOfLength(
span.start.line.toString().length,
this.addUnderLine(span)
)
this.charOfLength(span.start.line.toString().length, this.addUnderLine(span))
);

stringArray.push(
this.charOfLength(span.start.line.toString().length, "╵")
);
stringArray.push(this.charOfLength(span.start.line.toString().length, "╵"));

if (span.url) {
// possibly include `,${span.end.line}:${span.end.column}`, if VS Code ever supports it
stringArray.push(
`${span.url.toString()}:${span.start.line}:${
span.start.column
}`
);
stringArray.push(`${span.url.toString()}:${span.start.line}:${span.start.column}`);
}
}

if (deprecated === true) {
stringArray.push(
"THIS IS DEPRECATED AND WILL BE REMOVED IN SASS 2.0"
);
stringArray.push("THIS IS DEPRECATED AND WILL BE REMOVED IN SASS 2.0");
}

return stringArray;
}

private static charOfLength(
charCount: number,
suffix?: string,
char = " "
): string {
private static charOfLength(charCount: number, suffix?: string, char = " "): string {
if (charCount < 0) {
return suffix ?? "";
}
Expand All @@ -300,11 +246,7 @@ export class SassHelper {
} else {
outString +=
this.charOfLength(span.start.column - 2, "^") +
this.charOfLength(
span.end.column - span.start.column - 1,
"^",
"."
);
this.charOfLength(span.end.column - span.start.column - 1, "^", ".");
}

return outString;
Expand Down

0 comments on commit abf29d4

Please sign in to comment.