Skip to content

Commit 65cd0e4

Browse files
committed
build: fix import semantics for components
1 parent 3a619de commit 65cd0e4

File tree

6 files changed

+137
-37
lines changed

6 files changed

+137
-37
lines changed

.babelrc

-19
This file was deleted.

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ dist
3434

3535
# design system static build
3636
/storybook-static
37+
/src/gen

babel.config.js

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
const {existsSync, lstatSync} = require("fs");
2+
const {resolve, dirname} = require("path");
3+
4+
function isRelativeImport(path){
5+
return path.startsWith(".");
6+
}
7+
8+
function isDirectory(path) {
9+
return existsSync(path) && lstatSync(path).isDirectory();
10+
}
11+
12+
function resolveImport (from, to) {
13+
return resolve(dirname(from), to);
14+
}
15+
16+
function replaceDirectoryImports() {
17+
return {
18+
visitor: {
19+
ImportDeclaration: (path, state) => {
20+
const importPath = path.node.source.value;
21+
const fileName = state.file.opts.filename;
22+
if (isRelativeImport(importPath) && isDirectory(resolveImport(fileName, importPath))) {
23+
path.node.source.value += "/index";
24+
}
25+
}
26+
}
27+
}
28+
}
29+
30+
31+
// This config will output files to ./src/gen/components via the `yarn components` script
32+
// See https://shadow-cljs.github.io/docs/UsersGuide.html#_javascript_dialects
33+
module.exports = {
34+
presets: [
35+
"@babel/env",
36+
// Compile tsx files.
37+
"@babel/preset-typescript",
38+
// Use the react runtime import if available.
39+
["@babel/preset-react", {"runtime": "automatic"}]
40+
],
41+
plugins: [
42+
// Add /index to all relative directory imports, because Shadow-CLJS does not support
43+
// them (https://github.com/thheller/shadow-cljs/issues/841#issuecomment-777323477)
44+
// NB: Putting these files in node_modules would have fixed the directory imports
45+
// but broken hot reload (https://github.com/thheller/shadow-cljs/issues/764#issuecomment-663064549)
46+
replaceDirectoryImports,
47+
// Allow using @/ for root relative imports in the component library.
48+
["module-resolver", {alias: {"@": "./src/js/components"}}],
49+
// Transform material-ui imports into deep imports for faster reload.
50+
// material-ui is very big, and importing it all can slow down development rebuilds by a lot.
51+
// https://material-ui.com/guides/minimizing-bundle-size/#development-environment
52+
["transform-imports", {
53+
"@material-ui/core": {
54+
transform: "@material-ui/core/esm/${member}",
55+
preventFullImport: true
56+
},
57+
"@material-ui/icons": {
58+
transform: "@material-ui/icons/esm/${member}",
59+
preventFullImport: true
60+
}
61+
}],
62+
// Our build doesn't need the {loose: true} option, but if not included it wil
63+
// show a lot of warnings on the storybook build.
64+
["@babel/proposal-class-properties", {loose: true}],
65+
["@babel/proposal-object-rest-spread", {loose: true}],
66+
// Used only by storybook, but must be included to avoid build warnings/errors.
67+
["@babel/plugin-proposal-private-methods", {loose: true}],
68+
["@babel/plugin-proposal-private-property-in-object", {loose: true}],
69+
// Import helpers from @babel/runtime instead of duplicating them everywhere.
70+
"@babel/plugin-transform-runtime",
71+
// Better debug information for styled components.
72+
// https://styled-components.com/docs/tooling#babel-plugin
73+
"babel-plugin-styled-components"
74+
],
75+
// Do not apply this babel config to node_modules.
76+
// Shadow-CLJS also runs babel over node_modules and we don't want this
77+
// configuration to apply to it.
78+
// We still want it to be picked up by storybook though.
79+
exclude: ["node_modules"]
80+
}

package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
"update": "standard-version -p --releaseCommitMessageFormat v{{currentTag}}",
1313
"dev": "yarn components && concurrently \"yarn components:watch\" \"yarn client:watch\"",
1414
"client:watch": "shadow-cljs watch main renderer app",
15-
"components": "babel ./src/js/components/ --extensions \".ts,.tsx\" --out-dir ./dist/js/components/",
15+
"components": "babel ./src/js/components/ --extensions \".ts,.tsx\" --out-dir ./src/gen/components/",
1616
"components:watch": "yarn components --watch",
1717
"compile": "yarn components && shadow-cljs compile main renderer app",
1818
"prod": "yarn components && shadow-cljs release main renderer app",
19-
"clean": "rm -rf resources/public/**/*.js target .shadow-cljs ./src/stories/**/*.js",
19+
"clean": "rm -rf resources/public/**/*.js target .shadow-cljs src/gen",
2020
"dist": "electron-builder -p always",
2121
"storybook:watch": "start-storybook -p 6006",
2222
"storybook": "build-storybook",
@@ -59,6 +59,7 @@
5959
}
6060
},
6161
"dependencies": {
62+
"@babel/runtime": "^7.15.4",
6263
"@geometricpanda/storybook-addon-badges": "^0.0.4",
6364
"@js-joda/core": "1.12.0",
6465
"@js-joda/locale_en-us": "3.1.1",
@@ -100,6 +101,7 @@
100101
"@babel/plugin-proposal-object-rest-spread": "^7.15.6",
101102
"@babel/plugin-proposal-private-methods": "^7.14.5",
102103
"@babel/plugin-proposal-private-property-in-object": "^7.15.4",
104+
"@babel/plugin-transform-runtime": "^7.15.0",
103105
"@babel/preset-env": "^7.15.6",
104106
"@babel/preset-react": "^7.14.5",
105107
"@babel/preset-typescript": "^7.15.0",
@@ -110,6 +112,8 @@
110112
"@storybook/react": "^6.3.8",
111113
"babel-loader": "^8.2.2",
112114
"babel-plugin-module-resolver": "^4.1.0",
115+
"babel-plugin-styled-components": "^1.13.2",
116+
"babel-plugin-transform-imports": "^2.0.0",
113117
"concurrently": "^6.2.1",
114118
"electron": "^12.0.4",
115119
"electron-builder": "22.10",

project.clj

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757

5858
:min-lein-version "2.5.3"
5959

60-
:source-paths ["src/clj" "src/cljs" "src/cljc" "src/js" "dist/js"]
60+
:source-paths ["src/clj" "src/cljs" "src/cljc" "src/js" "src/gen"]
6161

6262
:main athens.self-hosted.core
6363
:aot [athens.self-hosted.core]

yarn.lock

+49-15
Original file line numberDiff line numberDiff line change
@@ -1134,6 +1134,18 @@
11341134
dependencies:
11351135
"@babel/helper-plugin-utils" "^7.14.5"
11361136

1137+
"@babel/plugin-transform-runtime@^7.15.0":
1138+
version "7.15.0"
1139+
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-runtime/-/plugin-transform-runtime-7.15.0.tgz#d3aa650d11678ca76ce294071fda53d7804183b3"
1140+
integrity sha512-sfHYkLGjhzWTq6xsuQ01oEsUYjkHRux9fW1iUA68dC7Qd8BS1Unq4aZ8itmQp95zUzIcyR2EbNMTzAicFj+guw==
1141+
dependencies:
1142+
"@babel/helper-module-imports" "^7.14.5"
1143+
"@babel/helper-plugin-utils" "^7.14.5"
1144+
babel-plugin-polyfill-corejs2 "^0.2.2"
1145+
babel-plugin-polyfill-corejs3 "^0.2.2"
1146+
babel-plugin-polyfill-regenerator "^0.2.2"
1147+
semver "^6.3.0"
1148+
11371149
"@babel/plugin-transform-shorthand-properties@^7.12.1", "@babel/plugin-transform-shorthand-properties@^7.14.5":
11381150
version "7.14.5"
11391151
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-shorthand-properties/-/plugin-transform-shorthand-properties-7.14.5.tgz#97f13855f1409338d8cadcbaca670ad79e091a58"
@@ -1411,20 +1423,20 @@
14111423
dependencies:
14121424
regenerator-runtime "^0.13.4"
14131425

1426+
"@babel/runtime@^7.15.4", "@babel/runtime@^7.6.2":
1427+
version "7.15.4"
1428+
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.15.4.tgz#fd17d16bfdf878e6dd02d19753a39fa8a8d9c84a"
1429+
integrity sha512-99catp6bHCaxr4sJ/DbTGgHS4+Rs2RVd2g7iOap6SLGPDknRK9ztKNsE/Fg6QhSeh1FGE5f6gHGQmvvn3I3xhw==
1430+
dependencies:
1431+
regenerator-runtime "^0.13.4"
1432+
14141433
"@babel/runtime@^7.3.1", "@babel/runtime@^7.4.4", "@babel/runtime@^7.5.5", "@babel/runtime@^7.8.3", "@babel/runtime@^7.8.7":
14151434
version "7.14.0"
14161435
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.14.0.tgz#46794bc20b612c5f75e62dd071e24dfd95f1cbe6"
14171436
integrity sha512-JELkvo/DlpNdJ7dlyw/eY7E0suy5i5GQH+Vlxaq1nsNJ+H7f4Vtv3jMeCEgRhZZQFXTjldYfQgv2qmM6M1v5wA==
14181437
dependencies:
14191438
regenerator-runtime "^0.13.4"
14201439

1421-
"@babel/runtime@^7.6.2":
1422-
version "7.15.4"
1423-
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.15.4.tgz#fd17d16bfdf878e6dd02d19753a39fa8a8d9c84a"
1424-
integrity sha512-99catp6bHCaxr4sJ/DbTGgHS4+Rs2RVd2g7iOap6SLGPDknRK9ztKNsE/Fg6QhSeh1FGE5f6gHGQmvvn3I3xhw==
1425-
dependencies:
1426-
regenerator-runtime "^0.13.4"
1427-
14281440
"@babel/template@^7.12.7", "@babel/template@^7.14.5":
14291441
version "7.14.5"
14301442
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.14.5.tgz#a9bc9d8b33354ff6e55a9c60d1109200a68974f4"
@@ -1481,7 +1493,7 @@
14811493
"@babel/helper-validator-identifier" "^7.14.9"
14821494
to-fast-properties "^2.0.0"
14831495

1484-
"@babel/types@^7.15.4", "@babel/types@^7.15.6":
1496+
"@babel/types@^7.15.4", "@babel/types@^7.15.6", "@babel/types@^7.4":
14851497
version "7.15.6"
14861498
resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.15.6.tgz#99abdc48218b2881c058dd0a7ab05b99c9be758f"
14871499
integrity sha512-BPU+7QhqNjmWyDO0/vitH/CuhpV8ZmK1wpKva8nuyNF5MJfuRNWMc+hc14+u9xT93kvykMdncrJT19h74uB1Ig==
@@ -4374,7 +4386,7 @@ babel-plugin-react-docgen@^4.2.1:
43744386
lodash "^4.17.15"
43754387
react-docgen "^5.0.0"
43764388

4377-
"babel-plugin-styled-components@>= 1", "babel-plugin-styled-components@>= 1.12.0":
4389+
"babel-plugin-styled-components@>= 1", "babel-plugin-styled-components@>= 1.12.0", babel-plugin-styled-components@^1.13.2:
43784390
version "1.13.2"
43794391
resolved "https://registry.yarnpkg.com/babel-plugin-styled-components/-/babel-plugin-styled-components-1.13.2.tgz#ebe0e6deff51d7f93fceda1819e9b96aeb88278d"
43804392
integrity sha512-Vb1R3d4g+MUfPQPVDMCGjm3cDocJEUTR7Xq7QS95JWWeksN1wdFRYpD2kulDgI3Huuaf1CZd+NK4KQmqUFh5dA==
@@ -4389,6 +4401,14 @@ babel-plugin-syntax-jsx@^6.18.0:
43894401
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-jsx/-/babel-plugin-syntax-jsx-6.18.0.tgz#0af32a9a6e13ca7a3fd5069e62d7b0f58d0d8946"
43904402
integrity sha1-CvMqmm4Tyno/1QaeYtew9Y0NiUY=
43914403

4404+
babel-plugin-transform-imports@^2.0.0:
4405+
version "2.0.0"
4406+
resolved "https://registry.yarnpkg.com/babel-plugin-transform-imports/-/babel-plugin-transform-imports-2.0.0.tgz#9e5f49f751a9d34ba8f4bb988c7e48ed2419c6b6"
4407+
integrity sha512-65ewumYJ85QiXdcB/jmiU0y0jg6eL6CdnDqQAqQ8JMOKh1E52VPG3NJzbVKWcgovUR5GBH8IWpCXQ7I8Q3wjgw==
4408+
dependencies:
4409+
"@babel/types" "^7.4"
4410+
is-valid-path "^0.1.1"
4411+
43924412
bail@^1.0.0:
43934413
version "1.0.5"
43944414
resolved "https://registry.yarnpkg.com/bail/-/bail-1.0.5.tgz#b6fa133404a392cbc1f8c4bf63f5953351e7a776"
@@ -8577,6 +8597,13 @@ is-installed-globally@^0.4.0:
85778597
global-dirs "^3.0.0"
85788598
is-path-inside "^3.0.2"
85798599

8600+
is-invalid-path@^0.1.0:
8601+
version "0.1.0"
8602+
resolved "https://registry.yarnpkg.com/is-invalid-path/-/is-invalid-path-0.1.0.tgz#307a855b3cf1a938b44ea70d2c61106053714f34"
8603+
integrity sha1-MHqFWzzxqTi0TqcNLGEQYFNxTzQ=
8604+
dependencies:
8605+
is-glob "^2.0.0"
8606+
85808607
is-map@^2.0.2:
85818608
version "2.0.2"
85828609
resolved "https://registry.yarnpkg.com/is-map/-/is-map-2.0.2.tgz#00922db8c9bf73e81b7a335827bc2a43f2b91127"
@@ -8697,6 +8724,13 @@ is-typedarray@^1.0.0:
86978724
resolved "https://registry.yarnpkg.com/is-typedarray/-/is-typedarray-1.0.0.tgz#e479c80858df0c1b11ddda6940f96011fcda4a9a"
86988725
integrity sha1-5HnICFjfDBsR3dppQPlgEfzaSpo=
86998726

8727+
is-valid-path@^0.1.1:
8728+
version "0.1.1"
8729+
resolved "https://registry.yarnpkg.com/is-valid-path/-/is-valid-path-0.1.1.tgz#110f9ff74c37f663e1ec7915eb451f2db93ac9df"
8730+
integrity sha1-EQ+f90w39mPh7HkV60UfLbk6yd8=
8731+
dependencies:
8732+
is-invalid-path "^0.1.0"
8733+
87008734
is-whitespace-character@^1.0.0:
87018735
version "1.0.4"
87028736
resolved "https://registry.yarnpkg.com/is-whitespace-character/-/is-whitespace-character-1.0.4.tgz#0858edd94a95594c7c9dd0b5c174ec6e45ee4aa7"
@@ -11872,9 +11906,9 @@ shadow-cljs-jar@1.3.2:
1187211906
integrity sha512-XmeffAZHv8z7451kzeq9oKh8fh278Ak+UIOGGrapyqrFBB773xN8vMQ3O7J7TYLnb9BUwcqadKkmgaq7q6fhZg==
1187311907

1187411908
shadow-cljs@^2.15.3:
11875-
version "2.15.3"
11876-
resolved "https://registry.yarnpkg.com/shadow-cljs/-/shadow-cljs-2.15.3.tgz#11a0a04f1c31d9277838a5f649457edbb06baafb"
11877-
integrity sha512-KK7G9kSc0dwrOkN74o5yrxhrpsJ3BJCL11nGHBakzHLodc7pdiCLDeq2ChXyKl2yu9aJIzfSz8Hum38wpLQ1DA==
11909+
version "2.15.9"
11910+
resolved "https://registry.yarnpkg.com/shadow-cljs/-/shadow-cljs-2.15.9.tgz#cb256a9af12c3df1f0b2bbef344e365dab74b519"
11911+
integrity sha512-t2KrrMvJZtUFf2xIAL+OL76ahYHPT8VAKxhDic3kloTgOYfZpHRm/S/3C0iW982U3ZJdLUZxmBeluH4Q7564dg==
1187811912
dependencies:
1187911913
node-libs-browser "^2.2.1"
1188011914
readline-sync "^1.4.7"
@@ -13541,9 +13575,9 @@ write-file-atomic@^3.0.0:
1354113575
typedarray-to-buffer "^3.1.5"
1354213576

1354313577
ws@^7.4.6:
13544-
version "7.5.3"
13545-
resolved "https://registry.yarnpkg.com/ws/-/ws-7.5.3.tgz#160835b63c7d97bfab418fc1b8a9fced2ac01a74"
13546-
integrity sha512-kQ/dHIzuLrS6Je9+uv81ueZomEwH0qVYstcAQ4/Z93K8zeko9gtAbttJWzoC5ukqXY1PpoouV3+VSOqEAFt5wg==
13578+
version "7.5.5"
13579+
resolved "https://registry.yarnpkg.com/ws/-/ws-7.5.5.tgz#8b4bc4af518cfabd0473ae4f99144287b33eb881"
13580+
integrity sha512-BAkMFcAzl8as1G/hArkxOxq3G7pjUqQ3gzYbLL0/5zNkph70e+lCoxBGnm6AW1+/aiNeV4fnKqZ8m4GZewmH2w==
1354713581

1354813582
ws@~7.4.2:
1354913583
version "7.4.6"

0 commit comments

Comments
 (0)