|
| 1 | +/* |
| 2 | + MIT License http://www.opensource.org/licenses/mit-license.php |
| 3 | + Author Tobias Koppers @sokra |
| 4 | +*/ |
| 5 | +const path = require('path'); |
| 6 | + |
| 7 | +const camelCase = require('lodash/camelCase'); |
| 8 | +const loaderUtils = require('loader-utils'); |
| 9 | + |
| 10 | +function dashesCamelCase(str) { |
| 11 | + return str.replace(/-+(\w)/g, (match, firstLetter) => |
| 12 | + firstLetter.toUpperCase() |
| 13 | + ); |
| 14 | +} |
| 15 | + |
| 16 | +function compileExports(result, importItemMatcher, camelCaseKeys) { |
| 17 | + if (!Object.keys(result.exports).length) { |
| 18 | + return ''; |
| 19 | + } |
| 20 | + |
| 21 | + const exportJs = Object.keys(result.exports) |
| 22 | + .reduce((res, key) => { |
| 23 | + let valueAsString = JSON.stringify(result.exports[key]); |
| 24 | + valueAsString = valueAsString.replace( |
| 25 | + result.importItemRegExpG, |
| 26 | + importItemMatcher |
| 27 | + ); |
| 28 | + function addEntry(k) { |
| 29 | + res.push(`\t${JSON.stringify(k)}: ${valueAsString}`); |
| 30 | + } |
| 31 | + |
| 32 | + let targetKey; |
| 33 | + switch (camelCaseKeys) { |
| 34 | + case true: |
| 35 | + addEntry(key); |
| 36 | + targetKey = camelCase(key); |
| 37 | + if (targetKey !== key) { |
| 38 | + addEntry(targetKey); |
| 39 | + } |
| 40 | + break; |
| 41 | + case 'dashes': |
| 42 | + addEntry(key); |
| 43 | + targetKey = dashesCamelCase(key); |
| 44 | + if (targetKey !== key) { |
| 45 | + addEntry(targetKey); |
| 46 | + } |
| 47 | + break; |
| 48 | + case 'only': |
| 49 | + addEntry(camelCase(key)); |
| 50 | + break; |
| 51 | + case 'dashesOnly': |
| 52 | + addEntry(dashesCamelCase(key)); |
| 53 | + break; |
| 54 | + default: |
| 55 | + addEntry(key); |
| 56 | + break; |
| 57 | + } |
| 58 | + return res; |
| 59 | + }, []) |
| 60 | + .join(',\n'); |
| 61 | + |
| 62 | + return `{\n${exportJs}\n}`; |
| 63 | +} |
| 64 | + |
| 65 | +function getImportPrefix(loaderContext, query) { |
| 66 | + if (query.importLoaders === false) { |
| 67 | + return ''; |
| 68 | + } |
| 69 | + |
| 70 | + const importLoaders = parseInt(query.importLoaders, 10) || 0; |
| 71 | + const loadersRequest = loaderContext.loaders |
| 72 | + .slice( |
| 73 | + loaderContext.loaderIndex, |
| 74 | + loaderContext.loaderIndex + 1 + importLoaders |
| 75 | + ) |
| 76 | + .map((x) => x.request) |
| 77 | + .join('!'); |
| 78 | + return `-!${loadersRequest}!`; |
| 79 | +} |
| 80 | + |
| 81 | +function getLocalIdent(loaderContext, localIdentName, localName, options) { |
| 82 | + if (!options.context) { |
| 83 | + if (loaderContext.rootContext) { |
| 84 | + // eslint-disable-next-line no-param-reassign |
| 85 | + options.context = loaderContext.rootContext; |
| 86 | + } else if ( |
| 87 | + loaderContext.options && |
| 88 | + typeof loaderContext.options.context === 'string' |
| 89 | + ) { |
| 90 | + // eslint-disable-next-line no-param-reassign |
| 91 | + options.context = loaderContext.options.context; |
| 92 | + } else { |
| 93 | + // eslint-disable-next-line no-param-reassign |
| 94 | + options.context = loaderContext.context; |
| 95 | + } |
| 96 | + } |
| 97 | + const request = path.relative(options.context, loaderContext.resourcePath); |
| 98 | + // eslint-disable-next-line no-param-reassign |
| 99 | + options.content = `${options.hashPrefix + request}+${localName}`; |
| 100 | + // eslint-disable-next-line no-param-reassign |
| 101 | + localIdentName = localIdentName.replace(/\[local\]/gi, localName); |
| 102 | + const hash = loaderUtils.interpolateName( |
| 103 | + loaderContext, |
| 104 | + localIdentName, |
| 105 | + options |
| 106 | + ); |
| 107 | + return hash |
| 108 | + .replace(new RegExp('[^a-zA-Z0-9\\-_\u00A0-\uFFFF]', 'g'), '-') |
| 109 | + .replace(/^((-?[0-9])|--)/, '_$1'); |
| 110 | +} |
| 111 | + |
| 112 | +module.exports = { |
| 113 | + dashesCamelCase, |
| 114 | + compileExports, |
| 115 | + getImportPrefix, |
| 116 | + getLocalIdent, |
| 117 | +}; |
0 commit comments