diff --git a/import.meta.url-polyfill.js b/import.meta.url-polyfill.js new file mode 100644 index 0000000..6b71383 --- /dev/null +++ b/import.meta.url-polyfill.js @@ -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) diff --git a/package.json b/package.json index 3c9511f..85abb12 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.ts b/src/index.ts index fbf951a..08f54e6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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' @@ -224,7 +225,7 @@ function resolveLaravelPlugin(pluginConfig: Required): 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) ) } @@ -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 '' } @@ -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)) +}