|
| 1 | +// @ts-check |
| 2 | +const fs = require('fs'); |
| 3 | +const fp = require('path'); |
| 4 | +const loaderUtils = require('loader-utils'); |
| 5 | + |
| 6 | +/** @type {import('webpack').loader.Loader} */ |
| 7 | +module.exports = function(content) { |
| 8 | + this.cacheable(); |
| 9 | + const callback = this.async(); |
| 10 | + const options = loaderUtils.getOptions(this); |
| 11 | + genTypings(this.resourcePath, content, options, callback); |
| 12 | +}; |
| 13 | + |
| 14 | +/** |
| 15 | + * @param {string} [path] |
| 16 | + * @param {string | Buffer} [content] |
| 17 | + * @param {import('loader-utils').OptionObject} [options] |
| 18 | + * @param {import('webpack').loader.loaderCallback} [callback] |
| 19 | + */ |
| 20 | +function genTypings(path, content, options, callback) { |
| 21 | + if (Buffer.isBuffer(content)) { |
| 22 | + content = content.toString('utf8'); |
| 23 | + } |
| 24 | + |
| 25 | + /** @type {string[]} */ |
| 26 | + let classes = []; |
| 27 | + { |
| 28 | + /** @type {RegExpExecArray} */ |
| 29 | + let match; |
| 30 | + while (match = classesRegex.exec(content)) { |
| 31 | + if (classes.indexOf(match[1]) === -1) { |
| 32 | + classes.push(match[1]); |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + let typings = ''; |
| 38 | + { |
| 39 | + if (options.banner) { |
| 40 | + typings = `${options.banner}\n`; |
| 41 | + } |
| 42 | + if (options.namedExport) { |
| 43 | + for (let c of classes) { |
| 44 | + typings += `export const ${c}: string;\n`; |
| 45 | + } |
| 46 | + } else { |
| 47 | + const name = getInterfaceName(path); |
| 48 | + typings += `export interface ${name} {\n`; |
| 49 | + for (let c of classes) { |
| 50 | + typings += ` '${c}': string\n`; |
| 51 | + } |
| 52 | + typings += `}\ndeclare const styles: ${name};\nexport default styles;\n`; |
| 53 | + } |
| 54 | + } |
| 55 | + writeFile(getDtsPath(path), typings); |
| 56 | + |
| 57 | + callback(null, content); |
| 58 | +} |
| 59 | + |
| 60 | +const classesRegex = /"([^"\\]+)":/g; |
| 61 | + |
| 62 | +/** |
| 63 | + * @param {string} [path] |
| 64 | + */ |
| 65 | +function getDtsPath(path) { |
| 66 | + return fp.join(fp.dirname(path), `${fp.basename(path)}.d.ts`); |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * @param {string} [path] |
| 71 | + */ |
| 72 | +function getInterfaceName(path) { |
| 73 | + return fp.basename(path) |
| 74 | + .replace(/^(\w)/, (_, c) => 'I' + c.toUpperCase()) |
| 75 | + .replace(/\W+(\w)/g, (_, c) => c.toUpperCase()); |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * @param {string} [path] |
| 80 | + * @param {string} [content] |
| 81 | + */ |
| 82 | +function writeFile(path, content) { |
| 83 | + if (!fs.existsSync(path) || fs.readFileSync(path).toString('utf8') !== content) { |
| 84 | + fs.writeFileSync(path, content); |
| 85 | + } |
| 86 | +} |
0 commit comments