@startupjs/babel-plugin-startupjs 0.56.0-alpha.0 → 0.57.0-canary.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.
Files changed (3) hide show
  1. package/index.js +58 -62
  2. package/package.json +3 -3
  3. package/moduleMap.json +0 -99
package/index.js CHANGED
@@ -1,77 +1,73 @@
1
- const moduleMap = require('./moduleMap.json')
1
+ const { readFileSync } = require('fs')
2
+ const { join } = require('path')
2
3
 
3
- function getDistLocation (moduleName, importName) {
4
- if (!(moduleMap[moduleName] && moduleMap[moduleName][importName])) {
5
- console.warn('>>>>>>\n>>>>>\n>>>>>>\n>>>>>> NO MODULE FOUND', moduleName, importName)
6
- return
7
- }
8
- return moduleName + moduleMap[moduleName][importName]
9
- }
4
+ const MAGIC_MODULE_NAME = '@startupjs/ui'
10
5
 
11
- const canProcess = ({ source, specifiers }) =>
12
- source && moduleMap[source.value] && specifiers.length
6
+ module.exports = function ({ template, types: t }) {
7
+ const buildImport = template('import %%name%% from %%source%%')
8
+ const buildExport = template('export { default as %%name%% } from %%source%%')
13
9
 
14
- module.exports = function ({ types: t }) {
15
10
  return {
16
11
  name: 'Unwrap imports for tree shaking.',
17
12
  visitor: {
18
- ImportDeclaration (path, state) {
19
- const { specifiers } = path.node
20
- if (canProcess(path.node)) {
21
- const moduleName = path.node.source.value
22
- const transformed = specifiers
23
- .map(specifier => {
24
- if (t.isImportSpecifier(specifier)) {
25
- const importName = specifier.imported.name
26
- const distLocation = getDistLocation(moduleName, importName)
27
-
28
- if (distLocation) {
29
- return t.importDeclaration(
30
- [t.importDefaultSpecifier(t.identifier(specifier.local.name))],
31
- t.stringLiteral(distLocation)
32
- )
33
- }
13
+ ImportDeclaration ($this) {
14
+ if ($this.get('source').node.value !== MAGIC_MODULE_NAME) return
15
+ let transformed = false // needed to prevent infinite loops
16
+ const theImports = $this.get('specifiers')
17
+ .map($specifier => {
18
+ // pluck out into a separate import
19
+ if ($specifier.isImportSpecifier()) {
20
+ transformed = true
21
+ const originalName = $specifier.get('imported').node.name
22
+ if (!checkNamedExportExists(originalName)) {
23
+ throw $specifier.buildCodeFrameError(
24
+ `Named export "${originalName}" does not exist in "${MAGIC_MODULE_NAME}" "exports" field in package.json`
25
+ )
34
26
  }
35
- return t.importDeclaration(
36
- [specifier],
37
- t.stringLiteral(moduleName + '/index')
38
- )
39
- })
40
- .filter(Boolean)
41
-
42
- path.replaceWithMultiple(transformed)
43
- }
27
+ const importedName = $specifier.get('local').node.name
28
+ return buildImport({
29
+ name: importedName,
30
+ source: `${MAGIC_MODULE_NAME}/${originalName}`
31
+ })
32
+ }
33
+ // pass as is
34
+ return t.importDeclaration([$specifier.node], t.stringLiteral(MAGIC_MODULE_NAME))
35
+ })
36
+ if (!transformed) return
37
+ $this.replaceWithMultiple(theImports)
44
38
  },
45
- ExportNamedDeclaration (path, state) {
46
- const { specifiers } = path.node
47
- if (canProcess(path.node)) {
48
- const moduleName = path.node.source.value
49
- const transformed = specifiers
50
- .map(specifier => {
51
- if (t.isExportSpecifier(specifier)) {
52
- const exportName = specifier.exported.name
53
- const localName = specifier.local.name
54
- const distLocation = getDistLocation(moduleName, localName)
55
-
56
- if (distLocation) {
57
- return t.exportNamedDeclaration(
58
- null,
59
- [t.exportSpecifier(t.identifier('default'), t.identifier(exportName))],
60
- t.stringLiteral(distLocation)
61
- )
62
- }
63
- }
64
- return t.exportNamedDeclaration(
65
- null,
66
- [specifier],
67
- t.stringLiteral(moduleName + '/index')
39
+ ExportNamedDeclaration ($this) {
40
+ if ($this.get('source').node.value !== MAGIC_MODULE_NAME) return
41
+ const theExports = $this.get('specifiers')
42
+ .map($specifier => {
43
+ const originalName = $specifier.get('local').node.name
44
+ if (!checkNamedExportExists(originalName)) {
45
+ throw $specifier.buildCodeFrameError(
46
+ `Named export "${originalName}" does not exist in "${MAGIC_MODULE_NAME}" "exports" field in package.json`
68
47
  )
48
+ }
49
+ const exportedName = $specifier.get('exported').node.name
50
+ return buildExport({
51
+ name: exportedName,
52
+ source: `${MAGIC_MODULE_NAME}/${originalName}`
69
53
  })
70
- .filter(Boolean)
54
+ })
71
55
 
72
- path.replaceWithMultiple(transformed)
73
- }
56
+ $this.replaceWithMultiple(theExports)
74
57
  }
75
58
  }
76
59
  }
77
60
  }
61
+
62
+ let namedExports
63
+ function checkNamedExportExists (name) {
64
+ try {
65
+ if (!namedExports) {
66
+ const packageJsonPath = join(require.resolve(MAGIC_MODULE_NAME), '../package.json')
67
+ namedExports = JSON.parse(readFileSync(packageJsonPath, 'utf8')).exports
68
+ }
69
+ return !!namedExports['./' + name]
70
+ } catch {
71
+ return false
72
+ }
73
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@startupjs/babel-plugin-startupjs",
3
- "version": "0.56.0-alpha.0",
3
+ "version": "0.57.0-canary.0",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -29,7 +29,7 @@
29
29
  ],
30
30
  "devDependencies": {
31
31
  "babel-plugin-tester": "^9.1.0",
32
- "jest": "^26.0.1"
32
+ "jest": "^29.2.1"
33
33
  },
34
- "gitHead": "076772b216a09281ed38b32866b576024562f099"
34
+ "gitHead": "0fefd437ff7830800d94d4ed964519a97363ea93"
35
35
  }
package/moduleMap.json DELETED
@@ -1,99 +0,0 @@
1
- {
2
- "// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.": 0,
3
- "@startupjs/ui": {
4
- "AbstractPopover": "/components/AbstractPopover",
5
- "Alert": "/components/Alert",
6
- "AutoSuggest": "/components/AutoSuggest",
7
- "Avatar": "/components/Avatar",
8
- "Badge": "/components/Badge",
9
- "Br": "/components/Br",
10
- "Breadcrumbs": "/components/Breadcrumbs",
11
- "Button": "/components/Button",
12
- "Card": "/components/Card",
13
- "Carousel": "/components/Carousel",
14
- "Collapse": "/components/Collapse",
15
- "Content": "/components/Content",
16
- "Div": "/components/Div",
17
- "Divider": "/components/Divider",
18
- "DrawerSidebar": "/components/DrawerSidebar",
19
- "FlatList": "/components/FlatList",
20
- "Icon": "/components/Icon",
21
- "Item": "/components/Item",
22
- "Layout": "/components/Layout",
23
- "Link": "/components/Link",
24
- "Loader": "/components/Loader",
25
- "Menu": "/components/Menu",
26
- "Modal": "/components/Modal",
27
- "Pagination": "/components/Pagination",
28
- "Portal": "/components/Portal",
29
- "Progress": "/components/Progress",
30
- "Rating": "/components/Rating",
31
- "Row": "/components/Row",
32
- "ScrollView": "/components/ScrollView",
33
- "Sidebar": "/components/Sidebar",
34
- "SmartSidebar": "/components/SmartSidebar",
35
- "Tabs": "/components/Tabs",
36
- "Tag": "/components/Tag",
37
- "Tooltip": "/components/Tooltip",
38
- "User": "/components/User",
39
- "ArrayInput": "/components/forms/ArrayInput",
40
- "Checkbox": "/components/forms/Checkbox",
41
- "ColorPicker": "/components/forms/ColorPicker",
42
- "DateTimePicker": "/components/forms/DateTimePicker",
43
- "Input": "/components/forms/Input",
44
- "Multiselect": "/components/forms/Multiselect",
45
- "NumberInput": "/components/forms/NumberInput",
46
- "ObjectInput": "/components/forms/ObjectInput",
47
- "PasswordInput": "/components/forms/PasswordInput",
48
- "Radio": "/components/forms/Radio",
49
- "RangeInput": "/components/forms/RangeInput",
50
- "Rank": "/components/forms/Rank",
51
- "Select": "/components/forms/Select",
52
- "TextInput": "/components/forms/TextInput",
53
- "Drawer": "/components/popups/Drawer",
54
- "Dropdown": "/components/popups/Dropdown",
55
- "Popover": "/components/popups/Popover",
56
- "Table": "/components/table/Table",
57
- "Tbody": "/components/table/Tbody",
58
- "Td": "/components/table/Td",
59
- "Th": "/components/table/Th",
60
- "Thead": "/components/table/Thead",
61
- "Tr": "/components/table/Tr",
62
- "Span": "/components/typography/Span",
63
- "H1": "/components/typography/headers/H1",
64
- "H2": "/components/typography/headers/H2",
65
- "H3": "/components/typography/headers/H3",
66
- "H4": "/components/typography/headers/H4",
67
- "H5": "/components/typography/headers/H5",
68
- "H6": "/components/typography/headers/H6",
69
- "ToastComponent": "/components/toast/ToastComponent.js",
70
- "ToastProvider": "/components/toast/ToastProvider.js",
71
- "toast": "/components/toast/toast.js",
72
- "DragDropContext": "/components/draggable/DragDropContext.js",
73
- "Draggable": "/components/draggable/Draggable.js",
74
- "Droppable": "/components/draggable/Droppable.js",
75
- "useColors": "/hooks/useColors.js",
76
- "useCssVariables": "/hooks/useCssVariables.js",
77
- "useDrawerDismiss": "/hooks/useDrawerDismiss.js",
78
- "useLayout": "/hooks/useLayout.js",
79
- "useMedia": "/hooks/useMedia.js",
80
- "useTransformCssVariables": "/hooks/useTransformCssVariables.js",
81
- "Colors": "/theming/Colors.js",
82
- "CssVariables": "/theming/CssVariables.js",
83
- "Palette": "/theming/Palette.js",
84
- "TheColor": "/theming/TheColor.js",
85
- "ThemeContext": "/theming/ThemeContext.js",
86
- "ThemeProvider": "/theming/ThemeProvider.js",
87
- "defaultPalette": "/theming/defaultPalette.js",
88
- "defaultUiVariables": "/theming/defaultUiVariables.js",
89
- "generateColors": "/theming/generateColors.js",
90
- "getCssVariable": "/theming/getCssVariable.js",
91
- "themed": "/theming/themed.js",
92
- "transformColors": "/theming/transformColors.js",
93
- "UiProvider": "/UiProvider.js",
94
- "DialogsProvider": "/components/dialogs/DialogsProvider.js",
95
- "alert": "/components/dialogs/alert.js",
96
- "confirm": "/components/dialogs/confirm.js",
97
- "prompt": "/components/dialogs/prompt.js"
98
- }
99
- }