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

feat: watch public files that are imported by JavaScript #9770

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions packages/vite/src/node/plugins/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export function assetPlugin(config: ResolvedConfig): Plugin {
// will fail to resolve in the main resolver. handle them here.
const publicFile = checkPublicFile(id, config)
if (publicFile) {
return id
return { id: publicFile, meta: { publicUrl: id } }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So basically, the full file path is returned as the resolved ID and the original module URL is cached in meta.publicUrl for the assetPlugin to use.

Copy link
Member

@bluwy bluwy Sep 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we call this.addWatchFile(publicFile) instead? Or maybe it's better to always return the absolute path, but I'm not sure any side effects with that.

}
},

Expand All @@ -148,15 +148,18 @@ export function assetPlugin(config: ResolvedConfig): Plugin {
return
}

const moduleInfo = this.getModuleInfo(id)
const meta = (moduleInfo?.meta || {}) as { publicUrl?: string }

// raw requests, read from disk
if (rawRE.test(id)) {
const file = checkPublicFile(id, config) || cleanUrl(id)
// raw query, read file and return as string
if (rawRE.test(meta.publicUrl || id)) {
const file = meta.publicUrl ? id : cleanUrl(id)
return `export default ${JSON.stringify(
await fsp.readFile(file, 'utf-8')
)}`
}

id = meta.publicUrl || id
if (!config.assetsInclude(cleanUrl(id)) && !urlRE.test(id)) {
return
}
Expand Down