nuxt-community / electron-template

Electron starter with nuxt.js
MIT License
251 stars 31 forks source link

Can't require most packages from client-side #3

Closed Kylart closed 7 years ago

Kylart commented 7 years ago

Hi,

I am using this starter to migrate my electron app to Nuxt and so far I really like it.

Yet, I have a problem. Actually I have a solution to it and I understand why it is this way but I'd like to get it working just like any electron page.

Electron allows one to get rid (until a certain point) of client and server-side. But here, it is not possible to require any package needing fs, tls and net (which includes a lot of packages). In a normal website or Nuxt project, this would be completely normal but here it can be problematic and troublesome sometimes isn't it ?

I could not even require('electron-router') in the store or on client-side.

So right now, I have to make routes like this for example:

// main.js
const route = require('./assets/scripts/init/main.js').route(nuxt)
const server = http.createServer(route)
// assets/scripts/init/main/js
const URL = require('url-parse')

const {openExternal} = require('./openExternal.js')
const releases = require('./releases.js')
const seasons = require('./seasons.js')

exports.route = (nuxt) => {
  return (req, res) => {
    const url = new URL(req.url)

    switch (url.pathname)
    {
      case '/openThis':
        openExternal(url, res)
        break

      case '/seasons.json':
        seasons.getSeason(url, res)
        break

      case '/releases.json':
        releases.getLatest(url, res)
        break

      default:
        nuxt.render(req, res)
    }
  }
}

Isn't it possible for client to have access to any module as well just like in a basic electron app where I could just require('fs') anywhere in the app?

I do not intend to criticise anything, just asking 😮

This question is available on Nuxt.js community (#c8)
detrohutt commented 7 years ago

@Kylart This is definitely a valid concern. I'm admittedly an electron(and nuxt) beginner myself. I only made this repo in response to an issue on another nuxt starter repo asking for support for electron. It seemed like a simple enough request lol. That was my first time using electron. Anyway I will do my best to get this fixed but it may take a while as it's been a pretty busy week for me and I'll also need to do some more research on electron and nuxt and probably webpack to figure out a solution to this. In the meantime, feedback from anyone who is more experienced with any/all of the above libraries is very welcome.

detrohutt commented 7 years ago

@Kylart You may want to also try what I suggested here. Let me know if that helps.

Kylart commented 7 years ago

@detrohutt I just read #4 's solution and it actually works really well! Seems like I can require all my packages in the store. I can even put them into the build vendor ! Good job!

detrohutt commented 7 years ago

Excellent! was it electron-main or electron-renderer target that worked for you?

Kylart commented 7 years ago

Only electron-renderer seems to work. If I put electron-main, my app seems not to be working (style and default vue shows up but buttons do not respond for example.

Still, everytime I start the app, I get something like this:

 warning  in ./~/ajv/lib/async.js

119:15-28 Critical dependency: the request of a dependency is an expression

What I did was const fs = require('fs') in store/index.js

Yet, it does not seem to affect my application!