|
| 1 | +const fs = __non_webpack_require__('fs'); |
| 2 | +const path = __non_webpack_require__('path'); |
| 3 | + |
| 4 | +__webpack_require__.p = 'PUBLIC_PATH'; |
| 5 | +it('should treeshake ui-lib correctly', async () => { |
| 6 | + const app = await import('./App.js'); |
| 7 | + expect(app.default()).toEqual( |
| 8 | + 'Uilib has 2 exports, and Button value is Button', |
| 9 | + ); |
| 10 | + |
| 11 | + const bundlePath = path.join(__dirname, 'node_modules_ui-lib_index_js.js'); |
| 12 | + const bundleContent = fs.readFileSync(bundlePath, 'utf-8'); |
| 13 | + expect(bundleContent).toContain('Button'); |
| 14 | + expect(bundleContent).toContain('Badge'); |
| 15 | + expect(bundleContent).not.toContain('List'); |
| 16 | +}); |
| 17 | + |
| 18 | +it('should treeshake ui-lib-dynamic-specific-export correctly', async () => { |
| 19 | + const { List } = await import('ui-lib-dynamic-specific-export'); |
| 20 | + expect(List).toEqual('List'); |
| 21 | + |
| 22 | + const bundlePath = path.join( |
| 23 | + __dirname, |
| 24 | + 'node_modules_ui-lib-dynamic-specific-export_index_js.js', |
| 25 | + ); |
| 26 | + const bundleContent = fs.readFileSync(bundlePath, 'utf-8'); |
| 27 | + expect(bundleContent).toContain('List'); |
| 28 | + expect(bundleContent).not.toContain('Button'); |
| 29 | + expect(bundleContent).not.toContain('Badge'); |
| 30 | +}); |
| 31 | + |
| 32 | +it('should not treeshake ui-lib-dynamic-default-export', async () => { |
| 33 | + const uiLib = await import('ui-lib-dynamic-default-export'); |
| 34 | + expect(uiLib.List).toEqual('List'); |
| 35 | + |
| 36 | + const bundlePath = path.join( |
| 37 | + __dirname, |
| 38 | + 'node_modules_ui-lib-dynamic-default-export_index_js.js', |
| 39 | + ); |
| 40 | + const bundleContent = fs.readFileSync(bundlePath, 'utf-8'); |
| 41 | + expect(bundleContent).toContain('List'); |
| 42 | + expect(bundleContent).toContain('Button'); |
| 43 | + expect(bundleContent).toContain('Badge'); |
| 44 | +}); |
| 45 | + |
| 46 | +it('should not treeshake ui-lib-side-effect if not set sideEffect:false ', async () => { |
| 47 | + const uiLibSideEffect = await import('ui-lib-side-effect'); |
| 48 | + expect(uiLibSideEffect.List).toEqual('List'); |
| 49 | + |
| 50 | + const bundlePath = path.join( |
| 51 | + __dirname, |
| 52 | + 'node_modules_ui-lib-side-effect_index_js.js', |
| 53 | + ); |
| 54 | + const bundleContent = fs.readFileSync(bundlePath, 'utf-8'); |
| 55 | + expect(bundleContent).toContain('List'); |
| 56 | + expect(bundleContent).toContain('Button'); |
| 57 | + expect(bundleContent).toContain('Badge'); |
| 58 | +}); |
| 59 | + |
| 60 | +it('should inject usedExports into entry chunk by default', async () => { |
| 61 | + expect( |
| 62 | + __webpack_require__.federation.usedExports['ui-lib']['main'].sort(), |
| 63 | + ).toEqual(['Badge', 'Button']); |
| 64 | +}); |
| 65 | + |
| 66 | +it('should inject usedExports into manifest and stats if enable manifest', async () => { |
| 67 | + const { Button } = await import('ui-lib'); |
| 68 | + expect(Button).toEqual('Button'); |
| 69 | + |
| 70 | + const manifestPath = path.join(__dirname, 'mf-manifest.json'); |
| 71 | + const manifestContent = JSON.parse(fs.readFileSync(manifestPath, 'utf-8')); |
| 72 | + expect( |
| 73 | + JSON.stringify( |
| 74 | + manifestContent.shared |
| 75 | + .find((s) => s.name === 'ui-lib') |
| 76 | + .usedExports.sort(), |
| 77 | + ), |
| 78 | + ).toEqual(JSON.stringify(['Badge', 'Button'])); |
| 79 | + |
| 80 | + const statsPath = path.join(__dirname, 'mf-stats.json'); |
| 81 | + const statsContent = JSON.parse(fs.readFileSync(statsPath, 'utf-8')); |
| 82 | + expect( |
| 83 | + JSON.stringify( |
| 84 | + statsContent.shared.find((s) => s.name === 'ui-lib').usedExports.sort(), |
| 85 | + ), |
| 86 | + ).toEqual(JSON.stringify(['Badge', 'Button'])); |
| 87 | +}); |
0 commit comments