Electron - Not allowed to load local resource

后端 未结 10 1887
夕颜
夕颜 2020-12-28 14:05

Evening,
I\'m looking into using electron to package an existing angular2 build. I thought I had a dry run working but the actual packaging seems to be failing (see fina

相关标签:
10条回答
  • 2020-12-28 14:40
    By Changing "private": true, to "private": false in pakage.json. works for me good luck.
    
    {
      "name": "dashboard",
      "version": "0.0.0",
      "main": "main.js",
      "scripts": {
        "ng": "ng",
        "start": "ng serve",
        "build": "ng build",
        "test": "ng test",
        "lint": "ng lint",
        "e2e": "ng e2e",
        "electron": "ng build && electron .",
        "pack":"electron-packager ."
      },
      "private": false,
      "dependencies": {
        "@angular/animations": "~10.2.0",
        "@angular/cdk": "^8.2.3",
        "@angular/common": "~10.2.0",
        "@angular/compiler": "~10.2.0",
        "@angular/core": "~10.2.0",
        "@angular/flex-layout": "^9.0.0-beta.29",
        "@angular/forms": "~10.2.0",
        "@angular/material": "^8.2.3",
        "@angular/platform-browser": "~10.2.0",
        "@angular/platform-browser-dynamic": "~10.2.0",
        "@angular/router": "~10.2.0",
        "hammerjs": "^2.0.8",
        "highcharts": "^7.2.2",
        "highcharts-angular": "^2.4.0",
        "rxjs": "~6.6.3",
        "tslib": "^1.14.1",
        "zone.js": "~0.10.3"
      },
      "devDependencies": {
        "@angular-devkit/build-angular": "^0.1002.0",
        "@angular/cli": "^10.2.0",
        "@angular/compiler-cli": "~10.2.0",
        "@angular/language-service": "~10.2.0",
        "@types/jasmine": "~3.3.8",
        "@types/jasminewd2": "~2.0.3",
        "@types/node": "~8.9.4",
        "codelyzer": "^5.0.0",
        "electron": "^10.1.5",
        "electron-packager": "^15.1.0",
        "jasmine-core": "~3.4.0",
        "jasmine-spec-reporter": "~4.2.1",
        "karma": "^5.2.3",
        "karma-chrome-launcher": "~2.2.0",
        "karma-coverage-istanbul-reporter": "~2.0.1",
        "karma-jasmine": "~2.0.1",
        "karma-jasmine-html-reporter": "^1.4.0",
        "protractor": "^7.0.0",
        "ts-node": "~7.0.0",
        "tslint": "~5.15.0",
        "typescript": "~4.0.5"
      }
    }
    
    
    0 讨论(0)
  • 2020-12-28 14:43

    In case this helps anyone, the issue I was having was I had the path set using process.cwd():

    win.loadURL(`file://${process.cwd()}/dist/index.html`);
    

    Which when packaged is not correct since the the main.js file is not in the root anymore. Changing to __dirname:

    win.loadURL(`file://${__dirname}/dist/index.html`);
    

    Fixed the issue for me.

    0 讨论(0)
  • 2020-12-28 14:51

    It took a lot of trial and error but I got this working. There is a lot here that I don't totally understand, and much that might be pointless or bad practice and it might all fall down at the very next step but if, like me, you are just trying to get over the first hump then maybe something I found will help you.

    I found the problem by unpacking the app.asar file that electron-builder produces. Instead of containing the bundled code from my dist folder it contained all the project code (*.ts *.scss etc). The problem was just that the packing functions were packing up the wrong stuff.
    The steps to get the right source into the final app are simple when you lay them out but my god they didn't half put up a fight! Starting from where I left off in my initial question I did the following...

    Remember that my project structure is the default one set up by angular-cli

    Electron specific files
    I moved the main.js down into src and changed its name to electron-main.js just to stop any confusion with the main.ts that is already in there. I also changed the path it references back to /index.html.
    Next I created a new application level package.json also in src and gave it the following content:

     {
      "name": "application-name",
      "description": "CLI app",
      "author": "Me me me",
      "version": "0.0.1",
      "main": "electron-main.js"
    }
    

    angular-cli.json
    I changed the outDir to build purely because electron seems to output to dist by default and I wanted some separation at this stage. Then I affffded package.json and electron-main.js to the assets array.

    Main package.json
    I removed the "main":"main.js" as it is apparently no longer needed here. In scripts I changed the test command to electron build to point it at the build folder where the bundled code will be.
    Finally, I went to the build field and added the following content:

    "directories": {
      "buildResources": "build",
      "app": "build"
    }
    

    Seems pretty obvious now. This just tells the compiler where to look for the assets that will make up the final app. It was defaulting to the working directory and that was my problem.

    Using this setup I can now run ng build to bundle my project into the build folder along with the electron-main.js and package.json.
    This done I can run npm run electron to quickly launch a test app or npm run pack to build an app that can be launched from Finder.

    Boom. Job done. I'll be back here in ten minutes with another annoying question I expect. Gotta love the dev these days :)

    0 讨论(0)
  • 2020-12-28 14:52

    You are just missing a '.' here. You only need to put relative path instead of absolute path.

    win.loadFile('./dist/index.html');
    
    0 讨论(0)
提交回复
热议问题