@atlaspack/core 2.12.1-canary.3418 → 2.12.1-canary.3420

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.
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.AssetCompat = void 0;
7
+ var _Environment = _interopRequireDefault(require("../../public/Environment"));
8
+ var _types = require("../../types");
9
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
+ class AssetCompat {
11
+ // TODO: Type properly
12
+
13
+ constructor(_inner, options) {
14
+ this._inner = _inner;
15
+ // $FlowFixMe This isn't correct to flow but is enough to satisfy the runtime checks
16
+ this.env = new _Environment.default(_inner.env, options);
17
+ this._dependencies = [];
18
+ }
19
+ get id() {
20
+ return this._inner.id;
21
+ }
22
+ get filePath() {
23
+ return this._inner.filePath;
24
+ }
25
+ get type() {
26
+ return this._inner.type;
27
+ }
28
+ set type(value) {
29
+ this._inner.type = value;
30
+ }
31
+ get bundleBehavior() {
32
+ let bundleBehavior = this._inner.bundleBehavior;
33
+ return bundleBehavior == null ? null : _types.BundleBehaviorNames[bundleBehavior];
34
+ }
35
+ set bundleBehavior(bundleBehavior) {
36
+ this._inner.bundleBehavior = bundleBehavior ? _types.BundleBehavior[bundleBehavior] : null;
37
+ }
38
+ getCode() {
39
+ return Buffer.from(this._inner.code).toString();
40
+ }
41
+ setCode(code) {
42
+ this._inner.code = Array.from(new TextEncoder().encode(code));
43
+ }
44
+ getAST() {
45
+ return this._ast;
46
+ }
47
+ setAST(ast) {
48
+ this._ast = ast;
49
+ }
50
+ addDependency() {
51
+ throw new Error('[V3] Unimplemented: Asset.addDependency not yet implemented');
52
+ }
53
+ addURLDependency() {
54
+ throw new Error('[V3] Unimplemented: Asset.addURLDependency not yet implemented');
55
+ }
56
+ }
57
+ exports.AssetCompat = AssetCompat;
@@ -4,6 +4,13 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.AtlaspackWorker = void 0;
7
+ function _assert() {
8
+ const data = _interopRequireDefault(require("assert"));
9
+ _assert = function () {
10
+ return data;
11
+ };
12
+ return data;
13
+ }
7
14
  function napi() {
8
15
  const data = _interopRequireWildcard(require("@atlaspack/rust"));
9
16
  napi = function () {
@@ -25,22 +32,68 @@ function _module() {
25
32
  };
26
33
  return data;
27
34
  }
35
+ var _compat = require("./compat");
28
36
  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); }
29
37
  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 && Object.prototype.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; }
38
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
39
+ const CONFIG = Symbol.for('parcel-plugin-config');
30
40
  class AtlaspackWorker {
31
- #transformers;
32
- constructor() {
33
- this.#transformers = new Map();
34
- }
35
41
  ping() {
36
42
  // console.log('Hi');
37
43
  }
38
- async transformerRegister(resolve_from, specifier) {
39
- let customRequire = _module().createRequire(resolve_from);
44
+ async runTransformer({
45
+ resolveFrom,
46
+ specifier,
47
+ options,
48
+ asset
49
+ }) {
50
+ let customRequire = _module().createRequire(resolveFrom);
40
51
  let resolvedPath = customRequire.resolve(specifier);
41
52
  // $FlowFixMe
42
- let transformer = await import(resolvedPath);
43
- this.#transformers.set(`${resolve_from}:${specifier}`, transformer);
53
+ let transformerModule = await import(resolvedPath);
54
+ let transformer = transformerModule.default.default[CONFIG];
55
+ let assetCompat = new _compat.AssetCompat(asset, options);
56
+ try {
57
+ if (transformer.parse) {
58
+ // $FlowFixMe
59
+ let ast = await transformer.parse({
60
+ asset: assetCompat
61
+ });
62
+ // $FlowFixMe
63
+ assetCompat.setAST(ast);
64
+ }
65
+
66
+ // $FlowFixMe
67
+ let result = await transformer.transform({
68
+ // $FlowFixMe
69
+ asset: assetCompat,
70
+ options,
71
+ config: null
72
+ });
73
+ if (transformer.generate) {
74
+ // $FlowFixMe
75
+ let output = await transformer.generate({
76
+ // $FlowFixMe
77
+ asset: assetCompat,
78
+ // $FlowFixMe
79
+ ast: assetCompat.getAST()
80
+ });
81
+ // $FlowFixMe
82
+ assetCompat.setCode(output.content);
83
+ }
84
+ (0, _assert().default)(result.length === 1, '[V3] Unimplemented: Multiple asset return from Node transformer');
85
+ (0, _assert().default)(result[0] === assetCompat, '[V3] Unimplemented: New asset returned from Node transformer');
86
+ return {
87
+ asset,
88
+ dependencies: assetCompat._dependencies
89
+ };
90
+ } catch (e) {
91
+ // TODO: Improve error logging from JS plugins. Without this you currently
92
+ // only see the error message, no stack trace.
93
+ // eslint-disable-next-line no-console
94
+ console.error(e);
95
+ throw e;
96
+ }
44
97
  }
45
98
  }
46
99
  exports.AtlaspackWorker = AtlaspackWorker;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaspack/core",
3
- "version": "2.12.1-canary.3418+49a56fea2",
3
+ "version": "2.12.1-canary.3420+5b4f23756",
4
4
  "license": "(MIT OR Apache-2.0)",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -20,20 +20,20 @@
20
20
  "check-ts": "tsc --noEmit index.d.ts"
21
21
  },
