Skip to content

Commit

Permalink
Polyfill import.meta.url for CJS builds (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald authored May 10, 2023
1 parent 312828b commit 6c0d05a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
3 changes: 3 additions & 0 deletions import.meta.url-polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const import_meta_url = typeof document === 'undefined'
? new (require('url'.replace('', '')).URL)('file:' + __filename).href
: (document.currentScript && document.currentScript.src || new URL('main.js', document.baseURI).href)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"build": "npm run build-plugin && npm run build-inertia-helpers",
"build-plugin": "rm -rf dist && npm run build-plugin-types && npm run build-plugin-esm && npm run build-plugin-cjs && cp src/dev-server-index.html dist/",
"build-plugin-types": "tsc --emitDeclarationOnly",
"build-plugin-cjs": "esbuild src/index.ts --platform=node --format=cjs --outfile=dist/index.cjs",
"build-plugin-cjs": "esbuild src/index.ts --platform=node --format=cjs --outfile=dist/index.cjs --define:import.meta.url=import_meta_url --inject:./import.meta.url-polyfill.js",
"build-plugin-esm": "esbuild src/index.ts --platform=node --format=esm --outfile=dist/index.mjs",
"build-inertia-helpers": "rm -rf inertia-helpers && tsc --project tsconfig.inertia-helpers.json",
"lint": "eslint --ext .ts ./src ./tests",
Expand Down
12 changes: 10 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'fs'
import { AddressInfo } from 'net'
import os from 'os'
import { fileURLToPath } from 'url'
import path from 'path'
import colors from 'picocolors'
import { Plugin, loadEnv, UserConfig, ConfigEnv, ResolvedConfig, SSROptions, PluginOption } from 'vite'
Expand Down Expand Up @@ -224,7 +225,7 @@ function resolveLaravelPlugin(pluginConfig: Required<PluginConfig>): LaravelPlug
res.statusCode = 404

res.end(
fs.readFileSync(path.join(__dirname, 'dev-server-index.html')).toString().replace(/{{ APP_URL }}/g, appUrl)
fs.readFileSync(path.join(dirname(), 'dev-server-index.html')).toString().replace(/{{ APP_URL }}/g, appUrl)
)
}

Expand Down Expand Up @@ -277,7 +278,7 @@ function laravelVersion(): string {
*/
function pluginVersion(): string {
try {
return JSON.parse(fs.readFileSync(path.join(__dirname, '../package.json')).toString())?.version
return JSON.parse(fs.readFileSync(path.join(dirname(), '../package.json')).toString())?.version
} catch {
return ''
}
Expand Down Expand Up @@ -537,3 +538,10 @@ function resolveValetHost(): string {

return path.basename(process.cwd()) + '.' + config.tld
}

/**
* The directory of the current file.
*/
function dirname(): string {
return fileURLToPath(new URL('.', import.meta.url))
}

0 comments on commit 6c0d05a

Please sign in to comment.