# DICOM Viewer Bundling: Decision Guide

**Date**: 2025-01-12  
**Purpose**: Help decide whether to bundle Cornerstone.js libraries now or later

## What Is Bundling?

### Current Situation (CDN Approach - Doesn't Work)

Right now, the DICOM viewer HTML file tries to load libraries from the internet:

```html
<script src="https://cdn.jsdelivr.net/npm/cornerstone-core@2.6.1/dist/cornerstone.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/cornerstone-wado-image-loader@4.9.1/dist/cornerstoneWADOImageLoader.min.js"></script>
<!-- etc... -->
```

**Problem**: Chrome MV3 extensions **block external scripts** for security. These CDN links fail silently, so the viewer can't render images.

### Bundling Solution (Local Files)

Instead of loading from CDN, we copy the library files into the extension:

```
filevine-plugin/
└── src/
    └── viewers/
        └── dicom/
            ├── viewer.html
            └── lib/                    ← New folder
                ├── cornerstone.min.js
                ├── cornerstoneWADOImageLoader.min.js
                ├── dicomParser.min.js
                ├── cornerstoneMath.min.js
                ├── cornerstoneTools.min.js
                ├── cornerstoneWADOImageLoaderWebWorker.js
                └── codecs/              ← WebAssembly codecs
                    ├── charls.js
                    ├── openjpeg.js
                    └── ...
```

Then update viewer.html to load from local files:
```html
<script src="lib/cornerstone.min.js"></script>
<script src="lib/cornerstoneWADOImageLoader.min.js"></script>
<!-- etc... -->
```

## What's Involved

### Option 1: Manual Copy (Simplest, ~15 minutes)

1. **Download libraries** (via npm or direct download)
2. **Copy files** to `src/viewers/dicom/lib/`
3. **Update viewer.html** script tags to point to local files
4. **Update manifest.json** to include lib folder in web_accessible_resources
5. **Test** that viewer works

**Pros:**
- Fast and simple
- No build tools needed
- Easy to understand
- Works immediately

**Cons:**
- Manual process (repeat if libraries update)
- Adds ~1.6MB to extension size
- Files committed to git (larger repo)

### Option 2: Automated Build Script (~30 minutes setup)

Create a script that automatically copies libraries from `node_modules`:

```javascript
// scripts/copy-dicom-libs.js
// Runs: npm install, then copies files to lib/
```

**Pros:**
- Repeatable
- Can be run anytime
- Libraries stay in node_modules (not committed)

**Cons:**
- Requires npm/node setup
- More complex
- Still need to run script manually

### Option 3: Full Build Pipeline (~1-2 hours)

Use webpack/rollup to bundle everything:

**Pros:**
- Professional approach
- Can optimize/minify
- Single bundle file

**Cons:**
- Much more complex
- Requires build tooling
- Overkill for this use case

## Size Impact

**Library sizes:**
- `cornerstone.min.js` - ~200KB
- `cornerstoneWADOImageLoader.min.js` - ~300KB
- `cornerstoneTools.min.js` - ~400KB
- `dicomParser.min.js` - ~150KB
- `cornerstoneMath.min.js` - ~50KB
- Codecs (WebAssembly) - ~500KB
- **Total: ~1.6MB**

**Extension size impact:**
- Current extension: ~50-100KB
- After bundling: ~1.7MB
- **Increase: ~1.6MB**

This is reasonable for a Chrome extension (many are 5-10MB+).

## Current State vs Bundled State

### Current State (CDN - Not Working)
- ✅ Viewer HTML/CSS/JS structure complete
- ✅ File fetching logic works
- ✅ UI controls implemented
- ❌ **Libraries don't load** (blocked by MV3)
- ❌ **Images don't render**
- ❌ **Viewer is non-functional**

### After Bundling (Working)
- ✅ Everything above, PLUS:
- ✅ Libraries load successfully
- ✅ DICOM images render
- ✅ All controls work (zoom, pan, window/level, etc.)
- ✅ **Viewer is fully functional**

## Decision Factors

### Do It Now If:
- ✅ You want to test the DICOM viewer functionality immediately
- ✅ You're ready to commit ~1.6MB to the repo
- ✅ You want the viewer working end-to-end
- ✅ You don't mind manual copy process (or simple script)

### Wait If:
- ⏸️ You want to test other features first
- ⏸️ You prefer automated build process (need more setup time)
- ⏸️ You want to optimize library selection first
- ⏸️ You're not ready to test DICOM viewing yet

## Recommendation

**Do it now** using **Option 1 (Manual Copy)** because:

1. **Fast**: 15 minutes to get working viewer
2. **Low risk**: Easy to undo if needed
3. **Immediate value**: Can test full DICOM viewing workflow
4. **Simple**: No complex tooling required
5. **Can optimize later**: Can always switch to automated approach

The viewer code is complete - it just needs the libraries to function. Getting it working now lets you:
- Test the full user workflow
- Verify DICOM files render correctly
- Test all viewer controls
- Identify any other issues early

## What Happens If We Wait?

**Short term:**
- Viewer opens but shows loading spinner forever
- Can't test DICOM viewing functionality
- Can't verify the viewer works with real files
- User experience is broken

**Long term:**
- Still need to bundle eventually (viewer won't work without it)
- No change in effort - same work either way
- Delays testing and validation

## Implementation Time Estimate

| Task | Time | Notes |
|------|------|-------|
| Download libraries | 2 min | npm install or direct download |
| Copy files to lib/ | 3 min | Create folder, copy files |
| Update viewer.html | 2 min | Change script src paths |
| Update manifest.json | 1 min | Add lib to web_accessible_resources |
| Test viewer | 5 min | Load extension, test with DICOM file |
| **Total** | **~15 min** | Simple manual approach |

## Alternative: Minimal Test Without Bundling

If you want to verify the viewer structure works first, we could:
1. Create a simple test that doesn't use Cornerstone
2. Just display the raw file info/metadata
3. Bundle later when ready

But this doesn't test actual DICOM rendering, which is the main feature.

## My Recommendation

**Bundle now** - it's quick, low-risk, and unlocks full functionality. The viewer is 95% complete; it just needs the libraries to work. Getting it functional now lets you test the complete workflow and catch any issues early.

Would you like me to proceed with bundling, or do you have questions about any of this?
