|
1 |
| -// Shamelessly modified from Eleventastic: |
2 |
| -// https://github.com/maxboeck/eleventastic |
3 |
| - |
4 |
| -const path = require("path") |
5 |
| -const sass = require("node-sass") |
6 |
| -const CleanCSS = require("clean-css") |
7 |
| -const cssesc = require("cssesc") |
8 |
| - |
9 |
| -const isProd = process.env.ELEVENTY_ENV === "production" |
10 |
| - |
11 |
| -// main entry point name |
12 |
| -const fileName = "style.scss" |
13 |
| - |
14 |
| -module.exports = class { |
15 |
| - async data() { |
16 |
| - const filePath = path.join(__dirname, `/scss/${fileName}`) |
17 |
| - return { |
18 |
| - permalink: '/assets/style.css', |
19 |
| - eleventyExcludeFromCollections: true, |
20 |
| - layout: 'blank', |
21 |
| - filePath, |
22 |
| - } |
23 |
| - } |
24 |
| - |
25 |
| - // Compile Sass to CSS, |
26 |
| - // Embed Source Map in Development |
27 |
| - async compile(config) { |
28 |
| - return new Promise((resolve, reject) => { |
29 |
| - if (!isProd) { |
30 |
| - config.sourceMap = true; |
31 |
| - config.sourceMapEmbed = true; |
32 |
| - config.outputStyle = "expanded"; |
33 |
| - } |
34 |
| - |
35 |
| - return sass.render(config, (err, result) => { |
36 |
| - if (err) { |
37 |
| - return reject(err); |
38 |
| - } |
39 |
| - resolve(result.css.toString()); |
40 |
| - }); |
41 |
| - }); |
42 |
| - } |
43 |
| - |
44 |
| - // Minify & Optimize with CleanCSS in Production |
45 |
| - async minify(css) { |
46 |
| - return new Promise((resolve, reject) => { |
47 |
| - if (!isProd) { |
48 |
| - resolve(css); |
49 |
| - } |
50 |
| - const minified = new CleanCSS().minify(css); |
51 |
| - if (!minified.styles) { |
52 |
| - return reject(minified.error); |
53 |
| - } |
54 |
| - resolve(minified.styles); |
55 |
| - }); |
56 |
| - } |
57 |
| - |
58 |
| - // display an error overlay when CSS build fails. |
59 |
| - // this brilliant idea is taken from Mike Riethmuller / Supermaya |
60 |
| - // @see https://github.com/MadeByMike/supermaya/blob/master/site/utils/compile-scss.js |
61 |
| - renderError(error) { |
62 |
| - return ` |
63 |
| - /* Error compiling stylesheet */ |
64 |
| - *, |
65 |
| - *::before, |
66 |
| - *::after { |
67 |
| - box-sizing: border-box; |
68 |
| - } |
69 |
| - html, |
70 |
| - body { |
71 |
| - margin: 0; |
72 |
| - padding: 0; |
73 |
| - min-height: 100vh; |
74 |
| - font-family: monospace; |
75 |
| - font-size: 1.25rem; |
76 |
| - line-height:1.5; |
77 |
| - } |
78 |
| - body::before { |
79 |
| - content: ''; |
80 |
| - background: #000; |
81 |
| - top: 0; |
82 |
| - bottom: 0; |
83 |
| - width: 100%; |
84 |
| - height: 100%; |
85 |
| - opacity: 0.7; |
86 |
| - position: fixed; |
87 |
| - } |
88 |
| - body::after { |
89 |
| - content: '${cssesc(error)}'; |
90 |
| - white-space: pre; |
91 |
| - display: block; |
92 |
| - top: 0; |
93 |
| - padding: 30px; |
94 |
| - margin: 50px; |
95 |
| - width: calc(100% - 100px); |
96 |
| - color:#721c24; |
97 |
| - background: #f8d7da; |
98 |
| - border: solid 2px red; |
99 |
| - position: fixed; |
100 |
| - }`; |
101 |
| - } |
102 |
| - |
103 |
| - // render the CSS file |
104 |
| - async render({filePath}) { |
105 |
| - try { |
106 |
| - const css = await this.compile({ file: filePath }) |
107 |
| - const result = await this.minify(css) |
108 |
| - return result; |
109 |
| - } catch (err) { |
110 |
| - // if things go wrong |
111 |
| - if (isProd) { |
112 |
| - // throw and abort in production |
113 |
| - throw new Error(err); |
114 |
| - } else { |
115 |
| - // otherwise display the error overly |
116 |
| - console.error(err); |
117 |
| - const msg = err.formatted || err.message; |
118 |
| - return this.renderError(msg); |
119 |
| - } |
120 |
| - } |
121 |
| - } |
122 |
| -}; |
| 1 | +version https://git-lfs.github.com/spec/v1 |
| 2 | +oid sha256:704cddb384430aff341d8ace1d21055a9d2c4aa62d2110cd14b7491ffda5577b |
| 3 | +size 3033 |
0 commit comments