Skip to content

Commit cb72810

Browse files
committed
fix: Set eyeglass root within a directory-scoped processor.
In a directory scoped processor, we want to treat the root directory of the processor as the eyeglass root. We override any root setting that the application's configuration has provided, but we allow the root to be overridden in the processor's setupOptions() method. Usually the application's root is good enough for compilation, but there are situation where the directory structures don't align and it's important for the scoped processor to have it's root take precendence. Namely: yarn link'ed development requires this.
1 parent eaf06b6 commit cb72810

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

packages/@css-blocks/eyeglass/src/index.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type { OptionalPreprocessor, OptionalPreprocessorSync, Preprocessor, PreprocessorSync, ProcessedFile, ResolvedConfiguration } from "@css-blocks/core";
22
import type { EyeglassOptions, default as Eyeglass } from "eyeglass"; // works, even tho a cjs export. huh.
3+
import cloneDeep = require("lodash.clonedeep");
34
import type { Result, SassError } from "node-sass";
45
import type SassImplementation from "node-sass";
56
import { sep as PATH_SEPARATOR } from "path";
6-
import cloneDeep = require("lodash.clonedeep");
77

88
export type Adaptor = (sass: typeof SassImplementation, eyeglass: typeof Eyeglass, options: EyeglassOptions) => Preprocessor;
99
export type AdaptorSync = (sass: typeof SassImplementation, eyeglass: typeof Eyeglass, options: EyeglassOptions) => PreprocessorSync;
@@ -123,6 +123,8 @@ export class DirectoryScopedPreprocessor implements PreprocessorProvider {
123123
* eyeglass.VERSION.
124124
*/
125125
init(sass: typeof SassImplementation, eyeglass: typeof Eyeglass, options: EyeglassOptions = {}) {
126+
options = cloneDeep(options);
127+
setEyeglassRoot(options, this.filePrefix);
126128
let sassOptions = this.setupOptions(options);
127129

128130
let sassOptionsSync = cloneDeep(sassOptions);
@@ -201,6 +203,14 @@ export class DirectoryScopedPreprocessor implements PreprocessorProvider {
201203
}
202204
}
203205

206+
function setEyeglassRoot(options: EyeglassOptions, root: string): EyeglassOptions {
207+
if (!options.eyeglass) {
208+
options.eyeglass = {};
209+
}
210+
options.eyeglass.root = root;
211+
return options;
212+
}
213+
204214
/**
205215
* Creates a unified preprocessor for an application to use when consuming
206216
* css blocks that have Sass preprocessed.

packages/@css-blocks/eyeglass/test/package-test.ts

+50
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ describe("@css-blocks/eyeglass", async () => {
141141
142142
/*# sourceMappingURL=two.block.css.map */`));
143143
});
144+
144145
it("can adapt from several optional adaptors - synchronous", () => {
145146
let package1Dir = fixture("package-1");
146147
let package1File = fixture("package-1/one.block.scss");
@@ -172,6 +173,55 @@ describe("@css-blocks/eyeglass", async () => {
172173
173174
/*# sourceMappingURL=two.block.css.map */`));
174175
});
176+
177+
it("sets the eyeglass root", async () => {
178+
let package1Dir = fixture("package-1");
179+
let package1File = fixture("package-1/one.block.scss");
180+
let optionsPassedToSetup: EyeglassOptions | undefined;
181+
class Adaptor1 extends DirectoryScopedPreprocessor {
182+
setupOptions(options: EyeglassOptions): EyeglassOptions {
183+
optionsPassedToSetup = options;
184+
return Object.assign({}, options, {outputStyle: "compact"});
185+
}
186+
}
187+
let adaptor1 = new Adaptor1(package1Dir);
188+
let originalOptions = {
189+
eyeglass: {
190+
root: process.cwd(),
191+
},
192+
};
193+
let processor = adaptAll([adaptor1], SassImplementation, Eyeglass, originalOptions);
194+
await processor(package1File, fs.readFileSync(package1File, "utf-8"), resolveConfiguration({}));
195+
assert.equal(optionsPassedToSetup?.eyeglass?.root, package1Dir + "/");
196+
assert.equal(originalOptions.eyeglass.root, process.cwd());
197+
});
198+
199+
it("can mutate options without changing the original value", async () => {
200+
let package1Dir = fixture("package-1");
201+
let package1File = fixture("package-1/one.block.scss");
202+
let optionsPassedToSetup: EyeglassOptions | undefined;
203+
class Adaptor1 extends DirectoryScopedPreprocessor {
204+
setupOptions(options: EyeglassOptions): EyeglassOptions {
205+
optionsPassedToSetup = options;
206+
options.outputStyle = "compact";
207+
return options;
208+
}
209+
setupOptionsSync(options: EyeglassOptions): EyeglassOptions {
210+
options.outputStyle = "expanded";
211+
return options;
212+
}
213+
}
214+
let adaptor1 = new Adaptor1(package1Dir);
215+
let originalOptions: EyeglassOptions = {
216+
eyeglass: {
217+
root: process.cwd(),
218+
},
219+
};
220+
let processor = adaptAll([adaptor1], SassImplementation, Eyeglass, originalOptions);
221+
await processor(package1File, fs.readFileSync(package1File, "utf-8"), resolveConfiguration({}));
222+
assert.isUndefined(originalOptions.outputStyle);
223+
assert.equal(optionsPassedToSetup?.outputStyle, "compact");
224+
});
175225
});
176226

177227
function dedent(s: string): string {

0 commit comments

Comments
 (0)