|
| 1 | +"use strict"; |
| 2 | +var __importDefault = (this && this.__importDefault) || function (mod) { |
| 3 | + return (mod && mod.__esModule) ? mod : { "default": mod }; |
| 4 | +}; |
| 5 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 6 | +// Adapted from https://github.com/gregberge/svgr/blob/main/packages/webpack/src/index.ts to remove babel |
| 7 | +const util_1 = require("util"); |
| 8 | +const core_1 = require("@svgr/core"); |
| 9 | +const path_1 = require("path"); |
| 10 | +const plugin_svgo_1 = __importDefault(require("@svgr/plugin-svgo")); |
| 11 | +const plugin_jsx_1 = __importDefault(require("@svgr/plugin-jsx")); |
| 12 | +const tranformSvg = (0, util_1.callbackify)(async (contents, options, state) => { |
| 13 | + const jsCode = await (0, core_1.transform)(contents, options, state); |
| 14 | + return jsCode; |
| 15 | +}); |
| 16 | +function svgrLoader(contents) { |
| 17 | + this.cacheable?.(); |
| 18 | + const callback = this.async(); |
| 19 | + const options = this.getOptions(); |
| 20 | + const previousExport = (() => { |
| 21 | + if (contents.startsWith('export ')) { |
| 22 | + return contents; |
| 23 | + } |
| 24 | + const exportMatches = contents.match(/^module.exports\s*=\s*(.*)/); |
| 25 | + return exportMatches ? `export default ${exportMatches[1]}` : null; |
| 26 | + })(); |
| 27 | + const state = { |
| 28 | + caller: { |
| 29 | + name: 'svgr-webpack-no-babel', |
| 30 | + previousExport, |
| 31 | + defaultPlugins: [plugin_svgo_1.default, plugin_jsx_1.default], |
| 32 | + }, |
| 33 | + filePath: (0, path_1.normalize)(this.resourcePath), |
| 34 | + }; |
| 35 | + if (!previousExport) { |
| 36 | + tranformSvg(contents, options, state, callback); |
| 37 | + } |
| 38 | + else { |
| 39 | + this.fs.readFile(this.resourcePath, (err, result) => { |
| 40 | + if (err) { |
| 41 | + callback(err); |
| 42 | + return; |
| 43 | + } |
| 44 | + tranformSvg(String(result), options, state, (err2, content) => { |
| 45 | + if (err2) { |
| 46 | + callback(err2); |
| 47 | + return; |
| 48 | + } |
| 49 | + callback(null, content); |
| 50 | + }); |
| 51 | + }); |
| 52 | + } |
| 53 | +} |
| 54 | +exports.default = svgrLoader; |
0 commit comments