@motifx/compiler 1.2.0-rc.1 → 1.2.0-rc.3

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 CHANGED
@@ -18,11 +18,10 @@ import compiler from '@motifx/compiler';
18
18
 
19
19
  export default defineConfig({
20
20
  plugins: [compiler()],
21
- esbuild: { jsx: 'preserve' },
22
21
  });
23
22
  ```
24
23
 
25
- Set `"jsx": "preserve"` in `tsconfig.json` as well, so that only `@motifx/compiler` compiles JSX. Vitest picks up the same plugin from the Vite config.
24
+ The plugin runs before Vite's own transform (`enforce: 'pre'`) and leaves no JSX behind, so Vite needs no JSX setting. Set `"jsx": "preserve"` in `tsconfig.json` so that TypeScript type-checks JSX without compiling it. Vitest picks up the same plugin from the Vite config.
26
25
 
27
26
  ## Rollup
28
27
 
package/dist/index.cjs CHANGED
@@ -457,7 +457,7 @@ const motifComponent = () => { return "_mc"; };
457
457
  const motifFragment = () => { return "_mf"; };
458
458
  const motifFunctionComponent = () => { return "_mfc"; };
459
459
  const motifCompiled = () => { return "_mv"; };
460
- const COMPILER_CONTRACT = 1;
460
+ const COMPILER_CONTRACT = 2;
461
461
 
462
462
  let active$1 = null;
463
463
  let seen = null;
@@ -2954,16 +2954,63 @@ function injectDeclaredElementTag(path) {
2954
2954
  }
2955
2955
  catch { /* enjeksiyon başarısızsa derleme bozulmasın */ }
2956
2956
  }
2957
- const elementTagPlugin = () => ({
2958
- name: 'motifjs-element-tag',
2959
- visitor: {
2960
- Class: {
2961
- enter(path) {
2962
- injectDeclaredElementTag(path);
2957
+ const isThisChilds = (e) => !!e && t__namespace.isMemberExpression(e) && !e.computed && t__namespace.isThisExpression(e.object)
2958
+ && t__namespace.isIdentifier(e.property) && e.property.name === 'childs';
2959
+ function placesChilds(path) {
2960
+ let found = false;
2961
+ path.traverse({
2962
+ Class(inner) { inner.skip(); },
2963
+ FunctionDeclaration(inner) { inner.skip(); },
2964
+ FunctionExpression(inner) { inner.skip(); },
2965
+ JSXExpressionContainer(slot) {
2966
+ if (!t__namespace.isJSXElement(slot.parent) && !t__namespace.isJSXFragment(slot.parent))
2967
+ return;
2968
+ const e = slot.node.expression;
2969
+ const value = t__namespace.isArrowFunctionExpression(e) && !t__namespace.isBlockStatement(e.body) ? e.body : e;
2970
+ if (isThisChilds(value)) {
2971
+ found = true;
2972
+ slot.stop();
2963
2973
  }
2964
2974
  }
2975
+ });
2976
+ return found;
2977
+ }
2978
+ function injectChildsSlotMarker(path) {
2979
+ try {
2980
+ const node = path.node;
2981
+ if (!node.superClass || hasStaticMember(node, '_placesChilds'))
2982
+ return;
2983
+ if (!placesChilds(path))
2984
+ return;
2985
+ node.body.body.unshift(t__namespace.classProperty(t__namespace.identifier('_placesChilds'), t__namespace.booleanLiteral(true), null, null, false, true));
2965
2986
  }
2966
- });
2987
+ catch { }
2988
+ }
2989
+ const isTypeOnlyField = (member) => t__namespace.isClassProperty(member) && (!!member.declare || !!member.abstract);
2990
+ function stripTypeOnlyFields(path) {
2991
+ const body = path.node.body.body;
2992
+ if (!body.some(isTypeOnlyField))
2993
+ return;
2994
+ path.node.body.body = body.filter(member => !isTypeOnlyField(member));
2995
+ }
2996
+ const elementTagPlugin = () => {
2997
+ const visited = new WeakSet();
2998
+ return {
2999
+ name: 'motifjs-element-tag',
3000
+ visitor: {
3001
+ Class: {
3002
+ enter(path) {
3003
+ if (visited.has(path.node))
3004
+ return;
3005
+ visited.add(path.node);
3006
+ stripTypeOnlyFields(path);
3007
+ injectDeclaredElementTag(path);
3008
+ injectChildsSlotMarker(path);
3009
+ }
3010
+ }
3011
+ }
3012
+ };
3013
+ };
2967
3014
  class Compiler {
2968
3015
  motifCompile(code, filename) {
2969
3016
  return this.start(code, filename);
@@ -3134,6 +3181,22 @@ class Compiler {
3134
3181
  path.node.body = [...setOriginal, cls];
3135
3182
  }
3136
3183
  state.set('filename', filename);
3184
+ path.traverse({
3185
+ CallExpression(call) {
3186
+ const node = call.node;
3187
+ if (!t__namespace.isIdentifier(node.callee, { name: motifComponent() }) || node.arguments.length !== 2)
3188
+ return;
3189
+ const [tag, props] = node.arguments;
3190
+ if (!t__namespace.isStringLiteral(tag) || !t__namespace.isObjectExpression(props) || props.properties.length !== 1)
3191
+ return;
3192
+ const only = props.properties[0];
3193
+ if (!t__namespace.isObjectProperty(only) || only.computed || !t__namespace.isIdentifier(only.key, { name: 'initializeComponent' }))
3194
+ return;
3195
+ if (!t__namespace.isArrowFunctionExpression(only.value) && !t__namespace.isFunctionExpression(only.value))
3196
+ return;
3197
+ node.arguments[1] = only.value;
3198
+ }
3199
+ });
3137
3200
  absorbGeneratedCoreImports(topLevel, coreImports);
3138
3201
  if (coreImports.size > 0) {
3139
3202
  const contractCall = t__namespace.callExpression(t__namespace.identifier(motifCompiled()), [t__namespace.numericLiteral(COMPILER_CONTRACT)]);