@atlaspack/transformer-svg 2.14.5-canary.39 → 2.14.5-canary.391

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/dist/inline.js ADDED
@@ -0,0 +1,124 @@
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
+ exports.default = extractInlineAssets;
7
+ const rust_1 = require("@atlaspack/rust");
8
+ const posthtml_1 = __importDefault(require("posthtml"));
9
+ const SCRIPT_TYPES = {
10
+ 'application/ecmascript': 'js',
11
+ 'application/javascript': 'js',
12
+ 'text/javascript': 'js',
13
+ module: 'js',
14
+ };
15
+ function extractInlineAssets(asset, ast) {
16
+ const program = ast.program;
17
+ let key = 0;
18
+ // Extract <style> elements for processing.
19
+ const parts = [];
20
+ // @ts-expect-error TS2339
21
+ (0, posthtml_1.default)().walk.call(program, (node) => {
22
+ if (node.tag === 'style' || node.tag === 'script') {
23
+ const value = node.content && node.content.join('');
24
+ if (!value) {
25
+ return node;
26
+ }
27
+ let type, env;
28
+ if (node.tag === 'style') {
29
+ if (node.attrs && node.attrs.type != null) {
30
+ type = node.attrs.type.split('/')[1];
31
+ }
32
+ else {
33
+ type = 'css';
34
+ }
35
+ }
36
+ else if (node.tag === 'script') {
37
+ // @ts-expect-error TS7053
38
+ if (node.attrs && SCRIPT_TYPES[node.attrs.type]) {
39
+ // @ts-expect-error TS7053
40
+ type = SCRIPT_TYPES[node.attrs.type];
41
+ }
42
+ else if (node.attrs) {
43
+ type = node.attrs.type.split('/')[1];
44
+ }
45
+ else {
46
+ type = 'js';
47
+ }
48
+ env = {
49
+ sourceType: node.attrs && node.attrs.type === 'module' ? 'module' : 'script',
50
+ // SVG script elements do not support type="module" natively yet.
51
+ outputFormat: 'global',
52
+ loc: node.location
53
+ ? {
54
+ filePath: asset.filePath,
55
+ start: node.location.start,
56
+ end: node.location.end,
57
+ }
58
+ : undefined,
59
+ };
60
+ }
61
+ if (!type) {
62
+ return node;
63
+ }
64
+ let attrs = node.attrs;
65
+ if (!attrs) {
66
+ attrs = {};
67
+ node.attrs = attrs;
68
+ }
69
+ // Inform packager to remove type, since CSS and JS are the defaults.
70
+ delete attrs.type;
71
+ let parcelKey;
72
+ // allow a script/style tag to declare its key
73
+ if (attrs['data-parcel-key']) {
74
+ parcelKey = attrs['data-parcel-key'];
75
+ }
76
+ else {
77
+ parcelKey = (0, rust_1.hashString)(`${asset.id}:${key++}`);
78
+ }
79
+ // insert parcelId to allow us to retrieve node during packaging
80
+ attrs['data-parcel-key'] = parcelKey;
81
+ asset.setAST(ast); // mark dirty
82
+ asset.addDependency({
83
+ specifier: parcelKey,
84
+ specifierType: 'esm',
85
+ });
86
+ parts.push({
87
+ type,
88
+ content: value,
89
+ uniqueKey: parcelKey,
90
+ bundleBehavior: 'inline',
91
+ // @ts-expect-error TS2322
92
+ env,
93
+ meta: {
94
+ type: 'tag',
95
+ node,
96
+ startLine: node.location?.start.line,
97
+ },
98
+ });
99
+ }
100
+ // Process inline style attributes.
101
+ let attrs = node.attrs;
102
+ let style = attrs?.style;
103
+ if (attrs != null && style != null) {
104
+ let parcelKey = (0, rust_1.hashString)(`${asset.id}:${key++}`);
105
+ attrs.style = asset.addDependency({
106
+ specifier: parcelKey,
107
+ specifierType: 'esm',
108
+ });
109
+ asset.setAST(ast); // mark dirty
110
+ parts.push({
111
+ type: 'css',
112
+ content: style,
113
+ uniqueKey: parcelKey,
114
+ bundleBehavior: 'inline',
115
+ meta: {
116
+ type: 'attr',
117
+ node,
118
+ },
119
+ });
120
+ }
121
+ return node;
122
+ });
123
+ return parts;
124
+ }
@@ -81,6 +81,7 @@ var _default = exports.default = new (_plugin().Transformer)({
81
81
  (0, _dependencies.default)(asset, ast);
82
82
  } catch (errors) {
83
83
  throw new (_diagnostic().default)({
84
+ // @ts-expect-error TS7006
84
85
  diagnostic: errors.map(error => ({
85
86
  message: error.message,
86
87
  origin: '@atlaspack/transformer-svg',
@@ -54,7 +54,9 @@ const OPTIONS = {
54
54
  };
55
55
  function collectDependencies(asset, ast) {
56
56
  let isDirty = false;
57
+ // @ts-expect-error TS7034
57
58
  const errors = [];
59
+ // @ts-expect-error TS2339
58
60
  (0, _posthtml().default)().walk.call(ast.program, node => {
59
61
  // Ideally we'd have location information for specific attributes...
60
62
  let getLoc = () => node.location ? {
@@ -82,6 +84,8 @@ function collectDependencies(asset, ast) {
82
84
  if (attrs[attr][0] === '#') {
83
85
  continue;
84
86
  }
87
+
88
+ // @ts-expect-error TS7053
85
89
  const elements = ATTRS[attr];
86
90
  if (elements && elements.includes(node.tag)) {
87
91
  var _OPTIONS$tag;
@@ -93,6 +97,8 @@ function collectDependencies(asset, ast) {
93
97
  loc: node.location
94
98
  });
95
99
  }
100
+
101
+ // @ts-expect-error TS7053
96
102
  let options = (_OPTIONS$tag = OPTIONS[tag]) === null || _OPTIONS$tag === void 0 ? void 0 : _OPTIONS$tag[attr];
97
103
  if (node.tag === 'script') {
98
104
  options = {
@@ -126,6 +132,7 @@ function collectDependencies(asset, ast) {
126
132
  return node;
127
133
  });
128
134
  if (errors.length > 0) {
135
+ // @ts-expect-error TS7005
129
136
  throw errors;
130
137
  }
131
138
  if (isDirty) {
package/lib/inline.js CHANGED
@@ -19,6 +19,8 @@ function _posthtml() {
19
19
  return data;
20
20
  }
21
21
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
22
+ // @ts-expect-error TS2724
23
+
22
24
  const SCRIPT_TYPES = {
23
25
  'application/ecmascript': 'js',
24
26
  'application/javascript': 'js',
@@ -31,6 +33,7 @@ function extractInlineAssets(asset, ast) {
31
33
 
32
34
  // Extract <style> elements for processing.
33
35
  const parts = [];
36
+ // @ts-expect-error TS2339
34
37
  (0, _posthtml().default)().walk.call(program, node => {
35
38
  if (node.tag === 'style' || node.tag === 'script') {
36
39
  var _node$location;
@@ -46,7 +49,9 @@ function extractInlineAssets(asset, ast) {
46
49
  type = 'css';
47
50
  }
48
51
  } else if (node.tag === 'script') {
52
+ // @ts-expect-error TS7053
49
53
  if (node.attrs && SCRIPT_TYPES[node.attrs.type]) {
54
+ // @ts-expect-error TS7053
50
55
  type = SCRIPT_TYPES[node.attrs.type];
51
56
  } else if (node.attrs) {
52
57
  type = node.attrs.type.split('/')[1];
@@ -96,10 +101,10 @@ function extractInlineAssets(asset, ast) {
96
101
  content: value,
97
102
  uniqueKey: parcelKey,
98
103
  bundleBehavior: 'inline',
104
+ // @ts-expect-error TS2322
99
105
  env,
100
106
  meta: {
101
107
  type: 'tag',
102
- // $FlowFixMe
103
108
  node,
104
109
  startLine: (_node$location = node.location) === null || _node$location === void 0 ? void 0 : _node$location.start.line
105
110
  }
@@ -124,7 +129,6 @@ function extractInlineAssets(asset, ast) {
124
129
  bundleBehavior: 'inline',
125
130
  meta: {
126
131
  type: 'attr',
127
- // $FlowFixMe
128
132
  node
129
133
  }
130
134
  });
@@ -0,0 +1,3 @@
1
+ import { Transformer } from '@atlaspack/plugin';
2
+ declare const _default: Transformer<unknown>;
3
+ export default _default;
@@ -0,0 +1,3 @@
1
+ import type { AST, MutableAsset } from '@atlaspack/types-internal';
2
+ export declare function parseFuncIRI(value: string): [string, string] | null | undefined;
3
+ export default function collectDependencies(asset: MutableAsset, ast: AST): void;
@@ -0,0 +1,2 @@
1
+ import type { AST, MutableAsset, TransformerResult } from '@atlaspack/types-internal';
2
+ export default function extractInlineAssets(asset: MutableAsset, ast: AST): Array<TransformerResult>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaspack/transformer-svg",
3
- "version": "2.14.5-canary.39+42a775de8",
3
+ "version": "2.14.5-canary.391+053e3754c",
4
4
  "license": "(MIT OR Apache-2.0)",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -9,15 +9,16 @@
9
9
  "type": "git",
10
10
  "url": "https://github.com/atlassian-labs/atlaspack.git"
11
11
  },
12
- "main": "lib/SVGTransformer.js",
13
- "source": "src/SVGTransformer.js",
12
+ "main": "./lib/SVGTransformer.js",
13
+ "source": "./src/SVGTransformer.ts",
14
+ "types": "./lib/types/SVGTransformer.d.ts",
14
15
  "engines": {
15
16
  "node": ">= 16.0.0"
16
17
  },
17
18
  "dependencies": {
18
- "@atlaspack/diagnostic": "2.14.1-canary.107+42a775de8",
19
- "@atlaspack/plugin": "2.14.5-canary.39+42a775de8",
20
- "@atlaspack/rust": "3.2.1-canary.39+42a775de8",
19
+ "@atlaspack/diagnostic": "2.14.1-canary.459+053e3754c",
20
+ "@atlaspack/plugin": "2.14.5-canary.391+053e3754c",
21
+ "@atlaspack/rust": "3.2.1-canary.391+053e3754c",
21
22
  "nullthrows": "^1.1.1",
22
23
  "posthtml": "^0.16.5",
23
24
  "posthtml-parser": "^0.10.1",
@@ -25,5 +26,11 @@
25
26
  "semver": "^7.5.2"
26
27
  },
27
28
  "type": "commonjs",
28
- "gitHead": "42a775de8eec638ad188f3271964170d8c04d84b"
29
- }
29
+ "scripts": {
30
+ "build:lib": "gulp build --gulpfile ../../../gulpfile.js --cwd ."
31
+ },
32
+ "devDependencies": {
33
+ "@types/semver": "^7.7.1"
34
+ },
35
+ "gitHead": "053e3754c08c5b5dc239e9f7267df289cbbf31ab"
36
+ }
@@ -1,5 +1,3 @@
1
- // @flow
2
-
3
1
  import {Transformer} from '@atlaspack/plugin';
4
2
  import nullthrows from 'nullthrows';
5
3
  import semver from 'semver';
@@ -9,7 +7,7 @@ import collectDependencies from './dependencies';
9
7
  import extractInlineAssets from './inline';
10
8
  import ThrowableDiagnostic from '@atlaspack/diagnostic';
11
9
 
12
- export default (new Transformer({
10
+ export default new Transformer({
13
11
  canReuseAST({ast}) {
14
12
  return ast.type === 'posthtml' && semver.satisfies(ast.version, '^0.4.0');
15
13
  },
@@ -39,8 +37,9 @@ export default (new Transformer({
39
37
 
40
38
  try {
41
39
  collectDependencies(asset, ast);
42
- } catch (errors) {
40
+ } catch (errors: any) {
43
41
  throw new ThrowableDiagnostic({
42
+ // @ts-expect-error TS7006
44
43
  diagnostic: errors.map((error) => ({
45
44
  message: error.message,
46
45
  origin: '@atlaspack/transformer-svg',
@@ -65,4 +64,4 @@ export default (new Transformer({
65
64
  content: render(ast.program),
66
65
  };
67
66
  },
68
- }): Transformer);
67
+ }) as Transformer<unknown>;
@@ -1,6 +1,4 @@
1
- // @flow
2
-
3
- import type {AST, MutableAsset} from '@atlaspack/types';
1
+ import type {AST, MutableAsset} from '@atlaspack/types-internal';
4
2
  import PostHTML from 'posthtml';
5
3
 
6
4
  // A list of all attributes that may produce a dependency
@@ -31,7 +29,7 @@ const ATTRS = {
31
29
  'tref',
32
30
  'color-profile',
33
31
  ],
34
- };
32
+ } as const;
35
33
 
36
34
  // Attributes that allow url() to reference another element, either in the same document or a different one.
37
35
  // https://www.w3.org/TR/SVG11/linking.html#processingIRI
@@ -58,7 +56,9 @@ const FUNC_IRI_ATTRS = new Set([
58
56
  const FUNC_IRI_RE =
59
57
  /^url\((?:((['"])(.*?)\2(\s+.*)?)|((?:\\[\s'"]|[^\s'"])+))\)$/;
60
58
  const ESCAPE_RE = /\\(.|\n|\r|\u2028|\u2029)/;
61
- export function parseFuncIRI(value: string): ?[string, string] {
59
+ export function parseFuncIRI(
60
+ value: string,
61
+ ): [string, string] | null | undefined {
62
62
  let m = value.match(FUNC_IRI_RE);
63
63
  if (m) {
64
64
  let url = (m[3] || m[5]).replace(ESCAPE_RE, '$1');
@@ -73,11 +73,13 @@ const OPTIONS = {
73
73
  href: {needsStableName: true},
74
74
  'xlink:href': {needsStableName: true},
75
75
  },
76
- };
76
+ } as const;
77
77
 
78
78
  export default function collectDependencies(asset: MutableAsset, ast: AST) {
79
79
  let isDirty = false;
80
+ // @ts-expect-error TS7034
80
81
  const errors = [];
82
+ // @ts-expect-error TS2339
81
83
  PostHTML().walk.call(ast.program, (node) => {
82
84
  // Ideally we'd have location information for specific attributes...
83
85
  let getLoc = () =>
@@ -107,6 +109,7 @@ export default function collectDependencies(asset: MutableAsset, ast: AST) {
107
109
  continue;
108
110
  }
109
111
 
112
+ // @ts-expect-error TS7053
110
113
  const elements = ATTRS[attr];
111
114
  if (elements && elements.includes(node.tag)) {
112
115
  // Check for empty string
@@ -118,6 +121,7 @@ export default function collectDependencies(asset: MutableAsset, ast: AST) {
118
121
  });
119
122
  }
120
123
 
124
+ // @ts-expect-error TS7053
121
125
  let options = OPTIONS[tag]?.[attr];
122
126
  if (node.tag === 'script') {
123
127
  options = {
@@ -154,6 +158,7 @@ export default function collectDependencies(asset: MutableAsset, ast: AST) {
154
158
  });
155
159
 
156
160
  if (errors.length > 0) {
161
+ // @ts-expect-error TS7005
157
162
  throw errors;
158
163
  }
159
164
 
@@ -1,7 +1,10 @@
1
- // @flow
2
-
3
- import type {AST, MutableAsset, TransformerResult} from '@atlaspack/types';
1
+ import type {
2
+ AST,
3
+ MutableAsset,
4
+ TransformerResult,
5
+ } from '@atlaspack/types-internal';
4
6
  import {hashString} from '@atlaspack/rust';
7
+ // @ts-expect-error TS2724
5
8
  import type {PostHTMLNode} from 'posthtml';
6
9
 
7
10
  import PostHTML from 'posthtml';
@@ -11,7 +14,7 @@ const SCRIPT_TYPES = {
11
14
  'application/javascript': 'js',
12
15
  'text/javascript': 'js',
13
16
  module: 'js',
14
- };
17
+ } as const;
15
18
 
16
19
  export default function extractInlineAssets(
17
20
  asset: MutableAsset,
@@ -22,6 +25,7 @@ export default function extractInlineAssets(
22
25
 
23
26
  // Extract <style> elements for processing.
24
27
  const parts: Array<TransformerResult> = [];
28
+ // @ts-expect-error TS2339
25
29
  PostHTML().walk.call(program, (node: PostHTMLNode) => {
26
30
  if (node.tag === 'style' || node.tag === 'script') {
27
31
  const value = node.content && node.content.join('');
@@ -37,7 +41,9 @@ export default function extractInlineAssets(
37
41
  type = 'css';
38
42
  }
39
43
  } else if (node.tag === 'script') {
44
+ // @ts-expect-error TS7053
40
45
  if (node.attrs && SCRIPT_TYPES[node.attrs.type]) {
46
+ // @ts-expect-error TS7053
41
47
  type = SCRIPT_TYPES[node.attrs.type];
42
48
  } else if (node.attrs) {
43
49
  type = node.attrs.type.split('/')[1];
@@ -95,10 +101,10 @@ export default function extractInlineAssets(
95
101
  content: value,
96
102
  uniqueKey: parcelKey,
97
103
  bundleBehavior: 'inline',
104
+ // @ts-expect-error TS2322
98
105
  env,
99
106
  meta: {
100
107
  type: 'tag',
101
- // $FlowFixMe
102
108
  node,
103
109
  startLine: node.location?.start.line,
104
110
  },
@@ -123,7 +129,6 @@ export default function extractInlineAssets(
123
129
  bundleBehavior: 'inline',
124
130
  meta: {
125
131
  type: 'attr',
126
- // $FlowFixMe
127
132
  node,
128
133
  },
129
134
  });
@@ -1,4 +1,3 @@
1
- // @flow
2
1
  import {parseFuncIRI} from '../src/dependencies';
3
2
  import assert from 'assert';
4
3
 
package/tsconfig.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "extends": "../../../tsconfig.base.json",
3
+ "include": ["src"],
4
+ "compilerOptions": {
5
+ "composite": true
6
+ },
7
+ "references": [
8
+ {
9
+ "path": "../../core/diagnostic/tsconfig.json"
10
+ },
11
+ {
12
+ "path": "../../core/plugin/tsconfig.json"
13
+ },
14
+ {
15
+ "path": "../../core/rust/tsconfig.json"
16
+ }
17
+ ]
18
+ }