[6a3a178] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 | exports.stringifyRequest = stringifyRequest;
|
---|
| 7 | exports.getImportInsertStyleElementCode = getImportInsertStyleElementCode;
|
---|
| 8 | exports.getImportInsertBySelectorCode = getImportInsertBySelectorCode;
|
---|
| 9 | exports.getImportStyleContentCode = getImportStyleContentCode;
|
---|
| 10 | exports.getImportStyleDomAPICode = getImportStyleDomAPICode;
|
---|
| 11 | exports.getImportStyleAPICode = getImportStyleAPICode;
|
---|
| 12 | exports.getImportLinkContentCode = getImportLinkContentCode;
|
---|
| 13 | exports.getImportLinkAPICode = getImportLinkAPICode;
|
---|
| 14 | exports.getStyleHmrCode = getStyleHmrCode;
|
---|
| 15 | exports.getLinkHmrCode = getLinkHmrCode;
|
---|
| 16 | exports.getdomAPI = getdomAPI;
|
---|
| 17 | exports.getImportIsOldIECode = getImportIsOldIECode;
|
---|
| 18 | exports.getStyleTagTransformFn = getStyleTagTransformFn;
|
---|
| 19 | exports.getExportStyleCode = getExportStyleCode;
|
---|
| 20 | exports.getExportLazyStyleCode = getExportLazyStyleCode;
|
---|
| 21 | exports.getSetAttributesCode = getSetAttributesCode;
|
---|
| 22 | exports.getInsertOptionCode = getInsertOptionCode;
|
---|
| 23 | exports.getStyleTagTransformFnCode = getStyleTagTransformFnCode;
|
---|
| 24 |
|
---|
| 25 | var _path = _interopRequireDefault(require("path"));
|
---|
| 26 |
|
---|
| 27 | var _isEqualLocals = _interopRequireDefault(require("./runtime/isEqualLocals"));
|
---|
| 28 |
|
---|
| 29 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
---|
| 30 |
|
---|
| 31 | const matchRelativePath = /^\.\.?[/\\]/;
|
---|
| 32 |
|
---|
| 33 | function isAbsolutePath(str) {
|
---|
| 34 | return _path.default.posix.isAbsolute(str) || _path.default.win32.isAbsolute(str);
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | function isRelativePath(str) {
|
---|
| 38 | return matchRelativePath.test(str);
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | function stringifyRequest(loaderContext, request) {
|
---|
| 42 | const splitted = request.split("!");
|
---|
| 43 | const {
|
---|
| 44 | context
|
---|
| 45 | } = loaderContext;
|
---|
| 46 | return JSON.stringify(splitted.map(part => {
|
---|
| 47 | // First, separate singlePath from query, because the query might contain paths again
|
---|
| 48 | const splittedPart = part.match(/^(.*?)(\?.*)/);
|
---|
| 49 | const query = splittedPart ? splittedPart[2] : "";
|
---|
| 50 | let singlePath = splittedPart ? splittedPart[1] : part;
|
---|
| 51 |
|
---|
| 52 | if (isAbsolutePath(singlePath) && context) {
|
---|
| 53 | singlePath = _path.default.relative(context, singlePath);
|
---|
| 54 |
|
---|
| 55 | if (isAbsolutePath(singlePath)) {
|
---|
| 56 | // If singlePath still matches an absolute path, singlePath was on a different drive than context.
|
---|
| 57 | // In this case, we leave the path platform-specific without replacing any separators.
|
---|
| 58 | // @see https://github.com/webpack/loader-utils/pull/14
|
---|
| 59 | return singlePath + query;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | if (isRelativePath(singlePath) === false) {
|
---|
| 63 | // Ensure that the relative path starts at least with ./ otherwise it would be a request into the modules directory (like node_modules).
|
---|
| 64 | singlePath = `./${singlePath}`;
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | return singlePath.replace(/\\/g, "/") + query;
|
---|
| 69 | }).join("!"));
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | function getImportLinkAPICode(esModule, loaderContext) {
|
---|
| 73 | const modulePath = stringifyRequest(loaderContext, `!${_path.default.join(__dirname, "runtime/injectStylesIntoLinkTag.js")}`);
|
---|
| 74 | return esModule ? `import API from ${modulePath};` : `var API = require(${modulePath});`;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | function getImportLinkContentCode(esModule, loaderContext, request) {
|
---|
| 78 | const modulePath = stringifyRequest(loaderContext, `!!${request}`);
|
---|
| 79 | return esModule ? `import content from ${modulePath};` : `var content = require(${modulePath});`;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | function getImportStyleAPICode(esModule, loaderContext) {
|
---|
| 83 | const modulePath = stringifyRequest(loaderContext, `!${_path.default.join(__dirname, "runtime/injectStylesIntoStyleTag.js")}`);
|
---|
| 84 | return esModule ? `import API from ${modulePath};` : `var API = require(${modulePath});`;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | function getImportStyleDomAPICode(esModule, loaderContext, isSingleton, isAuto) {
|
---|
| 88 | const styleAPI = stringifyRequest(loaderContext, `!${_path.default.join(__dirname, "runtime/styleDomAPI.js")}`);
|
---|
| 89 | const singletonAPI = stringifyRequest(loaderContext, `!${_path.default.join(__dirname, "runtime/singletonStyleDomAPI.js")}`);
|
---|
| 90 |
|
---|
| 91 | if (isAuto) {
|
---|
| 92 | return esModule ? `import domAPI from ${styleAPI};
|
---|
| 93 | import domAPISingleton from ${singletonAPI};` : `var domAPI = require(${styleAPI});
|
---|
| 94 | var domAPISingleton = require(${singletonAPI});`;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | return esModule ? `import domAPI from ${isSingleton ? singletonAPI : styleAPI};` : `var domAPI = require(${isSingleton ? singletonAPI : styleAPI});`;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | function getImportStyleContentCode(esModule, loaderContext, request) {
|
---|
| 101 | const modulePath = stringifyRequest(loaderContext, `!!${request}`);
|
---|
| 102 | return esModule ? `import content, * as namedExport from ${modulePath};` : `var content = require(${modulePath});`;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | function getImportInsertBySelectorCode(esModule, loaderContext, insertType, options) {
|
---|
| 106 | if (insertType === "selector") {
|
---|
| 107 | const modulePath = stringifyRequest(loaderContext, `!${_path.default.join(__dirname, "runtime/insertBySelector.js")}`);
|
---|
| 108 | return esModule ? `import insertFn from ${modulePath};` : `var insertFn = require(${modulePath});`;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | if (insertType === "module-path") {
|
---|
| 112 | const modulePath = stringifyRequest(loaderContext, `${options.insert}`);
|
---|
| 113 | loaderContext.addBuildDependency(options.insert);
|
---|
| 114 | return esModule ? `import insertFn from ${modulePath};` : `var insertFn = require(${modulePath});`;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | return "";
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | function getInsertOptionCode(insertType, options) {
|
---|
| 121 | if (insertType === "selector") {
|
---|
| 122 | const insert = options.insert ? JSON.stringify(options.insert) : '"head"';
|
---|
| 123 | return `
|
---|
| 124 | options.insert = insertFn.bind(null, ${insert});
|
---|
| 125 | `;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | if (insertType === "module-path") {
|
---|
| 129 | return `options.insert = insertFn;`;
|
---|
| 130 | } // Todo remove "function" type for insert option in next major release, because code duplication occurs. Leave require.resolve()
|
---|
| 131 |
|
---|
| 132 |
|
---|
| 133 | return `options.insert = ${options.insert.toString()};`;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | function getImportInsertStyleElementCode(esModule, loaderContext) {
|
---|
| 137 | const modulePath = stringifyRequest(loaderContext, `!${_path.default.join(__dirname, "runtime/insertStyleElement.js")}`);
|
---|
| 138 | return esModule ? `import insertStyleElement from ${modulePath};` : `var insertStyleElement = require(${modulePath});`;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | function getStyleHmrCode(esModule, loaderContext, request, lazy) {
|
---|
| 142 | const modulePath = stringifyRequest(loaderContext, `!!${request}`);
|
---|
| 143 | return `
|
---|
| 144 | if (module.hot) {
|
---|
| 145 | if (!content.locals || module.hot.invalidate) {
|
---|
| 146 | var isEqualLocals = ${_isEqualLocals.default.toString()};
|
---|
| 147 | var isNamedExport = ${esModule ? "!content.locals" : false};
|
---|
| 148 | var oldLocals = isNamedExport ? namedExport : content.locals;
|
---|
| 149 |
|
---|
| 150 | module.hot.accept(
|
---|
| 151 | ${modulePath},
|
---|
| 152 | function () {
|
---|
| 153 | ${esModule ? `if (!isEqualLocals(oldLocals, isNamedExport ? namedExport : content.locals, isNamedExport)) {
|
---|
| 154 | module.hot.invalidate();
|
---|
| 155 |
|
---|
| 156 | return;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | oldLocals = isNamedExport ? namedExport : content.locals;
|
---|
| 160 |
|
---|
| 161 | ${lazy ? `if (update && refs > 0) {
|
---|
| 162 | update(content);
|
---|
| 163 | }` : `update(content);`}` : `content = require(${modulePath});
|
---|
| 164 |
|
---|
| 165 | content = content.__esModule ? content.default : content;
|
---|
| 166 |
|
---|
| 167 | ${lazy ? "" : `if (typeof content === 'string') {
|
---|
| 168 | content = [[module.id, content, '']];
|
---|
| 169 | }`}
|
---|
| 170 |
|
---|
| 171 | if (!isEqualLocals(oldLocals, content.locals)) {
|
---|
| 172 | module.hot.invalidate();
|
---|
| 173 |
|
---|
| 174 | return;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | oldLocals = content.locals;
|
---|
| 178 |
|
---|
| 179 | ${lazy ? `if (update && refs > 0) {
|
---|
| 180 | update(content);
|
---|
| 181 | }` : `update(content);`}`}
|
---|
| 182 | }
|
---|
| 183 | )
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | module.hot.dispose(function() {
|
---|
| 187 | ${lazy ? `if (update) {
|
---|
| 188 | update();
|
---|
| 189 | }` : `update();`}
|
---|
| 190 | });
|
---|
| 191 | }
|
---|
| 192 | `;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | function getLinkHmrCode(esModule, loaderContext, request) {
|
---|
| 196 | const modulePath = stringifyRequest(loaderContext, `!!${request}`);
|
---|
| 197 | return `
|
---|
| 198 | if (module.hot) {
|
---|
| 199 | module.hot.accept(
|
---|
| 200 | ${modulePath},
|
---|
| 201 | function() {
|
---|
| 202 | ${esModule ? "update(content);" : `content = require(${modulePath});
|
---|
| 203 |
|
---|
| 204 | content = content.__esModule ? content.default : content;
|
---|
| 205 |
|
---|
| 206 | update(content);`}
|
---|
| 207 | }
|
---|
| 208 | );
|
---|
| 209 |
|
---|
| 210 | module.hot.dispose(function() {
|
---|
| 211 | update();
|
---|
| 212 | });
|
---|
| 213 | }`;
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | function getdomAPI(isAuto) {
|
---|
| 217 | return isAuto ? "isOldIE() ? domAPISingleton : domAPI" : "domAPI";
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | function getImportIsOldIECode(esModule, loaderContext) {
|
---|
| 221 | const modulePath = stringifyRequest(loaderContext, `!${_path.default.join(__dirname, "runtime/isOldIE.js")}`);
|
---|
| 222 | return esModule ? `import isOldIE from ${modulePath};` : `var isOldIE = require(${modulePath});`;
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | function getStyleTagTransformFnCode(esModule, loaderContext, options, isSingleton, styleTagTransformType) {
|
---|
| 226 | if (isSingleton) {
|
---|
| 227 | return "";
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | if (styleTagTransformType === "default") {
|
---|
| 231 | const modulePath = stringifyRequest(loaderContext, `!${_path.default.join(__dirname, "runtime/styleTagTransform.js")}`);
|
---|
| 232 | return esModule ? `import styleTagTransformFn from ${modulePath};` : `var styleTagTransformFn = require(${modulePath});`;
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | if (styleTagTransformType === "module-path") {
|
---|
| 236 | const modulePath = stringifyRequest(loaderContext, `${options.styleTagTransform}`);
|
---|
| 237 | loaderContext.addBuildDependency(options.styleTagTransform);
|
---|
| 238 | return esModule ? `import styleTagTransformFn from ${modulePath};` : `var styleTagTransformFn = require(${modulePath});`;
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | return "";
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | function getStyleTagTransformFn(options, isSingleton) {
|
---|
| 245 | // Todo remove "function" type for styleTagTransform option in next major release, because code duplication occurs. Leave require.resolve()
|
---|
| 246 | return isSingleton ? "" : typeof options.styleTagTransform === "function" ? `options.styleTagTransform = ${options.styleTagTransform.toString()}` : `options.styleTagTransform = styleTagTransformFn`;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | function getExportStyleCode(esModule, loaderContext, request) {
|
---|
| 250 | const modulePath = stringifyRequest(loaderContext, `!!${request}`);
|
---|
| 251 | return esModule ? `export * from ${modulePath};
|
---|
| 252 | export default content && content.locals ? content.locals : undefined;` : "module.exports = content && content.locals || {};";
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | function getExportLazyStyleCode(esModule, loaderContext, request) {
|
---|
| 256 | const modulePath = stringifyRequest(loaderContext, `!!${request}`);
|
---|
| 257 | return esModule ? `export * from ${modulePath};
|
---|
| 258 | export default exported;` : "module.exports = exported;";
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | function getSetAttributesCode(esModule, loaderContext, options) {
|
---|
| 262 | let modulePath;
|
---|
| 263 |
|
---|
| 264 | if (typeof options.attributes !== "undefined") {
|
---|
| 265 | modulePath = options.attributes.nonce !== "undefined" ? stringifyRequest(loaderContext, `!${_path.default.join(__dirname, "runtime/setAttributesWithAttributesAndNonce.js")}`) : stringifyRequest(loaderContext, `!${_path.default.join(__dirname, "runtime/setAttributesWithAttributes.js")}`);
|
---|
| 266 | } else {
|
---|
| 267 | modulePath = stringifyRequest(loaderContext, `!${_path.default.join(__dirname, "runtime/setAttributesWithoutAttributes.js")}`);
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | return esModule ? `import setAttributes from ${modulePath};` : `var setAttributes = require(${modulePath});`;
|
---|
| 271 | } // eslint-disable-next-line import/prefer-default-export |
---|