1 | 'use strict';
|
2 |
|
3 | var util = require('util');
|
4 | var core = require('@babel/core');
|
5 | var core$1 = require('@svgr/core');
|
6 | var path = require('path');
|
7 | var svgo = require('@svgr/plugin-svgo');
|
8 | var jsx = require('@svgr/plugin-jsx');
|
9 | var presetReact = require('@babel/preset-react');
|
10 | var presetEnv = require('@babel/preset-env');
|
11 | var presetTS = require('@babel/preset-typescript');
|
12 | var pluginTransformReactConstantElements = require('@babel/plugin-transform-react-constant-elements');
|
13 |
|
14 | function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
15 |
|
16 | var svgo__default = _interopDefaultLegacy(svgo);
|
17 | var jsx__default = _interopDefaultLegacy(jsx);
|
18 | var presetReact__default = _interopDefaultLegacy(presetReact);
|
19 | var presetEnv__default = _interopDefaultLegacy(presetEnv);
|
20 | var presetTS__default = _interopDefaultLegacy(presetTS);
|
21 | var pluginTransformReactConstantElements__default = _interopDefaultLegacy(pluginTransformReactConstantElements);
|
22 |
|
23 | var __defProp = Object.defineProperty;
|
24 | var __defProps = Object.defineProperties;
|
25 | var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
26 | var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
27 | var __hasOwnProp = Object.prototype.hasOwnProperty;
|
28 | var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
29 | var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
30 | var __spreadValues = (a, b) => {
|
31 | for (var prop in b || (b = {}))
|
32 | if (__hasOwnProp.call(b, prop))
|
33 | __defNormalProp(a, prop, b[prop]);
|
34 | if (__getOwnPropSymbols)
|
35 | for (var prop of __getOwnPropSymbols(b)) {
|
36 | if (__propIsEnum.call(b, prop))
|
37 | __defNormalProp(a, prop, b[prop]);
|
38 | }
|
39 | return a;
|
40 | };
|
41 | var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
42 | var __objRest = (source, exclude) => {
|
43 | var target = {};
|
44 | for (var prop in source)
|
45 | if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
46 | target[prop] = source[prop];
|
47 | if (source != null && __getOwnPropSymbols)
|
48 | for (var prop of __getOwnPropSymbols(source)) {
|
49 | if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
50 | target[prop] = source[prop];
|
51 | }
|
52 | return target;
|
53 | };
|
54 | const babelOptions = {
|
55 | babelrc: false,
|
56 | configFile: false,
|
57 | presets: [
|
58 | core.createConfigItem(presetReact__default["default"], { type: "preset" }),
|
59 | core.createConfigItem([presetEnv__default["default"], { modules: false }], { type: "preset" })
|
60 | ],
|
61 | plugins: [core.createConfigItem(pluginTransformReactConstantElements__default["default"])]
|
62 | };
|
63 | const typeScriptBabelOptions = __spreadProps(__spreadValues({}, babelOptions), {
|
64 | presets: [
|
65 | ...babelOptions.presets,
|
66 | core.createConfigItem([presetTS__default["default"], { allowNamespaces: true, allExtensions: true, isTSX: true }], { type: "preset" })
|
67 | ]
|
68 | });
|
69 | const tranformSvg = util.callbackify(async (contents, options, state) => {
|
70 | const _a = options, { babel = true } = _a, config = __objRest(_a, ["babel"]);
|
71 | const jsCode = await core$1.transform(contents, config, state);
|
72 | if (!babel)
|
73 | return jsCode;
|
74 | const result = await core.transformAsync(jsCode, options.typescript ? typeScriptBabelOptions : babelOptions);
|
75 | if (!(result == null ? void 0 : result.code)) {
|
76 | throw new Error(`Error while transforming using Babel`);
|
77 | }
|
78 | return result.code;
|
79 | });
|
80 | function svgrLoader(contents) {
|
81 | this.cacheable && this.cacheable();
|
82 | const callback = this.async();
|
83 | const options = this.getOptions();
|
84 | const previousExport = (() => {
|
85 | if (contents.startsWith("export "))
|
86 | return contents;
|
87 | const exportMatches = contents.match(/^module.exports\s*=\s*(.*)/);
|
88 | return exportMatches ? `export default ${exportMatches[1]}` : null;
|
89 | })();
|
90 | const state = {
|
91 | caller: {
|
92 | name: "@svgr/webpack",
|
93 | previousExport,
|
94 | defaultPlugins: [svgo__default["default"], jsx__default["default"]]
|
95 | },
|
96 | filePath: path.normalize(this.resourcePath)
|
97 | };
|
98 | if (!previousExport) {
|
99 | tranformSvg(contents, options, state, callback);
|
100 | } else {
|
101 | this.fs.readFile(this.resourcePath, (err, result) => {
|
102 | if (err) {
|
103 | callback(err);
|
104 | return;
|
105 | }
|
106 | tranformSvg(String(result), options, state, (err2, content) => {
|
107 | if (err2) {
|
108 | callback(err2);
|
109 | return;
|
110 | }
|
111 | callback(null, `${content}
|
112 | ${previousExport}`);
|
113 | });
|
114 | });
|
115 | }
|
116 | }
|
117 |
|
118 | module.exports = svgrLoader;
|
119 |
|