Skip to content

Commit

Permalink
Merge branch 'master' into add-formats.generateMap
Browse files Browse the repository at this point in the history
  • Loading branch information
glenn2223 authored Aug 8, 2023
2 parents 49bb79a + 30c98d2 commit 6a0c423
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 110 deletions.
6 changes: 4 additions & 2 deletions docs/faqs.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ Here's some of the most important changes:
- We now require VS Code version 1.74
- We are no longer dependant on `ritwickdey.LiveServer`. You can manually add this package to VS Code, if you need it
- Some settings have been changed
- `formats[].format` only accepts `compressed` or `expanded`
- `formats[]`:
- `format` only accepts `compressed` or `expanded`
- `extensionName` allows any string that ends in `.css` - and contains no path separators - meaning that `-min.css` is now valid
- `autoprefix`:
- The default is `defaults`
- `null` is no longer accepted, use `false` instead
Expand All @@ -36,7 +38,7 @@ Here's some of the most important changes:
- `compileOnWatch`: state if files should be compiled upon watching
- `forceBaseDirectory`: state the base directory of all you SASS files. Aids in reducing wasted resources while searching for files
- `partialsList`: specify what files are actually partials (or which folders contain them)
- `useNewCompiler`: use the latest version of the JS SASS compiler (which has been built to address some issues and has a modern, idiomatic API)
- `useNewCompiler`: use the latest version of the JS SASS compiler. It's quicker, and addresses some issues; but mainly, IT'S QUICKER!

Here are some things you probably won't care about as much

Expand Down
30 changes: 15 additions & 15 deletions docs/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@
An array of formats. Allows you save to multiple locations, with a customisable format and extension for each

| Properties | Type | Default | Notes |
| ------------------------- | -------------------------------- | ---------- | ---------------------------------------------------- |
| format | `expanded` OR `compressed` | `expanded` | The output style of the generated file |
| extensionName | `.css` OR `.min.css` | `.css` | The extension appended to the outputted file |
| savePath | `string?` | `null` | See [save path notes] |
| savePathReplacementPairs | `Record<string, string>?` | `null` | See [save path notes] |
| generateMap | `boolean?` | `null` | Choose to output maps at a format level instead |
| <sup>Ŧ</sup>_linefeed_ | `cr` OR `crlf` OR `lf` OR `lfcr` | `lf` | The linefeed terminator to use |
| <sup>Ŧ</sup>_indentType_ | `space` OR `tab` | `space` | The indentation to use for the `expanded` format |
| <sup>Ŧ</sup>_indentWidth_ | `number` | `2` | The indentation width used for the `expanded` format |
| Properties | Type | Default | Notes |
| ------------------------- | -------------------------------- | ---------- | -------------------------------------------------------------------- |
| format | `expanded` OR `compressed` | `expanded` | The output style of the generated file |
| extensionName | `string` | `.css` | The extension suffix added to the output file (must end with `.css`) |
| savePath | `string?` | `null` | See [save path notes] |
| savePathReplacementPairs | `Record<string, string>?` | `null` | See [save path notes] |
| generateMap | `boolean?` | `null` | Choose to output maps at a format level instead |
| <sup>Ŧ</sup>_linefeed_ | `cr` OR `crlf` OR `lf` OR `lfcr` | `lf` | The linefeed terminator to use |
| <sup>Ŧ</sup>_indentType_ | `space` OR `tab` | `space` | The indentation to use for the `expanded` format |
| <sup>Ŧ</sup>_indentWidth_ | `number` | `2` | The indentation width used for the `expanded` format |

<small><sup>Ŧ</sup> These will be removed in SASS v2.0 and are currently unavailable when `liveSassCompile.settings.useNewCompiler` is `true`</small>

Expand Down Expand Up @@ -56,7 +56,7 @@ An array of formats. Allows you save to multiple locations, with a customisable
// (See issue 26: https://github.com/ritwickdey/vscode-live-sass-compiler/issues/26)
{
"format": "compressed",
"extensionName": ".min.css",
"extensionName": ".m.css",

// ~ -> denotes relative to each sass file
"savePath": "~/../css/"
Expand All @@ -66,8 +66,8 @@ An array of formats. Allows you save to multiple locations, with a customisable
"format": "compressed",
"extensionName": ".min.css",

// "/Assets/SCSS/main.scss" => "/Assets/Style/main.css"
// "/Assets/_SASS/main.sass" => "/Assets/Style/main.css"
// "/Assets/SCSS/main.scss" => "/Assets/Style/main.min.css"
// "/Assets/_SASS/main.sass" => "/Assets/Style/main.min.css"
"savePathReplacementPairs": {
"/SCSS/": "/Style/",
"/_SASS/": "/Style/"
Expand All @@ -78,9 +78,9 @@ An array of formats. Allows you save to multiple locations, with a customisable
// Segment replacement can work with relative `savePath`s
{
"format": "compressed",
"extensionName": ".min.css",
"extensionName": "-min.css",

// "/src/sass/Homepage/AHH/main.scss" => "/dist/css/Homepage/main.css"
// "/src/sass/Homepage/AHH/main.scss" => "/dist/css/Homepage/main-min.css"
"savePath": "~/..",
"savePathReplacementPairs": {
"/src/sass": "/dist/css/"
Expand Down
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.

8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,8 @@
"extensionName": {
"description": "Extension Name of exported css",
"type": "string",
"enum": [
".css",
".min.css"
],
"pattern": "^[^\\/\\\\]*\\.css$",
"patternErrorMessage": "Must end with `.css` and not contain `/` or `\\`",
"default": ".css"
},
"savePath": {
Expand Down Expand Up @@ -359,7 +357,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 6a0c423

Please sign in to comment.