Skip to content

Commit dc59fde

Browse files
committed
feat(SassPlugin): Shared resource files in the config
1 parent 769055a commit dc59fde

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

docs/plugins/sass.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,54 @@ fuse.plugin(
9191
);
9292
```
9393

94+
## Resource file
95+
96+
To avoid setting resource file (which could contain variable and other shared
97+
resources) on each file, it's possible to define it in the config.
98+
99+
```js
100+
plugins: [
101+
[
102+
SassPlugin({
103+
resources: [{ test: /.*/, file: "resources.scss" }],
104+
}),
105+
CSSPlugin(),
106+
],
107+
];
108+
```
109+
110+
In the example above, all the files in the project, including node_modules will
111+
share the same resource file which in our case located next to `fuse.js`. It
112+
would also understand an absolute path.
113+
114+
For example, having an SCSS file with the contents:
115+
116+
`project/src/index.scss`
117+
118+
```scss
119+
body {
120+
background-color: $color1;
121+
}
122+
```
123+
124+
and a resource file in `project/resources.scss`
125+
126+
```scss
127+
$color1: orange;
128+
```
129+
130+
Will result in the following output:
131+
132+
```scss
133+
@import "../resources.scss";
134+
body {
135+
background-color: $color1;
136+
}
137+
```
138+
139+
As you can see here, the resource file is resolved automatically relatively to
140+
the file's original location.
141+
94142
## Macros
95143

96144
Macros is a unique feature available only in `FuseBox` to give you more

src/plugins/stylesheet/SassPlugin.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ import { File } from "../../core/File";
22
import { WorkFlowContext, Plugin } from "../../core/WorkflowContext";
33
import * as path from "path";
44
import { Config } from "../../Config";
5+
import { ensureAbsolutePath } from "../../Utils";
56

67
export interface SassPluginOptions {
78
includePaths?: string[];
89
macros?: { [key: string]: string };
910
importer?: boolean | ImporterFunc;
1011
cache?: boolean;
1112
header?: string;
13+
resources?: [{ test: RegExp; file: string }];
1214
indentedSyntax?: boolean;
1315
functions?: { [key: string]: (...args: any[]) => any };
1416
}
@@ -63,6 +65,19 @@ export class SassPluginClass implements Plugin {
6365
file.contents = this.options.header + "\n" + file.contents;
6466
}
6567

68+
if (this.options.resources) {
69+
let resourceFile;
70+
this.options.resources.forEach(item => {
71+
if (item.test.test(file.info.absPath)) {
72+
resourceFile = item.file;
73+
}
74+
});
75+
if (resourceFile) {
76+
const relativePath = path.relative(file.info.absDir, ensureAbsolutePath(resourceFile));
77+
file.contents = `@import "${relativePath}";` + "\n" + file.contents;
78+
}
79+
}
80+
6681
const options = Object.assign(
6782
{
6883
data: file.contents,

0 commit comments

Comments
 (0)