UNPKG

1.49 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 query: loaderUtils.parseQuery(this.resourceQuery || '?') || {}
20 });
21 }
22
23 if (!types.includes(options.type)) {
24 return this.callback('The given `type` option is invalid. \n\n' + "Expected:\n One of scoped|global|resolve \n\n" + 'Actual:\n ' + options.type);
25 } // Allows to define the type for each individual file using a CSS comment.
26
27
28 var commentType = content.match(/\/*\s*@styled-jsx=(scoped|global|resolve)/);
29
30 if (commentType) {
31 options.type = commentType[1];
32 }
33
34 var output = "import css from 'styled-jsx/css';\n\nexport default css";
35
36 if (options.type === 'global') {
37 // css.global``
38 output += '.global';
39 } else if (options.type === 'resolve') {
40 // css.resolve``
41 output += '.resolve';
42 } // default css``
43 // Escape backticks and backslashes: “`” ⇒ “\`”, “\” ⇒ “\\”
44 // (c) https://git.io/fNZzr
45
46
47 output += "`" + content.replace(/[`\\]/g, function (match) {
48 return '\\' + match;
49 }) + "`";
50 this.callback(null, output);
51};
\No newline at end of file