22
22
  "dependencies": {
23
- "@atlaspack/cache": "2.12.1-canary.3418+49a56fea2",
24
- "@atlaspack/diagnostic": "2.12.1-canary.3418+49a56fea2",
25
- "@atlaspack/events": "2.12.1-canary.3418+49a56fea2",
26
- "@atlaspack/feature-flags": "2.12.1-canary.3418+49a56fea2",
27
- "@atlaspack/fs": "2.12.1-canary.3418+49a56fea2",
28
- "@atlaspack/graph": "3.2.1-canary.3418+49a56fea2",
29
- "@atlaspack/logger": "2.12.1-canary.3418+49a56fea2",
30
- "@atlaspack/package-manager": "2.12.1-canary.3418+49a56fea2",
31
- "@atlaspack/plugin": "2.12.1-canary.3418+49a56fea2",
32
- "@atlaspack/profiler": "2.12.1-canary.3418+49a56fea2",
33
- "@atlaspack/rust": "2.12.1-canary.3418+49a56fea2",
34
- "@atlaspack/types": "2.12.1-canary.3418+49a56fea2",
35
- "@atlaspack/utils": "2.12.1-canary.3418+49a56fea2",
36
- "@atlaspack/workers": "2.12.1-canary.3418+49a56fea2",
23
+ "@atlaspack/cache": "2.12.1-canary.3420+5b4f23756",
24
+ "@atlaspack/diagnostic": "2.12.1-canary.3420+5b4f23756",
25
+ "@atlaspack/events": "2.12.1-canary.3420+5b4f23756",
26
+ "@atlaspack/feature-flags": "2.12.1-canary.3420+5b4f23756",
27
+ "@atlaspack/fs": "2.12.1-canary.3420+5b4f23756",
28
+ "@atlaspack/graph": "3.2.1-canary.3420+5b4f23756",
29
+ "@atlaspack/logger": "2.12.1-canary.3420+5b4f23756",
30
+ "@atlaspack/package-manager": "2.12.1-canary.3420+5b4f23756",
31
+ "@atlaspack/plugin": "2.12.1-canary.3420+5b4f23756",
32
+ "@atlaspack/profiler": "2.12.1-canary.3420+5b4f23756",
33
+ "@atlaspack/rust": "2.12.1-canary.3420+5b4f23756",
34
+ "@atlaspack/types": "2.12.1-canary.3420+5b4f23756",
35
+ "@atlaspack/utils": "2.12.1-canary.3420+5b4f23756",
36
+ "@atlaspack/workers": "2.12.1-canary.3420+5b4f23756",
37
37
  "@mischnic/json-sourcemap": "^0.1.0",
38
38
  "@parcel/source-map": "^2.1.1",
39
39
  "base-x": "^3.0.8",
@@ -55,5 +55,5 @@
55
55
  "browser": {
56
56
  "./src/serializerCore.js": "./src/serializerCore.browser.js"
57
57
  },
58
- "gitHead": "49a56fea20468bd4cb26ecf69f9b3a200986a35b"
58
+ "gitHead": "5b4f237563b0c1b826807a4aa01d78e4d8bb15f4"
59
59
  }
@@ -0,0 +1,82 @@
1
+ // @flow
2
+ import type {PluginOptions, BundleBehavior, AST} from '@atlaspack/types';
3
+
4
+ import Environment from '../../public/Environment';
5
+ import {
6
+ BundleBehaviorNames,
7
+ BundleBehavior as BundleBehaviorMap,
8
+ } from '../../types';
9
+
10
+ export type InnerAsset = {|
11
+ id: string,
12
+ filePath: string,
13
+ code: Array<number>,
14
+ type: string,
15
+ bundleBehavior: number | null,
16
+ env: any,
17
+ |};
18
+
19
+ export class AssetCompat {
20
+ _inner: InnerAsset;
21
+ _ast: ?AST;
22
+ // TODO: Type properly
23
+ _dependencies: Array<any>;
24
+
25
+ constructor(_inner: InnerAsset, options: PluginOptions) {
26
+ this._inner = _inner;
27
+ // $FlowFixMe This isn't correct to flow but is enough to satisfy the runtime checks
28
+ this.env = new Environment(_inner.env, options);
29
+ this._dependencies = [];
30
+ }
31
+
32
+ get id(): string {
33
+ return this._inner.id;
34
+ }
35
+
36
+ get filePath(): string {
37
+ return this._inner.filePath;
38
+ }
39
+
40
+ get type(): string {
41
+ return this._inner.type;
42
+ }
43
+ set type(value: string) {
44
+ this._inner.type = value;
45
+ }
46
+
47
+ get bundleBehavior(): ?BundleBehavior {
48
+ let bundleBehavior = this._inner.bundleBehavior;
49
+ return bundleBehavior == null ? null : BundleBehaviorNames[bundleBehavior];
50
+ }
51
+ set bundleBehavior(bundleBehavior: ?BundleBehavior): void {
52
+ this._inner.bundleBehavior = bundleBehavior
53
+ ? BundleBehaviorMap[bundleBehavior]
54
+ : null;
55
+ }
56
+
57
+ getCode(): string {
58
+ return Buffer.from(this._inner.code).toString();
59
+ }
60
+ setCode(code: string) {
61
+ this._inner.code = Array.from(new TextEncoder().encode(code));
62
+ }
63
+
64
+ getAST(): ?AST {
65
+ return this._ast;
66
+ }
67
+ setAST(ast: AST) {
68
+ this._ast = ast;
69
+ }
70
+
71
+ addDependency(): string {
72
+ throw new Error(
73
+ '[V3] Unimplemented: Asset.addDependency not yet implemented',
74
+ );
75
+ }
76
+
77
+ addURLDependency(): string {
78
+ throw new Error(
79
+ '[V3] Unimplemented: Asset.addURLDependency not yet implemented',
80
+ );
81
+ }
82
+ }
@@ -1,26 +1,87 @@
1
1
  // @flow
2
+ import assert from 'assert';
2
3
  import * as napi from '@atlaspack/rust';
3
- import type {Transformer} from '@atlaspack/types';
4
+ import type {Transformer, PluginOptions} from '@atlaspack/types';
4
5
  import {workerData} from 'worker_threads';
5
6
  import * as module from 'module';
6
7
 
7
- export class AtlaspackWorker {
8
- #transformers: Map<string, Transformer<any>>;
8
+ import {AssetCompat} from './compat';
9
+ import type {InnerAsset} from './compat';
9
10
 
10
- constructor() {
11
- this.#transformers = new Map();
12
- }
11
+ const CONFIG = Symbol.for('parcel-plugin-config');
13
12
 
13
+ export class AtlaspackWorker {
14
14
  ping() {
15
15
  // console.log('Hi');
16
16
  }
17
17
 
18
- async transformerRegister(resolve_from: string, specifier: string) {
19
- let customRequire = module.createRequire(resolve_from);
18
+ async runTransformer({
19
+ resolveFrom,
20
+ specifier,
21
+ options,
22
+ asset,
23
+ }: {|
24
+ resolveFrom: string,
25
+ specifier: string,
26
+ options: PluginOptions,
27
+ asset: InnerAsset,
28
+ |}): any {
29
+ let customRequire = module.createRequire(resolveFrom);
20
30
  let resolvedPath = customRequire.resolve(specifier);
21
31
  // $FlowFixMe
22
- let transformer = await import(resolvedPath);
23
- this.#transformers.set(`${resolve_from}:${specifier}`, transformer);
32
+ let transformerModule = await import(resolvedPath);
33
+ let transformer: Transformer<*> = transformerModule.default.default[CONFIG];
34
+
35
+ let assetCompat = new AssetCompat(asset, options);
36
+
37
+ try {
38
+ if (transformer.parse) {
39
+ // $FlowFixMe
40
+ let ast = await transformer.parse({asset: assetCompat});
41
+ // $FlowFixMe
42
+ assetCompat.setAST(ast);
43
+ }
44
+
45
+ // $FlowFixMe
46
+ let result = await transformer.transform({
47
+ // $FlowFixMe
48
+ asset: assetCompat,
49
+ options,
50
+ config: null,
51
+ });
52
+
53
+ if (transformer.generate) {
54
+ // $FlowFixMe
55
+ let output = await transformer.generate({
56
+ // $FlowFixMe
57
+ asset: assetCompat,
58
+ // $FlowFixMe
59
+ ast: assetCompat.getAST(),
60
+ });
61
+ // $FlowFixMe
62
+ assetCompat.setCode(output.content);
63
+ }
64
+
65
+ assert(
66
+ result.length === 1,
67
+ '[V3] Unimplemented: Multiple asset return from Node transformer',
68
+ );
69
+ assert(
70
+ result[0] === assetCompat,
71
+ '[V3] Unimplemented: New asset returned from Node transformer',
72
+ );
73
+
74
+ return {
75
+ asset,
76
+ dependencies: assetCompat._dependencies,
77
+ };
78
+ } catch (e) {
79
+ // TODO: Improve error logging from JS plugins. Without this you currently
80
+ // only see the error message, no stack trace.
81
+ // eslint-disable-next-line no-console
82
+ console.error(e);
83
+ throw e;
84
+ }
24
85
  }
25
86
  }
26
87