1 | 'use strict';
|
2 |
|
3 | var fs = require('fs');
|
4 | var core$1 = require('@svgr/core');
|
5 | var pluginutils = require('@rollup/pluginutils');
|
6 | var core = require('@babel/core');
|
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 _interopNamespaceDefault(e) {
|
15 | var n = Object.create(null);
|
16 | if (e) {
|
17 | Object.keys(e).forEach(function (k) {
|
18 | if (k !== 'default') {
|
19 | var d = Object.getOwnPropertyDescriptor(e, k);
|
20 | Object.defineProperty(n, k, d.get ? d : {
|
21 | enumerable: true,
|
22 | get: function () { return e[k]; }
|
23 | });
|
24 | }
|
25 | });
|
26 | }
|
27 | n.default = e;
|
28 | return Object.freeze(n);
|
29 | }
|
30 |
|
31 | var fs__namespace = _interopNamespaceDefault(fs);
|
32 |
|
33 | var __defProp = Object.defineProperty;
|
34 | var __defProps = Object.defineProperties;
|
35 | var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
36 | var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
37 | var __hasOwnProp = Object.prototype.hasOwnProperty;
|
38 | var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
39 | var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
40 | var __spreadValues = (a, b) => {
|
41 | for (var prop in b || (b = {}))
|
42 | if (__hasOwnProp.call(b, prop))
|
43 | __defNormalProp(a, prop, b[prop]);
|
44 | if (__getOwnPropSymbols)
|
45 | for (var prop of __getOwnPropSymbols(b)) {
|
46 | if (__propIsEnum.call(b, prop))
|
47 | __defNormalProp(a, prop, b[prop]);
|
48 | }
|
49 | return a;
|
50 | };
|
51 | var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
52 | const babelOptions = {
|
53 | babelrc: false,
|
54 | configFile: false,
|
55 | presets: [
|
56 | core.createConfigItem(presetReact, { type: "preset" }),
|
57 | core.createConfigItem([presetEnv, { modules: false }], { type: "preset" })
|
58 | ],
|
59 | plugins: [core.createConfigItem(pluginTransformReactConstantElements)]
|
60 | };
|
61 | const typeScriptBabelOptions = __spreadProps(__spreadValues({}, babelOptions), {
|
62 | presets: [
|
63 | ...babelOptions.presets,
|
64 | core.createConfigItem(
|
65 | [presetTS, { allowNamespaces: true, allExtensions: true, isTSX: true }],
|
66 | { type: "preset" }
|
67 | )
|
68 | ]
|
69 | });
|
70 | const plugin = (options = {}) => {
|
71 | const EXPORT_REGEX = /(module\.exports *= *|export default)/;
|
72 | const filter = pluginutils.createFilter(options.include || "**/*.svg", options.exclude);
|
73 | const { babel = true } = options;
|
74 | return {
|
75 | name: "svgr",
|
76 | async transform(data, id) {
|
77 | if (!filter(id))
|
78 | return null;
|
79 | if (id.slice(-4) !== ".svg")
|
80 | return null;
|
81 | const load = fs__namespace.readFileSync(id, "utf8");
|
82 | const previousExport = EXPORT_REGEX.test(data) ? data : null;
|
83 | const jsCode = await core$1.transform(load, options, {
|
84 | filePath: id,
|
85 | caller: {
|
86 | name: "@svgr/rollup",
|
87 | previousExport,
|
88 | defaultPlugins: [svgo, jsx]
|
89 | }
|
90 | });
|
91 | if (babel) {
|
92 | const result = await core.transformAsync(
|
93 | jsCode,
|
94 | options.typescript ? typeScriptBabelOptions : babelOptions
|
95 | );
|
96 | if (!(result == null ? void 0 : result.code)) {
|
97 | throw new Error(`Error while transforming using Babel`);
|
98 | }
|
99 | return { code: result.code, map: null };
|
100 | }
|
101 | return {
|
102 | ast: {
|
103 | type: "Program",
|
104 | start: 0,
|
105 | end: 0,
|
106 | sourceType: "module",
|
107 | body: []
|
108 | },
|
109 | code: jsCode,
|
110 | map: null
|
111 | };
|
112 | }
|
113 | };
|
114 | };
|
115 |
|
116 | module.exports = plugin;
|
117 |
|