Skip to content

Commit

Permalink
#98 - Updated plugins 'custom' sass file importer to enforce load order.
Browse files Browse the repository at this point in the history
- Updated 'SassImporterResult' type to match Sass's 'LegacyImporterResult' - We had defined our type because the version of sass the library using, and sass versions users might be using, did not/may not contain this type (so basically our type was that type but slightly imprecise (we had both 'file?:...', and 'content?:...'  as part of the same interface, but actually they need to be separate ones (:-))).
  • Loading branch information
elycruz committed Jul 21, 2022
1 parent 3af202b commit bd90509
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 26 deletions.
13 changes: 7 additions & 6 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions dist/types.d.ts
Expand Up @@ -16,10 +16,11 @@ export interface RollupPluginSassOptions {
output?: boolean | string | RollupPluginSassOutputFn;
runtime?: any;
}
export declare type SassImporterResult = null | {
file?: string;
contents?: string;
} | Error;
export declare type SassImporterResult = {
file: string;
} | {
contents: string;
} | Error | null;
export declare type SassDoneFn<T extends SassImporterResult = SassImporterResult> = (result: T) => void | T;
export declare type SassImporter<T extends SassImporterResult = SassImporterResult> = (url: string, prev: string, done: SassDoneFn<T>) => void | T;
export interface SassFunctionsObject {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "rollup-plugin-sass",
"version": "1.2.13",
"version": "1.2.13-alpha.1",
"description": "Rollup Sass files.",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
28 changes: 19 additions & 9 deletions src/index.ts
Expand Up @@ -6,6 +6,7 @@ import * as fs from 'fs';
import {createFilter} from '@rollup/pluginutils';
import {insertStyle} from './style';
import {
SassImporterResult,
RollupAssetInfo,
RollupChunkInfo,
RollupPluginSassOptions,
Expand All @@ -29,33 +30,42 @@ const MATCH_SASS_FILENAME_RE = /\.sass$/,
* @see https://sass-lang.com/documentation/js-api#importer
*/
getImporterList = (sassOptions: SassOptions) => {
const importer1 = (url, importer, done) => {
// `Promise` to chain all `importer1` calls to; E.g., subsequent `importer1` calls won't call `done` until previous `importer1` calls have called `done` (import order enforcement) - Required since importer below is actually 'async'.
let lastResult = Promise.resolve();

/**
* Legacy Sass (*.scss/*.sass) file importer (works in new, and older, versions of `sass` module).
* @see https://sass-lang.com/documentation/js-api/modules#LegacyAsyncImporter
* @param {string} url - Url found in `@import`/`@use`, found in parent sass file, exactly as it appears in sass file.
* @param {string} prevUrl - Url of file that contains '@import' rule for `url`.
* @param {(result: LegacyImporterResult | SassImporterResult) => void} done - Signals import completion. Note: `LegacyImporterResult`, and `SassImporterResult`, are the same here - We've defined the type for out plugin, since older versions of sass don't have the type defined amongst their types.
* @returns {void}
*/
const importer1 = (url: string, prevUrl: string, done: (rslt: SassImporterResult) => void): void => {
if (!MATCH_NODE_MODULE_RE.test(url)) {
return null;
}

const moduleUrl = url.slice(1);
const resolveOptions = {
basedir: dirname(importer),
basedir: dirname(prevUrl),
extensions: ['.scss', '.sass'],
};

let file: string;

try {
file = resolve.sync(moduleUrl, resolveOptions);
done({file});
const file = resolve.sync(moduleUrl, resolveOptions);
lastResult = lastResult.then(() => done({file}));
} catch (err) {
warn('[rollup-plugin-sass]: Recovered from error: ', err);
// If importer has sibling importers then exit and allow one of the other
// importers to attempt file path resolution.
if (sassOptions.importer && sassOptions.importer.length > 1) {
done(null);
lastResult = lastResult.then(() => done(null));
return;
}
done({
lastResult = lastResult.then(() => done({
file: url,
});
}));
}
}
return [importer1].concat(sassOptions.importer || [])
Expand Down
5 changes: 1 addition & 4 deletions src/types.ts
Expand Up @@ -53,10 +53,7 @@ export interface RollupPluginSassOptions {
runtime?: any,
}

export type SassImporterResult = null | {
file?: string,
contents?: string
} | Error;
export type SassImporterResult = { file: string } | { contents: string } | Error | null;

export type SassDoneFn<T extends SassImporterResult = SassImporterResult> =
(result: T) => void | T;
Expand Down

0 comments on commit bd90509

Please sign in to comment.