Skip to content

Instantly share code, notes, and snippets.

@songlairui
Created November 4, 2019 05:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save songlairui/fad3675873985676bb47b14829e4186e to your computer and use it in GitHub Desktop.
Save songlairui/fad3675873985676bb47b14829e4186e to your computer and use it in GitHub Desktop.
electron next 多inst
'use strict';
var __importDefault =
(this && this.__importDefault) ||
function(mod) {
return mod && mod.__esModule ? mod : { default: mod };
};
var __importStar =
(this && this.__importStar) ||
function(mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null)
for (var k in mod)
if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result['default'] = mod;
return result;
};
Object.defineProperty(exports, '__esModule', { value: true });
// Native
const path_1 = require('path');
// Packages
const electron_1 = require('electron');
const electron_is_dev_1 = __importDefault(require('electron-is-dev'));
const app_root_path_1 = require('app-root-path');
// TODO build 支持多入口
const adjustRenderer = directories => {
const paths = ['/_next', '/static'];
const isWindows = process.platform === 'win32';
electron_1.protocol.interceptFileProtocol('file', (request, callback) => {
let path = request.url.substr(isWindows ? 8 : 7);
path = path.replace(/(_m_\d*)\/(_m_\d*)/, '$1');
if (path.startsWith('/_m_')) {
path = path_1.join(__dirname, '../', path);
} else if (path === '/about') {
path = path_1.join(__dirname, '../_m_0', `${path}.html`);
} else if (path === '/initial-props') {
path = path_1.join(__dirname, '../_m_1', `${path}.html`);
}
for (const prefix of paths) {
let newPath = path;
// On windows the request looks like: file:///C:/static/bar
// On other systems it's file:///static/bar
if (isWindows) {
newPath = newPath.substr(2);
}
if (!newPath.startsWith(prefix)) {
continue;
}
// Strip volume name from path on Windows
if (isWindows) {
newPath = path_1.normalize(newPath);
}
newPath = path_1.join(directories[0], 'out', newPath);
path = newPath;
}
// Electron doesn't like anything in the path to be encoded,
// so we need to undo that. This specifically allows for
// Electron apps with spaces in their app names.
path = decodeURIComponent(path);
callback(path);
});
};
exports.default = async (directory, port) => {
if (!directory || !directory.length) {
throw new Error('Renderer location not defined');
}
if (!Array.isArray(directory)) {
directory = [directory];
}
const directories = directory.map(item =>
path_1.isAbsolute(item) ? item : app_root_path_1.resolve(item),
);
if (!electron_is_dev_1.default) {
adjustRenderer(directories);
return;
}
// 不可前置加载
const { devServer } = await Promise.resolve().then(() =>
__importStar(require('./multiDevServ')),
);
return await devServer(directories, port, server => {
electron_1.app.on('before-quit', () => server.close());
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment