Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.

Commit 1a6d264

Browse files
Merge branch 'develop'
2 parents 08fc6cd + 158ee9b commit 1a6d264

File tree

10 files changed

+3647
-4808
lines changed

10 files changed

+3647
-4808
lines changed

.eslintignore

Lines changed: 0 additions & 2 deletions
This file was deleted.

.eslintrc.json

Lines changed: 0 additions & 19 deletions
This file was deleted.

README.md

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,39 +19,35 @@ npm install --save-dev @lmcd/gulp-dartsass sass
1919
For _sync compilation_:
2020

2121
```js
22-
const { src, dest } = require('gulp');
23-
const { sync } = require('@lmcd/gulp-dartsass');
24-
const sass = require('sass');
22+
import { src, dest } from 'gulp';
23+
import { sync } from '@lmcd/gulp-dartsass';
24+
import sass from 'sass';
2525

26-
function compile() {
26+
export function compile() {
2727
return src('./sass/**/*.scss')
2828
.pipe(sync(sass))
2929
.pipe(dest('./css'));
3030
};
31-
32-
exports.styles = compile;
3331
```
3432

3533
Or for _async compilation_:
3634

3735
```js
38-
const { src, dest } = require('gulp');
39-
const { async } = require('@lmcd/gulp-dartsass');
40-
const sass = require('sass');
36+
import { src, dest } from 'gulp';
37+
import { async } from '@lmcd/gulp-dartsass';
38+
import sass from 'sass';
4139

42-
function compile() {
40+
export function compile() {
4341
return src('./sass/**/*.scss')
4442
.pipe(async(sass))
4543
.pipe(dest('./css'));
4644
};
47-
48-
exports.styles = compile;
4945
```
5046

5147
### API
5248

5349
```js
54-
const { sync, async } = require('@lmcd/gulp-dartsass');
50+
import { sync, async } from '@lmcd/gulp-dartsass';
5551
```
5652

5753
**gulp-dartsass** exports both `sync` and `async` factory methods with the following signature:
@@ -70,13 +66,15 @@ Where:
7066
Gulp's `src` and `dest` built-in support for sourcemaps is the preferred way to use include sourcemaps in your output. However, **gulp-dartsass** will also function with [gulp-sourcemaps].
7167

7268
```js
73-
const { src, dest } = require('gulp');
74-
const { sync } = require('@lmcd/gulp-dartsass');
75-
const sass = require('sass');
69+
import { src, dest } from 'gulp';
70+
import { sync } from '@lmcd/gulp-dartsass';
71+
import sass from 'sass';
7672

77-
.src('./sass/**/*.scss', { sourcemaps: true })
78-
.pipe(sync())
79-
.pipe(dest('./css', { sourcemaps: true }));
73+
export function compile() {
74+
return src('./sass/**/*.scss', { sourcemaps: true })
75+
.pipe(sync())
76+
.pipe(dest('./css', { sourcemaps: true }));
77+
};
8078
```
8179

8280
### Tests

eslint.config.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import globals from 'globals';
2+
import pluginJs from '@eslint/js';
3+
import stylistic from '@stylistic/eslint-plugin';
4+
import { rules } from '@lmcd/eslint-config';
5+
import jest from 'eslint-plugin-jest';
6+
7+
export default [
8+
pluginJs.configs.recommended,
9+
{
10+
rules,
11+
files: ['**/*.js'],
12+
plugins: {
13+
'@stylistic': stylistic,
14+
},
15+
languageOptions: {
16+
ecmaVersion: 'latest',
17+
sourceType: 'module',
18+
globals: {
19+
...globals.node,
20+
...globals.browser,
21+
'jest/globals': true,
22+
},
23+
},
24+
}, {
25+
files: ['**/*.test.js'],
26+
...jest.configs['flat/recommended'],
27+
rules: {
28+
...jest.configs['flat/recommended'].rules,
29+
},
30+
},
31+
];

index.js

Lines changed: 70 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const PluginError = require('plugin-error');
2-
const through2 = require('through2');
3-
const replaceExtension = require('replace-ext');
4-
const path = require('path');
5-
const { pathToFileURL } = require('url');
6-
const applySourceMap = require('vinyl-sourcemaps-apply');
1+
import path from 'node:path';
2+
import { pathToFileURL } from 'node:url';
3+
import PluginError from 'plugin-error';
4+
import { Transform } from 'stream';
5+
import replaceExtension from 'replace-ext';
6+
import applySourceMap from 'vinyl-sourcemaps-apply';
77

88
const PLUGIN_NAME = 'gulp-dartsass';
99

@@ -21,11 +21,11 @@ const PLUGIN_NAME = 'gulp-dartsass';
2121
*/
2222

