Skip to content

Commit

Permalink
feature(MultipleCssFiles) rkazakov#22 Concat all custom styles to a s…
Browse files Browse the repository at this point in the history
…ingle <style>
  • Loading branch information
Guillem Blanché committed Oct 1, 2018
1 parent 7637d44 commit 78eae92
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
16 changes: 10 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,31 +113,35 @@ module.exports = (html, options) => {
}
});

let inlineCss = '';
/* inline styles */
$('link[rel=stylesheet]').each((index, element) => {
const src = $(element).attr('href');
let path = src;
let file = '';
const setFile = (data) => {
const minified = new CleanCss().minify(data).styles;
return `<style amp-custom>${minified}</style>`;
return new CleanCss().minify(data).styles;
// return `<style amp-custom>${minified}</style>`;
};

try {
if (src.indexOf('//') === -1) {
path = `${options.cwd}/${src}`;
if (fs.existsSync(path)) {
file = setFile(String(fs.readFileSync(path)));
inlineCss += setFile(String(fs.readFileSync(path)));
}
} else if (src.indexOf('//') !== -1) {
file = setFile(String(request('GET', path).body));
inlineCss += setFile(String(request('GET', path).body));
}
} catch (err) {
console.dir(err);
}
$(element).replaceWith(file);
$(element).remove();
});

if (inlineCss !== '') {
$('head').append(`<style amp-custom>${inlineCss}</style>`);
}

/* youtube */
$('iframe[src*="http://www.youtube.com"],iframe[src*="https://www.youtube.com"],iframe[src*="http://youtu.be/"],iframe[src*="https://youtu.be/"]').each((index, element) => {
youtube = true;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ampify",
"version": "0.4.2",
"version": "0.5.0",
"description": "Convert plain HTML to Google Accelerated Mobile Pages (AMP)",
"main": "index.js",
"author": {
Expand Down
11 changes: 9 additions & 2 deletions test/styles.test.js

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

3 changes: 3 additions & 0 deletions test/styles1.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
footer {
background-color: #FFF;
}

0 comments on commit 78eae92

Please sign in to comment.