123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432 |
- "use strict";
- Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
- const fs = require("node:fs");
- const path = require("node:path");
- const node_module = require("node:module");
- const vite = require("vite");
- const esbuild = require("esbuild");
- const os = require("node:os");
- const keywords = [
- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#reserved_words
- ...[
- "break",
- "case",
- "catch",
- "class",
- "const",
- "continue",
- "debugger",
- "default",
- "delete",
- "do",
- "else",
- "export",
- "extends",
- "false",
- "finally",
- "for",
- "function",
- "if",
- "import",
- "in",
- "instanceof",
- "new",
- "null",
- "return",
- "super",
- "switch",
- "this",
- "throw",
- "true",
- "try",
- "typeof",
- "var",
- "void",
- "while",
- "with",
- // The following are only reserved when they are found in strict mode code
- "const",
- "let",
- "static",
- "yield",
- // The following are only reserved when they are found in module code or async function bodies
- "await"
- ],
- ...[
- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#future_reserved_words
- "enum",
- // The following are only reserved when they are found in strict mode code
- "implements",
- "interface",
- "package",
- "private",
- "protected",
- "public",
- // Future reserved words in older standards
- // The following are reserved as future keywords by older ECMAScript specifications (ECMAScript 1 till 3).
- "abstract",
- "boolean",
- "byte",
- "char",
- "double",
- "final",
- "float",
- "goto",
- "int",
- "long",
- "native",
- "short",
- "synchronized",
- "throws",
- "transient",
- "volatile"
- ],
- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#identifiers_with_special_meanings
- ...[
- "arguments",
- "as",
- "async",
- "eval",
- "from",
- "get",
- "of",
- "set"
- ]
- ];
- function libEsm(options) {
- const {
- window,
- require: require2,
- exports: members = [],
- conflict = ""
- } = options;
- const _M_ = "_M_" + conflict;
- const windowSnippet = window == null ? "" : `const ${_M_} = window["${window}"];`;
- const requireSnippet = require2 == null ? "" : `
- import { createRequire } from "node:module";
- const ${_M_} = createRequire(import.meta.url)("${require2}");
- `.trim();
- !members.includes("default") && members.push("default");
- const alias = members.filter((member) => keywords.includes(member)).reduce((memo, keyword) => Object.assign(memo, { [keyword]: `keyword_${keyword + conflict}` }), {});
- const exportsSnippet = `
- ${members.map((member) => {
- const LV = alias[member] ? `const ${alias[member]}` : `export const ${member}`;
- const RV = member === "default" ? `${_M_}.default || ${_M_}` : `${_M_}.${member}`;
- return `${LV} = ${RV};`;
- }).join("\n")}
- export {
- ${Object.entries(alias).map(([member, alias2]) => `${alias2} as ${member},`).join("\n ")}
- };
- `.trim();
- return {
- /** `window[iife-name]` snippets */
- window: windowSnippet,
- /** `require(id)` snippets */
- require: requireSnippet,
- /** `export` snippets */
- exports: exportsSnippet,
- /** Keywords alias */
- keywords: alias
- };
- }
- function relativeify(relative) {
- if (relative === "") {
- return ".";
- }
- if (!relative.startsWith("./") || !relative.startsWith(".\\")) {
- return "./" + relative;
- }
- return relative;
- }
- const isWindows = os.platform() === "win32";
- function slash(p) {
- return p.replace(/\\/g, "/");
- }
- function normalizePath(id) {
- return path.posix.normalize(isWindows ? slash(id) : id);
- }
- const COLOURS = {
- $: (c) => (str) => `\x1B[${c}m` + str + "\x1B[0m",
- gary: (str) => COLOURS.$(90)(str),
- cyan: (str) => COLOURS.$(36)(str),
- yellow: (str) => COLOURS.$(33)(str),
- green: (str) => COLOURS.$(32)(str),
- red: (str) => COLOURS.$(31)(str)
- };
- const VOLUME_RE = /^[A-Z]:/i;
- function node_modules(root, paths = []) {
- if (!root)
- return paths;
- if (!(root.startsWith("/") || VOLUME_RE.test(root)))
- return paths;
- const p = path.posix.join(normalizePath(root), "node_modules");
- if (fs.existsSync(p) && fs.statSync(p).isDirectory()) {
- paths = paths.concat(p);
- }
- root = path.posix.join(root, "..");
- return root === "/" || /^[A-Z]:$/i.test(root) ? paths : node_modules(root, paths);
- }
- const require$1 = node_module.createRequire(typeof document === "undefined" ? require("url").pathToFileURL(__filename).href : document.currentScript && document.currentScript.src || new URL("index.js", document.baseURI).href);
- const builtins = node_module.builtinModules.filter((m) => !m.startsWith("_"));
- const electronBuiltins = [
- "electron",
- ...builtins,
- ...builtins.map((module2) => `node:${module2}`)
- ];
- const CACHE_DIR = ".vite-electron-renderer";
- const TAG = "[electron-renderer]";
- const cwd = vite.normalizePath(process.cwd());
- const electron = `
- const electron = typeof require !== 'undefined'
- // All exports module see https://www.electronjs.org -> API -> Renderer process Modules
- ? (function requireElectron() {
- const avoid_parse_require = require;
- return avoid_parse_require("electron");
- }())
- : (function nodeIntegrationWarn() {
- console.error(\`If you need to use "electron" in the Renderer process, make sure that "nodeIntegration" is enabled in the Main process.\`);
- return {
- // TODO: polyfill
- };
- }());
- // Proxy in Worker
- let _ipcRenderer;
- if (typeof document === 'undefined') {
- _ipcRenderer = {};
- const keys = [
- 'invoke',
- 'postMessage',
- 'send',
- 'sendSync',
- 'sendTo',
- 'sendToHost',
- // propertype
- 'addListener',
- 'emit',
- 'eventNames',
- 'getMaxListeners',
- 'listenerCount',
- 'listeners',
- 'off',
- 'on',
- 'once',
- 'prependListener',
- 'prependOnceListener',
- 'rawListeners',
- 'removeAllListeners',
- 'removeListener',
- 'setMaxListeners',
- ];
- for (const key of keys) {
- _ipcRenderer[key] = () => {
- throw new Error(
- 'ipcRenderer doesn\\'t work in a Web Worker.\\n' +
- 'You can see https://github.com/electron-vite/vite-plugin-electron/issues/69'
- );
- };
- }
- } else {
- _ipcRenderer = electron.ipcRenderer;
- }
- export { electron as default };
- export const clipboard = electron.clipboard;
- export const contextBridge = electron.contextBridge;
- export const crashReporter = electron.crashReporter;
- export const ipcRenderer = _ipcRenderer;
- export const nativeImage = electron.nativeImage;
- export const shell = electron.shell;
- export const webFrame = electron.webFrame;
- export const deprecate = electron.deprecate;
- `.trim();
- function renderer(options = {}) {
- let root;
- let cacheDir;
- const resolveKeys = [];
- const moduleCache = /* @__PURE__ */ new Map();
- return {
- name: "vite-plugin-electron-renderer",
- async config(config, { command }) {
- root = vite.normalizePath(config.root ? path.resolve(config.root) : cwd);
- cacheDir = path.posix.join(node_modules(root)[0] ?? cwd, CACHE_DIR);
- for (const [key, option] of Object.entries(options.resolve ?? {})) {
- if (command === "build" && option.type === "esm") {
- continue;
- }
- resolveKeys.push(key);
- }
- const aliases = [{
- find: new RegExp(`^(?:node:)?(${["electron", ...builtins].join("|")})$`),
- // https://github.com/rollup/plugins/blob/alias-v5.0.0/packages/alias/src/index.ts#L90
- replacement: "$1",
- async customResolver(source) {
- let id = moduleCache.get(source);
- if (!id) {
- id = path.posix.join(cacheDir, source) + ".mjs";
- if (!fs.existsSync(id)) {
- ensureDir(path.dirname(id));
- fs.writeFileSync(
- // lazy build
- id,
- source === "electron" ? electron : getSnippets({ import: source, export: source })
- );
- }
- moduleCache.set(source, id);
- }
- return { id };
- }
- }];
- resolveKeys.length && aliases.push({
- find: new RegExp(`^(${resolveKeys.join("|")})$`),
- replacement: "$1",
- async customResolver(source, importer, resolveOptions) {
- var _a;
- let id = moduleCache.get(source);
- if (!id) {
- const filename = path.posix.join(cacheDir, source) + ".mjs";
- if (fs.existsSync(filename)) {
- id = filename;
- } else {
- const resolved = (_a = options.resolve) == null ? void 0 : _a[source];
- if (resolved) {
- let snippets;
- if (typeof resolved.build === "function") {
- snippets = await resolved.build({
- cjs: (module2) => Promise.resolve(getSnippets({ import: module2, export: module2 })),
- esm: (module2, buildOptions) => getPreBundleSnippets({
- module: module2,
- outdir: cacheDir,
- buildOptions
- })
- });
- } else if (resolved.type === "cjs") {
- snippets = getSnippets({ import: source, export: source });
- } else if (resolved.type === "esm") {
- snippets = await getPreBundleSnippets({
- module: source,
- outdir: cacheDir
- });
- }
- console.log(
- COLOURS.gary(TAG),
- COLOURS.cyan("pre-bundling"),
- COLOURS.yellow(source)
- );
- ensureDir(path.dirname(filename));
- fs.writeFileSync(filename, snippets ?? `/* ${TAG}: empty */`);
- id = filename;
- } else {
- id = source;
- }
- }
- moduleCache.set(source, id);
- }
- return id === source ? this.resolve(
- source,
- importer,
- Object.assign({ skipSelf: true }, resolveOptions)
- ).then((resolved) => resolved || { id: source }) : { id };
- }
- });
- modifyAlias(config, aliases);
- modifyOptimizeDeps(config, resolveKeys);
- adaptElectron(config);
- }
- };
- }
- function adaptElectron(config) {
- var _a;
- config.base ?? (config.base = "./");
- config.build ?? (config.build = {});
- (_a = config.build).rollupOptions ?? (_a.rollupOptions = {});
- setOutputFreeze(config.build.rollupOptions);
- withIgnore(config.build, electronBuiltins);
- }
- function setOutputFreeze(rollupOptions) {
- var _a;
- rollupOptions.output ?? (rollupOptions.output = {});
- if (Array.isArray(rollupOptions.output)) {
- for (const o of rollupOptions.output) {
- o.freeze ?? (o.freeze = false);
- }
- } else {
- (_a = rollupOptions.output).freeze ?? (_a.freeze = false);
- }
- }
- function withIgnore(configBuild, modules) {
- configBuild.commonjsOptions ?? (configBuild.commonjsOptions = {});
- if (configBuild.commonjsOptions.ignore) {
- if (typeof configBuild.commonjsOptions.ignore === "function") {
- const userIgnore = configBuild.commonjsOptions.ignore;
- configBuild.commonjsOptions.ignore = (id) => {
- if ((userIgnore == null ? void 0 : userIgnore(id)) === true) {
- return true;
- }
- return modules.includes(id);
- };
- } else {
- configBuild.commonjsOptions.ignore.push(...modules);
- }
- } else {
- configBuild.commonjsOptions.ignore = modules;
- }
- }
- function modifyOptimizeDeps(config, exclude) {
- var _a;
- config.optimizeDeps ?? (config.optimizeDeps = {});
- (_a = config.optimizeDeps).exclude ?? (_a.exclude = []);
- for (const str of exclude) {
- if (!config.optimizeDeps.exclude.includes(str)) {
- config.optimizeDeps.exclude.push(str);
- }
- }
- }
- function modifyAlias(config, aliases) {
- var _a;
- config.resolve ?? (config.resolve = {});
- (_a = config.resolve).alias ?? (_a.alias = []);
- if (Object.prototype.toString.call(config.resolve.alias) === "[object Object]") {
- config.resolve.alias = Object.entries(config.resolve.alias).reduce((memo, [find, replacement]) => memo.concat({ find, replacement }), []);
- }
- config.resolve.alias.push(...aliases);
- }
- function getSnippets(module2) {
- const { exports: exports2 } = libEsm({ exports: Object.getOwnPropertyNames(
- /* not await import */
- require$1(module2.import)
- ) });
- return `const avoid_parse_require = require; const _M_ = avoid_parse_require("${module2.export}");
- ${exports2}`;
- }
- async function getPreBundleSnippets(options) {
- const {
- module: module2,
- outdir,
- buildOptions = {}
- } = options;
- const outfile = path.posix.join(outdir, module2) + ".cjs";
- await esbuild.build({
- entryPoints: [module2],
- outfile,
- target: "node14",
- format: "cjs",
- bundle: true,
- sourcemap: "inline",
- platform: "node",
- external: electronBuiltins,
- ...buildOptions
- });
- return getSnippets({
- import: outfile,
- // `require()` in script-module lookup path based on `process.cwd()` 🤔
- export: relativeify(path.posix.relative(cwd, outfile))
- });
- }
- function ensureDir(dirname) {
- if (!fs.existsSync(dirname)) {
- fs.mkdirSync(dirname, { recursive: true });
- }
- }
- exports.default = renderer;
- exports.electron = electron;
|