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

Improve our Rollup config for server bundle to include wasp/ modules #2446

Open
infomiho opened this issue Jan 10, 2025 · 0 comments
Open

Improve our Rollup config for server bundle to include wasp/ modules #2446

infomiho opened this issue Jan 10, 2025 · 0 comments
Labels
server shouldfix We should do/fix this at some point

Comments

@infomiho
Copy link
Contributor

infomiho commented Jan 10, 2025

We bundle our server app to give users more freedom with how they write their imports paths i.e. they can use the .js suffix or can leave it out.

One thing we noticed is that in our bundle, the wasp/ package is not bundled and we have to copy over the Wasp SDK when building the server Docker image: #2362 (comment)

We are missing the@rollup/plugin-node-resolve plugin which enables Rollup to bundle packages from node_modules.

Here's a working rollup.config.js file that bundles the Wasp SDK:

import esbuild from 'rollup-plugin-esbuild'
import nodeResolve from "@rollup/plugin-node-resolve";

export default [
  createBundle('src/server.ts', 'bundle/server.js'),
  createBundle('src/dbSeed.ts', 'bundle/dbSeed.js'),
]

/**
 * @param {string} inputFilePath
 * @param {string} outputFilePath
 * @returns {import('rollup').RollupOptions}
 */
function createBundle(inputFilePath, outputFilePath) {
  return {
    input: inputFilePath,
    output: {
      file: outputFilePath,
      format: 'es',
      sourcemap: true,
    },
    plugins: [
      esbuild({
        target: 'esnext',
      }),
      // Using `modulesOnly` because I found it here: https://stackoverflow.com/questions/45707711/does-rollup-bundle-node-modules-into-bundle-js
      nodeResolve({
        modulesOnly: true
      })
    ],
    external: (id) => {
      // Anything that's not a relative or absolute import or a wasp/ module - should not be bundled e.g. Express won't be bundled
      return !id.startsWith('.') && !id.startsWith('/') && !id.startsWith('wasp/')
    },
    treeshake: 'smallest'
  }
}
@infomiho infomiho added shouldfix We should do/fix this at some point server labels Jan 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
server shouldfix We should do/fix this at some point
Projects
None yet
Development

No branches or pull requests

1 participant