babel-plugin-mapples-sid 0.2.0-beta.9 → 0.2.0
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/README.md +9 -2
- package/dist/index.d.ts +14 -5
- package/dist/index.js +61 -7
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
# babel-plugin-mapples-sid
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
Two jobs for a Mapples-synced Expo app:
|
|
4
|
+
|
|
5
|
+
- **Runs Creator behaviour.** An element carrying Creator sidecars (`$data` bindings, `$actions`,
|
|
6
|
+
`$refs`) is rendered through `MapplesElement` from `@mapples/app`
|
|
7
|
+
(`<Button $actions={…} />` → `<MapplesElement $as={Button} $actions={…} />`), which resolves them
|
|
8
|
+
the way the Creator canvas does — variables, API responses, navigation, set-variable and request
|
|
9
|
+
actions. The source file keeps the attributes the two-way sync reads.
|
|
10
|
+
- **Strips tooling props.** `$sid` and `$fallback` are removed from **production** bundles and kept
|
|
11
|
+
in development (enables tap-element → select-in-Creator).
|
|
5
12
|
|
|
6
13
|
```js
|
|
7
14
|
// babel.config.js
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { PluginObj } from '@babel/core';
|
|
1
|
+
import type { PluginObj, types as BabelTypes } from '@babel/core';
|
|
2
2
|
export interface MapplesSidPluginOptions {
|
|
3
3
|
/**
|
|
4
4
|
* Force strip/keep regardless of NODE_ENV. When absent, the plugin strips
|
|
@@ -6,13 +6,22 @@ export interface MapplesSidPluginOptions {
|
|
|
6
6
|
*/
|
|
7
7
|
strip?: boolean;
|
|
8
8
|
}
|
|
9
|
+
type T = typeof BabelTypes;
|
|
9
10
|
/**
|
|
10
11
|
* babel-plugin-mapples-sid
|
|
11
12
|
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
13
|
+
* 1. Rewrites every element that carries Creator behaviour —
|
|
14
|
+
* `<Button $actions={…} label="Go" />` — into
|
|
15
|
+
* `<MapplesElement $as={Button} $actions={…} label="Go" />`, so the
|
|
16
|
+
* bindings, actions and asset references a synced file declares run in
|
|
17
|
+
* the app. The source file keeps the attributes the two-way sync reads.
|
|
18
|
+
* 2. Removes the tooling attributes (`$sid`, `$fallback`) from production
|
|
19
|
+
* bundles. In development they are kept so runtime tooling (tap element
|
|
20
|
+
* -> select in Creator) can resolve elements back to canvas nodes.
|
|
15
21
|
*/
|
|
16
|
-
export default function mapplesSidPlugin(
|
|
22
|
+
export default function mapplesSidPlugin({ types: t }: {
|
|
23
|
+
types: T;
|
|
24
|
+
}): PluginObj<{
|
|
17
25
|
opts: MapplesSidPluginOptions;
|
|
18
26
|
}>;
|
|
27
|
+
export {};
|
package/dist/index.js
CHANGED
|
@@ -2,21 +2,75 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.default = mapplesSidPlugin;
|
|
4
4
|
/**
|
|
5
|
-
* The Mapples tooling props: `$sid` (canvas node id)
|
|
6
|
-
* size)
|
|
5
|
+
* The Mapples tooling props: `$sid` (canvas node id) and `$fallback` (Custom
|
|
6
|
+
* node size). Tooling only — stripped from production bundles.
|
|
7
7
|
*/
|
|
8
|
-
const TOOLING_PROPS = new Set(['$sid', '$fallback'
|
|
8
|
+
const TOOLING_PROPS = new Set(['$sid', '$fallback']);
|
|
9
|
+
/**
|
|
10
|
+
* The Creator behaviour sidecars — data bindings, actions, asset references.
|
|
11
|
+
* They are the app's behaviour, never stripped: an element carrying one is
|
|
12
|
+
* rendered through `MapplesElement` (@mapples/app), which resolves them the
|
|
13
|
+
* way the canvas does.
|
|
14
|
+
*/
|
|
15
|
+
const BEHAVIOUR_PROPS = new Set(['$data', '$actions', '$refs']);
|
|
16
|
+
/** Where the element wrapper comes from — every exported app depends on it. */
|
|
17
|
+
const RUNTIME_MODULE = '@mapples/app';
|
|
18
|
+
const RUNTIME_EXPORT = 'MapplesElement';
|
|
19
|
+
function tagExpression(t, name) {
|
|
20
|
+
if (t.isJSXIdentifier(name))
|
|
21
|
+
return t.identifier(name.name);
|
|
22
|
+
if (t.isJSXMemberExpression(name)) {
|
|
23
|
+
return t.memberExpression(tagExpression(t, name.object), t.identifier(name.property.name));
|
|
24
|
+
}
|
|
25
|
+
throw new Error(`babel-plugin-mapples-sid: cannot wrap <${name.namespace.name}:${name.name.name}>`);
|
|
26
|
+
}
|
|
27
|
+
function hasBehaviour(t, element) {
|
|
28
|
+
return element.attributes.some((attr) => t.isJSXAttribute(attr) && t.isJSXIdentifier(attr.name) && BEHAVIOUR_PROPS.has(attr.name.name));
|
|
29
|
+
}
|
|
9
30
|
/**
|
|
10
31
|
* babel-plugin-mapples-sid
|
|
11
32
|
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
33
|
+
* 1. Rewrites every element that carries Creator behaviour —
|
|
34
|
+
* `<Button $actions={…} label="Go" />` — into
|
|
35
|
+
* `<MapplesElement $as={Button} $actions={…} label="Go" />`, so the
|
|
36
|
+
* bindings, actions and asset references a synced file declares run in
|
|
37
|
+
* the app. The source file keeps the attributes the two-way sync reads.
|
|
38
|
+
* 2. Removes the tooling attributes (`$sid`, `$fallback`) from production
|
|
39
|
+
* bundles. In development they are kept so runtime tooling (tap element
|
|
40
|
+
* -> select in Creator) can resolve elements back to canvas nodes.
|
|
15
41
|
*/
|
|
16
|
-
function mapplesSidPlugin() {
|
|
42
|
+
function mapplesSidPlugin({ types: t }) {
|
|
17
43
|
return {
|
|
18
44
|
name: 'mapples-sid',
|
|
19
45
|
visitor: {
|
|
46
|
+
Program: {
|
|
47
|
+
// Rewritten on entry, before any other plugin (the JSX transform,
|
|
48
|
+
// the module transform) sees the file, so the added import is an
|
|
49
|
+
// ordinary one to them.
|
|
50
|
+
enter(program) {
|
|
51
|
+
let local = null;
|
|
52
|
+
program.traverse({
|
|
53
|
+
JSXElement(path) {
|
|
54
|
+
const opening = path.node.openingElement;
|
|
55
|
+
if (!hasBehaviour(t, opening))
|
|
56
|
+
return;
|
|
57
|
+
// Intrinsic (lowercase) tags have no component to hand over.
|
|
58
|
+
if (t.isJSXIdentifier(opening.name) && /^[a-z]/.test(opening.name.name))
|
|
59
|
+
return;
|
|
60
|
+
local ??= program.scope.generateUidIdentifier(RUNTIME_EXPORT);
|
|
61
|
+
const as = t.jsxAttribute(t.jsxIdentifier('$as'), t.jsxExpressionContainer(tagExpression(t, opening.name)));
|
|
62
|
+
opening.name = t.jsxIdentifier(local.name);
|
|
63
|
+
opening.attributes.unshift(as);
|
|
64
|
+
if (path.node.closingElement) {
|
|
65
|
+
path.node.closingElement.name = t.jsxIdentifier(local.name);
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
if (!local)
|
|
70
|
+
return;
|
|
71
|
+
program.unshiftContainer('body', t.importDeclaration([t.importSpecifier(local, t.identifier(RUNTIME_EXPORT))], t.stringLiteral(RUNTIME_MODULE)));
|
|
72
|
+
},
|
|
73
|
+
},
|
|
20
74
|
JSXAttribute(path, state) {
|
|
21
75
|
const shouldStrip = state.opts.strip ?? process.env.NODE_ENV === 'production';
|
|
22
76
|
if (!shouldStrip)
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AA2DA,mCAkDC;AAnGD;;;GAGG;AACH,MAAM,aAAa,GAAG,IAAI,GAAG,CAAC,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC;AAErD;;;;;GAKG;AACH,MAAM,eAAe,GAAG,IAAI,GAAG,CAAC,CAAC,OAAO,EAAE,UAAU,EAAE,OAAO,CAAC,CAAC,CAAC;AAEhE,+EAA+E;AAC/E,MAAM,cAAc,GA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