Skip to content

Commit

Permalink
Process urls in SCSS
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelthomas2774 committed Mar 17, 2019
1 parent 835cc31 commit c9e1cd7
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 13 deletions.
23 changes: 23 additions & 0 deletions core/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ import deepmerge from 'deepmerge';
import ContentSecurityPolicy from 'csp-parse';
import keytar from 'keytar';

import postcss from 'postcss';
import postcssUrl from 'postcss-url';
import postcssScss from 'postcss-scss';

import { FileUtils, BDIpc, Config, WindowUtils, Updater, Editor, Database } from './modules';

const sparkplug = path.resolve(__dirname, 'sparkplug.js');
Expand Down Expand Up @@ -83,12 +87,31 @@ class Comms {
});
});

const sassImporter = async (context, url, prev) => {
const file = path.resolve(prev, url);

console.log('Importing', file, url, prev, context);

const scss = await FileUtils.readFile(file);

const result = await postcss([postcssUrl({url: 'inline', encodeType: 'base64', optimizeSvgEncode: true})])
.process(scss, {from: file, syntax: postcssScss});

console.log('Processed', file, result);

return {contents: result.css};
};

BDIpc.on('bd-compileSass', (event, options) => {
if (typeof options.path === 'string' && typeof options.data === 'string') {
options.data = `${options.data} @import '${options.path.replace(/\\/g, '\\\\').replace(/'/g, '\\\'')}';`;
options.path = undefined;
}

options.importer = function (url, prev, done) {
sassImporter(this, url, prev).then(done, done);
};

sass.render(options, (err, result) => {
if (err) event.reject(err);
else event.reply(result);
Expand Down
50 changes: 37 additions & 13 deletions package-lock.json

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

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
"nedb": "^1.8.0",
"node-sass": "^4.11.0",
"original-fs": "^1.0.0",
"postcss": "^7.0.14",
"postcss-scss": "^2.0.0",
"postcss-url": "^8.0.0",
"semver": "^5.6.0",
"tar-fs": "^2.0.0"
},
Expand Down

0 comments on commit c9e1cd7

Please sign in to comment.