sindresorhus / grunt-sass

Compile Sass to CSS
MIT License
1.01k stars 209 forks source link

Deprecation warning with newest Dart Sass (1.79.2) #311

Open stefangabos opened 1 month ago

stefangabos commented 1 month ago

With the newest Dart Sass, I am getting

Deprecation Warning: The legacy JS API is deprecated and will be removed in Dart Sass 2.0.0.

According to their docs we can pass this option to silence the warning: silenceDeprecations: ['legacy-js-api'] (and it does work)

So, having something like

minified: {
    options: {
        silenceDeprecations: ['legacy-js-api'],
        implementation: sass,
        outputStyle: 'compressed'
    },
    files: {
        'path/to/compiled': 'path/to/source'
    }
}

does the job of silencing the warning.

They mention that setting an api option to modern should actually be used instead but setting it has no effect.

Help?

mattyrob commented 2 weeks ago

I believe a fix / enhancement to the current code that will make use of the newer API can be achieved as follows.

In tasks/sass.js after line 22, add a new line for scope `result:

const [src] = item.src;
let result;

Then replace the code from line 27:

const result = await util.promisify(options.implementation.render)(Object.assign({}, options, {
    file: src,
    outFile: item.dest
}));

with the following:

if ('modern' === options.api) {
    result = await options.implementation.compileAsync(src , options);
} else {
    result = await util.promisify(options.implementation.render)(Object.assign({}, options, {
        file: src,
        outFile: item.dest
    }));
}

This will introduce the option to pass api and if it is defined as modern then the newer sass functions are called to create the css output.

stefangabos commented 2 weeks ago

I confirm that @mattyrob's changes work as expected.

For anyone else wanting to try it out, replace in your package.json file the call to

"grunt-sass": "^3.1.0",

with

"grunt-sass": "github:mattyrob/grunt-sass#add/modern-api",

and do a npm install.

Alternatively, making a call to

npm i https://github.com/mattyrob/grunt-sass.git#add/modern-api

in your project's folder will do the change for you in package.json