2323
/**
24-
* @param {boolean} async
24+
* @param {boolean} isAsync
2525
* @param {SassCompiler} sass
2626
* @param {Record<string, any>} options
2727
*/
28-
const gulpDartSass = (async, sass, options) => {
28+
const gulpDartSass = (isAsync, sass, options) => {
2929
const handleResult = (file, result, callback) => {
3030
file.contents = Buffer.from(result.css, 'utf-8');
3131
file.path = replaceExtension(file.path, '.css');
@@ -50,82 +50,80 @@ const gulpDartSass = (async, sass, options) => {
5050
return callback(null, file);
5151
};
5252

53-
const transformer = through2.obj((file, _encoding, callback) => {
54-
// Ignore null files
55-
if (file.isNull()) {
56-
return callback(null, file);
57-
}
53+
return new Transform({
54+
objectMode: true,
55+
transform(file, _encoding, callback) {
56+
// Ignore null files
57+
if (file.isNull()) {
58+
return callback(null, file);
59+
}
5860

59-
// Streams are not supported
60-
if (file.isStream()) {
61-
return callback(new PluginError(PLUGIN_NAME, 'Streams not supported!'));
62-
}
61+
// Streams are not supported
62+
if (file.isStream()) {
63+
return callback(new PluginError(PLUGIN_NAME, 'Streams not supported!'));
64+
}
6365

64-
// Ignore files beginning with an underscore
65-
if (path.basename(file.path).startsWith('_')) {
66-
return callback();
67-
}
66+
// Ignore files beginning with an underscore
67+
if (path.basename(file.path).startsWith('_')) {
68+
return callback();
69+
}
6870

69-
// Ignore empty files
70-
if (file.contents.length === 0) {
71-
file.path = replaceExtension(file.path, '.css');
72-
return callback(null, file);
73-
}
71+
// Ignore empty files
72+
if (file.contents.length === 0) {
73+
file.path = replaceExtension(file.path, '.css');
74+
return callback(null, file);
75+
}
7476

75-
const sassOptions = {
76-
syntax: 'scss',
77-
url: pathToFileURL(file.path).toString(),
78-
};
77+
const defaultOptions = {
78+
syntax: 'scss',
79+
url: pathToFileURL(file.path).toString(),
80+
};
7981

80-
// Generate Source Maps
81-
sassOptions.sourceMap = Boolean(file.sourceMap);
82+
// Generate Source Maps
83+
defaultOptions.sourceMap = Boolean(file.sourceMap);
8284

83-
// Update syntax to reflect the file-extension
84-
if (path.extname(file.path) === '.sass') {
85-
sassOptions.syntax = 'indented';
86-
}
85+
// Update syntax to reflect the file-extension
86+
if (path.extname(file.path) === '.sass') {
87+
defaultOptions.syntax = 'indented';
88+
}
8789

88-
if (async) {
89-
return sass.compileStringAsync(file.contents.toString('utf-8'), {
90-
...sassOptions,
91-
...options || {},
92-
}).then(result => {
93-
return handleResult(file, result, callback);
94-
}).catch(error => {
95-
return callback(new PluginError(PLUGIN_NAME, error));
96-
});
97-
} else {
98-
try {
99-
const result = sass.compileString(file.contents.toString('utf-8'), {
100-
...sassOptions,
90+
if (isAsync) {
91+
return sass.compileStringAsync(file.contents.toString('utf-8'), {
92+
...defaultOptions,
10193
...options || {},
94+
}).then(result => {
95+
return handleResult(file, result, callback);
96+
}).catch(error => {
97+
return callback(new PluginError(PLUGIN_NAME, error));
10298
});
103-
104-
return handleResult(file, result, callback);
105-
} catch (error) {
106-
return callback(new PluginError(PLUGIN_NAME, error));
99+
} else {
100+
try {
101+
const result = sass.compileString(file.contents.toString('utf-8'), {
102+
...defaultOptions,
103+
...options || {},
104+
});
105+
106+
return handleResult(file, result, callback);
107+
} catch (error) {
108+
return callback(new PluginError(PLUGIN_NAME, error));
109+
}
107110
}
108-
}
111+
},
109112
});
113+
};
110114

111-
return transformer;
115+
/**
116+
* @param {SassCompiler} sass
117+
* @param {Record<string, any>} [options]
118+
*/
119+
export const sync = (sass, options) => {
120+
return gulpDartSass(false, sass, options);
112121
};
113122

114-
module.exports = {
115-
/**
116-
* @param {SassCompiler} sass
117-
* @param {Record<string, any>} [options]
118-
* @returns
119-
*/
120-
sync(sass, options) {
121-
return gulpDartSass(false, sass, options);
122-
},
123-
/**
124-
* @param {SassCompiler} sass
125-
* @param {Record<string, any>} [options]
126-
* @returns
127-
*/
128-
async(sass, options) {
129-
return gulpDartSass(true, sass, options);
130-
},
123+
/**
124+
* @param {SassCompiler} sass
125+
* @param {Record<string, any>} [options]
126+
*/
127+
export const async = (sass, options) => {
128+
return gulpDartSass(true, sass, options);
131129
};

0 commit comments

Comments
 (0)