unframer 2.7.5 → 2.7.7

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.
Files changed (50) hide show
  1. package/README.md +0 -5
  2. package/dist/cli.d.ts +14 -0
  3. package/dist/cli.d.ts.map +1 -1
  4. package/dist/cli.js +7 -31
  5. package/dist/cli.js.map +1 -1
  6. package/dist/esbuild.d.ts +7 -0
  7. package/dist/esbuild.d.ts.map +1 -1
  8. package/dist/esbuild.js +15 -1
  9. package/dist/esbuild.js.map +1 -1
  10. package/dist/exporter.d.ts +7 -14
  11. package/dist/exporter.d.ts.map +1 -1
  12. package/dist/exporter.js +52 -14
  13. package/dist/exporter.js.map +1 -1
  14. package/dist/exporter.test.d.ts +2 -0
  15. package/dist/exporter.test.d.ts.map +1 -0
  16. package/dist/exporter.test.js +83 -0
  17. package/dist/exporter.test.js.map +1 -0
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js.map +1 -1
  20. package/dist/unframer-loader.d.ts.map +1 -1
  21. package/dist/unframer-loader.js +4 -3
  22. package/dist/unframer-loader.js.map +1 -1
  23. package/esm/cli.d.ts +14 -0
  24. package/esm/cli.d.ts.map +1 -1
  25. package/esm/cli.js +7 -31
  26. package/esm/cli.js.map +1 -1
  27. package/esm/esbuild.d.ts +7 -0
  28. package/esm/esbuild.d.ts.map +1 -1
  29. package/esm/esbuild.js +13 -0
  30. package/esm/esbuild.js.map +1 -1
  31. package/esm/exporter.d.ts +7 -14
  32. package/esm/exporter.d.ts.map +1 -1
  33. package/esm/exporter.js +52 -15
  34. package/esm/exporter.js.map +1 -1
  35. package/esm/exporter.test.d.ts +2 -0
  36. package/esm/exporter.test.d.ts.map +1 -0
  37. package/esm/exporter.test.js +81 -0
  38. package/esm/exporter.test.js.map +1 -0
  39. package/esm/index.d.ts.map +1 -1
  40. package/esm/index.js.map +1 -1
  41. package/esm/unframer-loader.d.ts.map +1 -1
  42. package/esm/unframer-loader.js +4 -3
  43. package/esm/unframer-loader.js.map +1 -1
  44. package/package.json +2 -1
  45. package/src/cli.tsx +9 -40
  46. package/src/esbuild.ts +24 -2
  47. package/src/exporter.test.ts +116 -0
  48. package/src/exporter.ts +59 -20
  49. package/src/index.ts +2 -0
  50. package/src/unframer-loader.ts +7 -3
package/src/exporter.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { BuildResult, context } from 'esbuild'
2
+ import { Config } from './cli'
2
3
  import url from 'url'
3
4
 
4
5
  import { Sema } from 'async-sema'
