Skip to content

Commit

Permalink
issue-#72 - Fixed bug where generated css was being duplicated in com…
Browse files Browse the repository at this point in the history
…piled css output.

- Added test/example showing how to extract sass variables to generated output *.js module files.
- Simplified 'License' section in main README.
- Updated 'LICENSE' to point to './humans.txt' for maintaners/owners list.
  • Loading branch information
elycruz committed Sep 17, 2022
1 parent dc21cdb commit 17031fb
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 23 deletions.
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2016 rollup-plugin-sass maintainers (see './MAINTAINERS' file)
Copyright (c) 2016 rollup-plugin-sass maintainers (see './humans.txt')

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
8 changes: 2 additions & 6 deletions README.md
Expand Up @@ -155,11 +155,7 @@ sass({
})
```

## Maintainers

- [ElyCruz](https://github.com/elycruz)

## License

[MIT](./LICENSE) © 2022 [elycruz](https://github.com/elycruz),
© 2016 [BinRui.Guan](mailto:differui@gmail.com)
[MIT](./LICENSE) [elycruz](https://github.com/elycruz),
[BinRui.Guan](mailto:differui@gmail.com)
5 changes: 1 addition & 4 deletions MAINTAINERS → humans.txt
@@ -1,8 +1,5 @@
# Maintainers
# Maintainers/Authors

2022 Ely De La Cruz (https://github.com/elycruz)

# Legacy

2016 BinRui.Guan <differui@gmail.com> - Author
2016 Thomas Ghysels (https://github.com/thgh)
87 changes: 83 additions & 4 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -57,6 +57,7 @@
},
"dependencies": {
"@rollup/pluginutils": "^3.1.0",
"icss-utils": "^5.1.0",
"resolve": "^1.5.0",
"sass": "^1.7.2"
},
Expand All @@ -72,6 +73,7 @@
"eslint": "^7.32.0",
"jsdom": "^17.0.0",
"nyc": "^15.1.0",
"postcss": "^8.4.16",
"rollup": "^1.32.1",
"sinon": "^7.2.2",
"ts-node": "^10.9.1",
Expand Down
5 changes: 2 additions & 3 deletions src/index.ts
Expand Up @@ -88,10 +88,9 @@ const MATCH_SASS_FILENAME_RE = /\.sass$/,
'See https://github.com/differui/rollup-plugin-sass#processor');
}
const outCss = result.css;
delete result.css;
const restExports = Object.keys(result).reduce((agg, name) =>
name === 'css' ? agg : agg + `export const ${name} = ${JSON.stringify(result[name])};\n`
, ''
);
agg + `export const ${name} = ${JSON.stringify(result[name])};\n`, '');
return [outCss, restExports];
})
.then(([resolvedCss, restExports]) => {
Expand Down
12 changes: 12 additions & 0 deletions test/assets/actual_a--with-icss-exports.scss
@@ -0,0 +1,12 @@
$color_red: red;
$color_blue: blue;

body {
color: $color_red;
background: $color_blue;
}

:export {
color: $color_red;
color2: $color_blue;
}
1 change: 1 addition & 0 deletions test/assets/expect_a--with-icss-exports.css
@@ -0,0 +1 @@
body{color:red;background:blue;}
6 changes: 6 additions & 0 deletions test/fixtures/processor-promise/with-icss-exports.js
@@ -0,0 +1,6 @@
import actualA2, {color, color2} from '../../assets/actual_a--with-icss-exports.scss';
import actualB from '../../assets/actual_b.scss';

export {color, color2};

export default actualA2 + actualB;
46 changes: 41 additions & 5 deletions test/index.test.ts
Expand Up @@ -7,6 +7,8 @@ import * as sassJs from 'sass';
import sass from '../src/index';
import {SassOptions} from "../src/types";
import {error} from "../src/utils";
import postcss from "postcss";
import {extractICSS} from "icss-utils";

const repoRoot = path.join(__dirname, '../'),

Expand Down Expand Up @@ -39,7 +41,7 @@ const repoRoot = path.join(__dirname, '../'),

unwrap = output => output[0].code;

let expectA, expectB, expectC, expectD, expectE;
let expectA, expectA2, expectB, expectC, expectD, expectE;

test.before(async () => {
const mkDir = () => fs.mkdir(tmpDir);
Expand All @@ -48,15 +50,17 @@ test.before(async () => {
.then(mkDir, mkDir)
.then(() => Promise.all([
'test/assets/expect_a.css',
'test/assets/expect_a--with-icss-exports.css',
'test/assets/expect_b.css',
'test/assets/expect_c.css',
'test/assets/expect_d.css',
'test/assets/expect_e.css'
]
.map(xs => fs.readFile(xs).then(buff => buff.toString()))
))
.then(([a, b, c, d, e]) => {
.then(([a, a2, b, c, d, e]) => {
expectA = squash(a);
expectA2 = squash(a2);
expectB = squash(b);
expectC = squash(c);
expectD = squash(d);
Expand Down Expand Up @@ -266,7 +270,7 @@ test('should processor return as object', async t => {
t.true(squash(unwrap(output)).indexOf('bar') > -1);
});

test('should processor return as promise', async t => {
test('should support processor return type `Promise<string>`', async t => {
const outputBundle = await rollup({
input: 'test/fixtures/processor-promise/index.js',
plugins: [
Expand All @@ -283,6 +287,38 @@ test('should processor return as promise', async t => {
t.true(squash(unwrap(output)).indexOf(expectB) > -1);
});

test('should support processor return type `Promise<{css: string, icssExport: {}, icssImport: {}}}>', async t => {
const outputBundle = await rollup({
input: 'test/fixtures/processor-promise/with-icss-exports.js',
plugins: [
sass({
processor: (css) => new Promise((resolve, reject) => {
const pcssRootNodeRslt = postcss.parse(css),
extractedIcss = extractICSS(pcssRootNodeRslt, true),
cleanedCss = pcssRootNodeRslt.toString(),
out = Object.assign({}, extractedIcss.icssExports, {
css: cleanedCss,
});
console.table(extractedIcss);
console.log(out);
resolve(out);
}),
options: sassOptions,
}),
],
}),
{output} = await outputBundle.generate(generateOptions);

outputBundle.write({
...generateOptions,
file: path.join(tmpDir, 'with--icss-exports.js'),
});
// console.log('output:', output);

t.true(squash(unwrap(output)).includes(expectA2));
t.true(squash(unwrap(output)).includes(expectB));
});

test('should processor throw error', async t => {
await t.throwsAsync(async () => rollup({
input: 'test/fixtures/processor-error/index.js',
Expand Down Expand Up @@ -446,6 +482,6 @@ test('When `sourcemap` is set, to `true`, adjacent source map file should be out
});

test.after(async (): Promise<any> => {
return fs.rmdir(tmpDir, {recursive: true})
.catch(error);
// return fs.rmdir(tmpDir, {recursive: true})
// .catch(error);
});

0 comments on commit 17031fb

Please sign in to comment.