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