@parcel/transformer-svg-jsx 2.14.5-canary.3441

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017-present Devon Govett
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,121 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ function _plugin() {
8
+ const data = require("@parcel/plugin");
9
+ _plugin = function () {
10
+ return data;
11
+ };
12
+ return data;
13
+ }
14
+ function _path() {
15
+ const data = _interopRequireDefault(require("path"));
16
+ _path = function () {
17
+ return data;
18
+ };
19
+ return data;
20
+ }
21
+ function _utils() {
22
+ const data = require("@parcel/utils");
23
+ _utils = function () {
24
+ return data;
25
+ };
26
+ return data;
27
+ }
28
+ function _diagnostic() {
29
+ const data = _interopRequireWildcard(require("@parcel/diagnostic"));
30
+ _diagnostic = function () {
31
+ return data;
32
+ };
33
+ return data;
34
+ }
35
+ function _rust() {
36
+ const data = require("@parcel/rust");
37
+ _rust = function () {
38
+ return data;
39
+ };
40
+ return data;
41
+ }
42
+ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
43
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
44
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
45
+ var _default = exports.default = new (_plugin().Transformer)({
46
+ async loadConfig({
47
+ config,
48
+ options
49
+ }) {
50
+ var _svgrResult$contents;
51
+ let svgrResult = await config.getConfig(['.svgrrc', '.svgrrc.json', '.svgrrc.js', '.svgrrc.cjs', '.svgrrc.mjs', 'svgr.config.json', 'svgr.config.js', 'svgr.config.cjs', 'svgr.config.mjs']);
52
+ let svgoResult = await config.getConfig(['svgo.config.js', 'svgo.config.cjs', 'svgo.config.mjs', 'svgo.config.json']);
53
+ let contents = (svgrResult === null || svgrResult === void 0 || (_svgrResult$contents = svgrResult.contents) === null || _svgrResult$contents === void 0 ? void 0 : _svgrResult$contents.svgoConfig) ?? (svgoResult === null || svgoResult === void 0 ? void 0 : svgoResult.contents);
54
+ let svgoConfig = {
55
+ default: true
56
+ };
57
+ if (contents) {
58
+ var _svgrResult$contents2, _svgrResult$contents3;
59
+ svgoConfig = await (0, _utils().convertSVGOConfig)(contents, svgrResult !== null && svgrResult !== void 0 && (_svgrResult$contents2 = svgrResult.contents) !== null && _svgrResult$contents2 !== void 0 && _svgrResult$contents2.svgoConfig ? svgrResult.filePath : svgoResult.filePath, svgrResult !== null && svgrResult !== void 0 && (_svgrResult$contents3 = svgrResult.contents) !== null && _svgrResult$contents3 !== void 0 && _svgrResult$contents3.svgoConfig ? '/svgoConfig' : '', options.inputFS, 'Use the legacy @parcel/transformer-svg-react instead of @parcel/transformer-svg-jsx if needed.');
60
+ }
61
+ let svgr = svgrResult === null || svgrResult === void 0 ? void 0 : svgrResult.contents;
62
+ if (svgoResult && svgr !== null && svgr !== void 0 && svgr.jsx) {
63
+ throw new (_diagnostic().default)({
64
+ diagnostic: await createDiagnostic((0, _diagnostic().md)`
65
+ Unsupported SVGR option "jsx".
66
+ `, svgrResult.filePath, '/jsx', options)
67
+ });
68
+ }
69
+ if (svgr !== null && svgr !== void 0 && svgr.template) {
70
+ throw new (_diagnostic().default)({
71
+ diagnostic: await createDiagnostic((0, _diagnostic().md)`
72
+ Unsupported SVGR option "template".
73
+ `, svgrResult.filePath, '/template', options)
74
+ });
75
+ }
76
+
77
+ // Prefix ids by default so they don't conflict between svgs.
78
+ if (svgoConfig && svgoConfig.prefixIds == null) {
79
+ let hash = (0, _rust().hashString)((0, _utils().relativePath)(options.projectRoot, config.searchPath));
80
+ svgoConfig.prefixIds = {
81
+ prefix: hash.slice(-6),
82
+ prefixClassNames: false
83
+ };
84
+ }
85
+ return {
86
+ svgr,
87
+ svgo: svgoConfig
88
+ };
89
+ },
90
+ async transform({
91
+ asset,
92
+ config
93
+ }) {
94
+ let code = await asset.getBuffer();
95
+ let {
96
+ code: jsx
97
+ } = (0, _rust().svgReact)({
98
+ code,
99
+ config: {
100
+ svgoConfig: config.svgo,
101
+ ...config.svgr
102
+ }
103
+ });
104
+ asset.type = 'jsx';
105
+ asset.bundleBehavior = null;
106
+ asset.setBuffer(jsx);
107
+ return [asset];
108
+ }
109
+ });
110
+ async function createDiagnostic(message, filePath, jsonPath, options) {
111
+ return {
112
+ message: message,
113
+ codeFrames: [{
114
+ filePath: filePath,
115
+ codeHighlights: _path().default.basename(filePath) === '.svgrrc' || _path().default.extname(filePath) === '.json' ? (0, _diagnostic().generateJSONCodeHighlights)(await options.inputFS.readFile(filePath, 'utf8'), [{
116
+ key: jsonPath
117
+ }]) : []
118
+ }],
119
+ hints: ['Use the legacy @parcel/transformer-svg-react instead of @parcel/transformer-svg-jsx if needed.']
120
+ };
121
+ }
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@parcel/transformer-svg-jsx",
3
+ "version": "2.14.5-canary.3441+d0c8ddd99",
4
+ "license": "MIT",
5
+ "publishConfig": {
6
+ "access": "public"
7
+ },
8
+ "funding": {
9
+ "type": "opencollective",
10
+ "url": "https://opencollective.com/parcel"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "https://github.com/parcel-bundler/parcel.git"
15
+ },
16
+ "main": "lib/SvgJsxTransformer.js",
17
+ "source": "src/SvgJsxTransformer.js",
18
+ "engines": {
19
+ "node": ">= 16.0.0",
20
+ "parcel": "^2.0.0-canary.1816+d0c8ddd99"
21
+ },
22
+ "dependencies": {
23
+ "@parcel/diagnostic": "2.0.0-canary.1818+d0c8ddd99",
24
+ "@parcel/plugin": "2.0.0-canary.1818+d0c8ddd99",
25
+ "@parcel/rust": "2.14.5-canary.3441+d0c8ddd99",
26
+ "@parcel/utils": "2.0.0-canary.1818+d0c8ddd99"
27
+ },
28
+ "gitHead": "d0c8ddd99d1937e8e1d13043684a8a10617187a6"
29
+ }
@@ -0,0 +1,128 @@
1
+ // @flow
2
+
3
+ import {Transformer} from '@parcel/plugin';
4
+
5
+ import path from 'path';
6
+ import {convertSVGOConfig, relativePath} from '@parcel/utils';
7
+ import ThrowableDiagnostic, {
8
+ md,
9
+ generateJSONCodeHighlights,
10
+ } from '@parcel/diagnostic';
11
+ import {svgReact, hashString} from '@parcel/rust';
12
+
13
+ export default (new Transformer({
14
+ async loadConfig({config, options}) {
15
+ let svgrResult: any = await config.getConfig([
16
+ '.svgrrc',
17
+ '.svgrrc.json',
18
+ '.svgrrc.js',
19
+ '.svgrrc.cjs',
20
+ '.svgrrc.mjs',
21
+ 'svgr.config.json',
22
+ 'svgr.config.js',
23
+ 'svgr.config.cjs',
24
+ 'svgr.config.mjs',
25
+ ]);
26
+ let svgoResult: any = await config.getConfig([
27
+ 'svgo.config.js',
28
+ 'svgo.config.cjs',
29
+ 'svgo.config.mjs',
30
+ 'svgo.config.json',
31
+ ]);
32
+
33
+ let contents = svgrResult?.contents?.svgoConfig ?? svgoResult?.contents;
34
+ let svgoConfig: any = {default: true};
35
+ if (contents) {
36
+ svgoConfig = await convertSVGOConfig(
37
+ contents,
38
+ svgrResult?.contents?.svgoConfig
39
+ ? svgrResult.filePath
40
+ : svgoResult.filePath,
41
+ svgrResult?.contents?.svgoConfig ? '/svgoConfig' : '',
42
+ options.inputFS,
43
+ 'Use the legacy @parcel/transformer-svg-react instead of @parcel/transformer-svg-jsx if needed.',
44
+ );
45
+ }
46
+
47
+ let svgr = svgrResult?.contents;
48
+ if (svgoResult && svgr?.jsx) {
49
+ throw new ThrowableDiagnostic({
50
+ diagnostic: await createDiagnostic(
51
+ md`
52
+ Unsupported SVGR option "jsx".
53
+ `,
54
+ svgrResult.filePath,
55
+ '/jsx',
56
+ options,
57
+ ),
58
+ });
59
+ }
60
+
61
+ if (svgr?.template) {
62
+ throw new ThrowableDiagnostic({
63
+ diagnostic: await createDiagnostic(
64
+ md`
65
+ Unsupported SVGR option "template".
66
+ `,
67
+ svgrResult.filePath,
68
+ '/template',
69
+ options,
70
+ ),
71
+ });
72
+ }
73
+
74
+ // Prefix ids by default so they don't conflict between svgs.
75
+ if (svgoConfig && svgoConfig.prefixIds == null) {
76
+ let hash = hashString(
77
+ relativePath(options.projectRoot, config.searchPath),
78
+ );
79
+ svgoConfig.prefixIds = {
80
+ prefix: hash.slice(-6),
81
+ prefixClassNames: false,
82
+ };
83
+ }
84
+
85
+ return {svgr, svgo: svgoConfig};
86
+ },
87
+
88
+ async transform({asset, config}) {
89
+ let code = await asset.getBuffer();
90
+
91
+ let {code: jsx} = svgReact({
92
+ code,
93
+ config: {svgoConfig: config.svgo, ...config.svgr},
94
+ });
95
+
96
+ asset.type = 'jsx';
97
+ asset.bundleBehavior = null;
98
+ asset.setBuffer(jsx);
99
+
100
+ return [asset];
101
+ },
102
+ }): Transformer);
103
+
104
+ async function createDiagnostic(message, filePath, jsonPath, options) {
105
+ return {
106
+ message: message,
107
+ codeFrames: [
108
+ {
109
+ filePath: filePath,
110
+ codeHighlights:
111
+ path.basename(filePath) === '.svgrrc' ||
112
+ path.extname(filePath) === '.json'
113
+ ? generateJSONCodeHighlights(
114
+ await options.inputFS.readFile(filePath, 'utf8'),
115
+ [
116
+ {
117
+ key: jsonPath,
118
+ },
119
+ ],
120
+ )
121
+ : [],
122
+ },
123
+ ],
124
+ hints: [
125
+ 'Use the legacy @parcel/transformer-svg-react instead of @parcel/transformer-svg-jsx if needed.',
126
+ ],
127
+ };
128
+ }