y-lohse / inkjs

A javascript port of inkle's ink scripting language.
http://www.inklestudios.com/ink/
MIT License
503 stars 101 forks source link

vite CommonJS error #1057

Closed ewilliam closed 4 months ago

ewilliam commented 4 months ago

Describe the bug

CommonJS issue with recent SvelteKit versions (2.0+ and vite 4.0+)

SyntaxError: [vite] Named export 'Story' not found. The requested module 'inkjs' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'inkjs';
const {Story} = pkg;

recommended default export doesn't work neither

To Reproduce

import { Story } from 'inkjs'

this is all it takes to trigger an error in SvelteKit (presumably any vite app?)

Expected behavior

access to Story export without errors

Ink files

N/A

Environment

Additional Comments

might be helpful/relevant:

https://kit.svelte.dev/docs/faq#how-do-i-fix-the-error-i-m-getting-trying-to-include-a-package

https://publint.dev/inkjs@2.2.2

smwhr commented 4 months ago

While a fix for this problem is investigated, be aware that ink is known to work with Vite, see : https://github.com/floriancargoet/vite-plugin-ink

ewilliam commented 4 months ago

thanks - i ended up forking the project and specifying type: module for now https://github.com/ewilliam/inkjs

doubt this works for all cases though

changing es6 output file to .mjs and importing from inkjs/dist/ink-es6.mjs works too

ewilliam commented 4 months ago

just to add context - looks like vite-plugin-ink doesnt work as is for similar reasons


error when starting dev server:
file:///Users/ewilliam/Projects/inkling/node_modules/.pnpm/vite-plugin-ink@0.1.2_inkjs@https+++codeload.github.com+ewilliam+inkjs+tar.gz+f82e13848368065184de7e230e92d38851c49606/node_modules/vite-plugin-ink/dist/index.js:4
import inkjs from "inkjs";
       ^^^^^
SyntaxError: The requested module 'inkjs' does not provide an export named 'default'
    at ModuleJob._instantiate (node:internal/modules/esm/module_job:134:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:217:5)
    at async ModuleLoader.import (node:internal/modules/esm/loader:323:24)
    at async loadConfigFromBundledFile (file:///Users/ewilliam/Projects/inkling/node_modules/.pnpm/vite@5.2.11_@types+node@20.12.12/node_modules/vite/dist/node/chunks/dep-cNe07EU9.js:68705:21)
    at async loadConfigFromFile (file:///Users/ewilliam/Projects/inkling/node_modules/.pnpm/vite@5.2.11_@types+node@20.12.12/node_modules/vite/dist/node/chunks/dep-cNe07EU9.js:68558:28)
    at async resolveConfig (file:///Users/ewilliam/Projects/inkling/node_modules/.pnpm/vite@5.2.11_@types+node@20.12.12/node_modules/vite/dist/node/chunks/dep-cNe07EU9.js:68161:28)
    at async _createServer (file:///Users/ewilliam/Projects/inkling/node_modules/.pnpm/vite@5.2.11_@types+node@20.12.12/node_modules/vite/dist/node/chunks/dep-cNe07EU9.js:64686:20)
    at async CAC.<anonymous> (file:///Users/ewilliam/Projects/inkling/node_modules/.pnpm/vite@5.2.11_@types+node@20.12.12/node_modules/vite/dist/node/cli.js:762:24)
 ELIFECYCLE  Command failed with exit code 1.```
smwhr commented 4 months ago

Latest version (2.2.4) should solve all your problems ! Let me know.

ewilliam commented 4 months ago

yes it does!! thanks so much

GavanWilhite commented 4 months ago

I'm having issues importing Story in next.js I got it to work in 2.2.3 by doing this:

//@ts-ignore import { Story } from 'inkjs/dist/ink-full';

I also cannot seem to import Choice anymore like I used to: import Choice from 'inkjs/ink/engine/Choice';

(This works in 2.2.3)

Thoughts?

smwhr commented 4 months ago

In 2.2.4, you should be able to do

import {Choice} from "inkjs"

and if you need the Compiler

import {Compiler} from "inkjs/full"

There are no improvement between 2.2.3 and 2.2.4 except this change of packaging so if 2.2.3 works,you're better off not changing version

GavanWilhite commented 4 months ago

Hmm, neither of those work for me in 2.2.4

{
    "compilerOptions": {
        "target": "es2017",
        "lib": ["dom", "dom.iterable", "esnext"],
        "allowJs": true,
        "skipLibCheck": true,
        "strict": true,
        "forceConsistentCasingInFileNames": true,
        "noEmit": true,
        "esModuleInterop": true,
        "module": "esnext",
        "moduleResolution": "bundler",
        "resolveJsonModule": true,
        "isolatedModules": true,
        "jsx": "preserve",
        "incremental": true,
        "plugins": [
            {
                "name": "next"
            }
        ],
        "paths": {
            "@/*": ["./app/*"]
        }
    },
    "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
    "exclude": ["node_modules"]
}

I can freeze my version for now, but I'm expecting we'll need to update at some point here

smwhr commented 4 months ago

As of now, there are no scheduled updates of the ink runtime or the inkjs library.