Code Monkey home page Code Monkey logo

electron-svelte-typescript's Introduction

electron-svelte-typescript

Template for apps written with Electron, Svelte and Typescript.

The template does hot module replacement and reloads electron on main process file changes out of the box. It also follows some good security practices, such as Content-Security-Policy meta tags in html, context isolation set to true, remote modules set to false etc.

Get started

To create a new project based on this template you must first clone the repo and then delete the folder .git:

git clone https://github.com/fuzzc0re/electron-svelte-typescript MyAppName
cd MyAppName
rm -rf .git

Note that you will need to have Node.js installed.

Install the dependencies...

npm i

...then start coding in dev mode:

npm start

The start script spins up Rollup in watch mode with a Rollup-Plugin-Serve instance serving the frontend static files on localhost:5000 and a nodemon server to watch for file changes related to the main electron process.

Electron loads its html content from localhost:5000 in dev mode and from build/public/index.html in production mode.

The Svelte development happens in src/frontend and the Electron development in src/electron.

Edit a file in src, save it, and see the changes in the app.

Building and running in production mode

To create an optimized build of the app:

npm run build

To create a distributable version of the app with electron-builder:

npm run dist

In production mode, sourcemaps are disabled, html css and js files are compressed and mangled, devTools are disabled and CSP allows only same origin scripts to load.

Contributing

In order to lint the code you run:

npm run lint

In order to prettify the code you run:

npm run format

You should run the following command, which runs lint and then format, on your contributed code:

npm run preversion

before creating a pull request.

All suggestions are welcome!

Licence

This project is licensed under the terms described in LICENSE.

electron-svelte-typescript's People

Contributors

agarwalpragy avatar dependabot[bot] avatar enricozb avatar fuzzc0re avatar kiamazi avatar silentechogm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

electron-svelte-typescript's Issues

VSCode run and debug task

It would be useful to have a VSCode launch.json entry that starts Electron (with auto-compilation and auto reload?) and attaches the VSCode debugger to it so that the chrome devtools don't have to be used.
I've tried fiddling around with the launch.json a bit, but couldn't make a task work that properly accomplishes this.
I think it would also be a good addition to the template.

svelte(missing-declaration)

I have created a Test.svelte component

<script lang="ts">
    export let hello:string = "World"
</script>

<div>{hello}</div>

I have this worning in VScode

image

Then I imported the component into App.svelte

<script lang="ts">
  import Test from "./Test.svelte";
  export let name: string;
</script>

<main>
  <h1>Hello {name}!</h1>
  <p>Visit the <a href="https://svelte.dev/tutorial">Svelte tutorial</a> to learn how to build Svelte apps.</p>
  <Test />
</main>

I have this worning in VScode

image

By searching I found this hypothesis for a solution

Getting svelte(missing-declaration) warnings for props

But I don't know how to do it.

Question: What do I need to change to import TS files into a Svelte component?

Thank you for making this template, it simplifies the process so much!

I have a question that may be simple to solve, or I may be misunderstanding something more fundamental.

I want to import a function from a TS file into the App svelte component from your template. The function uses the Twitter v2 API, so that might be causing an issue.
I use the following code to import the function into App:

<script lang="ts">
  import { getUserDesc } from "../electron/utils/Twitter/twitter";
  export let name: string;
</script>

Where the twitter.ts file looks like so:

import TwitterApi from 'twitter-api-v2';

const twitter = new TwitterApi()

const roClient = twitter.readOnly

export async function getUserDesc(username: string) {
    const user = await roClient.v2.userByUsername(username)
    console.log(user.data.description)
}

When running the code, I get the following error: Error: Unexpected token (Note that you need plugins to import files that are not JavaScript).

Is it that I just need a rollup-plugin? Please let me know if there is something to do differently.

Thank you for reading this :)

npx degit crashes

Using the command described in readme I ran npx degit fuzzc0re/electron-svelte-typescript-boilerplate MyApp and it crashed with the following error:

! could not fetch remote https://github.com/fuzzc0re/electron-svelte-typescript-boilerplate
! could not find commit hash for master

Error running "npm run build"

I get follwing error, when I run "npm run build"

tsconfigSvelteJSON.compilerOptions.sourceMap = false;
TypeError: Cannot set property 'sourceMap' of undefined
    at generateProdTSConfig (<MYFILEPATH>\scripts\preBuild.js:31:48)
    at Object.<anonymous> (<MYFILEPATH>\scripts\preBuild.js:50:3)

Warning message running `npm test`

Using this template unmodified, I get this message running npm test:

ts-jest[config] (WARN) message TS151001: If you have issues related to imports, you should consider setting `esModuleInterop` to `true` in your TypeScript configuration file (usually `tsconfig.json`). See https://blogs.msdn.microsoft.com/typescript/2018/01/31/announcing-typescript-2-7/#easier-ecmascript-module-interoperability for more information.

With this dummy test file:

import * as path from 'path'

test("dummy test", () => {
  expect(path.join("xyz", "pdq")).toEqual("xyz/pdq")
})

The message remains no matter in which tsconfig.json I put the following lines:

  "allowSyntheticDefaultImports": true,
  "esModuleInterop": true,

Dist version tries to connect to http://localhost:5000/ and crashes on startup

I'm trying to get this template to work when the app is packaged, however it tries to connect to localhost and crashes when it's built.

git clone https://github.com/fuzzc0re/electron-svelte-typescript MyAppName
cd MyAppName
npm i
npm run build
npm run dist

now run the installer in dist and open the installed exe:

20:38:28.247 > App starting...
20:38:28.307 > Checking if settings store works correctly.
20:38:28.312 > Settings store works correctly.
20:38:28.398 > {
  preventDefault: '[function] function preventDefault() { [native code] }',
  sendReply: '[function] function sendReply() { [native code] }'
}
20:38:29.877 > {
  preventDefault: '[function] function preventDefault() { [native code] }',
  sendReply: '[function] function sendReply() { [native code] }'
}
20:38:37.143 > {"errno":-102,"code":"ERR_CONNECTION_REFUSED","url":"http://localhost:5000/"}

Enabling Node Integration

I'm trying to enable node:fs in the frontend part of a project that I started using your template.
Actually enabling nodeIntegration was easy enough, by just changing config arguments passed to the BrowserWindow, but I am unable to get rollup to properly transpile my imports to inbuilt libraries.

After the config changes, require() works fine in the electron browser console, but using import fs from "fs" fails with rollup warnings that it's unable to locate global variables for that library, and the import is simply gone in the compiled bundle.js, instead assuming that fs is available as a global variable.

I've tried changing the browser option of @rollup/plugin-node-resolve to false, but that did not help.
I have also created a Stackoverflow question about this 2 days ago, but not gotten any responses.

I would appreciate it if you could help me resolve this so that I am able to use node libraries (not just polyfills) in the frontend. (You could also make a branch for it on the template repo, so people can create a project from the template in whichever configuration they wish)

Thanks for your time!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.