|
| 1 | +import { readdirSync } from 'node:fs'; |
| 2 | +import { resolve } from 'node:path'; |
| 3 | + |
| 4 | +import { defineExtensionConfig } from './index'; |
| 5 | + |
| 6 | + |
| 7 | +jest.mock('./flatten-html-pages-directory', () => 'flattenHtmlPagesDirectoryPluginStub'); |
| 8 | + |
| 9 | +jest.mock('node:url', () => ({ |
| 10 | + fileURLToPath: jest.fn().mockReturnValue('urlFileUrlToPathStub'), |
| 11 | +})); |
| 12 | + |
| 13 | +jest.mock('node:fs', () => ({ |
| 14 | + readdirSync: jest.fn().mockReturnValue(['fsReaddirSyncStub']), |
| 15 | +})); |
| 16 | + |
| 17 | +jest.mock('node:path', () => ({ |
| 18 | + resolve: jest.fn().mockReturnValue('pathResolveStub'), |
| 19 | +})); |
| 20 | + |
| 21 | +describe('#defineExtensionConfig function', () => { |
| 22 | + let result; |
| 23 | + |
| 24 | + describe('required options', () => { |
| 25 | + it.each([ |
| 26 | + // expectedErrorMessage, config |
| 27 | + [ |
| 28 | + '"srcDir" is required', |
| 29 | + { |
| 30 | + srcDir: undefined, |
| 31 | + srcUrl: 'bar', |
| 32 | + outputDir: 'baz', |
| 33 | + vuePlugin: 'qux', |
| 34 | + }, |
| 35 | + ], |
| 36 | + [ |
| 37 | + '"srcUrl" is required', |
| 38 | + { |
| 39 | + srcDir: 'foo', |
| 40 | + srcUrl: undefined, |
| 41 | + outputDir: 'baz', |
| 42 | + vuePlugin: 'qux', |
| 43 | + }, |
| 44 | + ], |
| 45 | + [ |
| 46 | + '"outputDir" is required', |
| 47 | + { |
| 48 | + srcDir: 'foo', |
| 49 | + srcUrl: 'bar', |
| 50 | + outputDir: undefined, |
| 51 | + vuePlugin: 'qux', |
| 52 | + }, |
| 53 | + ], |
| 54 | + [ |
| 55 | + '"vuePlugin" is required', |
| 56 | + { |
| 57 | + srcDir: 'foo', |
| 58 | + srcUrl: 'bar', |
| 59 | + outputDir: 'baz', |
| 60 | + vuePlugin: undefined, |
| 61 | + }, |
| 62 | + ], |
| 63 | + ])( |
| 64 | + 'throws an error with the message %s if config=%o', |
| 65 | + (expectedErrorMessage, config) => { |
| 66 | + let error; |
| 67 | + |
| 68 | + try { |
| 69 | + defineExtensionConfig(config); |
| 70 | + } catch (e) { |
| 71 | + error = e; |
| 72 | + } |
| 73 | + |
| 74 | + expect(error.message).toEqual(expectedErrorMessage); |
| 75 | + }, |
| 76 | + ); |
| 77 | + }); |
| 78 | + |
| 79 | + it('returns the base config', () => { |
| 80 | + const config = { |
| 81 | + srcDir: '/my/source/dir', |
| 82 | + srcUrl: 'file://my/source/dir', |
| 83 | + outputDir: '/my/output/dir', |
| 84 | + vuePlugin: { name: 'vuepluginstub' }, |
| 85 | + }; |
| 86 | + |
| 87 | + result = defineExtensionConfig(config); |
| 88 | + |
| 89 | + expect(result).toEqual({ |
| 90 | + resolve: { |
| 91 | + alias: { |
| 92 | + '~': 'urlFileUrlToPathStub', |
| 93 | + }, |
| 94 | + }, |
| 95 | + plugins: [ |
| 96 | + { name: 'vuepluginstub' }, |
| 97 | + 'flattenHtmlPagesDirectoryPluginStub', |
| 98 | + ], |
| 99 | + root: '/my/source/dir', |
| 100 | + base: '/static', |
| 101 | + build: { |
| 102 | + outDir: '/my/output/dir', |
| 103 | + emptyOutDir: true, |
| 104 | + rollupOptions: { |
| 105 | + input: { |
| 106 | + fsReaddirSyncStub: 'pathResolveStub', |
| 107 | + }, |
| 108 | + output: { |
| 109 | + format: 'es', |
| 110 | + dir: '/my/output/dir', |
| 111 | + manualChunks: expect.any(Function), |
| 112 | + }, |
| 113 | + }, |
| 114 | + }, |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + it('returns the base config merged with a custom Vite config', () => { |
| 119 | + const config = { |
| 120 | + srcDir: '/my/source/dir', |
| 121 | + srcUrl: 'file://my/source/dir', |
| 122 | + outputDir: '/my/output/dir', |
| 123 | + vuePlugin: { name: 'vuepluginstub' }, |
| 124 | + }; |
| 125 | + |
| 126 | + const customViteConfig = { |
| 127 | + foo: 'bar', |
| 128 | + resolve: { |
| 129 | + one: 'two', |
| 130 | + alias: { |
| 131 | + '@': '/some/path', |
| 132 | + }, |
| 133 | + }, |
| 134 | + plugins: ['other-vite-plugin'], |
| 135 | + build: { |
| 136 | + someProperty: 'someValue', |
| 137 | + rollupOptions: { |
| 138 | + bar: 'baz', |
| 139 | + output: { |
| 140 | + baz: 'qux', |
| 141 | + }, |
| 142 | + }, |
| 143 | + }, |
| 144 | + }; |
| 145 | + |
| 146 | + result = defineExtensionConfig(config, customViteConfig); |
| 147 | + |
| 148 | + expect(result).toEqual({ |
| 149 | + foo: 'bar', |
| 150 | + resolve: { |
| 151 | + one: 'two', |
| 152 | + alias: { |
| 153 | + '~': 'urlFileUrlToPathStub', |
| 154 | + '@': '/some/path', |
| 155 | + }, |
| 156 | + }, |
| 157 | + plugins: [ |
| 158 | + { name: 'vuepluginstub' }, |
| 159 | + 'flattenHtmlPagesDirectoryPluginStub', |
| 160 | + 'other-vite-plugin' |
| 161 | + ], |
| 162 | + root: '/my/source/dir', |
| 163 | + base: '/static', |
| 164 | + build: { |
| 165 | + someProperty: 'someValue', |
| 166 | + outDir: '/my/output/dir', |
| 167 | + emptyOutDir: true, |
| 168 | + rollupOptions: { |
| 169 | + bar: 'baz', |
| 170 | + input: { |
| 171 | + fsReaddirSyncStub: 'pathResolveStub', |
| 172 | + }, |
| 173 | + output: { |
| 174 | + baz: 'qux', |
| 175 | + format: 'es', |
| 176 | + dir: '/my/output/dir', |
| 177 | + manualChunks: expect.any(Function), |
| 178 | + }, |
| 179 | + }, |
| 180 | + }, |
| 181 | + }); |
| 182 | + }); |
| 183 | + |
| 184 | + it('does proper input entrypoints resolution', () => { |
| 185 | + const config = { |
| 186 | + srcDir: '/my/source/dir', |
| 187 | + srcUrl: 'file://my/source/dir', |
| 188 | + outputDir: '/my/output/dir', |
| 189 | + vuePlugin: { name: 'vuepluginstub' }, |
| 190 | + }; |
| 191 | + |
| 192 | + result = defineExtensionConfig(config); |
| 193 | + |
| 194 | + expect(resolve).toHaveBeenCalledWith('/my/source/dir', 'pages'); |
| 195 | + expect(readdirSync).toHaveBeenCalledWith('pathResolveStub'); |
| 196 | + expect(resolve).toHaveBeenCalledWith('/my/source/dir', 'pages/', 'fsReaddirSyncStub', 'index.html'); |
| 197 | + expect(result.build.rollupOptions.input).toEqual({ |
| 198 | + fsReaddirSyncStub: 'pathResolveStub', |
| 199 | + }); |
| 200 | + }); |
| 201 | + |
| 202 | + describe('#config.build.rollupOptions.output.manualChunks', () => { |
| 203 | + it.each([ |
| 204 | + // expected, moduleId |
| 205 | + ['connect', 'foo/bar/@cloudblueconnect/material-svg/baseline/googlePhoneBaseline.svg'], |
| 206 | + ['connect', 'foo/bar/@cloudblueconnect/connect-ui-toolkit/tools/vue/toolkitPlugin.js'], |
| 207 | + ['connect', 'node_modules/@cloudblueconnect/connect-ui-toolkit/index.js'], |
| 208 | + ['vendor', 'node_modules/@cloudgreendisconnect/disconnect-backend-toolkit/index.js'], |
| 209 | + ['vendor', 'node_modules/vue/index.js'], |
| 210 | + ['vendor', 'foo/bar/baz/node_modules/vuex/index.js'], |
| 211 | + [undefined, 'foo/bar/baz/index.js'], |
| 212 | + [undefined, 'main.css'], |
| 213 | + ])( |
| 214 | + 'returns %s if the module id=%s', |
| 215 | + (expected, moduleId) => { |
| 216 | + const config = { |
| 217 | + srcDir: '/my/source/dir', |
| 218 | + srcUrl: 'file://my/source/dir', |
| 219 | + outputDir: '/my/output/dir', |
| 220 | + vuePlugin: { name: 'vuepluginstub' }, |
| 221 | + }; |
| 222 | + const manualChunksFn = defineExtensionConfig(config).build.rollupOptions.output.manualChunks; |
| 223 | + |
| 224 | + result = manualChunksFn(moduleId); |
| 225 | + |
| 226 | + expect(result).toEqual(expected); |
| 227 | + }, |
| 228 | + ); |
| 229 | + }); |
| 230 | +}); |
0 commit comments