Skip to content

Commit 32206bf

Browse files
authored
refactor(core): merge native-bridge.js into core runtime (ionic-team#3748)
1 parent e1b1cfe commit 32206bf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2423
-1697
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
build
22
cli/assets
33
dist
4+
types

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.idea/
2-
dist
2+
dist/
3+
types/
34
.sourcemaps
45
xcuserdata/
56
node_modules/

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
build
2+
core/types
23
cli/assets
34
dist

android/capacitor/src/main/java/com/getcapacitor/Bridge.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -666,15 +666,14 @@ protected void savePermissionCall(PluginCall call) {
666666
private JSInjector getJSInjector() {
667667
try {
668668
String globalJS = JSExport.getGlobalJS(context, isDevMode());
669-
String coreJS = JSExport.getCoreJS(context);
670669
String pluginJS = JSExport.getPluginJS(plugins.values());
671670
String cordovaJS = JSExport.getCordovaJS(context);
672671
String cordovaPluginsJS = JSExport.getCordovaPluginJS(context);
673672
String cordovaPluginsFileJS = JSExport.getCordovaPluginsFileJS(context);
674673
String localUrlJS = "window.WEBVIEW_SERVER_URL = '" + localUrl + "';";
675674

676-
return new JSInjector(globalJS, coreJS, pluginJS, cordovaJS, cordovaPluginsJS, cordovaPluginsFileJS, localUrlJS);
677-
} catch (JSExportException ex) {
675+
return new JSInjector(globalJS, pluginJS, cordovaJS, cordovaPluginsJS, cordovaPluginsFileJS, localUrlJS);
676+
} catch (Exception ex) {
678677
Logger.error("Unable to export Capacitor JS. App will not function!", ex);
679678
}
680679
return null;

android/capacitor/src/main/java/com/getcapacitor/JSExport.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,7 @@ public class JSExport {
1515
private static String CALLBACK_PARAM = "_callback";
1616

1717
public static String getGlobalJS(Context context, boolean isDebug) {
18-
return "window.Capacitor = { DEBUG: " + isDebug + " };";
19-
}
20-
21-
public static String getCoreJS(Context context) throws JSExportException {
22-
try {
23-
return getJS(context, "public/native-bridge.js");
24-
} catch (IOException ex) {
25-
throw new JSExportException("Unable to load native-bridge.js. Capacitor will not function!", ex);
26-
}
18+
return "window.Capacitor = { DEBUG: " + isDebug + ", Plugins: {} };";
2719
}
2820

2921
private static String getJS(Context context, String fileName) throws IOException {
@@ -70,10 +62,11 @@ public static String getPluginJS(Collection<PluginHandle> plugins) {
7062
for (PluginHandle plugin : plugins) {
7163
lines.add(
7264
"(function(w) {\n" +
73-
"var a = w.Capacitor; var p = a.Plugins;\n" +
74-
"var t = p['" +
65+
"var a = (w.Capacitor = w.Capacitor || {});\n" +
66+
"var p = (a.Plugins = a.Plugins || {});\n" +
67+
"var t = (p['" +
7568
plugin.getId() +
76-
"'] = {};\n" +
69+
"'] = {});\n" +
7770
"t.addListener = function(eventName, callback) {\n" +
7871
" return w.Capacitor.addListener('" +
7972
plugin.getId() +

android/capacitor/src/main/java/com/getcapacitor/JSInjector.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,25 @@
1414
class JSInjector {
1515

1616
private String globalJS;
17-
private String coreJS;
1817
private String pluginJS;
1918
private String cordovaJS;
2019
private String cordovaPluginsJS;
2120
private String cordovaPluginsFileJS;
2221
private String localUrlJS;
2322

24-
public JSInjector(String globalJS, String coreJS, String pluginJS) {
25-
this(globalJS, coreJS, pluginJS, ""/* cordovaJS */, ""/* cordovaPluginsJS */, ""/* cordovaPluginsFileJS */, ""/* localUrlJS */);
23+
public JSInjector(String globalJS, String pluginJS) {
24+
this(globalJS, pluginJS, ""/* cordovaJS */, ""/* cordovaPluginsJS */, ""/* cordovaPluginsFileJS */, ""/* localUrlJS */);
2625
}
2726

2827
public JSInjector(
2928
String globalJS,
30-
String coreJS,
3129
String pluginJS,
3230
String cordovaJS,
3331
String cordovaPluginsJS,
3432
String cordovaPluginsFileJS,
3533
String localUrlJS
3634
) {
3735
this.globalJS = globalJS;
38-
this.coreJS = coreJS;
3936
this.pluginJS = pluginJS;
4037
this.cordovaJS = cordovaJS;
4138
this.cordovaPluginsJS = cordovaPluginsJS;
@@ -52,8 +49,6 @@ public String getScriptString() {
5249
return (
5350
globalJS +
5451
"\n\n" +
55-
coreJS +
56-
"\n\n" +
5752
pluginJS +
5853
"\n\n" +
5954
cordovaJS +

cli/src/tasks/copy.ts

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ import {
1616
writeCordovaAndroidManifest,
1717
} from '../cordova';
1818
import type { Config } from '../definitions';
19-
import { logger, logFatal } from '../log';
20-
import { resolveNode } from '../util/node';
19+
import { logger } from '../log';
2120
import { allSerial } from '../util/promise';
2221
import { copyWeb } from '../web/copy';
2322

@@ -63,13 +62,11 @@ export async function copy(
6362

6463
if (platformName === config.ios.name) {
6564
await copyWebDir(config, config.ios.webDirAbs);
66-
await copyNativeBridge(config.app.rootDir, config.ios.webDirAbs);
6765
await copyCapacitorConfig(config, config.ios.nativeTargetDirAbs);
6866
const cordovaPlugins = await getCordovaPlugins(config, platformName);
6967
await handleCordovaPluginsJS(cordovaPlugins, config, platformName);
7068
} else if (platformName === config.android.name) {
7169
await copyWebDir(config, config.android.webDirAbs);
72-
await copyNativeBridge(config.app.rootDir, config.android.webDirAbs);
7370
await copyCapacitorConfig(config, config.android.assetsDirAbs);
7471
const cordovaPlugins = await getCordovaPlugins(config, platformName);
7572
await handleCordovaPluginsJS(cordovaPlugins, config, platformName);
@@ -82,28 +79,6 @@ export async function copy(
8279
});
8380
}
8481

85-
async function copyNativeBridge(rootDir: string, nativeAbsDir: string) {
86-
const nativeRelDir = relative(rootDir, nativeAbsDir);
87-
const bridgePath = resolveNode(
88-
rootDir,
89-
'@capacitor/core',
90-
'native-bridge.js',
91-
);
92-
if (!bridgePath) {
93-
logFatal(
94-
`Unable to find node_modules/@capacitor/core/native-bridge.js.\n` +
95-
`Are you sure ${c.strong('@capacitor/core')} is installed?`,
96-
);
97-
}
98-
99-
await runTask(
100-
`Copying ${c.strong('native-bridge.js')} to ${nativeRelDir}`,
101-
async () => {
102-
return fsCopy(bridgePath!, join(nativeAbsDir, 'native-bridge.js'));
103-
},
104-
);
105-
}
106-
10782
async function copyCapacitorConfig(config: Config, nativeAbsDir: string) {
10883
const nativeRelDir = relative(config.app.rootDir, nativeAbsDir);
10984
const nativeConfigFile = 'capacitor.config.json';

cli/tsconfig.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,5 @@
1212
"strict": true,
1313
"target": "es2019"
1414
},
15-
"files": [
16-
"src/index.ts"
17-
]
15+
"files": ["src/index.ts"]
1816
}

0 commit comments

Comments
 (0)