Skip to content

fix!: drop CJS exports - #482

Merged
harlan-zw merged 3 commits into
mainfrom
drop-cjs-support
Feb 5, 2025
Merged

harlan-zw merged 3 commits into
mainfrom
drop-cjs-support

Conversation

@harlan-zw

@harlan-zw harlan-zw commented Feb 5, 2025 •

Copy link
Copy Markdown
Collaborator

🔗 Linked issue

❓ Type of change

  • 📖 Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

Inspired by https://antfu.me/posts/move-on-to-esm-only.

For v2 we no longer export CJS.

⚠️ This may break for some users although not exactly sure who.

@harlan-zw harlan-zw mentioned this pull request Feb 5, 2025
53 of 54 tasks
@harlan-zw harlan-zw added the v2 label Feb 5, 2025
@harlan-zw harlan-zw changed the title fix!: drop CJS exports fix(v2)!: drop CJS exports Feb 5, 2025
@harlan-zw harlan-zw changed the title fix(v2)!: drop CJS exports fix!: drop CJS exports Feb 5, 2025
@harlan-zw
harlan-zw merged commit 0f14f9f into main Feb 5, 2025
@olamedia

olamedia commented Mar 5, 2025 •

Copy link
Copy Markdown

Looks like this breaks server.ts for me using vue 3, nodeExternals and webpack. I get "does not provide an export named ./server" errors, since I build app to commonjs bundle before running server.ts with tsx/ts-node.

@harlan-zw

Copy link
Copy Markdown
Collaborator Author

You're definitly using the v2 right? If so and you're able to provide a reproduction I can help.

You may need to tell webpack to transpile Unhead.

@olamedia

olamedia commented Mar 5, 2025 •

Copy link
Copy Markdown

in webpack:

// configureWebpack
entry: { app: './src/entry-server.ts' },
output: {
              filename: 'server-bundle.js',
              libraryTarget: 'commonjs2',
              libraryExport: 'default',
          },
// chainWebpack
config.externals(nodeExternals({ allowlist: [
                /\.(css|vue)$/
            ]}));

server.ts

import serverBundle from '../../dist/server/server-bundle.js';

Running with

tsx --tsconfig tsconfig.server.json server/src/server.ts

tsconfig.server.json

//...
"compilerOptions": {
        "target": "ESNext",
        "module": "ESNext",
        "baseUrl": ".",
        "moduleResolution": "node",
        "moduleDetection": "auto",
        "allowImportingTsExtensions": true,
        "resolveJsonModule": true,
        "esModuleInterop": true,
        "isolatedModules": true,
        "types": ["node"],

general tsconfig.json (used to build bundle.js and client's app.js)

"compilerOptions": {
        "target": "es5",
        "module": "esnext",
        "strict": true,
        "jsx": "preserve",
        "skipLibCheck": true,
        "importHelpers": true,
        "moduleResolution": "node",
        "experimentalDecorators": true,
        "esModuleInterop": true,
        "allowSyntheticDefaultImports": true,
        "sourceMap": true,
        "baseUrl": ".",

Importing at entry-server.ts triggers error in server.ts

import { createHead } from '@unhead/vue/server';

I will try to make esm build of server bundle and run it in esm mode...

@harlan-zw

Copy link
Copy Markdown
Collaborator Author

@olamedia

olamedia commented Mar 5, 2025

Copy link
Copy Markdown

@harlan-zw harlan-zw mentioned this pull request Mar 6, 2025
1 of 6 tasks
@harlan-zw

harlan-zw commented Mar 6, 2025 •

Copy link
Copy Markdown
Collaborator Author

@olamedia

olamedia commented Mar 6, 2025

Copy link
Copy Markdown

Yes, it fixed everything for me.

@harlan-zw harlan-zw mentioned this pull request Apr 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants