|
1 | 1 | import fs from 'node:fs'
|
2 | 2 | import path from 'node:path'
|
3 | 3 | import { describe, expect, test, vi } from 'vitest'
|
| 4 | +import type { OutputBundle, OutputChunk } from 'rollup' |
4 | 5 | import { resolveConfig } from '../../config'
|
5 | 6 | import type { InlineConfig } from '../../config'
|
6 | 7 | import {
|
7 | 8 | convertTargets,
|
8 | 9 | cssPlugin,
|
9 | 10 | cssUrlRE,
|
10 | 11 | hoistAtRules,
|
| 12 | + removePureCssChunks, |
11 | 13 | } from '../../plugins/css'
|
12 | 14 |
|
13 | 15 | describe('search css url function', () => {
|
@@ -258,3 +260,193 @@ describe('convertTargets', () => {
|
258 | 260 | })
|
259 | 261 | })
|
260 | 262 | })
|
| 263 | + |
| 264 | +describe('removePureCssChunks', () => { |
| 265 | + test('import of removed chunk is dropped', () => { |
| 266 | + const bundle: OutputBundle = { |
| 267 | + 'main.js': { |
| 268 | + code: 'import "some-module";\nimport "pure_css_chunk.js";\nimport "other-module";\n', |
| 269 | + imports: ['pure_css_chunk.js', 'some-module', 'other-module'], |
| 270 | + type: 'chunk', |
| 271 | + viteMetadata: { |
| 272 | + importedAssets: new Set<string>(), |
| 273 | + importedCss: new Set<string>(), |
| 274 | + }, |
| 275 | + } as any as OutputChunk, |
| 276 | + 'pure_css_chunk.js': { |
| 277 | + type: 'chunk', |
| 278 | + code: '', |
| 279 | + imports: [], |
| 280 | + viteMetadata: { |
| 281 | + importedAssets: new Set<string>(), |
| 282 | + importedCss: new Set<string>(), |
| 283 | + }, |
| 284 | + } as any as OutputChunk, |
| 285 | + } |
| 286 | + |
| 287 | + removePureCssChunks(bundle, ['pure_css_chunk.js'], 'es') |
| 288 | + |
| 289 | + const chunk = bundle['main.js'] as OutputChunk |
| 290 | + expect(chunk.code).toMatchSnapshot() |
| 291 | + // import is removed |
| 292 | + expect(chunk.imports).toEqual(['some-module', 'other-module']) |
| 293 | + }) |
| 294 | + |
| 295 | + test('require of removed chunk is dropped', () => { |
| 296 | + const bundle: OutputBundle = { |
| 297 | + 'main.js': { |
| 298 | + code: 'require("some-module");\nrequire("pure_css_chunk.js");\nrequire("other-module");\n', |
| 299 | + imports: ['pure_css_chunk.js', 'some-module', 'other-module'], |
| 300 | + type: 'chunk', |
| 301 | + viteMetadata: { |
| 302 | + importedAssets: new Set<string>(), |
| 303 | + importedCss: new Set<string>(), |
| 304 | + }, |
| 305 | + } as any as OutputChunk, |
| 306 | + 'pure_css_chunk.js': { |
| 307 | + type: 'chunk', |
| 308 | + code: '', |
| 309 | + imports: [], |
| 310 | + viteMetadata: { |
| 311 | + importedAssets: new Set<string>(), |
| 312 | + importedCss: new Set<string>(), |
| 313 | + }, |
| 314 | + } as any as OutputChunk, |
| 315 | + } |
| 316 | + |
| 317 | + removePureCssChunks(bundle, ['pure_css_chunk.js'], 'cjs') |
| 318 | + |
| 319 | + const chunk = bundle['main.js'] as OutputChunk |
| 320 | + expect(chunk.code).toMatchSnapshot() |
| 321 | + // import is removed |
| 322 | + expect(chunk.imports).toEqual(['some-module', 'other-module']) |
| 323 | + }) |
| 324 | + |
| 325 | + test('require of removed chunk is dropped (minified, no new line)', () => { |
| 326 | + const bundle: OutputBundle = { |
| 327 | + 'main.js': { |
| 328 | + code: 'require("some-module");require("pure_css_chunk.js");require("other-module");', |
| 329 | + imports: ['pure_css_chunk.js', 'some-module', 'other-module'], |
| 330 | + type: 'chunk', |
| 331 | + viteMetadata: { |
| 332 | + importedAssets: new Set<string>(), |
| 333 | + importedCss: new Set<string>(), |
| 334 | + }, |
| 335 | + } as any as OutputChunk, |
| 336 | + 'pure_css_chunk.js': { |
| 337 | + type: 'chunk', |
| 338 | + code: '', |
| 339 | + imports: [], |
| 340 | + viteMetadata: { |
| 341 | + importedAssets: new Set<string>(), |
| 342 | + importedCss: new Set<string>(), |
| 343 | + }, |
| 344 | + } as any as OutputChunk, |
| 345 | + } |
| 346 | + |
| 347 | + removePureCssChunks(bundle, ['pure_css_chunk.js'], 'cjs') |
| 348 | + |
| 349 | + const chunk = bundle['main.js'] as OutputChunk |
| 350 | + expect(chunk.code).toMatchSnapshot() |
| 351 | + // import is removed |
| 352 | + expect(chunk.imports).toEqual(['some-module', 'other-module']) |
| 353 | + }) |
| 354 | + |
| 355 | + /* Currently broken as the code still contains the css chunk |
| 356 | + test('require of removed chunk is dropped (minified, with comma operator)', () => { |
| 357 | + const bundle: OutputBundle = { |
| 358 | + 'main.js': { |
| 359 | + code: 'require("some-module"),require("pure_css_chunk.js"),require("other-module");', |
| 360 | + imports: ['pure_css_chunk.js', 'some-module', 'other-module'], |
| 361 | + type: 'chunk', |
| 362 | + viteMetadata: { |
| 363 | + importedAssets: new Set<string>(), |
| 364 | + importedCss: new Set<string>(), |
| 365 | + } |
| 366 | + } as any as OutputChunk, |
| 367 | + 'pure_css_chunk.js': { |
| 368 | + type: 'chunk', |
| 369 | + code: '', |
| 370 | + imports: [], |
| 371 | + viteMetadata: { |
| 372 | + importedAssets: new Set<string>(), |
| 373 | + importedCss: new Set<string>(), |
| 374 | + } |
| 375 | + } as any as OutputChunk, |
| 376 | + } |
| 377 | +
|
| 378 | + removePureCssChunks(bundle, ['pure_css_chunk.js'], 'cjs') |
| 379 | +
|
| 380 | + const chunk = bundle['main.js'] as OutputChunk |
| 381 | + expect(chunk.code).toMatchSnapshot() |
| 382 | + // import is removed |
| 383 | + expect(chunk.imports).toEqual(['some-module', 'other-module']) |
| 384 | + expect(chunk.code.match(/pure_css_chunk\.js/)).toBeNull() |
| 385 | + }) |
| 386 | + */ |
| 387 | + |
| 388 | + /* Currently broken as the code is not valid |
| 389 | + test('require of removed chunk is dropped (minified, with comma and assignment)', () => { |
| 390 | + const bundle: OutputBundle = { |
| 391 | + 'main.js': { |
| 392 | + code: 'require("some-module"),require("pure_css_chunk.js");const v=require("other-module");', |
| 393 | + imports: ['pure_css_chunk.js', 'some-module', 'other-module'], |
| 394 | + type: 'chunk', |
| 395 | + viteMetadata: { |
| 396 | + importedAssets: new Set<string>(), |
| 397 | + importedCss: new Set<string>(), |
| 398 | + } |
| 399 | + } as any as OutputChunk, |
| 400 | + 'pure_css_chunk.js': { |
| 401 | + type: 'chunk', |
| 402 | + code: '', |
| 403 | + imports: [], |
| 404 | + viteMetadata: { |
| 405 | + importedAssets: new Set<string>(), |
| 406 | + importedCss: new Set<string>(), |
| 407 | + } |
| 408 | + } as any as OutputChunk, |
| 409 | + } |
| 410 | +
|
| 411 | + removePureCssChunks(bundle, ['pure_css_chunk.js'], 'cjs') |
| 412 | +
|
| 413 | + const chunk = bundle['main.js'] as OutputChunk |
| 414 | + expect(chunk.code).toMatchSnapshot() |
| 415 | + // import is removed |
| 416 | + expect(chunk.imports).toEqual(['some-module', 'other-module']) |
| 417 | + }) |
| 418 | + */ |
| 419 | + |
| 420 | + test('imported assets of css chunk are transfered', () => { |
| 421 | + const bundle: OutputBundle = { |
| 422 | + 'main.js': { |
| 423 | + code: 'import "some-module";\nimport "pure_css_chunk.js";\nimport "other-module";\n', |
| 424 | + imports: ['pure_css_chunk.js', 'some-module', 'other-module'], |
| 425 | + type: 'chunk', |
| 426 | + viteMetadata: { |
| 427 | + importedAssets: new Set<string>(), |
| 428 | + importedCss: new Set<string>(), |
| 429 | + }, |
| 430 | + } as any as OutputChunk, |
| 431 | + 'pure_css_chunk.js': { |
| 432 | + type: 'chunk', |
| 433 | + code: '', |
| 434 | + imports: [], |
| 435 | + viteMetadata: { |
| 436 | + importedAssets: new Set<string>(['some-asset.svg']), |
| 437 | + importedCss: new Set<string>(['some-style.css']), |
| 438 | + }, |
| 439 | + } as any as OutputChunk, |
| 440 | + } |
| 441 | + |
| 442 | + removePureCssChunks(bundle, ['pure_css_chunk.js'], 'es') |
| 443 | + |
| 444 | + const chunk = bundle['main.js'] as OutputChunk |
| 445 | + expect(chunk.code).toMatchSnapshot() |
| 446 | + // import is removed |
| 447 | + expect(chunk.imports).toEqual(['some-module', 'other-module']) |
| 448 | + // metadata is transfered |
| 449 | + expect(chunk.viteMetadata?.importedAssets.has('some-asset.svg')).toBe(true) |
| 450 | + expect(chunk.viteMetadata?.importedCss.has('some-style.css')).toBe(true) |
| 451 | + }) |
| 452 | +}) |
0 commit comments