2022-04-06 13:28:42 -04:00
|
|
|
const pluginConfig = require('../pluginConfig.json');
|
|
|
|
|
const fs = require('fs');
|
2023-05-08 21:18:35 -04:00
|
|
|
const os = require('os');
|
2026-06-12 16:04:56 -04:00
|
|
|
const path = require('path');
|
2022-04-06 13:28:42 -04:00
|
|
|
|
|
|
|
|
const autogenerationDisclaimer = `
|
|
|
|
|
// THIS FILE IS AUTOGENERATED AS PART OF THE EXTENSION AND MODE PLUGIN PROCESS.
|
|
|
|
|
// IT SHOULD NOT BE MODIFIED MANUALLY \n`;
|
|
|
|
|
|
2023-09-01 16:19:39 -04:00
|
|
|
const extractName = val => (typeof val === 'string' ? val : val.packageName);
|
2023-05-08 21:18:35 -04:00
|
|
|
|
2025-03-27 16:47:16 -04:00
|
|
|
const publicURL = process.env.PUBLIC_URL || '/';
|
|
|
|
|
|
|
|
|
|
function isAbsolutePath(path) {
|
|
|
|
|
return path.startsWith('http') || path.startsWith('/');
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-06 13:28:42 -04:00
|
|
|
function constructLines(input, categoryName) {
|
|
|
|
|
let pluginCount = 0;
|
|
|
|
|
|
|
|
|
|
const lines = {
|
|
|
|
|
importLines: [],
|
|
|
|
|
addToWindowLines: [],
|
|
|
|
|
};
|
|
|
|
|
|
2022-07-27 16:39:04 +00:00
|
|
|
if (!input) return lines;
|
|
|
|
|
|
2022-04-06 13:28:42 -04:00
|
|
|
input.forEach(entry => {
|
2023-02-17 15:04:07 -05:00
|
|
|
if (entry.default === false) return;
|
2022-04-06 13:28:42 -04:00
|
|
|
|
2023-05-08 21:18:35 -04:00
|
|
|
const packageName = extractName(entry);
|
2022-04-06 13:28:42 -04:00
|
|
|
|
2023-09-01 16:19:39 -04:00
|
|
|
lines.addToWindowLines.push(`${categoryName}.push("${packageName}");\n`);
|
2022-04-06 13:28:42 -04:00
|
|
|
|
|
|
|
|
pluginCount++;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return lines;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getFormattedImportBlock(importLines) {
|
|
|
|
|
let content = '';
|
|
|
|
|
// Imports
|
|
|
|
|
importLines.forEach(importLine => {
|
|
|
|
|
content += importLine;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return content;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getFormattedWindowBlock(addToWindowLines) {
|
2022-08-17 15:13:59 -04:00
|
|
|
let content =
|
|
|
|
|
'const extensions = [];\n' +
|
|
|
|
|
'const modes = [];\n' +
|
2023-05-08 21:18:35 -04:00
|
|
|
'\n// Not required any longer\n' +
|
2022-08-17 15:13:59 -04:00
|
|
|
'window.extensions = extensions;\n' +
|
|
|
|
|
'window.modes = modes;\n\n';
|
2022-04-06 13:28:42 -04:00
|
|
|
|
|
|
|
|
addToWindowLines.forEach(addToWindowLine => {
|
|
|
|
|
content += addToWindowLine;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return content;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-08 21:18:35 -04:00
|
|
|
function getRuntimeLoadModesExtensions(modules) {
|
|
|
|
|
const dynamicLoad = [];
|
|
|
|
|
dynamicLoad.push(
|
|
|
|
|
'\n\n// Add a dynamic runtime loader',
|
|
|
|
|
'async function loadModule(module) {',
|
2023-09-01 16:19:39 -04:00
|
|
|
" if (typeof module !== 'string') return module;"
|
|
|
|
|
);
|
2023-05-08 21:18:35 -04:00
|
|
|
modules.forEach(module => {
|
|
|
|
|
const packageName = extractName(module);
|
2024-07-05 13:19:42 -04:00
|
|
|
if (!packageName) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (module.importPath) {
|
|
|
|
|
dynamicLoad.push(
|
|
|
|
|
` if( module==="${packageName}") {`,
|
2025-03-27 16:47:16 -04:00
|
|
|
` const imported = await window.browserImportFunction('${isAbsolutePath(module.importPath) ? '' : publicURL}${module.importPath}');`,
|
2024-11-28 13:22:40 -05:00
|
|
|
' return ' +
|
|
|
|
|
(module.globalName
|
|
|
|
|
? `window["${module.globalName}"];`
|
|
|
|
|
: `imported["${module.importName || 'default'}"];`),
|
2024-07-05 13:19:42 -04:00
|
|
|
' }'
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-05-08 21:18:35 -04:00
|
|
|
dynamicLoad.push(
|
|
|
|
|
` if( module==="${packageName}") {`,
|
|
|
|
|
` const imported = await import("${packageName}");`,
|
|
|
|
|
' return imported.default;',
|
|
|
|
|
' }'
|
|
|
|
|
);
|
2023-09-01 16:19:39 -04:00
|
|
|
});
|
2024-07-05 13:19:42 -04:00
|
|
|
// TODO - handle more cases for import than just default
|
2023-05-08 21:18:35 -04:00
|
|
|
dynamicLoad.push(
|
2024-07-05 13:19:42 -04:00
|
|
|
' return (await window.browserImportFunction(module)).default;',
|
2023-05-08 21:18:35 -04:00
|
|
|
'}\n',
|
|
|
|
|
'// Import a list of items (modules or string names)',
|
|
|
|
|
'// @return a Promise evaluating to a list of modules',
|
|
|
|
|
'export default function importItems(modules) {',
|
|
|
|
|
' return Promise.all(modules.map(loadModule));',
|
|
|
|
|
'}\n',
|
2023-09-01 16:19:39 -04:00
|
|
|
'export { loadModule, modes, extensions, importItems };\n\n'
|
|
|
|
|
);
|
2023-05-08 21:18:35 -04:00
|
|
|
return dynamicLoad.join('\n');
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-12 16:04:56 -04:00
|
|
|
const fromDirectory = (srcDir, dirPath) => {
|
|
|
|
|
if (!dirPath) return;
|
|
|
|
|
if (dirPath[0] === '.') return srcDir + '/../../..' + dirPath.substring(1);
|
|
|
|
|
if (dirPath[0] === '~') return os.homedir() + dirPath.substring(1);
|
|
|
|
|
return dirPath;
|
2023-09-01 16:19:39 -04:00
|
|
|
};
|
2022-07-27 16:39:04 +00:00
|
|
|
|
2026-06-12 16:04:56 -04:00
|
|
|
const APP_SRC_DIR = path.resolve(__dirname, '../src');
|
|
|
|
|
const REPO_ROOT = path.resolve(__dirname, '../../../');
|
|
|
|
|
|
|
|
|
|
// The set of plugin package names declared in pluginConfig.json. Resolution and
|
|
|
|
|
// asset copying are driven entirely by this list — a package present in the
|
|
|
|
|
// extensions/ or modes/ workspaces but NOT listed here is ignored, and an
|
|
|
|
|
// external (out-of-tree) package listed here with a `directory` is included.
|
|
|
|
|
let declaredPluginNamesCache;
|
|
|
|
|
function getDeclaredPluginNames() {
|
|
|
|
|
if (declaredPluginNamesCache) {
|
|
|
|
|
return declaredPluginNamesCache;
|
|
|
|
|
}
|
|
|
|
|
const names = new Set();
|
|
|
|
|
for (const entry of [...(pluginConfig.extensions || []), ...(pluginConfig.modes || [])]) {
|
|
|
|
|
const name = extractName(entry);
|
|
|
|
|
if (name) {
|
|
|
|
|
names.add(name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
declaredPluginNamesCache = names;
|
|
|
|
|
return names;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Map each in-tree plugin's real package name to its directory, but ONLY for the
|
|
|
|
|
// plugins declared in pluginConfig.json. This lets the bundler resolve those
|
|
|
|
|
// plugins from their source without them being dependencies of platform/app
|
|
|
|
|
// (and therefore without entries in package.json / the lockfile), while leaving
|
|
|
|
|
// undeclared workspace packages out of the build entirely.
|
|
|
|
|
let workspacePluginDirsCache;
|
|
|
|
|
function getWorkspacePluginDirs() {
|
|
|
|
|
if (workspacePluginDirsCache) {
|
|
|
|
|
return workspacePluginDirsCache;
|
|
|
|
|
}
|
|
|
|
|
const declared = getDeclaredPluginNames();
|
|
|
|
|
const map = {};
|
|
|
|
|
for (const group of ['extensions', 'modes']) {
|
|
|
|
|
const root = path.join(REPO_ROOT, group);
|
|
|
|
|
if (!fs.existsSync(root)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
for (const dir of fs.readdirSync(root)) {
|
|
|
|
|
const pkgJsonPath = path.join(root, dir, 'package.json');
|
|
|
|
|
if (!fs.existsSync(pkgJsonPath)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
const { name } = JSON.parse(fs.readFileSync(pkgJsonPath, 'utf8'));
|
|
|
|
|
if (name && declared.has(name)) {
|
|
|
|
|
map[name] = path.join(root, dir);
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
// ignore an unparseable package.json
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
workspacePluginDirsCache = map;
|
|
|
|
|
return map;
|
|
|
|
|
}
|
2022-08-17 15:13:59 -04:00
|
|
|
|
2026-06-12 16:04:56 -04:00
|
|
|
// Source directory of a workspace plugin: an explicit `directory` override wins
|
|
|
|
|
// (out-of-tree plugins), otherwise look it up among the in-tree workspaces by
|
|
|
|
|
// package name. Returns undefined for an external plugin that is instead
|
|
|
|
|
// installed as a normal dependency (resolved from node_modules — see
|
|
|
|
|
// pluginAssetDir and getPluginResolveAliases).
|
|
|
|
|
function workspacePluginDir(plugin) {
|
|
|
|
|
if (plugin.directory) {
|
|
|
|
|
return fromDirectory(APP_SRC_DIR, plugin.directory);
|
|
|
|
|
}
|
|
|
|
|
return getWorkspacePluginDirs()[extractName(plugin)];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Where a plugin's copyable assets (public/, dist/) live. In-tree and
|
|
|
|
|
// `directory`-overridden plugins use their source dir; anything else declared
|
|
|
|
|
// in pluginConfig falls back to node_modules. This is what lets an external
|
|
|
|
|
// extension/mode be included by adding it to the root package.json as a normal
|
|
|
|
|
// dependency (e.g. third-party packages such as dicom-microscopy-viewer).
|
|
|
|
|
function pluginAssetDir(plugin) {
|
|
|
|
|
const dir = workspacePluginDir(plugin);
|
|
|
|
|
if (dir) {
|
|
|
|
|
return dir;
|
|
|
|
|
}
|
|
|
|
|
const name = extractName(plugin);
|
|
|
|
|
const inNodeModules = name && path.join(REPO_ROOT, 'node_modules', name);
|
|
|
|
|
return inNodeModules && fs.existsSync(inNodeModules) ? inNodeModules : undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Alias map fed into webpack `resolve.alias`. The trailing `$` makes each alias
|
|
|
|
|
// an EXACT match for the bare package specifier that the generated
|
|
|
|
|
// pluginImports.js imports, so deep subpath imports (e.g.
|
|
|
|
|
// `@ohif/extension-cornerstone/types`) still flow through normal resolution and
|
|
|
|
|
// honor each package's `exports` map.
|
|
|
|
|
//
|
|
|
|
|
// Only in-tree / `directory`-overridden plugins get an alias. An external
|
|
|
|
|
// plugin installed as a root dependency intentionally gets none: its bare
|
|
|
|
|
// specifier then resolves through webpack's normal node_modules walk-up
|
|
|
|
|
// (resolve.modules includes the repo-root node_modules), exactly like any other
|
|
|
|
|
// installed package.
|
|
|
|
|
function getPluginResolveAliases() {
|
|
|
|
|
const alias = {};
|
|
|
|
|
for (const entry of [...(pluginConfig.extensions || []), ...(pluginConfig.modes || [])]) {
|
|
|
|
|
const name = extractName(entry);
|
|
|
|
|
const dir = workspacePluginDir(entry);
|
|
|
|
|
if (name && dir) {
|
|
|
|
|
alias[`${name}$`] = dir;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return alias;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Build CopyPlugin patterns for a set of plugins.
|
|
|
|
|
//
|
|
|
|
|
// For `public`-section entries (literalDirectory=true) a `directory` is the
|
|
|
|
|
// asset source itself — e.g. `./platform/public` or
|
|
|
|
|
// dicom-microscopy-viewer's prebuilt dist folder — so it is copied directly.
|
|
|
|
|
//
|
|
|
|
|
// For extension/mode entries a `directory` is instead the package ROOT (it
|
|
|
|
|
// doubles as the resolve alias target), so we copy its <folderName> (public/
|
|
|
|
|
// or dist/) subdirectory, exactly as we do for in-tree and node_modules
|
|
|
|
|
// plugins. This keeps an out-of-tree extension's assets landing in the same
|
|
|
|
|
// place as an in-tree one.
|
|
|
|
|
const createCopyPluginToDist = (distDir, plugins, folderName, { literalDirectory = false } = {}) => {
|
2022-08-17 15:13:59 -04:00
|
|
|
return plugins
|
|
|
|
|
.map(plugin => {
|
2026-06-12 16:04:56 -04:00
|
|
|
let from;
|
|
|
|
|
if (literalDirectory && plugin.directory) {
|
|
|
|
|
from = fromDirectory(APP_SRC_DIR, plugin.directory);
|
|
|
|
|
} else {
|
|
|
|
|
const dir = pluginAssetDir(plugin);
|
|
|
|
|
from = dir && path.join(dir, folderName);
|
|
|
|
|
}
|
|
|
|
|
return from && fs.existsSync(from)
|
2022-08-17 15:13:59 -04:00
|
|
|
? {
|
|
|
|
|
from,
|
2026-06-12 16:04:56 -04:00
|
|
|
to: `${distDir}${plugin.to || ''}`,
|
2022-08-17 15:13:59 -04:00
|
|
|
toType: 'dir',
|
|
|
|
|
}
|
|
|
|
|
: undefined;
|
|
|
|
|
})
|
2026-06-12 16:04:56 -04:00
|
|
|
.filter(Boolean);
|
2022-08-17 15:13:59 -04:00
|
|
|
};
|
2022-07-27 16:39:04 +00:00
|
|
|
|
|
|
|
|
function writePluginImportsFile(SRC_DIR, DIST_DIR) {
|
2022-04-06 13:28:42 -04:00
|
|
|
let pluginImportsJsContent = autogenerationDisclaimer;
|
|
|
|
|
|
|
|
|
|
const extensionLines = constructLines(pluginConfig.extensions, 'extensions');
|
|
|
|
|
const modeLines = constructLines(pluginConfig.modes, 'modes');
|
|
|
|
|
|
|
|
|
|
pluginImportsJsContent += getFormattedImportBlock([
|
|
|
|
|
...extensionLines.importLines,
|
|
|
|
|
...modeLines.importLines,
|
|
|
|
|
]);
|
|
|
|
|
pluginImportsJsContent += getFormattedWindowBlock([
|
|
|
|
|
...extensionLines.addToWindowLines,
|
|
|
|
|
...modeLines.addToWindowLines,
|
|
|
|
|
]);
|
|
|
|
|
|
2023-05-08 21:18:35 -04:00
|
|
|
pluginImportsJsContent += getRuntimeLoadModesExtensions([
|
|
|
|
|
...pluginConfig.extensions,
|
|
|
|
|
...pluginConfig.modes,
|
2024-07-05 13:19:42 -04:00
|
|
|
...pluginConfig.public,
|
2023-05-08 21:18:35 -04:00
|
|
|
]);
|
2022-07-27 16:39:04 +00:00
|
|
|
|
2023-09-01 16:19:39 -04:00
|
|
|
fs.writeFileSync(`${SRC_DIR}/pluginImports.js`, pluginImportsJsContent, { flag: 'w+' }, err => {
|
|
|
|
|
if (err) {
|
|
|
|
|
console.error(err);
|
|
|
|
|
return;
|
2022-04-06 13:28:42 -04:00
|
|
|
}
|
2023-09-01 16:19:39 -04:00
|
|
|
});
|
2022-07-27 16:39:04 +00:00
|
|
|
|
2026-06-12 16:04:56 -04:00
|
|
|
// Copy each extension/mode's static `public/` assets into the app dist.
|
|
|
|
|
// Plugins are resolved from their source dir (see pluginAssetDir), so this
|
|
|
|
|
// works whether they are in-tree, out-of-tree (`directory`), or installed as
|
|
|
|
|
// dependencies of platform/app.
|
|
|
|
|
const copyPluginPublicToDist = createCopyPluginToDist(
|
2022-08-17 15:13:59 -04:00
|
|
|
DIST_DIR,
|
2023-09-01 16:19:39 -04:00
|
|
|
[...pluginConfig.modes, ...pluginConfig.extensions],
|
2022-08-17 15:13:59 -04:00
|
|
|
'public'
|
|
|
|
|
);
|
|
|
|
|
|
2026-06-12 16:04:56 -04:00
|
|
|
// Some extensions/modes ship prebuilt chunks/workers/wasm in dist/; copy them
|
|
|
|
|
// if present.
|
|
|
|
|
const copyPluginDistToDist = createCopyPluginToDist(
|
2022-08-17 15:13:59 -04:00
|
|
|
DIST_DIR,
|
2023-09-01 16:19:39 -04:00
|
|
|
[...pluginConfig.modes, ...pluginConfig.extensions],
|
2022-08-17 15:13:59 -04:00
|
|
|
'dist'
|
|
|
|
|
);
|
|
|
|
|
|
2026-06-12 16:04:56 -04:00
|
|
|
// `public`-section entries (e.g. ./platform/public, dicom-microscopy-viewer)
|
|
|
|
|
// point `directory` at the asset folder itself, so copy it verbatim.
|
|
|
|
|
const copyPublicSectionToDist = createCopyPluginToDist(
|
2022-08-17 15:13:59 -04:00
|
|
|
DIST_DIR,
|
2026-06-12 16:04:56 -04:00
|
|
|
pluginConfig.public || [],
|
|
|
|
|
'public',
|
|
|
|
|
{ literalDirectory: true }
|
2022-08-17 15:13:59 -04:00
|
|
|
);
|
2022-07-27 16:39:04 +00:00
|
|
|
|
2026-06-12 16:04:56 -04:00
|
|
|
return [...copyPluginPublicToDist, ...copyPluginDistToDist, ...copyPublicSectionToDist];
|
2022-04-06 13:28:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = writePluginImportsFile;
|
2026-06-12 16:04:56 -04:00
|
|
|
module.exports.getPluginResolveAliases = getPluginResolveAliases;
|