* Attaches a WebSocket based connection to the Packager to expose * Hot Module Replacement updates to the simulator.
({httpServer, path, packagerServer}: HMROptions)
| 38 | * Hot Module Replacement updates to the simulator. |
| 39 | */ |
| 40 | function attachHMRServer({httpServer, path, packagerServer}: HMROptions) { |
| 41 | let client = null; |
| 42 | |
| 43 | function disconnect() { |
| 44 | client = null; |
| 45 | packagerServer.setHMRFileChangeListener(null); |
| 46 | } |
| 47 | |
| 48 | // For the give platform and entry file, returns a promise with: |
| 49 | // - The full list of dependencies. |
| 50 | // - The shallow dependencies each file on the dependency list has |
| 51 | // - Inverse shallow dependencies map |
| 52 | function getDependencies(platform: string, bundleEntry: string): Promise<{ |
| 53 | dependenciesCache: Array<string>, |
| 54 | dependenciesModulesCache: {[mixed]: Module}, |
| 55 | shallowDependencies: {[string]: Array<Module>}, |
| 56 | inverseDependenciesCache: mixed, |
| 57 | resolutionResponse: ResolutionResponse<Module, *>, |
| 58 | }> { |
| 59 | return packagerServer.getDependencies({ |
| 60 | dev: true, |
| 61 | entryFile: bundleEntry, |
| 62 | hot: true, |
| 63 | minify: false, |
| 64 | platform: platform, |
| 65 | recursive: true, |
| 66 | }).then(response => { |
| 67 | /* $FlowFixMe: getModuleId might be null */ |
| 68 | const {getModuleId}: {getModuleId: () => number} = response; |
| 69 | |
| 70 | // for each dependency builds the object: |
| 71 | // `{path: '/a/b/c.js', deps: ['modA', 'modB', ...]}` |
| 72 | return Promise.all(response.dependencies.map((dep: Module) => { |
| 73 | return dep.getName().then(depName => { |
| 74 | if (dep.isAsset() || dep.isJSON()) { |
| 75 | return Promise.resolve({path: dep.path, deps: []}); |
| 76 | } |
| 77 | return packagerServer.getShallowDependencies({ |
| 78 | dev: true, |
| 79 | entryFile: dep.path, |
| 80 | hot: true, |
| 81 | minify: false, |
| 82 | platform: platform, |
| 83 | recursive: true, |
| 84 | }).then(deps => { |
| 85 | return { |
| 86 | path: dep.path, |
| 87 | name: depName, |
| 88 | deps, |
| 89 | }; |
| 90 | }); |
| 91 | }); |
| 92 | })) |
| 93 | .then((deps: Array<{path: string, name?: string, deps: Array<Module>}>) => { |
| 94 | // list with all the dependencies' filenames the bundle entry has |
| 95 | const dependenciesCache = response.dependencies.map(dep => dep.path); |
| 96 | |
| 97 | // map from module name to path |
no test coverage detected
searching dependent graphs…