[Next][Fix] Remove require() in index.js for scriptDir #397
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
This PR post-processes web-llm's
index.jsby replacing allnew (require('u' + 'rl').URL)('file:' + __filename).hrefwith"MLC_DUMMY_PATH":which previously would raise error as shown in #383 and other issues.
Other occurrences of
"MLC_DUMMY_PATH"are only in branches ofif (ENVIRONMENT_IS_NODE)in runtime, which we do not consider / support as of now.We use
"MLC_DUMMY_PATH"instead ofnullbecause we do not expect the value to be used at all. If that is not the case, it would be easier to debug with"MLC_DUMMY_PATH".Details
When building projects that use web-llm with

next(e.g.examples/next-simple-chat), the compile time would complain about the call forrequire(); runtime does not run into it becausedocumentis notundefinedwhen evaluating_scriptDir. Other examples, likeexamples/chrome-extension, do not have this issue because they build withparcel, which would fix it for us with@parcel/resolver-default:This PR's fix does not affect correctness because, by inspecting
index.js,_scriptDiris used to populatescriptDirectory, which is used in the functionlocateFile(), which currently is only used forwasmBinaryFile(butisDataURI(wasmBinaryFile)would never evaluate tofalse):We also do not remove other
require()inindex.jsas of now, as from the current understanding, they would not cause issues -- but we can come back later when they do.One observation that is not yet explainable is that, if we set
"@mlc-ai/web-llm": "^0.2.35",inexamples/next-simple-chat/package.json, #383 would be observed. However, if we use"@mlc-ai/web-llm": "../..",, no issue is observed -- we are able to userequire()in compile time.