簡體   English   中英

Phonegap 7搞砸了package.json

[英]Phonegap 7 is messing with package.json

我正在嘗試將用react和webpack構建的phonegap / cordova android應用程序升級到最新版本7.0.1

當我執行phonegap build android時,phonegap所做的新動作之一是自動將具有我Cordova插件的所有依賴關系的package.json更新到文件中,此外還要添加關於cordova的部分,請檢查以下代碼段:

運行phonegap build ,我在package.json中看到:

"dependencies": {            
        // my normal dependencies
        ...
        "counterpart": "0.18.2",
        "react": "15.6.1",
        ...

        // added by phonegap    
        "cordova-android": "^6.2.3",
        "cordova-plugin-battery-status": "~1.1.1",
        "cordova-plugin-camera": "~2.1.1",
        "cordova-plugin-console": "~1.0.2",
        "cordova-plugin-contacts": "~2.0.1",
        "cordova-plugin-crosswalk-webview": "^2.3.0",
        "cordova-plugin-customurlscheme": "~4.3.0",
        "cordova-plugin-device": "~1.1.2",
        "cordova-plugin-device-motion": "~1.2.0",
        "cordova-plugin-device-orientation": "^1.0.7",
        "cordova-plugin-dialogs": "~1.2.0",
        "cordova-plugin-file": "~4.1.1",
        "cordova-plugin-file-transfer": "~1.5.0",
        "cordova-plugin-geolocation": "~2.2.0",
        "cordova-plugin-globalization": "~1.0.3",
        "cordova-plugin-inappbrowser": "~1.3.0",
        "cordova-plugin-media": "~2.2.0",
        "cordova-plugin-media-capture": "~1.2.0",
        "cordova-plugin-network-information": "~1.2.1",
        "cordova-plugin-screen-orientation": "^2.0.1",
        "cordova-plugin-screensize": "1.3.1",
        "cordova-plugin-splashscreen": "~3.2.1",
        "cordova-plugin-statusbar": "~2.1.3",
        "cordova-plugin-vibration": "~2.1.5",
        "cordova-plugin-whitelist": "~1.2.2"
    },
    "cordova": {
    "platforms": [
        "android"
    ],
    "plugins": {
        "cordova-plugin-battery-status": {},
        "cordova-plugin-camera": {},
        "cordova-plugin-console": {},
        "cordova-plugin-contacts": {},
        "cordova-plugin-crosswalk-webview": {},
        "cordova-plugin-customurlscheme": {
            "URL_SCHEME": "essforms",
            "ANDROID_SCHEME": " ",
            "ANDROID_HOST": " ",
            "ANDROID_PATHPREFIX": "/"
        },
        "cordova-plugin-device": {},
        "cordova-plugin-device-motion": {},
        "cordova-plugin-device-orientation": {},
        "cordova-plugin-dialogs": {},
        "cordova-plugin-file": {},
        "cordova-plugin-file-transfer": {},
        "cordova-plugin-geolocation": {},
        "cordova-plugin-globalization": {},
        "cordova-plugin-inappbrowser": {},
        "cordova-plugin-media": {},
        "cordova-plugin-media-capture": {},
        "cordova-plugin-network-information": {},
        "cordova-plugin-screen-orientation": {},
        "cordova-plugin-screensize": {},
        "cordova-plugin-splashscreen": {},
        "cordova-plugin-statusbar": {},
        "cordova-plugin-vibration": {},
        "cordova-plugin-whitelist": {}
    }
}

這在PhoneGap的新版本有所解釋7+ 這里

我的問題是,發生這種情況時,我將無法再構建應用程序或使用npm start獨立啟動它,因為某些依賴項會產生以下錯誤:

WARNING in ./node_modules/cordova-android/bin/templates/cordova/Api.js
118:30-79 Critical dependency: the request of a dependency is an expression

WARNING in ./node_modules/cordova-android/bin/templates/cordova/Api.js
152:30-79 Critical dependency: the request of a dependency is an expression

WARNING in ./node_modules/cordova-android/bin/lib/create.js
Module parse failed: /storage/projects/exus/inachus/source/ess-forms-android/node_modules/cordova-android/bin/lib/create.js Unexpected character '#' (1:0)
You may need an appropriate loader to handle this file type.
| #!/usr/bin/env node
| 

如何克服這個問題? 我可以添加一些配置來防止這種情況發生嗎?

我發現了一個解決方法,可以解決此問題,因為似乎在構建您的React應用程序時我們必須手動排除cordova的軟件包,您可以執行以下操作(我不知道是否有更好的解決方案):

...

function removeMatching(originalArray, regex) {
    var j = 0;
    while (j < originalArray.length) {
        if (regex.test(originalArray[j]))
            originalArray.splice(j, 1);
        else
            j++;
    }
    return originalArray;
}

module.exports = {
    entry: {
        vendor: removeMatching(Object.keys(require('./package.json').dependencies), /cordova-.*/)
    },

 ...

聲明條目時,請確保排除cordova依賴項

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM