-
Notifications
You must be signed in to change notification settings - Fork 0
/
gemstone-loader-css.js
98 lines (90 loc) · 3.78 KB
/
gemstone-loader-css.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
** GemstoneJS -- Gemstone JavaScript Technology Stack
** Copyright (c) 2016-2019 Gemstone Project <http://gemstonejs.com>
** Licensed under Apache License 2.0 <https://spdx.org/licenses/Apache-2.0>
*/
/* load external requirements */
const co = require("co")
const loaderUtils = require("loader-utils")
const inlineAssets = require("inline-assets")
const PostCSS = require("postcss")
const PostCSSSCSS = require("postcss-scss")
const PostCSSImport = require("postcss-import")
const PostCSSExtendRule = require("postcss-extend-rule")
const PostCSSAdvancedVariables = require("postcss-advanced-variables")
const PostCSSPresetEnv = require("postcss-preset-env")
const PostCSSPropertyLookup = require("postcss-property-lookup")
const PostCSSAlias = require("postcss-alias")
const PostCSSEasings = require("postcss-easings")
const PostCSSHexRGBA = require("postcss-hexrgba")
const PostCSSNested = require("postcss-nested")
const PostCSSScope = require("style-scope/postcss")
/* the exported Webpack loader function */
module.exports = function (content) {
const done = this.async()
co(function * () {
/* determine Webpack loader query parameters */
const options = Object.assign({}, {
scope: "none"
}, loaderUtils.getOptions(this), this.resourceQuery ? loaderUtils.parseQuery(this.resourceQuery) : null)
/* indicate to Webpack that our results are
fully deterministic and can be cached */
this.cacheable(true)
/* transpile CSS via PostCSS */
const plugins = [
PostCSSImport(),
PostCSSExtendRule(),
PostCSSAdvancedVariables(),
PostCSSPresetEnv({
stage: 3, /* stage 2-3, plus... */
features: {
"nesting-rules": true, /* from stage 0 */
"overflow-shorthand": true /* from stage 1 */
},
browsers: "last 2 versions",
autoprefixer: {
browsers: [ "last 2 versions" ]
}
}),
PostCSSPropertyLookup(),
PostCSSAlias(),
PostCSSEasings(),
PostCSSHexRGBA(),
PostCSSNested(),
PostCSSScope({
rootScope: options.scope
})
]
let result = yield PostCSS(plugins).process(content, {
from: this.resourcePath,
to: this.resourcePath,
parser: PostCSSSCSS,
map: { inline: true }
}).catch((err) => {
this.emitError(`gemstone-loader-css: PostCSS [transpile]: ERROR: ${err}`)
})
if (typeof result === "object" && typeof result.warnings === "function") {
const warnings = result.warnings()
if (warnings.length > 0)
this.emitError(`gemstone-loader-css: PostCSS [transpile]: ERROR: ${warnings.join("\n")}`)
}
if (typeof result === "object" && typeof result.css === "string")
result = result.css
else
result = "/* INTERNAL ERROR */"
/* inline all referenced assets to be self-contained */
result = inlineAssets(this.resourcePath, this.resourcePath, result, {
htmlmin: false,
cssmin: this.minimize,
jsmin: false,
pattern: [ ".+" ],
purge: false
})
/* generate output */
result = "module.exports = " + JSON.stringify(result)
done(null, result)
}.bind(this)).catch((err) => {
this.emitError("gemstone-loader-css: ERROR: " + err)
done(err)
})
}