Skip to content

Commit bd779d8

Browse files
authored
Merge pull request #163 from NathanBeddoeWebDev/main
Merging Nathan's async changes
2 parents 97e8de1 + 91baa8d commit bd779d8

File tree

6 files changed

+645
-572
lines changed

6 files changed

+645
-572
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
},
4343
"dependencies": {
4444
"resolve": "^1.22.6",
45-
"sass": "^1.7.3"
45+
"sass-embedded": "1.70.0"
4646
},
4747
"devDependencies": {
4848
"@types/node": "^18.15.12",

src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import {OnLoadResult} from 'esbuild'
2-
import {StringOptions} from 'sass'
2+
import {StringOptions} from 'sass-embedded'
33
import {sassPlugin} from './plugin'
44

55
export type Type = 'css' | 'style' | 'css-text' | 'lit-css'
66

7-
export type SassPluginOptions = StringOptions<'sync'> & {
7+
export type SassPluginOptions = StringOptions<'async'> & {
88

99
/**
1010
* Careful: this RegExp has to respect Go limitations!!!

src/plugin.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export function sassPlugin(options: SassPluginOptions = {}): Plugin {
3131

3232
return {
3333
name: 'sass-plugin',
34-
setup({initialOptions, onResolve, onLoad, resolve, onStart}) {
34+
async setup({initialOptions, onResolve, onLoad, resolve, onStart}) {
3535

3636
options.loadPaths = Array.from(new Set([
3737
...options.loadPaths || modulesPaths(initialOptions.absWorkingDir),
@@ -72,11 +72,11 @@ export function sassPlugin(options: SassPluginOptions = {}): Plugin {
7272
}))
7373
}
7474

75-
const renderSync = createRenderer(options, options.sourceMap ?? sourcemap)
75+
const renderSync = await createRenderer(options, options.sourceMap ?? sourcemap)
7676

7777
onLoad({filter: options.filter ?? DEFAULT_FILTER}, useCache(options, fsStatCache, async path => {
7878
try {
79-
let {cssText, watchFiles, warnings} = renderSync(path)
79+
let {cssText, watchFiles, warnings} = await renderSync(path)
8080
if (!warnings) {
8181
warnings = []
8282
}

src/render.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@ import {dirname, parse, relative, resolve, sep} from 'path'
22
import fs, {readFileSync} from 'fs'
33
import {createResolver, fileSyntax, sourceMappingURL} from './utils'
44
import {PartialMessage} from 'esbuild'
5-
import * as sass from 'sass'
6-
import {ImporterResult} from 'sass'
5+
import * as sass from 'sass-embedded'
6+
import {ImporterResult, initAsyncCompiler} from 'sass-embedded'
77
import {fileURLToPath, pathToFileURL} from 'url'
88
import {SassPluginOptions} from './index'
99

10-
export type RenderSync = (path: string) => RenderResult
10+
export type RenderSync = (path: string) => Promise<RenderResult>
1111

1212
export type RenderResult = {
1313
cssText: string
1414
watchFiles: string[]
1515
warnings?: PartialMessage[]
1616
}
1717

18-
export function createRenderer(options: SassPluginOptions = {}, sourcemap: boolean): RenderSync {
18+
export async function createRenderer(options: SassPluginOptions = {}, sourcemap: boolean): Promise<RenderSync> {
1919

2020
const loadPaths = options.loadPaths!
2121
const resolveModule = createResolver(options, loadPaths)
@@ -61,10 +61,12 @@ export function createRenderer(options: SassPluginOptions = {}, sourcemap: boole
6161

6262
const sepTilde = `${sep}~`
6363

64+
const compiler = await initAsyncCompiler();
65+
6466
/**
6567
* renderSync
6668
*/
67-
return function (path: string): RenderResult {
69+
return async function (path: string): Promise<RenderResult> {
6870

6971
const basedir = dirname(path)
7072

@@ -112,7 +114,7 @@ export function createRenderer(options: SassPluginOptions = {}, sourcemap: boole
112114
css,
113115
loadedUrls,
114116
sourceMap
115-
} = sass.compileString(source, {
117+
} = await compiler.compileStringAsync(source, {
116118
sourceMapIncludeSources: true,
117119
...options,
118120
logger,

src/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {SassPluginOptions, Type} from './index'
22
import {AcceptedPlugin, Postcss} from 'postcss'
33
import PostcssModulesPlugin from 'postcss-modules'
44
import {BuildOptions, OnLoadResult} from 'esbuild'
5-
import {Syntax} from 'sass'
5+
import {Syntax} from 'sass-embedded'
66
import {parse, relative, resolve} from 'path'
77
import {existsSync} from 'fs'
88
import {SyncOpts} from 'resolve'

0 commit comments

Comments
 (0)