@@ -21,6 +22,7 @@ import {
21
22
  import {
22
23
  esbuildPluginBundleDependencies,
23
24
  externalPackages,
25
+ replaceWebPageIds,
24
26
  resolveRedirect,
25
27
  } from './esbuild'
26
28
  import {
@@ -47,13 +49,17 @@ export type StyleToken = {
47
49
  }
48
50
 
49
51
  export async function bundle({
52
+ config,
50
53
  cwd: out = '',
51
54
  watch = false,
52
- components = {} as Record<string, string>,
53
- tokens = [] as StyleToken[],
54
- breakpoints = {} as BreakpointSizes,
55
55
  signal = undefined as AbortSignal | undefined,
56
+ }: {
57
+ config: Config
58
+ cwd: string
59
+ watch?: boolean
60
+ signal?: AbortSignal
56
61
  }) {
62
+ const { components, breakpoints, tokens, outDir, framerWebPages } = config
57
63
  out ||= path.resolve(process.cwd(), 'example')
58
64
  out = path.resolve(out)
59
65
  try {
@@ -156,6 +162,12 @@ export async function bundle({
156
162
  trailingCommas: 'always',
157
163
  semiColons: 'always',
158
164
  })
165
+ if (framerWebPages?.length) {
166
+ codeNew = replaceWebPageIds({
167
+ code: codeNew,
168
+ elements: framerWebPages,
169
+ })
170
+ }
159
171
  const lines = findRelativeLinks(codeNew)
160
172
  if (lines.length) {
161
173
  spinner.error(
@@ -236,7 +248,7 @@ export async function bundle({
236
248
  '/* This file was generated by Unframer, do not edit manually */\n' +
237
249
  '/* This css file has all the necessary styles to run all your components */\n' +
238
250
  '\n' +
239
- getStyleTokensCss(tokens) +
251
+ getStyleTokensCss(tokens || []) +
240
252
  breakpointsStyles(breakpoints) +
241
253
  '\n\n' +
242
254
  combinedCSSRules
@@ -317,11 +329,17 @@ export async function bundle({
317
329
  console.log()
318
330
  console.log()
319
331
 
320
- let exampleComponent = result?.components?.find((x) => {
332
+ let withBreakpoints = result?.components?.filter((x) => {
321
333
  if (!x.propertyControls) return false
322
334
  const variants = getVariantsFromPropControls(x.propertyControls)
323
335
  return variants?.breakpoints.length >= 2
324
336
  })
337
+ withBreakpoints = withBreakpoints?.sort((a, b) => {
338
+ const aProp = findExampleProperty(a.propertyControls)
339
+ const bProp = findExampleProperty(b.propertyControls)
340
+ return (bProp ? 1 : 0) - (aProp ? 1 : 0)
341
+ })
342
+ let exampleComponent = withBreakpoints?.[0]
325
343
  if (!exampleComponent) {
326
344
  logger.log(
327
345
  `No example component found with breakpoints, using random example`,
@@ -507,10 +525,6 @@ export function getStyleTokensCss(
507
525
  )
508
526
  }
509
527
 
510
- function decapitalize(str: string) {
511
- return str.charAt(0).toLowerCase() + str.slice(1)
512
- }
513
-
514
528
  export function findRelativeLinks(text: string) {
515
529
  const regex = /webPageId:\s+/g
516
530
  const lines = text.split('\n')
@@ -661,10 +675,6 @@ function getTokensCss({
661
675
 
662
676
  const nodePath = process.argv[0] || 'node'
663
677
 
664
- let UNFRAMER_RUNTIME_PATH = url.pathToFileURL(
665
- require.resolve('../esm/index.js'),
666
- ).href
667
-
668
678
  export async function extractPropControlsUnsafe(
669
679
  filename,
670
680
  name,
@@ -683,6 +693,11 @@ export async function extractPropControlsUnsafe(
683
693
  )}); console.log(${propCode}) })`
684
694
 
685
695
  const TIMEOUT = 5 * 1000
696
+ const UNFRAMER_MAP_PACKAGES = JSON.stringify({
697
+ unframer: url.pathToFileURL(require.resolve('../esm/index.js')).href,
698
+ react: url.pathToFileURL(require.resolve('react')).href,
699
+ 'react-dom': url.pathToFileURL(require.resolve('react-dom')).href,
700
+ })
686
701
  let stdout = await new Promise<string>((res, rej) => {
687
702
  let childProcess = exec(
688
703
  `${JSON.stringify(
@@ -693,14 +708,17 @@ export async function extractPropControlsUnsafe(
693
708
  {
694
709
  env: {
695
710
  // ...process.env,
696
- UNFRAMER_RUNTIME_PATH,
711
+ UNFRAMER_MAP_PACKAGES,
697
712
  },
698
713
  },
699
- (err, stdout) => {
714
+ (err, stdout, stderr) => {
700
715
  clearTimeout(timer)
701
716
  if (err) {
717
+ spinner.error(`error extracting types for ${name}`)
718
+ spinner.error(stderr)
702
719
  return rej(err)
703
720
  }
721
+
704
722
  res(stdout)
705
723
  },
706
724
  )
@@ -714,12 +732,15 @@ export async function extractPropControlsUnsafe(
714
732
  )
715
733
  }, TIMEOUT)
716
734
  }).catch((e) => {
717
- spinner.error(`error extracting types for ${name}`)
718
735
  logger.log(e.stack)
719
- throw e
736
+ return ''
720
737
  })
721
738
 
722
- stdout = stdout.split(delimiter)[1]
739
+ stdout = stdout.split(delimiter)[1] || ''
740
+ if (!stdout) {
741
+ return {}
742
+ }
743
+
723
744
  // console.log(stdout)
724
745
  return safeJsonParse(stdout)
725
746
  }
@@ -788,7 +809,7 @@ export function propControlsToType(controls: PropertyControls, fileName) {
788
809
  return 'Function'
789
810
  }
790
811
  }
791
- let name = decapitalize(value.title || key || '')
812
+ let name = propCamelCase(value.title || key || '')
792
813
  if (!name) {
793
814
  return ''
794
815
  }
@@ -990,5 +1011,23 @@ function findExampleProperty(propertyControls?: PropertyControls) {
990
1011
  return null
991
1012
  }
992
1013
 
993
- return stringProp[0]
1014
+ return propCamelCase(stringProp[1]?.title || '')
1015
+ }
1016
+ export function propCamelCase(str: string) {
1017
+ if (!str) {
1018
+ return ''
1019
+ }
1020
+ // Convert dashes to camelCase (e.g. foo-bar -> fooBar)
1021
+ str = str.replace(/-([\w])/g, (g) => g[1].toUpperCase())
1022
+ // Convert underscores to camelCase (e.g. foo_bar -> fooBar)
1023
+ str = str.replace(/_([a-z])/g, (g) => g[1].toUpperCase())
1024
+ // Remove spaces (e.g. "Foo Bar" -> "fooBar")
1025
+ str = str.replace(/\s+(.)/g, (_, c) => c.toUpperCase())
1026
+ // Ensure first character is lowercase
1027
+ str = str[0].toLowerCase() + str.slice(1)
1028
+ // Add underscore prefix if starts with number
1029
+ if (/^\d/.test(str)) {
1030
+ str = '_' + str
1031
+ }
1032
+ return str
994
1033
  }
package/src/index.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  export * from './framer.js'
2
+
3
+
2
4
  export {
3
5
  FramerStyles,
4
6
  UnframerBreakpoint,
@@ -1,10 +1,14 @@
1
+ const mapPackages = JSON.parse(process.env.UNFRAMER_MAP_PACKAGES || '{}')
2
+
1
3
  export async function resolve(specifier, context, defaultResolve) {
2
- if (specifier === 'unframer') {
4
+ if (mapPackages[specifier]) {
3
5
  return {
4
- url: process.env.UNFRAMER_RUNTIME_PATH,
5
- format: 'module', // Specify that unframer is an ES module
6
+ url: mapPackages[specifier],
7
+ // format: 'module', // Specify that unframer is an ES module
6
8
  shortCircuit: true, // Signal that we're intentionally not calling next hook
7
9
  }
8
10
  }
11
+
12
+
9
13
  return defaultResolve(specifier, context, defaultResolve)
10
14
  }