ohif-viewer/platform/app/.webpack/webpack.pwa.js
Bill Wallace 3dd5c70cb2
feat: Add customization URL parameter (#5992)
* Add customization URL parameter

* fix: Preserve should be customizeable

* Update customizations docs

* fix: Overlay items on patient name

* Add customization test

* Fix resolve to absolute path

* fix: Warn on no data in load

* Remove unused customization stuff

* fix: PR comments

* Update stored parameters to only use an array for mulitples

* Remove requires ohif.* special call out

* Remove strict mode

* PR comments

* Document segmentation examples

* Add three examples as requested

* PR comments

* lock

* Remove old customizatoin export

* fix: Ordering issues on customization loads

* fix: Use correct default for dev builds app config

* Fixes for conflicts

* chore: restore pnpm-lock.yaml to match master

The lockfile diff was incidental peer-descriptor churn and carried no
functional dependency change. It tripped the CircleCI security-audit gate
(which only runs when pnpm-lock.yaml is in the PR diff), surfacing a
pre-existing critical `decompress` transitive vuln that also exists on
master. Restoring master's lockfile removes the audit trigger.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(ci): restore json5 lockfile entry; ignore unfixable decompress GHSA

The previous commit restored pnpm-lock.yaml from master, which dropped the
json5@2.2.3 entry that platform/core legitimately depends on (JSONC parsing
for the customization feature). That broke `--frozen-lockfile` install
(ERR_PNPM_OUTDATED_LOCKFILE). This restores the correct lockfile.

Because the lockfile must change (json5), the CircleCI security-audit gate
runs and previously failed on a critical `decompress` <=4.2.1 zip-slip
advisory. This is a pre-existing transitive vuln (present on master too) with
no published patch — decompress's latest release is 4.2.1, so no version
bump/override can resolve it. It reaches the tree only via @itk-wasm/dam, a
build/data-asset extraction tool under @cornerstonejs/labelmap-interpolation.

Add GHSA-mp2f-45pm-3cg9 to the existing pnpm-workspace.yaml auditConfig
ignoreGhsas accepted-risk list, matching how the repo already exempts other
build-tooling advisories. `pnpm audit --audit-level high` now passes locally
(1 critical ignored, 0 high).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* test(e2e): fix visitStudy URL encoding that broke mpr2 study load

The visitStudy rewrite (added for the ?customization= option) built the URL
with new URLSearchParams({ StudyInstanceUIDs: studyInstanceUID }), which
percent-encodes the value. mpr2.spec.ts embeds an extra param in the UID
string ('<uid>&hangingprotocolid=mpr'), so the & and = were encoded and the
whole thing collapsed into one invalid StudyInstanceUIDs value -> the study
could not be found ('studies are not available'), the viewer never rendered,
and the side-panel-header-right click timed out.

Restore master's raw concatenation for StudyInstanceUIDs (so embedded params
survive as separate query params) while still appending the customization
option separately. Only mpr2 embeds & in the UID, matching the single failure.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* PR comments

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-07 15:30:27 -04:00

269 lines
8.8 KiB
JavaScript

