|
1 | 1 | import fs from 'node:fs/promises'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { fileURLToPath } from 'node:url'; |
2 | 4 |
|
3 | 5 | import { nodeResolve } from '@rollup/plugin-node-resolve'; |
4 | 6 | import chokidar from 'chokidar'; |
@@ -289,12 +291,14 @@ function getCDNBundles() { |
289 | 291 | * @returns {import('rollup').RollupOptions} |
290 | 292 | */ |
291 | 293 | function defineCDNBundle({ dev = false, input, dir, file, legacy = false }) { |
| 294 | + const npm = defineNPMBundle({ |
| 295 | + type: dev ? 'dev' : 'prod', |
| 296 | + minify: !dev, |
| 297 | + target: 'es2020', |
| 298 | + }); |
| 299 | + |
292 | 300 | return { |
293 | | - ...defineNPMBundle({ |
294 | | - type: dev ? 'dev' : 'prod', |
295 | | - minify: !dev, |
296 | | - target: 'es2020', |
297 | | - }), |
| 301 | + ...npm, |
298 | 302 | input: dev ? input : { [file]: input, ...getProviderInputs() }, |
299 | 303 | preserveEntrySignatures: dev ? 'allow-extension' : 'strict', |
300 | 304 | output: { |
@@ -324,6 +328,28 @@ function defineCDNBundle({ dev = false, input, dir, file, legacy = false }) { |
324 | 328 | }, |
325 | 329 | }, |
326 | 330 | external: CDN_EXTERNAL_PACKAGES, |
| 331 | + plugins: [ |
| 332 | + .../** @type {*} */ (npm.plugins), |
| 333 | + { |
| 334 | + // This plugin rewrites chunk paths so our URL rewrites to jsDelivr work. |
| 335 | + name: 'cdn-chunks', |
| 336 | + async generateBundle(_, bundle) { |
| 337 | + const __dirname = path.dirname(fileURLToPath(import.meta.url)), |
| 338 | + version = JSON.parse( |
| 339 | + await fs.readFile(path.join(__dirname, 'package.json'), 'utf-8'), |
| 340 | + ).version; |
| 341 | + |
| 342 | + for (const chunk of Object.values(bundle)) { |
| 343 | + if (chunk.type === 'chunk' && chunk.isEntry && chunk.name === file) { |
| 344 | + chunk.code = chunk.code.replace( |
| 345 | + /\"\.\/(chunks|providers)\/(.*?)\"/g, |
| 346 | + `"https://cdn.jsdelivr.net/npm/@vidstack/cdn@${version}/$1/$2"`, |
| 347 | + ); |
| 348 | + } |
| 349 | + } |
| 350 | + }, |
| 351 | + }, |
| 352 | + ], |
327 | 353 | }; |
328 | 354 | } |
329 | 355 |
|
|
0 commit comments