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

Need to treat json files and content as javascript #19129

Closed
4 tasks done
rawasaditya opened this issue Jan 3, 2025 · 2 comments
Closed
4 tasks done

Need to treat json files and content as javascript #19129

rawasaditya opened this issue Jan 3, 2025 · 2 comments

Comments

@rawasaditya
Copy link

rawasaditya commented Jan 3, 2025

Description

In our project we have alias imports we have written a plugin called aliasResolPlugin which will return absolute path in resolveId
Eg:
index.js
import js/xyz

after resolveId hook
index.js
import <absolute_path>/xyz.js

Now
I have written another plugin JsonUpdatePlugin which will add javascript code in *.json files, I have disabled vite:json plugin so that parsing will not throw an error

Eg:
index.json
import js/xyz

Since vite is not invoking resolveId hook for imports in json file of aliasResolPlugin I am not able replace these aliases with absolute Path

Suggested solution

There has to be a way to treat txt file or json file as javascript so that hook can get triggered

Alternative

No response

Additional context

No response

Validations

@nozomuikuta
Copy link
Contributor

Would you provide a minimal reproduction or a link to the repository so that other people can understand your issue better.
Knowing what to achieve (why you want to add JS code to JSON code), we can discuss how to achieve (currently only how is described).

@bluwy
Copy link
Member

bluwy commented Jan 7, 2025

I assume this code is blocking from what you want to achieve:

const skipRE = /\.(?:map|json)(?:$|\?)/
export const canSkipImportAnalysis = (id: string): boolean =>
skipRE.test(id) || isDirectCSSRequest(id)

Since JSON processing is builtin and your project is overriding its behaviour, I'd suggest having your plugin worked around it for now. In resolveId() you can resolve the .json file to e.g. .json.js, that way Vite doesn't see it as a JSON file and will process it.

I don't think Vite should open up or process JSON files as it adds overhead for everyone even if they don't change the JSON handling.

@bluwy bluwy closed this as not planned Won't fix, can't repro, duplicate, stale Jan 7, 2025
@github-actions github-actions bot locked and limited conversation to collaborators Jan 22, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants