Skip to content

Commit 27184be

Browse files
committed
Fix building packages and apps
Number of small changes was required for packages (components/hooks) and for Next.js app to build properly. * Next.js' config now includes "esmExternals: loose". In commit ee0a27f "react-markdown" and "remark-gfm" were changed to use dynamic imports to avoid errors. While this worked in deve mode, it still broke down when running prebuilt app. This setting once again sweeps the problem under the rug. * Added @babel/runtime for packages/components to fix the build process done by preconstruct. * Renamed "types" fields in package.json of packages/components and packages/hooks to use the names of the files actually generated when the packages are built with preconstruct. * In packages/components, import React in all files where it's used. While this is not required by the IDE or Next.js dev server, they will cause errors when built Next.js app tries to use the packages. * In packages/components, import React in all files where it's used directly, e.g. by calling "React.FC". Don't import React in files that just use JSX elements, since that's not required anymore and goes against our linting rules. However, for this to work the same way consistently between the packages and the Next.js app using them, "runtime: automatic" was added for "@babel/preset-react" setting.
1 parent fcbf3fb commit 27184be

File tree

8 files changed

+36
-6
lines changed

8 files changed

+36
-6
lines changed

README.md

+19
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,25 @@ yarn workspace @thunderstore/nextjs dev
2828
That's it. Changes done to `apps/nextjs` and `packages/components` should both
2929
be automatically visible at [http://localhost:3000/].
3030

31+
```
32+
// production build, assumes yarn install has already been run
33+
// build packages/* with preconstruct
34+
yarn build
35+
36+
// build and start Next.js prod server
37+
yarn workspace @thunderstore/nextjs build
38+
yarn workspace @thunderstore/nextjs start
39+
```
40+
41+
### Troubleshooting
42+
43+
**After running `yarn build`, all the links disappeared from the page**
44+
45+
**Solution**: This seems to occur only when Next.js dev server has already been
46+
up before the packages were built. Most likely this is a cache issue which
47+
results in a rehydration issue on client-side. Simply running `yarn` or manually
48+
deleting `apps/nextjs/.next` after running `yarn build` should solve the issue.
49+
3150
### Adding dependencies
3251

3352
To add new dependencies to existing packages, simply run something like:

apps/nextjs/next.config.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
const withPreconstruct = require("@preconstruct/next");
22

3-
module.exports = withPreconstruct();
3+
module.exports = withPreconstruct({
4+
experimental: {
5+
esmExternals: "loose",
6+
},
7+
});

babel.config.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ module.exports = {
77
loose: true,
88
},
99
],
10-
"@babel/preset-react",
10+
[
11+
"@babel/preset-react",
12+
{
13+
runtime: "automatic",
14+
},
15+
],
1116
"@babel/preset-typescript",
1217
],
1318
plugins: ["@babel/plugin-transform-runtime"],

packages/components/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"repository": "https://github.com/thunderstore-io/thunderstore-ui/tree/master/packages/components",
66
"main": "dist/thunderstore-components.cjs.js",
77
"module": "dist/thunderstore-components.esm.js",
8-
"types": "./dist/index.d.ts",
8+
"types": "dist/thunderstore-components.cjs.d.ts",
99
"files": [
1010
"dist"
1111
],
@@ -14,6 +14,7 @@
1414
"dev": "tsc --watch"
1515
},
1616
"dependencies": {
17+
"@babel/runtime": "^7.16.7",
1718
"@chakra-ui/react": "0.0.0-pr-2022062070",
1819
"@emotion/react": "^11.4.1",
1920
"@emotion/styled": "^11.3.0",

packages/components/src/components/BreadCrumbs.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Flex, Heading, Text } from "@chakra-ui/react";
2-
import { Fragment } from "react";
2+
import React, { Fragment } from "react";
33

44
import { Raquo } from "./Icons";
55

packages/components/src/components/Internals.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Collection of small, shared components intended for internal use.
33
*/
44
import { Flex, Image } from "@chakra-ui/react";
5+
import React from "react";
56

67
import { QuestionMarkIcon } from "./Icons";
78

packages/hooks/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"repository": "https://github.com/thunderstore-io/thunderstore-ui/tree/master/packages/hooks",
66
"main": "dist/thunderstore-hooks.cjs.js",
77
"module": "dist/thunderstore-hooks.esm.js",
8-
"types": "./dist/index.d.ts",
8+
"types": "./dist/thunderstore-hooks.cjs.d.ts",
99
"files": [
1010
"dist"
1111
],

yarn.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -1085,7 +1085,7 @@
10851085
dependencies:
10861086
regenerator-runtime "^0.13.4"
10871087

1088-
"@babel/runtime@^7.0.0", "@babel/runtime@^7.12.13":
1088+
"@babel/runtime@^7.0.0", "@babel/runtime@^7.12.13", "@babel/runtime@^7.16.7":
10891089
version "7.16.7"
10901090
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.16.7.tgz#03ff99f64106588c9c403c6ecb8c3bafbbdff1fa"
10911091
integrity sha512-9E9FJowqAsytyOY6LG+1KuueckRL+aQW+mKvXRXnuFGyRAyepJPmEo9vgMfXUA6O9u3IeEdv9MAkppFcaQwogQ==

0 commit comments

Comments
 (0)