Skip to content

CJS lib issue "TypeError: invariant is not a function" #17

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

Closed
ep-mark opened this issue Mar 22, 2023 · 3 comments
Closed

CJS lib issue "TypeError: invariant is not a function" #17

ep-mark opened this issue Mar 22, 2023 · 3 comments

Comments

@ep-mark
Copy link

ep-mark commented Mar 22, 2023

When you try and use the DefaultParser from @yozora/core-parser using the cjs lib you get the following error:

TypeError: invariant is not a function
    at consumeNewOpener (https://cigod8.csb.app/node_modules/@yozora/core-parser/lib/cjs/index.cjs:143:13)
    at Object.consume (https://cigod8.csb.app/node_modules/@yozora/core-parser/lib/cjs/index.cjs:303:13)
    at matchBlockTokens (https://cigod8.csb.app/node_modules/@yozora/core-parser/lib/cjs/index.cjs:793:27)
    at Object.process (https://cigod8.csb.app/node_modules/@yozora/core-parser/lib/cjs/index.cjs:736:32)
    at CustomParser.parse (https://cigod8.csb.app/node_modules/@yozora/core-parser/lib/cjs/index.cjs:975:32)
    at Object.eval (https://cigod8.csb.app/src/CustomParser.test.ts:15:25)
    at https://codesandbox.io/static/js/3.09a963795.chunk.js:1:338381
    at new Promise (<anonymous>)
    at t.callAsyncFn (https://codesandbox.io/static/js/3.09a963795.chunk.js:1:338040)
    at https://codesandbox.io/static/js/7.916d1bd34.chunk.js:1:6522
    at c (https://codesandbox.io/static/js/vendors~app~embed~sandbox~sandbox-startup.7424373eb.chunk.js:1:4328)

Here is a code sandbox for the problem:
https://codesandbox.io/s/delicate-haze-cigod8?file=/index.html

The @yozora/invariant/lib/cjs/index.cjs looks like:

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });
...
function invariant(condition, message) {
    ...
}

exports.default = invariant;
exports.invariant = invariant;

which comes from https://github.com/yozorajs/yozora/blob/main/packages/invariant/src/index.ts

export function invariant(
  condition: boolean,
  message?: string | (() => string),
): asserts condition {
  ...
}

export default invariant

The problem occurs when @yozora/core-parse/lib/cjs/index.cjs requires the @yozora/invariant', expecting the default to be the invariant function.

var invariant = require('@yozora/invariant');

But for this to work it would need to use the default:

var invariant = require('@yozora/invariant').default;

I am no rollup expert but I think it may be something to do with this issue: rollup/rollup#1961

Thanks

@guanghechen
Copy link
Collaborator

Hi @ep-mark , thank you for trying out yozora, the issue you reported is ACK, and I will take the time to fix it. Maybe I will remove all default export of all packages under this monorepo, which would completely avoid this bundle issue (this is a breaking change, but I think it's okay since there are no third-party dependencies).

@guanghechen
Copy link
Collaborator

Hi @ep-mark, can you try the latest version (2.1.3) yozora, I think the problem has been fixed.

@ep-mark
Copy link
Author

ep-mark commented Mar 23, 2023

@guanghechen That fixed it! Thanks!

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