|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const webpack = require("webpack"); |
| 4 | +const requireFromString = require("require-from-string"); |
| 5 | +const Server = require("../../lib/Server"); |
| 6 | +const simpleConfig = require("../fixtures/module-federation-config/webpack.config"); |
| 7 | +const objectEntryConfig = require("../fixtures/module-federation-config/webpack.object-entry.config"); |
| 8 | +const multiConfig = require("../fixtures/module-federation-config/webpack.multi.config"); |
| 9 | +const runBrowser = require("../helpers/run-browser"); |
| 10 | +const isWebpack5 = require("../helpers/isWebpack5"); |
| 11 | +const port = require("../ports-map")["universal-compiler"]; |
| 12 | + |
| 13 | +const describeOnlyWebpack5 = isWebpack5 ? describe : describe.skip; |
| 14 | + |
| 15 | +let pluginConfig; |
| 16 | + |
| 17 | +if (isWebpack5) { |
| 18 | + pluginConfig = require("../fixtures/module-federation-config/webpack.plugin"); |
| 19 | +} |
| 20 | + |
| 21 | +describe("Module federation", () => { |
| 22 | + describe("should work with simple multi-entry config", () => { |
| 23 | + let compiler; |
| 24 | + let server; |
| 25 | + let page; |
| 26 | + let browser; |
| 27 | + let pageErrors; |
| 28 | + let consoleMessages; |
| 29 | + |
| 30 | + beforeEach(async () => { |
| 31 | + compiler = webpack(simpleConfig); |
| 32 | + server = new Server({ port }, compiler); |
| 33 | + |
| 34 | + await server.start(); |
| 35 | + |
| 36 | + ({ page, browser } = await runBrowser()); |
| 37 | + |
| 38 | + pageErrors = []; |
| 39 | + consoleMessages = []; |
| 40 | + }); |
| 41 | + |
| 42 | + afterEach(async () => { |
| 43 | + await browser.close(); |
| 44 | + await server.stop(); |
| 45 | + }); |
| 46 | + |
| 47 | + it("should use the last entry export", async () => { |
| 48 | + page |
| 49 | + .on("console", (message) => { |
| 50 | + consoleMessages.push(message); |
| 51 | + }) |
| 52 | + .on("pageerror", (error) => { |
| 53 | + pageErrors.push(error); |
| 54 | + }); |
| 55 | + |
| 56 | + await page.goto(`http://127.0.0.1:${port}/main.js`, { |
| 57 | + waitUntil: "networkidle0", |
| 58 | + }); |
| 59 | + |
| 60 | + const bodyHandle = await page.$("body"); |
| 61 | + const textContent = await page.evaluate( |
| 62 | + (body) => body.textContent, |
| 63 | + bodyHandle |
| 64 | + ); |
| 65 | + |
| 66 | + expect(textContent).toContain("entry1"); |
| 67 | + |
| 68 | + let exports; |
| 69 | + |
| 70 | + expect(() => { |
| 71 | + exports = requireFromString(textContent); |
| 72 | + }).not.toThrow(); |
| 73 | + |
| 74 | + expect(exports).toEqual("entry2"); |
| 75 | + |
| 76 | + expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( |
| 77 | + "console messages" |
| 78 | + ); |
| 79 | + |
| 80 | + expect(pageErrors).toMatchSnapshot("page errors"); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + describe("should work with object multi-entry config", () => { |
| 85 | + let compiler; |
| 86 | + let server; |
| 87 | + let page; |
| 88 | + let browser; |
| 89 | + let pageErrors; |
| 90 | + let consoleMessages; |
| 91 | + |
| 92 | + beforeEach(async () => { |
| 93 | + compiler = webpack(objectEntryConfig); |
| 94 | + server = new Server({ port }, compiler); |
| 95 | + |
| 96 | + await server.start(); |
| 97 | + |
| 98 | + ({ page, browser } = await runBrowser()); |
| 99 | + |
| 100 | + pageErrors = []; |
| 101 | + consoleMessages = []; |
| 102 | + }); |
| 103 | + |
| 104 | + afterEach(async () => { |
| 105 | + await browser.close(); |
| 106 | + await server.stop(); |
| 107 | + }); |
| 108 | + |
| 109 | + it("should use the last entry export", async () => { |
| 110 | + page |
| 111 | + .on("console", (message) => { |
| 112 | + consoleMessages.push(message); |
| 113 | + }) |
| 114 | + .on("pageerror", (error) => { |
| 115 | + pageErrors.push(error); |
| 116 | + }); |
| 117 | + |
| 118 | + await page.goto(`http://127.0.0.1:${port}/main.js`, { |
| 119 | + waitUntil: "networkidle0", |
| 120 | + }); |
| 121 | + |
| 122 | + const bodyHandle = await page.$("body"); |
| 123 | + const textContent = await page.evaluate( |
| 124 | + (body) => body.textContent, |
| 125 | + bodyHandle |
| 126 | + ); |
| 127 | + |
| 128 | + expect(textContent).toContain("entry1"); |
| 129 | + |
| 130 | + let exports; |
| 131 | + |
| 132 | + expect(() => { |
| 133 | + exports = requireFromString(textContent); |
| 134 | + }).not.toThrow(); |
| 135 | + |
| 136 | + expect(exports).toEqual("entry2"); |
| 137 | + |
| 138 | + expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( |
| 139 | + "console messages" |
| 140 | + ); |
| 141 | + |
| 142 | + expect(pageErrors).toMatchSnapshot("page errors"); |
| 143 | + }); |
| 144 | + |
| 145 | + it("should support the named entry export", async () => { |
| 146 | + page |
| 147 | + .on("console", (message) => { |
| 148 | + consoleMessages.push(message); |
| 149 | + }) |
| 150 | + .on("pageerror", (error) => { |
| 151 | + pageErrors.push(error); |
| 152 | + }); |
| 153 | + |
| 154 | + await page.goto(`http://127.0.0.1:${port}/foo.js`, { |
| 155 | + waitUntil: "networkidle0", |
| 156 | + }); |
| 157 | + |
| 158 | + const bodyHandle = await page.$("body"); |
| 159 | + const textContent = await page.evaluate( |
| 160 | + (body) => body.textContent, |
| 161 | + bodyHandle |
| 162 | + ); |
| 163 | + |
| 164 | + expect(textContent).not.toContain("entry2"); |
| 165 | + |
| 166 | + let exports; |
| 167 | + |
| 168 | + expect(() => { |
| 169 | + exports = requireFromString(textContent); |
| 170 | + }).not.toThrow(); |
| 171 | + |
| 172 | + expect(exports).toEqual("entry1"); |
| 173 | + |
| 174 | + expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( |
| 175 | + "console messages" |
| 176 | + ); |
| 177 | + |
| 178 | + expect(pageErrors).toMatchSnapshot("page errors"); |
| 179 | + }); |
| 180 | + }); |
| 181 | + |
| 182 | + describe("should work with multi compiler config", () => { |
| 183 | + let compiler; |
| 184 | + let server; |
| 185 | + let page; |
| 186 | + let browser; |
| 187 | + let pageErrors; |
| 188 | + let consoleMessages; |
| 189 | + |
| 190 | + beforeEach(async () => { |
| 191 | + compiler = webpack(multiConfig); |
| 192 | + server = new Server({ port }, compiler); |
| 193 | + |
| 194 | + await server.start(); |
| 195 | + |
| 196 | + ({ page, browser } = await runBrowser()); |
| 197 | + |
| 198 | + pageErrors = []; |
| 199 | + consoleMessages = []; |
| 200 | + }); |
| 201 | + |
| 202 | + afterEach(async () => { |
| 203 | + await browser.close(); |
| 204 | + await server.stop(); |
| 205 | + }); |
| 206 | + |
| 207 | + it("should use the last entry export", async () => { |
| 208 | + page |
| 209 | + .on("console", (message) => { |
| 210 | + consoleMessages.push(message); |
| 211 | + }) |
| 212 | + .on("pageerror", (error) => { |
| 213 | + pageErrors.push(error); |
| 214 | + }); |
| 215 | + |
| 216 | + await page.goto(`http://127.0.0.1:${port}/main.js`, { |
| 217 | + waitUntil: "networkidle0", |
| 218 | + }); |
| 219 | + |
| 220 | + const bodyHandle = await page.$("body"); |
| 221 | + const textContent = await page.evaluate( |
| 222 | + (body) => body.textContent, |
| 223 | + bodyHandle |
| 224 | + ); |
| 225 | + |
| 226 | + expect(textContent).toContain("entry1"); |
| 227 | + |
| 228 | + let exports; |
| 229 | + |
| 230 | + expect(() => { |
| 231 | + exports = requireFromString(textContent); |
| 232 | + }).not.toThrow(); |
| 233 | + |
| 234 | + expect(exports).toEqual("entry2"); |
| 235 | + |
| 236 | + expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( |
| 237 | + "console messages" |
| 238 | + ); |
| 239 | + |
| 240 | + expect(pageErrors).toMatchSnapshot("page errors"); |
| 241 | + }); |
| 242 | + }); |
| 243 | + |
| 244 | + describeOnlyWebpack5("should use plugin", () => { |
| 245 | + let compiler; |
| 246 | + let server; |
| 247 | + let page; |
| 248 | + let browser; |
| 249 | + let pageErrors; |
| 250 | + let consoleMessages; |
| 251 | + |
| 252 | + beforeEach(async () => { |
| 253 | + compiler = webpack(pluginConfig); |
| 254 | + server = new Server({ port }, compiler); |
| 255 | + |
| 256 | + await server.start(); |
| 257 | + |
| 258 | + ({ page, browser } = await runBrowser()); |
| 259 | + |
| 260 | + pageErrors = []; |
| 261 | + consoleMessages = []; |
| 262 | + }); |
| 263 | + |
| 264 | + afterEach(async () => { |
| 265 | + await browser.close(); |
| 266 | + await server.stop(); |
| 267 | + }); |
| 268 | + |
| 269 | + it("should contain hot script in remoteEntry.js", async () => { |
| 270 | + page |
| 271 | + .on("console", (message) => { |
| 272 | + consoleMessages.push(message); |
| 273 | + }) |
| 274 | + .on("pageerror", (error) => { |
| 275 | + pageErrors.push(error); |
| 276 | + }); |
| 277 | + |
| 278 | + await page.goto(`http://127.0.0.1:${port}/remoteEntry.js`, { |
| 279 | + waitUntil: "networkidle0", |
| 280 | + }); |
| 281 | + |
| 282 | + const bodyHandle = await page.$("body"); |
| 283 | + const remoteEntryTextContent = await page.evaluate( |
| 284 | + (body) => body.textContent, |
| 285 | + bodyHandle |
| 286 | + ); |
| 287 | + |
| 288 | + expect(remoteEntryTextContent).toMatch(/webpack\/hot\/dev-server\.js/); |
| 289 | + |
| 290 | + expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( |
| 291 | + "console messages" |
| 292 | + ); |
| 293 | + |
| 294 | + expect(pageErrors).toMatchSnapshot("page errors"); |
| 295 | + }); |
| 296 | + |
| 297 | + it("should contain hot script in main.js", async () => { |
| 298 | + page |
| 299 | + .on("console", (message) => { |
| 300 | + consoleMessages.push(message); |
| 301 | + }) |
| 302 | + .on("pageerror", (error) => { |
| 303 | + pageErrors.push(error); |
| 304 | + }); |
| 305 | + |
| 306 | + await page.goto(`http://127.0.0.1:${port}/main.js`, { |
| 307 | + waitUntil: "networkidle0", |
| 308 | + }); |
| 309 | + |
| 310 | + const bodyHandle = await page.$("body"); |
| 311 | + const mainEntryTextContent = await page.evaluate( |
| 312 | + (body) => body.textContent, |
| 313 | + bodyHandle |
| 314 | + ); |
| 315 | + |
| 316 | + expect(mainEntryTextContent).toMatch(/webpack\/hot\/dev-server\.js/); |
| 317 | + |
| 318 | + expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( |
| 319 | + "console messages" |
| 320 | + ); |
| 321 | + |
| 322 | + expect(pageErrors).toMatchSnapshot("page errors"); |
| 323 | + }); |
| 324 | + }); |
| 325 | +}); |
0 commit comments