UNPKG

1.42 kBJavaScriptView Raw
1"use strict";
2
3var loaderUtils = require('loader-utils');
4
5var types = ['scoped', 'global', 'resolve'];
6
7module.exports = function (content) {
8 if (this.cacheable) this.cacheable();
9 this.addDependency(this.resourcePath);
10 var options = Object.assign({}, loaderUtils.getOptions(this));
11
12 if (!options.type) {
13 options.type = 'scoped';
14 } // Calls type with the current file name.
15
16
17 if (typeof options.type === 'function') {
18 options.type = options.type(this.resourcePath);
19 }
20
21 if (!types.includes(options.type)) {
22 return this.callback('The given `type` option is invalid. \n\n' + "Expected:\n One of scoped|global|resolve \n\n" + 'Actual:\n ' + options.type);
23 } // Allows to define the type for each individual file using a CSS comment.
24
25
26 var commentType = content.match(/\/*\s*@styled-jsx=(scoped|global|resolve)/);
27
28 if (commentType) {
29 options.type = commentType[1];
30 }
31
32 var output = "import css from 'styled-jsx/css';\n\nexport default css";
33
34 if (options.type === 'global') {
35 // css.global``
36 output += '.global';
37 } else if (options.type === 'resolve') {
38 // css.resolve``
39 output += '.resolve';
40 } // default css``
41 // Escape backticks and backslashes: “`” ⇒ “\`”, “\” ⇒ “\\”
42 // (c) https://git.io/fNZzr
43
44
45 output += "`".concat(content.replace(/[`\\]/g, function (match) {
46 return '\\' + match;
47 }), "`");
48 this.callback(null, output);
49};
\No newline at end of file