Skip to content

Conversation

@CharlieFRuan
Copy link
Member

@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
atebites-hub pushed a commit to atebites-hub/web-llm that referenced this pull request Oct 4, 2025
### 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"`:

```javascript
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
mlc-ai#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](https://github.com/mlc-ai/web-llm/assets/53290280/0b9df99a-f80e-4fed-8c19-88deb8aabfbd)

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`):

```javascript
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`,
mlc-ai#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.
atebites-hub pushed a commit to atebites-hub/web-llm that referenced this pull request Oct 4, 2025
### Changes
Main changes include:
- New model `Hermes-2-Pro-Mistral-7B` in `prebuiltAppConfig` via:
  - mlc-ai#390
- Various `index.js` and `index.js.map` post-processings to resolve
frontend compatibility issues with `require()` and `perf_hoooks`
  - mlc-ai#397
  - mlc-ai#406
- Catch WebGPU OOM error upon `reload()` and `CreateEngine()`:
  - mlc-ai#402
- Service Worker support (in addition to Extension Service Worker):
  - mlc-ai#395
  - mlc-ai#400
  - mlc-ai#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.

2 participants