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

[Next][Fix] Remove require() in index.js for scriptDir #397

Merged
merged 1 commit into from
May 14, 2024

Conversation

CharlieFRuan
Copy link
Contributor

@CharlieFRuan CharlieFRuan commented May 14, 2024

Overview

This PR post-processes web-llm's index.js by replacing all new (require('u' + 'rl').URL)('file:' + __filename).href with "MLC_DUMMY_PATH":

var _scriptDir = 
(typeof document === 'undefined' && typeof location === 'undefined' ? 
  "MLC_DUMMY_PATH": 
  typeof document === 'undefined' ? 
    location.href : 
    (document.currentScript && document.currentScript.src || new URL('index.js', document.baseURI).href)
);

which previously would raise error as shown in #383 and other issues.

Other occurrences of "MLC_DUMMY_PATH" are only in branches of if (ENVIRONMENT_IS_NODE) in runtime, which we do not consider / support as of now.

We use "MLC_DUMMY_PATH" instead of null because 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 for require(); runtime does not run into it because document is not undefined when evaluating _scriptDir. Other examples, like examples/chrome-extension, do not have this issue because they build with parcel, which would fix it for us with @parcel/resolver-default:
image

This PR's fix does not affect correctness because, by inspecting index.js, _scriptDir is used to populate scriptDirectory, which is used in the function locateFile(), which currently is only used for wasmBinaryFile (but isDataURI(wasmBinaryFile) would never evaluate to false):

function locateFile(path) {
    if (Module["locateFile"]) { return Module["locateFile"](path, scriptDirectory) }
    return scriptDirectory + path
}

if (!isDataURI(wasmBinaryFile)) {
    wasmBinaryFile = locateFile(wasmBinaryFile);
}

We also do not remove other require() in index.js as 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", in examples/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 use require() in compile time.

@tqchen tqchen merged commit e891078 into mlc-ai:main May 14, 2024
CharlieFRuan added a commit that referenced this pull request May 21, 2024
### Changes
Main changes include:
- New model `Hermes-2-Pro-Mistral-7B` in `prebuiltAppConfig` via:
  - #390
- Various `index.js` and `index.js.map` post-processings to resolve
frontend compatibility issues with `require()` and `perf_hoooks`
  - #397
  - #406
- Catch WebGPU OOM error upon `reload()` and `CreateEngine()`:
  - #402
- Service Worker support (in addition to Extension Service Worker):
  - #395
  - #400
  - #401

### WASM Version
v0_2_34 as no change is required.

### TVMjs
TVMjs compiled at
apache/tvm@a5862a5,
with only one change in `tvm/web`:
apache/tvm#17005
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants