Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/try dep #1

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ The `@types` directive in comments might help having IntelliSense in VS code, wi
leading to stale imports
- NPM and/or Yarn may cache .tgz files in an unexpected way, reinstalling a stale version everytime:https://github.com/yarnpkg/yarn/issues/6811

### Next.js app

- In v12.1, I hit following bug:https://github.com/vercel/next.js/issues/35112 with "type":"module". It seems that the CJS code might in some case try to load the ESM exports of its submodule, instead of sticking to CJS.


## Learning - per build tools


Expand Down Expand Up @@ -124,6 +129,8 @@ Conditional exports are supposed to avoid this issue (you don't need to use `typ
- Doesn't respect the "outDir" in our demo
- Based on rollup, at the time of writing there is no official doc for the config, but you can write in TypeScript and read the source directly: https://github.com/unjs/unbuild/blob/main/src/types.ts



## Contribute

- If you want to test a new bundler, copy `package-template` and setup `package.json` accordingly.
Expand Down
1,666 changes: 172 additions & 1,494 deletions demo-next-app/package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion demo-next-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
"my-package-esbuild": "file:../my-package-esbuild/my-package-esbuild-1.0.0.tgz",
"my-package-tsup": "file:../my-package-tsup/my-package-tsup-1.0.0.tgz",
"my-package-webpack": "file:../my-package-webpack/my-package-webpack-1.0.0.tgz",
"next": "12.0.7",
"next": "^12.1.1-canary.6",
"react": "17.0.2",
"react-dom": "17.0.2"
},
"devDependencies": {
"@types/node": "^17.0.21",
"@types/react": "^17.0.38",
"eslint": "8.6.0",
"eslint-config-next": "12.0.7"
Expand Down
26 changes: 24 additions & 2 deletions demo-next-app/pages/with-tsup.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import { shared, isClient, SharedType } from "my-package-tsup";
import { serverOnly, ServerOnlyType } from "my-package-tsup/server";
import {
serverOnly,
ServerOnlyType,
//serverOnlyDependency,
sharedDependency,
} from "my-package-tsup/server";
import { DynamicClientOnly } from "../components/DynamicClientOnly";

export default function WithTsupPage(props: { serverOnly: ServerOnlyType }) {
export default function WithTsupPage(props: {
serverOnly: ServerOnlyType;
serverOnlyDependency;
}) {
return (
<div>
<div>Server-only: {props.serverOnly}</div>
Expand All @@ -15,6 +23,19 @@ export default function WithTsupPage(props: { serverOnly: ServerOnlyType }) {
that's just for testing`}
</p>
<div>Is client: {isClient() ? "true" : "false"} </div>
<div>
<h2>Same for a dependency of my-package-tsup:</h2>
{/*<div>Server-only dep: {props.serverOnlyDependency}</div>*/}
<div>
Client-only: <DynamicClientOnly />
</div>
<div>Shared:{sharedDependency}</div>
<p>
{`Note: this line should provoke an SSR issue, it\'s perfectly normal,
that's just for testing`}
</p>
<div>Is client: {isClient() ? "true" : "false"} </div>
</div>
</div>
);
}
Expand All @@ -23,6 +44,7 @@ export function getStaticProps() {
return {
props: {
serverOnly,
//serverOnlyDependency,
},
};
}
1,489 changes: 129 additions & 1,360 deletions demo-next-app/yarn.lock

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions my-package-tsup-dependency/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
dist
*.tgz
yarn-error.log
12 changes: 12 additions & 0 deletions my-package-tsup-dependency/client/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export * from "../shared";

if (typeof window === "undefined" || !window) {
console.log("client window dependency", window);
throw new Error(
"You tried to import client-only code on the server from dependency."
);
}

export type ClientOnlyDependencyType = "client-only-dependency";
export const clientOnlyDependency: ClientOnlyDependencyType =
"client-only-dependency";
46 changes: 46 additions & 0 deletions my-package-tsup-dependency/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "my-package-tsup-dependency",
"version": "1.0.0",
"description": "",
"main": "./dist/shared/index.js",
"type": "module",
"files": [
"dist/"
],
"exports": {
".": {
"node": "./dist/server/index.cjs",
"import": "./dist/shared/index.js"
},
"./server": "./dist/server/index.cjs",
"./client": "./dist/client/index.js"
},
"types": "./dist/shared/index.d.ts",
"typesVersions": {
"*": {
"server": [
"./dist/server/index.d.ts"
],
"client": [
"./dist/client/index.d.ts"
]
}
},
"scripts": {
"clean": "rm -Rf ./dist && rm -Rf *.tgz",
"prebuild": "yarn run clean",
"build:tsup": "tsup",
"build": "yarn run build:tsup",
"publish": "yarn run build && npm pack"
},
"author": "",
"license": "MIT",
"devDependencies": {
"@types/node": "^17.0.8",
"tsup": "^5.11.11",
"typescript": "4.2.2"
},
"dependencies": {
"react": "^17.0.2"
}
}
14 changes: 14 additions & 0 deletions my-package-tsup-dependency/server/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import fs from "fs"; // example of a server-only import

export * from "../shared";

/*
FIXME: currently we set window to "42" so this condition works in client-code
if (typeof window !== "undefined" && !!window) {
throw new Error("You tried to import server-only code on the client.");
}
*/

export type ServerOnlyDependencyType = "server-only-dependency";
export const serverOnlyDependency: ServerOnlyDependencyType =
"server-only-dependency";
9 changes: 9 additions & 0 deletions my-package-tsup-dependency/shared/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Import a non-trivial shared lib to test "externals" management
import "react";

export type SharedDependencyType = "shared-dependency";
export const sharedDependency: SharedDependencyType = "shared-dependency";

export const isClientDependency = (): boolean => {
return typeof window !== "undefined";
};
15 changes: 15 additions & 0 deletions my-package-tsup-dependency/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"extends": "./tsconfig.json",
"include": [
"./shared/index.ts",
"./client/index.ts",
"./server/index.ts"
],
"compilerOptions": {
"emitDeclarationOnly": true,
"isolatedModules": false,
"declaration": true,
// helps local development, could be dropped out of the NPM package though
"declarationMap": true
},
}
6 changes: 6 additions & 0 deletions my-package-tsup-dependency/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "./dist"
},
}
28 changes: 28 additions & 0 deletions my-package-tsup-dependency/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { defineConfig } from "tsup";

const commonConfig = {
clean: true,
splitting: false,
dts: true,
sourcemap: true,
};
export default defineConfig([
{
entry: ["shared/index.ts"],
...commonConfig,
format: ["cjs", "esm", "iife"],
outDir: "dist/shared",
},
{
entry: ["server/index.ts"],
...commonConfig,
format: ["cjs"],
outDir: "dist/server",
},
{
entry: ["client/index.ts"],
...commonConfig,
format: ["esm", "iife"],
outDir: "dist/client",
},
]);
Loading