Skip to content

[vite 3.0.0-alpha.5+] Cannot define with stringified object value #8593

Closed
@IanVS

Description

Describe the bug

Starting in alpha.5, it's no longer possible to have a config like:

export default defineConfig({
  define: {
    foo: JSON.stringify({ foo: true }),
  },
});

During build (but not dev), it will throw an error like:

Unexpected token (Note that you need plugins to import files that are not JavaScript)
file: /home/projects/vitejs-vite-bjukr5/node_modules/.vite/depsBuild/chunk-UC5OZ2ED.js?v=cf0d5223:11:13
 9: // <define:foo>
10: var init_define_foo = __esm({
11:   "<define:foo>"() {
                 ^
12:   }
13: });
error during build:
Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
    at error (file:///home/projects/vitejs-vite-bjukr5/node_modules/rollup/dist/es/shared/rollup.js:1888:30)
    at Module.error (file:///home/projects/vitejs-vite-bjukr5/node_modules/rollup/dist/es/shared/rollup.js:12452:16)
    at Module.tryParse (file:///home/projects/vitejs-vite-bjukr5/node_modules/rollup/dist/es/shared/rollup.js:12829:25)
    at Module.setSource (file:///home/projects/vitejs-vite-bjukr5/node_modules/rollup/dist/es/shared/rollup.js:12734:24)
    at ModuleLoader.addModuleSource (file:///home/projects/vitejs-vite-bjukr5/node_modules/rollup/dist/es/shared/rollup.js:22208:20)

This is currently what's breaking the storybook vite-builder in vite 3.

Reproduction

https://stackblitz.com/edit/vitejs-vite-bjukr5?file=package.json

System Info

n/a

Used Package Manager

npm

Logs

No response

Validations

Metadata

Assignees

No one assigned

    Labels

    p4-importantViolate documented behavior or significantly improves performance (priority)

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions