rollup-plugin-sass icon indicating copy to clipboard operation
rollup-plugin-sass copied to clipboard

add `api` option to allow switching to `sass` modern compiler

Open marcalexiei opened this issue 1 year ago • 1 comments

New api option

  • Close #159
  • Close #154
  • Related to #152

Add new optional api option to allow opt-in for sass modern compiler API. Allowed values are:

  • legacy (defaultOne) to preserve backward compatibility (consider to change this in a new major)
  • modern uses compileStringAsync to compile sass code to css. The compile result transformations are the same of legacy option value.

Due to the new API structure the two syntax are handled inside a switch with two different blocks.

Code examples
// should default to legacy when `api` is not specified
sass({ options: { data: '', outputStyle: 'compact' } });

// should error when LegacyOptions are used with `api: 'modern'`
// @ts-expect-error
sass({ api: 'modern', options: { data: '', outputStyle: 'compact' } });

// should only accept api parameter
sass({ api: 'modern' });

// should correctly infer modern API options
sass({ api: 'modern', options: { style: 'compressed' } });

Refactors

In order to support the new syntax and retain backward compatibility different refactors have been made:

  • processRenderResponse has been moved in a separate file inside utils
  • getImporterList has been moved in a separate file inside utils and renamed to getImporterListLegacy, the same file houses the modern function variant: getImporterListModern
  • src/types
    • removed unused rollup types
    • removed sass types aliases, now sass related types are imported from sass
    • RollupPluginSassOptions now is a type due the new api option
      • relevant unit tests are added
    • PluginState has been renamed to RollupPluginSassState and moved here
  • src/utils/index content has been divided into 2 files with self-explanatory names:
    • src/utils/logger
    • src/utils/helpers
  • Plugin transform function now is an async function

Pending PR tasks

  • [x] find a way to dedupe test code (use macro)
  • ~~silence deprecation warning in legacy tests~~ used sass 1.78 which doesn't have legacy-js-api warning
  • [x] update README documentation
  • ~~check logger function usages and consider to a prefix so user can clearly understand that they are coming from the plugin (e.g.: [rollup-plugin-sass]:)~~ can be done in a separate PR

Follow up

  • Check #140
    • drop old versions of node
    • convert package to ESM
    • add support to other sass runtimes using dynamic resolve (see web pack)
    • ...
  • Consider using a modern package to resolve files (e.g.: import-meta-resolve)

[!WARNING] Pending PRs:

  • #165
  • #163

marcalexiei avatar Oct 14 '24 12:10 marcalexiei