Skip to content

Commit

Permalink
fix: build process causing esm issues (#48)
Browse files Browse the repository at this point in the history
* fix: build process causing esm issues

* fix: add back main and module to pkg json

* fix: pr feedback

* docs(changeset): add changeset

* fix: pr feedback
  • Loading branch information
kylemcd authored Feb 8, 2024
1 parent 5d06f5d commit 59ce044
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/beige-bugs-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@knocklabs/react": patch
---

fix: build process causing esm issues
4 changes: 1 addition & 3 deletions examples/nextjs-example/next.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
/** @type {import('next').NextConfig} */
module.exports = {
transpilePackages: ["@knocklabs/react"],
};
module.exports = {};
7 changes: 4 additions & 3 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"version": "0.1.5",
"license": "MIT",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.mjs",
"module": "dist/esm/index.esm.js",
"types": "dist/types/index.d.ts",
"typings": "dist/types/index.d.ts",
"style": "dist/index.css",
Expand All @@ -14,7 +14,8 @@
".": {
"require": "./dist/cjs/index.js",
"types": "./dist/types/index.d.ts",
"default": "./dist/esm/index.mjs"
"import": "./dist/esm/index.esm.js",
"default": "./dist/esm/index.esm.js"
}
},
"files": [
Expand Down Expand Up @@ -78,4 +79,4 @@
"vite-plugin-no-bundle": "^3.0.0",
"vitest": "^1.2.2"
}
}
}
18 changes: 10 additions & 8 deletions packages/react/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,30 @@ import execute from "rollup-plugin-execute";
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), "");
const CJS = env.BUILD_TARGET?.toLocaleLowerCase()?.match("cjs");
const formats: LibraryFormats[] = CJS ? ["cjs"] : ["es"];
const target = env?.BUILD_TARGET?.toLowerCase() as LibraryFormats;

return {
plugins: [
dts({
outDir: "dist/types",
}),
react(),
noBundlePlugin({ root: "./src" }),
noBundlePlugin({ root: resolve(__dirname, "src") }),
],
build: {
outDir: CJS ? "dist/cjs" : "dist/esm",
outDir: target === "cjs" ? "dist/cjs" : "dist/esm",
sourcemap: true,
lib: {
entry: resolve(__dirname, "src/index.ts"),
fileName: "[name]",
formats: [target],
name: "react",
formats,
},
rollupOptions: {
// External packages that should not be bundled
external: ["react", "react-dom"],
output: {
interop: "compat",
format: formats[0],
globals: {
react: "React",
},
Expand All @@ -50,7 +49,10 @@ export default defineConfig(({ mode }) => {
// Move index.css to root of dist
`mv dist/esm/index.css dist/index.css`,
// Delete extra .css.js files
`rm dist/**/*.css.*`,
`find ./dist -name "*.css.js" -delete`,
`find ./dist -name "*.css.js.map" -delete`,
`find ./dist -name "*.css.esm.js" -delete`,
`find ./dist -name "*.css.esm.js.map" -delete`,
]),
// Remove css imports
{
Expand All @@ -62,7 +64,7 @@ export default defineConfig(({ mode }) => {
if (file?.type === "chunk") {
// Replace .css imports and requires
const pattern =
/(import\s+['"].+\.css(\.mjs)?['"];?)|(require\(['"][^()]+\.css(\.js)?['"]\);?)/;
/(import ".*?\.css\..*?";)|(require\(['"][^()]+\.css(\.js)?['"]\);?)/g;
file.code = file.code.replace(pattern, "");
}
}
Expand Down

0 comments on commit 59ce044

Please sign in to comment.