|
1 | 1 | const os = require("os"); |
2 | | - |
3 | | -const { |
4 | | - getAppPathFromProjectData, |
5 | | - getAppResourcesPathFromProjectData, |
6 | | - isAndroid, |
7 | | -} = require("../projectHelpers"); |
8 | | - |
9 | | -const eventHandlers = {}; |
10 | | - |
11 | | -function debuggingEnabled(liveSyncService, projectDir) { |
12 | | - const deviceDescriptors = liveSyncService.getLiveSyncDeviceDescriptors(projectDir); |
13 | | - return deviceDescriptors.some(device => device.debugggingEnabled); |
14 | | -} |
15 | | - |
16 | | -function buildEnvData($projectData, platform, env) { |
17 | | - const envData = Object.assign({}, |
18 | | - env, |
19 | | - { [platform.toLowerCase()]: true } |
20 | | - ); |
21 | | - |
22 | | - const appPath = getAppPathFromProjectData($projectData); |
23 | | - const appResourcesPath = getAppResourcesPathFromProjectData($projectData); |
24 | | - Object.assign(envData, |
25 | | - appPath && { appPath }, |
26 | | - appResourcesPath && { appResourcesPath } |
27 | | - ); |
28 | | - |
29 | | - return envData; |
30 | | -} |
31 | | - |
32 | | -/** |
33 | | - * Checks if there's a file in the following pattern 5e0326f3bb50f9f26cf0.hot-update.json |
34 | | - * if yes this is a HMR update and remove all bundle files as we don't need them to be synced, |
35 | | - * but only the update chunks |
36 | | - */ |
37 | | -function getUpdatedEmittedFiles(emittedFiles, webpackRuntimeFiles) { |
38 | | - let fallbackFiles = []; |
39 | | - let hotHash; |
40 | | - if (emittedFiles.some(x => x.endsWith('.hot-update.json'))) { |
41 | | - let result = emittedFiles.slice(); |
42 | | - const hotUpdateScripts = emittedFiles.filter(x => x.endsWith('.hot-update.js')); |
43 | | - hotUpdateScripts.forEach(hotUpdateScript => { |
44 | | - const { name, hash } = parseHotUpdateChunkName(hotUpdateScript); |
45 | | - hotHash = hash; |
46 | | - // remove bundle/vendor.js files if there's a bundle.XXX.hot-update.js or vendor.XXX.hot-update.js |
47 | | - result = result.filter(file => file !== `${name}.js`); |
48 | | - if (webpackRuntimeFiles && webpackRuntimeFiles.length) { |
49 | | - // remove files containing only the Webpack runtime (e.g. runtime.js) |
50 | | - result = result.filter(file => webpackRuntimeFiles.indexOf(file) === -1); |
51 | | - } |
52 | | - }); |
53 | | - //if applying of hot update fails, we must fallback to the full files |
54 | | - fallbackFiles = emittedFiles.filter(file => result.indexOf(file) === -1); |
55 | | - return { emittedFiles: result, fallbackFiles, hash: hotHash }; |
56 | | - } |
57 | | - else { |
58 | | - return { emittedFiles, fallbackFiles }; |
59 | | - } |
60 | | -} |
61 | | - |
62 | | -/** |
63 | | - * Parse the filename of the hot update chunk. |
64 | | - * @param name bundle.deccb264c01d6d42416c.hot-update.js |
65 | | - * @returns { name: string, hash: string } { name: 'bundle', hash: 'deccb264c01d6d42416c' } |
66 | | - */ |
67 | | -function parseHotUpdateChunkName(name) { |
68 | | - const matcher = /^(.+)\.(.+)\.hot-update/gm; |
69 | | - const matches = matcher.exec(name); |
70 | | - return { |
71 | | - name: matches[1] || "", |
72 | | - hash: matches[2] || "", |
73 | | - }; |
74 | | -} |
| 2 | +const { isAndroid } = require("../projectHelpers"); |
75 | 3 |
|
76 | 4 | function shouldSnapshot(config) { |
77 | 5 | const platformSupportsSnapshot = isAndroid(config.platform); |
78 | 6 | const osSupportsSnapshot = os.type() !== "Windows_NT"; |
79 | 7 |
|
80 | | - return config.bundle && config.release && platformSupportsSnapshot && osSupportsSnapshot; |
81 | | -} |
82 | | - |
83 | | -function addListener(eventEmitter, name, handler) { |
84 | | - if (!eventHandlers[name]) { |
85 | | - eventEmitter.on(name, handler); |
86 | | - eventHandlers[name] = handler; |
87 | | - } |
88 | | -} |
89 | | - |
90 | | -function removeListener(eventEmitter, name) { |
91 | | - if (eventHandlers[name]) { |
92 | | - eventEmitter.removeListener(name, eventHandlers[name]); |
93 | | - delete eventHandlers[name]; |
94 | | - } |
| 8 | + return config.release && platformSupportsSnapshot && osSupportsSnapshot; |
95 | 9 | } |
96 | 10 |
|
97 | 11 | function convertToUnixPath(relativePath) { |
98 | 12 | return relativePath.replace(/\\/g, "/"); |
99 | 13 | } |
100 | 14 |
|
101 | 15 | module.exports = { |
102 | | - buildEnvData, |
103 | | - debuggingEnabled, |
104 | 16 | shouldSnapshot, |
105 | | - getUpdatedEmittedFiles, |
106 | | - parseHotUpdateChunkName, |
107 | | - addListener, |
108 | | - removeListener, |
109 | 17 | convertToUnixPath |
110 | 18 | }; |
0 commit comments