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

Weird CJS module hack is bombing Rollup builds #19

Open
Prophet32j opened this issue May 17, 2023 · 1 comment
Open

Weird CJS module hack is bombing Rollup builds #19

Prophet32j opened this issue May 17, 2023 · 1 comment

Comments

@Prophet32j
Copy link

ono/src/index.ts

Lines 10 to 13 in a1fa89a

// CommonJS default export hack
if (typeof module === "object" && typeof module.exports === "object") {
module.exports = Object.assign(module.exports.default, module.exports);
}

This code block is bombing my rollup build. Not caught during build. This code makes it into the bundle, and then it fails on runtime startup. module.exports.default is undefined and is killing the build.

/path-to-my-build/node_modules/@jsdevtools/ono/esm/index.js:12
 module.exports = Object.assign(module.exports.default, module.exports);
                                            ^
TypeError: Cannot convert undefined or null to object

I found this by preserving the modules from Rollup so I could get a good stacktrace to the code.

// my rollup
import commonjs from '@rollup/plugin-commonjs'
import nodeResolve from '@rollup/plugin-node-resolve'
import json from '@rollup/plugin-json'

export default {
  input: 'src/service.js',
  output: {
    // file: 'dist/service.js',
    dir: 'dist/',
    format: 'cjs',
    sourcemap: 'inline',
    preserveModules: true
  },
  plugins: [
    nodeResolve({ preferBuiltins: true }),
    commonjs({
      dynamicRequireTargets: ['node_modules/nconf/lib/nconf/stores/*.js']
    }),
    json()
  ]
}

I found if i do this it makes it past this part:

if (typeof module === "object" && typeof module.exports === "object") {
  module.exports.default = module.exports.default || {}
  module.exports = Object.assign(module.exports.default, module.exports); 
}
mquan added a commit to mquan/api2ai that referenced this issue Jul 21, 2023
Details in JS-DevTools/ono#19, It's fixed in JS-DevTools/ono#20
This package is not maintained but it's depended on by swagger-parser
@kanadgupta
Copy link

Running into the same issue — as a not-so-great workaround, I used the @rollup/plugin-replace plugin to fix up that patch of code with the changes in #20 and I'm no longer seeing runtime errors:

import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import replace from '@rollup/plugin-replace';
import { defineConfig } from 'rollup';

export default defineConfig({
  input: 'input.js',
  output: { file: 'output.cjs', format: 'cjs' },
  plugins: [
    commonjs(),
    json(),
    nodeResolve({ preferBuiltins: true }),
    replace({
      delimiters: ['', ''],
      preventAssignment: true,
      values: {
        'if (typeof module === "object" && typeof module.exports === "object") {':
          'if (typeof module === "object" && typeof module.exports === "object" && typeof module.exports.default === "object") {',
      },
    }),
  ],
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants