UNPKG

6.14 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7var _path = _interopRequireDefault(require("path"));
8var _utils = require("./utils");
9var _options = _interopRequireDefault(require("./options.json"));
10function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11// eslint-disable-next-line consistent-return
12const loader = function loader(content) {
13 if (this._compiler && this._compiler.options && this._compiler.options.experiments && this._compiler.options.experiments.css && this._module && (this._module.type === "css" || this._module.type === "css/global" || this._module.type === "css/module" || this._module.type === "css/auto")) {
14 return content;
15 }
16};
17loader.pitch = function pitch(request) {
18 if (this._compiler && this._compiler.options && this._compiler.options.experiments && this._compiler.options.experiments.css && this._module && (this._module.type === "css" || this._module.type === "css/global" || this._module.type === "css/module" || this._module.type === "css/auto")) {
19 this.emitWarning(new Error('You can\'t use `experiments.css` (`experiments.futureDefaults` enable built-in CSS support by default) and `style-loader` together, please set `experiments.css` to `false` or set `{ type: "javascript/auto" }` for rules with `style-loader` in your webpack config (now `style-loader` does nothing).'));
20 return;
21 }
22 const options = this.getOptions(_options.default);
23 const injectType = options.injectType || "styleTag";
24 const esModule = typeof options.esModule !== "undefined" ? options.esModule : true;
25 const runtimeOptions = {};
26 if (options.attributes) {
27 runtimeOptions.attributes = options.attributes;
28 }
29 if (options.base) {
30 runtimeOptions.base = options.base;
31 }
32 const insertType = options.insert && _path.default.isAbsolute(options.insert) ? "module-path" : "selector";
33 switch (injectType) {
34 case "linkTag":
35 {
36 const hmrCode = this.hot ? (0, _utils.getLinkHmrCode)(esModule, this, request) : "";
37
38 // eslint-disable-next-line consistent-return
39 return `
40 ${(0, _utils.getImportLinkAPICode)(esModule, this)}
41 ${(0, _utils.getImportInsertBySelectorCode)(esModule, this, insertType, options)}
42 ${(0, _utils.getImportLinkContentCode)(esModule, this, request)}
43 ${esModule ? "" : `content = content.__esModule ? content.default : content;`}
44
45var options = ${JSON.stringify(runtimeOptions)};
46
47${(0, _utils.getInsertOptionCode)(insertType, options)}
48
49var update = API(content, options);
50
51${hmrCode}
52
53${esModule ? "export default {}" : ""}`;
54 }
55 case "lazyStyleTag":
56 case "lazyAutoStyleTag":
57 case "lazySingletonStyleTag":
58 {
59 const isSingleton = injectType === "lazySingletonStyleTag";
60 const isAuto = injectType === "lazyAutoStyleTag";
61 const hmrCode = this.hot ? (0, _utils.getStyleHmrCode)(esModule, this, request, true) : "";
62
63 // eslint-disable-next-line consistent-return
64 return `
65 var exported = {};
66
67 ${(0, _utils.getImportStyleAPICode)(esModule, this)}
68 ${(0, _utils.getImportStyleDomAPICode)(esModule, this, isSingleton, isAuto)}
69 ${(0, _utils.getImportInsertBySelectorCode)(esModule, this, insertType, options)}
70 ${(0, _utils.getSetAttributesCode)(esModule, this, options)}
71 ${(0, _utils.getImportInsertStyleElementCode)(esModule, this)}
72 ${(0, _utils.getStyleTagTransformFnCode)(esModule, this, options, isSingleton)}
73 ${(0, _utils.getImportStyleContentCode)(esModule, this, request)}
74 ${isAuto ? (0, _utils.getImportIsOldIECode)(esModule, this) : ""}
75 ${esModule ? `if (content && content.locals) {
76 exported.locals = content.locals;
77 }
78 ` : `content = content.__esModule ? content.default : content;
79
80 exported.locals = content.locals || {};`}
81
82var refs = 0;
83var update;
84var options = ${JSON.stringify(runtimeOptions)};
85
86${(0, _utils.getStyleTagTransformFn)(options, isSingleton)};
87options.setAttributes = setAttributes;
88${(0, _utils.getInsertOptionCode)(insertType, options)}
89options.domAPI = ${(0, _utils.getdomAPI)(isAuto)};
90options.insertStyleElement = insertStyleElement;
91
92exported.use = function(insertOptions) {
93 options.options = insertOptions || {};
94
95 if (!(refs++)) {
96 update = API(content, options);
97 }
98
99 return exported;
100};
101exported.unuse = function() {
102 if (refs > 0 && !--refs) {
103 update();
104 update = null;
105 }
106};
107
108${hmrCode}
109
110${(0, _utils.getExportLazyStyleCode)(esModule, this, request)}
111`;
112 }
113 case "styleTag":
114 case "autoStyleTag":
115 case "singletonStyleTag":
116 default:
117 {
118 const isSingleton = injectType === "singletonStyleTag";
119 const isAuto = injectType === "autoStyleTag";
120 const hmrCode = this.hot ? (0, _utils.getStyleHmrCode)(esModule, this, request, false) : "";
121
122 // eslint-disable-next-line consistent-return
123 return `
124 ${(0, _utils.getImportStyleAPICode)(esModule, this)}
125 ${(0, _utils.getImportStyleDomAPICode)(esModule, this, isSingleton, isAuto)}
126 ${(0, _utils.getImportInsertBySelectorCode)(esModule, this, insertType, options)}
127 ${(0, _utils.getSetAttributesCode)(esModule, this, options)}
128 ${(0, _utils.getImportInsertStyleElementCode)(esModule, this)}
129 ${(0, _utils.getStyleTagTransformFnCode)(esModule, this, options, isSingleton)}
130 ${(0, _utils.getImportStyleContentCode)(esModule, this, request)}
131 ${isAuto ? (0, _utils.getImportIsOldIECode)(esModule, this) : ""}
132 ${esModule ? "" : `content = content.__esModule ? content.default : content;`}
133
134var options = ${JSON.stringify(runtimeOptions)};
135
136${(0, _utils.getStyleTagTransformFn)(options, isSingleton)};
137options.setAttributes = setAttributes;
138${(0, _utils.getInsertOptionCode)(insertType, options)}
139options.domAPI = ${(0, _utils.getdomAPI)(isAuto)};
140options.insertStyleElement = insertStyleElement;
141
142var update = API(content, options);
143
144${hmrCode}
145
146${(0, _utils.getExportStyleCode)(esModule, this, request)}
147`;
148 }
149 }
150};
151var _default = exports.default = loader;
\No newline at end of file