Skip to content

Commit 7c86273

Browse files
author
Loïc Mangeonjean
committed
fix(demo): properly resolve import.meta.url in watch mode
1 parent e641ac3 commit 7c86273

File tree

3 files changed

+41
-16
lines changed

3 files changed

+41
-16
lines changed

README.md

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ While it works great in `build` mode (because rollup is used), there is some iss
4646

4747
There are workarounds for both:
4848

49-
- We can help vite by replacing `import.meta.url` by the original module path:
49+
- We can help vite by replacing `import.meta.url` by the original module path (you need the --experimental-import-meta-resolve note option):
5050
```typescript
5151
{
5252
...
@@ -56,13 +56,25 @@ There are workarounds for both:
5656
name: 'import.meta.url',
5757
setup ({ onLoad }) {
5858
// Help vite that bundles/move files in dev mode without touching `import.meta.url` which breaks asset urls
59-
onLoad({ filter: /.*\.js/, namespace: 'file' }, args => {
60-
let code = fs.readFileSync(args.path, 'utf8')
61-
code = code.replace(
62-
/\bimport\.meta\.url\b/g,
63-
`new URL('/@fs${args.path}', window.location.origin)`
64-
)
65-
return { contents: code }
59+
onLoad({ filter: /.*\.js/, namespace: 'file' }, async args => {
60+
const code = fs.readFileSync(args.path, 'utf8')
61+
62+
const assetImportMetaUrlRE = /\bnew\s+URL\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*,\s*import\.meta\.url\s*(?:,\s*)?\)/g
63+
let i = 0
64+
let newCode = ''
65+
for (let match = assetImportMetaUrlRE.exec(code); match != null; match = assetImportMetaUrlRE.exec(code)) {
66+
newCode += code.slice(i, match.index)
67+
68+
const path = match[1].slice(1, -1)
69+
const resolved = await import.meta.resolve!(path, url.pathToFileURL(args.path))
70+
71+
newCode += `new URL(${JSON.stringify(url.fileURLToPath(resolved))}, import.meta.url)`
72+
73+
i = assetImportMetaUrlRE.lastIndex
74+
}
75+
newCode += code.slice(i)
76+
77+
return { contents: newCode }
6678
})
6779
}
6880
}]

demo/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"postinstall": "monaco-treemending",
88
"clean": "tsc -b -c",
99
"compile": "tsc",
10-
"start": "vite --config vite.config.ts",
10+
"start": "NODE_OPTIONS=--experimental-import-meta-resolve vite --config vite.config.ts",
1111
"start:debug": "vite --config vite.config.ts --debug --force",
1212
"build": "vite --config vite.config.ts build",
1313
"build:github": "vite --config vite.github-page.config.ts build && touch dist/.nojekyll",

demo/vite.config.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { defineConfig } from 'vite'
22
import * as fs from 'fs'
3+
import url from 'url'
34

45
const cdnDomain = 'http://127.0.0.2:5173'
56

@@ -55,13 +56,25 @@ export default defineConfig({
5556
name: 'import.meta.url',
5657
setup ({ onLoad }) {
5758
// Help vite that bundles/move files in dev mode without touching `import.meta.url` which breaks asset urls
58-
onLoad({ filter: /.*\.js/, namespace: 'file' }, args => {
59-
let code = fs.readFileSync(args.path, 'utf8')
60-
code = code.replace(
61-
/\bimport\.meta\.url\b/g,
62-
`new URL('${cdnDomain}/@fs${args.path}', window.location.origin)`
63-
)
64-
return { contents: code }
59+
onLoad({ filter: /.*\.js/, namespace: 'file' }, async args => {
60+
const code = fs.readFileSync(args.path, 'utf8')
61+
62+
const assetImportMetaUrlRE = /\bnew\s+URL\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*,\s*import\.meta\.url\s*(?:,\s*)?\)/g
63+
let i = 0
64+
let newCode = ''
65+
for (let match = assetImportMetaUrlRE.exec(code); match != null; match = assetImportMetaUrlRE.exec(code)) {
66+
newCode += code.slice(i, match.index)
67+
68+
const path = match[1].slice(1, -1)
69+
const resolved = await import.meta.resolve!(path, url.pathToFileURL(args.path))
70+
71+
newCode += `new URL(${JSON.stringify(url.fileURLToPath(resolved))}, import.meta.url)`
72+
73+
i = assetImportMetaUrlRE.lastIndex
74+
}
75+
newCode += code.slice(i)
76+
77+
return { contents: newCode }
6578
})
6679
}
6780
}]

0 commit comments

Comments
 (0)