Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process urls when compiling SCSS #321

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/src/modules/thememanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export default class ThemeManager extends ContentManager {
const name = setting.id.replace(/[^a-zA-Z0-9-]/g, '-').replace(/--/g, '-');
const scss = await setting.toSCSS();

if (scss) return [name, scss];
if (typeof scss !== 'undefined') return [name, scss];
}

/**
Expand Down
34 changes: 32 additions & 2 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,15 +87,41 @@ class Comms {
});
});

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

const scss = await FileUtils.readFile(file)
.catch(err => FileUtils.readFile(file += '.scss'))
.catch(err => FileUtils.readFile(file = path.join(path.dirname(file), '_' + path.basename(file).substr(0, path.basename(file).length - 5))))
.catch(err => FileUtils.readFile(file += '.scss'));

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

for (const message of result.messages) {
if (message.type !== 'dependency') continue;
inlinedFiles.push(message.file);
}

return {file, 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;
}

const inlinedFiles = [];

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

sass.render(options, (err, result) => {
if (err) event.reject(err);
else event.reply(result);
if (err) return event.reject(err);
result.stats.includedFiles = result.stats.includedFiles.concat(inlinedFiles);
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
File renamed without changes.
4 changes: 4 additions & 0 deletions tests/ext/themes/Example/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ $index: 0;

$index: $index + 1;
}

.bd-scroller {
background-image: url('icon.svg');
}