Skip to content

Commit b1cc372

Browse files
committed
revert(): Back to 1.4
1 parent e542d90 commit b1cc372

File tree

6 files changed

+66
-13
lines changed

6 files changed

+66
-13
lines changed

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@stencil/sass",
3-
"version": "1.4.1",
3+
"version": "1.4.0",
44
"license": "MIT",
55
"main": "dist/index.js",
66
"module": "dist/index.mjs",

readme.md

+25-3
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,37 @@ Sass options can be passed to the plugin within the stencil config, which are us
3333

3434
### Inject Globals Sass Paths
3535

36-
The `injectGlobalPaths` config is an array of paths that automatically get added as `@import` declarations to all components. This can be useful to inject Sass variables, mixins and functions to override defaults of external collections. For example, apps can override default Sass variables of [Ionic components](https://www.npmjs.com/package/@ionic/core). Relative paths within `injectGlobalPaths` should be relative to the stencil config file.
36+
The `injectGlobalPaths` config is an array of paths that automatically get added as imports to all components. This can be useful to inject Sass variables, mixins and functions to override defaults of external collections. For example, apps can override default Sass variables of [Ionic components](https://www.npmjs.com/package/@ionic/core). Relative paths within `injectGlobalPaths` should be relative to the stencil config file.
37+
38+
#### v1
39+
40+
v1.x of stencil/sass uses sass `@import` syntax to add files listed in the `injectGlobalPaths` option to each stylesheet. Do not use `@use` in your components if using v1 because it is not permitted by sass to have `@import` statements before `@use` statements. Below is an example of using `injectGlobalPaths` in v1.
41+
42+
```js
43+
exports.config = {
44+
plugins: [
45+
sass({
46+
injectGlobalPaths: [
47+
'src/global/variables.scss', //adds @import 'src/global/variables.scss' statement
48+
'src/global/mixins.scss' //adds @import 'src/global/mixins.scss' statement
49+
]
50+
})
51+
]
52+
};
53+
```
54+
55+
#### v2
56+
57+
v2.x of stencil/sass uses sass `@use` syntax to add files listed in `injectGlobalPaths` to each stylesheet. Because the `@use` syntax also supports namespacing by default, the option is now available to customize the namespace. `injectGlobalPaths` can now be an array of TS tuples. The first position is the filepath and the second position is the namespace. There is still the option to only use a string, which will default the namespace to the name of the file. These methods can be combined.
3758

3859
```js
3960
exports.config = {
4061
plugins: [
4162
sass({
4263
injectGlobalPaths: [
43-
'src/globals/variables.scss',
44-
'src/globals/mixins.scss'
64+
['src/global/variables.scss', 'var'], //adds "@use 'src/global/variables.scss' as var" statement
65+
['src/global/mixins.scss', '*'], //root namespace, no prefix needed to access
66+
'src/global/animations.scss' //namespace defaults to 'animations'
4567
]
4668
})
4769
]

src/declarations.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export interface PluginOptions {
3636
* Used for Sass variables, mixins and functions files that do not contain any CSS.
3737
* This config is custom to `@stencil/sass`.
3838
*/
39-
injectGlobalPaths?: string[];
39+
injectGlobalPaths?: ([string, string] | string)[];
4040

4141
/**
4242
* Enable Sass Indented Syntax for parsing the data string or file.

src/util.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,25 @@ export function getRenderOptions(opts: d.PluginOptions, sourceText: string, file
3737

3838
if (injectGlobalPaths.length > 0) {
3939
// automatically inject each of these paths into the source text
40-
const injectText = injectGlobalPaths.map(injectGlobalPath => {
41-
if (!path.isAbsolute(injectGlobalPath)) {
40+
const injectText = injectGlobalPaths.map((injectGlobalPath) => {
41+
const includesNamespace = Array.isArray(injectGlobalPath);
42+
let importPath = includesNamespace ? injectGlobalPath[0] as string : injectGlobalPath as string;
43+
44+
if (!path.isAbsolute(importPath)) {
4245
// convert any relative paths to absolute paths relative to the project root
4346

4447
if (context.sys && typeof context.sys.normalizePath === 'function') {
4548
// context.sys.normalizePath added in stencil 1.11.0
46-
injectGlobalPath = context.sys.normalizePath(path.join(context.config.rootDir, injectGlobalPath));
49+
importPath = context.sys.normalizePath(path.join(context.config.rootDir, importPath));
4750
} else {
4851
// TODO, eventually remove normalizePath() from @stencil/sass
49-
injectGlobalPath = normalizePath(path.join(context.config.rootDir, injectGlobalPath));
52+
importPath = normalizePath(path.join(context.config.rootDir, importPath));
5053
}
5154
}
5255

5356
const importTerminator = renderOpts.indentedSyntax ? '\n' : ';';
5457

55-
return `@import "${injectGlobalPath}"${importTerminator}`;
58+
return `@use "${importPath}"${includesNamespace ? ` as ${injectGlobalPath[1]}` : ''}${importTerminator}`;
5659
}).join('');
5760

5861
renderOpts.data = injectText + renderOpts.data;

test/utils.spec.ts

+30-2
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,45 @@ describe('getRenderOptions', () => {
2525
expect(output.file).toBeUndefined();
2626
});
2727

28-
it('should inject global sass array and not change input options or include globals in output opts', () => {
28+
it('should inject global sass array, not change input options or include globals in output opts', () => {
2929
const input: d.PluginOptions = {
3030
injectGlobalPaths: ['/my/global/variables.scss']
3131
};
3232
const output = util.getRenderOptions(input, sourceText, fileName, context);
33-
expect(output.data).toBe(`@import "/my/global/variables.scss";body { color: blue; }`);
33+
expect(output.data).toBe(`@use "/my/global/variables.scss";body { color: blue; }`);
3434
expect(output.injectGlobalPaths).toBeUndefined();
3535
expect(input.injectGlobalPaths).toHaveLength(1);
3636
expect(input.injectGlobalPaths[0]).toBe('/my/global/variables.scss');
3737
});
3838

39+
it('should inject global sass array, not change input options or include globals in output opts, and add namespace', () => {
40+
const input: d.PluginOptions = {
41+
injectGlobalPaths: [['/my/global/variables.scss', 'var']]
42+
};
43+
const output = util.getRenderOptions(input, sourceText, fileName, context);
44+
expect(output.data).toBe(`@use "/my/global/variables.scss" as var;body { color: blue; }`);
45+
expect(output.injectGlobalPaths).toBeUndefined();
46+
expect(input.injectGlobalPaths).toHaveLength(1);
47+
expect(input.injectGlobalPaths[0][0]).toBe('/my/global/variables.scss');
48+
expect(input.injectGlobalPaths[0][1]).toBe('var');
49+
});
50+
51+
it('should inject global sass array, not change input options or include globals in output opts, and discern when to add namespace', () => {
52+
const input: d.PluginOptions = {
53+
injectGlobalPaths: [
54+
['/my/global/variables.scss', 'var'],
55+
'/my/global/mixins.scss'
56+
]
57+
};
58+
const output = util.getRenderOptions(input, sourceText, fileName, context);
59+
expect(output.data).toBe(`@use "/my/global/variables.scss" as var;@use "/my/global/mixins.scss";body { color: blue; }`);
60+
expect(output.injectGlobalPaths).toBeUndefined();
61+
expect(input.injectGlobalPaths).toHaveLength(2);
62+
expect(input.injectGlobalPaths[0][0]).toBe('/my/global/variables.scss');
63+
expect(input.injectGlobalPaths[0][1]).toBe('var');
64+
expect(input.injectGlobalPaths[1]).toBe('/my/global/mixins.scss');
65+
});
66+
3967
it('should add dirname of filename to existing includePaths array and not change input options', () => {
4068
const input: d.PluginOptions = {
4169
includePaths: ['/some/other/include/path']

0 commit comments

Comments
 (0)