// https://developers.google.com/web/tools/workbox/guides/codelabs/webpack
// ~~ WebPack
const path = require('path');
const fs = require('fs');
const { merge } = require('webpack-merge');
const rspack = require('@rspack/core');
const webpackBase = require('./../../../.webpack/webpack.base.js');
// ~~ Directories
const SRC_DIR = path.join(__dirname, '../src');
const DIST_DIR = path.join(__dirname, '../dist');
const PUBLIC_DIR = path.join(__dirname, '../public');
// Ignore node_modules except @cornerstonejs (symlinked local development).
const WATCH_IGNORED = /node_modules[\\/](?!@cornerstonejs(?:[\\/]|$))/;
// ~~ Env Vars
const HTML_TEMPLATE = process.env.HTML_TEMPLATE || 'index.html';
const PUBLIC_URL = process.env.PUBLIC_URL || '/';
// proxy settings
const PROXY_TARGET = process.env.PROXY_TARGET;
const PROXY_DOMAIN = process.env.PROXY_DOMAIN;
const PROXY_PATH_REWRITE_FROM = process.env.PROXY_PATH_REWRITE_FROM;
const PROXY_PATH_REWRITE_TO = process.env.PROXY_PATH_REWRITE_TO;
const IS_COVERAGE = process.env.COVERAGE === 'true';
const OHIF_PORT = Number(process.env.OHIF_PORT || 3000);
const ENTRY_TARGET = process.env.ENTRY_TARGET || `${SRC_DIR}/index.js`;
const dotenv = require('dotenv');
dotenv.config();
const writePluginImportFile = require('./writePluginImportsFile.js');
// const MillionLint = require('@million/lint');
const open = process.env.OHIF_OPEN !== 'false';
const copyPluginFromExtensions = writePluginImportFile(SRC_DIR, DIST_DIR);
class InjectServiceWorkerManifestPlugin {
constructor({ swSrc, swDest, publicPath, exclude, maximumFileSizeToCacheInBytes }) {
this.swSrc = swSrc;
this.swDest = swDest;
this.publicPath = publicPath;
this.exclude = exclude;
this.maximumFileSizeToCacheInBytes = maximumFileSizeToCacheInBytes;
}
apply(compiler) {
const pluginName = 'InjectServiceWorkerManifestPlugin';
const publicPath = this.publicPath.endsWith('/') ? this.publicPath : `${this.publicPath}/`;
compiler.hooks.thisCompilation.tap(pluginName, compilation => {
compilation.hooks.processAssets.tap(
{
name: pluginName,
stage: rspack.Compilation.PROCESS_ASSETS_STAGE_REPORT,
},
() => {
const manifest = compilation
.getAssets()
.filter(asset => {
if (asset.name === this.swDest || asset.name.endsWith('.map')) {
return false;
}
if (this.exclude.some(pattern => pattern.test(asset.name))) {
return false;
}
return asset.source.size() <= this.maximumFileSizeToCacheInBytes;
})
.map(asset => ({
url: `${publicPath}${asset.name}`,
revision: asset.info.contenthash ? null : compilation.hash,
}));
const source = fs
.readFileSync(this.swSrc, 'utf8')
.replace('self.__WB_MANIFEST', JSON.stringify(manifest));
compilation.emitAsset(this.swDest, new rspack.sources.RawSource(source));
}
);
});
}
}
const setHeaders = (res, path) => {
if (path.indexOf('.gz') !== -1) {
res.setHeader('Content-Encoding', 'gzip');
} else if (path.indexOf('.br') !== -1) {
res.setHeader('Content-Encoding', 'br');
}
if (path.indexOf('thumbnail') !== -1) {
res.setHeader('Content-Type', 'image/jpeg');
} else if (path.indexOf('.pdf') !== -1) {
res.setHeader('Content-Type', 'application/pdf');
} else if (path.indexOf('mp4') !== -1) {
res.setHeader('Content-Type', 'video/mp4');
} else if (path.indexOf('frames') !== -1) {
res.setHeader('Content-Type', 'multipart/related');
} else {
res.setHeader('Content-Type', 'application/json');
}
};
module.exports = (env, argv) => {
const baseConfig = webpackBase(env, argv, { SRC_DIR, DIST_DIR });
const isProdBuild = process.env.NODE_ENV === 'production';
// Honor an explicit APP_CONFIG; otherwise the dev server gets the
// full-featured `config/dev.js` and a production build the locked-down
// `config/default.js`. This lets `APP_CONFIG=config/foo.js pnpm run dev`
// override the default instead of being clobbered by the script.
const APP_CONFIG =
process.env.APP_CONFIG || (isProdBuild ? 'config/default.js' : 'config/dev.js');
const hasProxy = PROXY_TARGET && PROXY_DOMAIN;
const mergedConfig = merge(baseConfig, {
entry: {
app: ENTRY_TARGET,
},
output: {
clean: true,
path: DIST_DIR,
filename: isProdBuild ? '[name].bundle.[chunkhash].js' : '[name].js',
publicPath: PUBLIC_URL, // Used by HtmlWebPackPlugin for asset prefix
devtoolModuleFilenameTemplate: function (info) {
if (isProdBuild) {
return `webpack:///${info.resourcePath}`;
} else {
return 'file:///' + encodeURI(info.absoluteResourcePath);
}
},
},
resolve: {
// Resolve every extension/mode declared in pluginConfig.json to its
// workspace source, so the dynamic import()s in pluginImports.js link
// without the plugins being dependencies of platform/app. Merged with the
// base aliases (webpack-merge deep-merges resolve.alias).
alias: writePluginImportFile.getPluginResolveAliases(),
modules: [
// Preserve importer-relative node_modules walk-up for pnpm.
'node_modules',
path.resolve(__dirname, '../node_modules'),
path.resolve(__dirname, '../../../node_modules'),
SRC_DIR,
],
},
plugins: [
// Copy "Public" Folder to Dist (rspack built-in)
new rspack.CopyRspackPlugin({
patterns: [
...copyPluginFromExtensions,
{
from: PUBLIC_DIR,
to: DIST_DIR,
toType: 'dir',
globOptions: {
ignore: ['**/config/**', '**/html-templates/**', '.DS_Store'],
},
},
{
from: '../../../node_modules/onnxruntime-web/dist',
to: `${DIST_DIR}/ort`,
},
{
from: `${PUBLIC_DIR}/config/google.js`,
to: `${DIST_DIR}/google.js`,
},
{
from: `${PUBLIC_DIR}/${APP_CONFIG}`,
to: `${DIST_DIR}/app-config.js`,
},
],
}),
// Generate "index.html" w/ correct includes/imports (rspack built-in)
new rspack.HtmlRspackPlugin({
template: `${PUBLIC_DIR}/html-templates/${HTML_TEMPLATE}`,
filename: 'index.html',
templateParameters: {
PUBLIC_URL: PUBLIC_URL,
},
}),
// Generate a service worker for fast local loads.
...(IS_COVERAGE
? []
: [
new InjectServiceWorkerManifestPlugin({
swDest: 'sw.js',
swSrc: path.join(SRC_DIR, 'service-worker.js'),
publicPath: PUBLIC_URL,
exclude: [/theme/],
maximumFileSizeToCacheInBytes: 1024 * 1024 * 50,
}),
]),
],
// https://webpack.js.org/configuration/dev-server/
devServer: {
// gzip compression of everything served
// Causes Cypress: `wait-on` issue in CI
// compress: true,
// http2: true,
// https: true,
open,
port: OHIF_PORT,
client: {
// During e2e (COVERAGE=true) disable the dev-server overlay: its
// injected iframe intercepts pointer events and breaks Playwright/Cypress
// clicks. Keep it for normal local dev.
overlay: IS_COVERAGE ? false : { errors: true, warnings: false },
},
proxy: [
{
context: ['/dicomweb'],
target: 'http://localhost:5000',
},
],
static: [
{
directory: '../../testdata',
staticOptions: {
extensions: ['gz', 'br', 'mht'],
index: ['index.json.gz', 'index.mht.gz'],
redirect: true,
setHeaders,
},
publicPath: '/viewer-testdata',
watch: false,
},
],
//public: 'http://localhost:' + 3000,
//writeToDisk: true,
historyApiFallback: {
disableDotRule: !IS_COVERAGE,
index: PUBLIC_URL + 'index.html',
htmlAcceptHeaders: ['text/html'],
},
devMiddleware: {
writeToDisk: true,
},
},
});
if (hasProxy) {
mergedConfig.devServer.proxy = mergedConfig.devServer.proxy || {};
mergedConfig.devServer.proxy = [
{
context: [PROXY_PATH_REWRITE_FROM || '/dicomweb'],
target: PROXY_DOMAIN,
changeOrigin: true,
pathRewrite: {
[`^${PROXY_PATH_REWRITE_FROM}`]: PROXY_PATH_REWRITE_TO,
},
},
];
}
if (isProdBuild) {
mergedConfig.plugins.push(
new rspack.CssExtractRspackPlugin({
filename: '[name].bundle.css',
chunkFilename: '[id].css',
})
);
}
mergedConfig.watchOptions = {
ignored: WATCH_IGNORED,
followSymlinks: true,
};
return mergedConfig;
};