Skip to content

@rollup/plugin-commonjs strictRequires should handle dinamically required builtins #1900

@lucalooz

Description

@lucalooz

Expected Behavior

strictRequires with true handle dynamically external required builtins without hoisting them.

After a npm run build, both npm run exec:dev and npm run exec:build should work with same behavior and output.

Actual Behavior

Even if there is no usage of SqliteCacheStore of nodejs/undici the node:sqlite builtin is hoisted and eagerly loaded.

Node 20

Error [ERR_UNKNOWN_BUILTIN_MODULE]: No such built-in module: node:sqlite
    at ModuleLoader.builtinStrategy (node:internal/modules/esm/translators:163:4686)
    at #a (node:internal/modules/esm/loader:157:4044)
    at ModuleLoader.loadAndTranslate (node:internal/modules/esm/loader:157:4594) {
  code: 'ERR_UNKNOWN_BUILTIN_MODULE'
}

Node 22

(node:33652) ExperimentalWarning: SQLite is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)

Additional Information

Both rolldown/rolldown and webpack/webpack automatically handle this by default if the target is node. By injecting a shim for require and using createRequire.

Workaround

Manually use ignore and banner to inject the usage of createRequire

import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';

export default {
  input: 'src/main.js',
  output: {
    dir: 'dist',
    format: 'es',
    banner: "import { createRequire } from 'node:module';\nconst require = createRequire(import.meta.url);\n"
  },
  external: ['node:sqlite'],
  plugins: [
    commonjs({ strictRequires: true, ignore: ['node:sqlite']  }),
    resolve({ preferBuiltins: true }),
  ],
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions