DEV Community

Cover image for Sapper(svelte) + CodyFrame Ui
zakaria chahboun
zakaria chahboun

Posted on โ€ข Edited on

6 4

Sapper(svelte) + CodyFrame Ui

Sapper + Codyframe

This project is a template for Sapper if you want to work with CodyFrame framework UI (From Cody House), And you want to compile the SASS files and using it directly with Rollup ๐Ÿ‘Œ


Note: You can clone this template directly or you can follow the instructions below ๐Ÿต


First things ๐Ÿคš

After getting the template of sapper for Rollup by:

๐Ÿ‘‰ npx degit "sveltejs/sapper-template#rollup" my-app

You have to install the ordinary dependencies by:

๐Ÿ‘‰ npm install

And try to run it (on http://localhost:3000) by:

๐Ÿ‘‰ npm run dev

Issues could happen ๐Ÿ“›

If you have this issue:

UnhandledPromiseRejectionWarning: Error: No valid exports main found for /..node_modules/@rollup/pluginutils

Just remove the rollup package from npm and reinstall a new version of it like "2.13.0".

Also if you don't have Polka install it by:

npm install --save polka

You can use express js ๐ŸŒฑ instead of Polka, learn how to do that from this Youtube video

Dependencies for SASS compiler ๐Ÿต๏ธ

  • svelte-preprocess ๐Ÿ‘ˆ
  • autoprefixer ๐Ÿ‘ˆ
  • node-sass ๐Ÿ‘ˆ

npm install -D node-sass autoprefixer svelte-preprocess

or

yarn add -D node-sass autoprefixer svelte-preprocess

Rollup Configurations ๐ŸฆŠ

Inside the rollup.config.js file, add these lines outside of export default to be accessible globally:

// for sass (codyframe)
import sveltePreprocess from 'svelte-preprocess';
const preprocess = sveltePreprocess({
  scss: {
    includePaths: ['src'],
  },
  postcss: {
    plugins: [require('autoprefixer')],
  },
});
Enter fullscreen mode Exit fullscreen mode

Also add these lines in both ๐Ÿค  Client and Server sections inside of svelte({...}):

svelte({
  ...
  preprocess // Add this line
  ...
}),
Enter fullscreen mode Exit fullscreen mode

Get the CodyFrame ๐Ÿคฆโ€โ™‚

Clone the official project from GitHub: Here

We just want the assets folder ๐Ÿ™„, So copy it inside codyframe folder in your src folder (src/codyframe) .

We want the style.scss and util.js later ๐Ÿ‘Œ.

Sapper Tepmlate File ๐Ÿช

In the top of template.html file in the src folder, We have to add a class="js" attribute to the <html ..> tag:

<html class="js">
Enter fullscreen mode Exit fullscreen mode

Sapper Layout File ๐Ÿง

In _layout.svelte file inside of routes folder, We want to be sure that codyframe script run after the DOM is loaded ๐Ÿ‘ง, So for that we have to use onMount from svelte just like this:

<script>
  import { onMount } from "svelte";

  let codyFrameScripts = "";
  onMount(async () => {
    // ---- To mount the CodyFrame scripts ----
    codyFrameScripts = "codyframe/util.js";
  });
</script>
Enter fullscreen mode Exit fullscreen mode

Then we can now add the javascript libraries to the head tag like that:

<!--  cody framework - js libraries -->
<svelte:head>
    <script defer src={codyFrameScripts}></script>
</svelte:head>
Enter fullscreen mode Exit fullscreen mode

And of course we have also to import the scss style ๐ŸŒป!

<!-- Codyframework Global Scss -->
<style lang="scss" global>
@import "./codyframe/assets/css/style.scss"
</style>
Enter fullscreen mode Exit fullscreen mode

Last Step! ๐Ÿค—

Don't forget to add util.js inside of codyframe folder in the static folder of your project ๐Ÿ‘ˆ.

Testing! ๐Ÿ‘

In your index.svelte route, Add any code to test codyframe components, Like this Button:

 <div><button class="btn btn--primary btn--md">Zaki Button!</button></div>
Enter fullscreen mode Exit fullscreen mode

One more thing! ๐Ÿ‘Ž

The codyhouse doesn't have components for modern JavaScript frameworks like Svelte/Vue/React .., And also his bad JavaScript functionality run one time when the page is loaded ๐Ÿคฆโ€โ™‚

So you'll get a lot of troubles when you work with a SPA project (Single Page Application), But there is a solution out of the box from sapper ๐Ÿ’•

Each time you have to switch to another route, You have to be sure that the link (for example) <a href="/profile"> has a rel="external" attribute:

<a rel="external" href="/profile">
Enter fullscreen mode Exit fullscreen mode

Enjoy! You can buy me a coffee ๐Ÿฟ ๐Ÿ’•

Top comments (2)

Collapse
 
arjarn profile image
arjarn โ€ข

Great article.
I'm working with Svelte / Routify, can I apply with it ?

Collapse
 
zakariachahboun profile image
zakaria chahboun โ€ข โ€ข Edited

Yes you can do this with Svelte too, But in "rollup.config.js" of Svelte, there is no server or client section like Sapper,

You have to install just two dependencies: npm install -D svelte-preprocess rollup-plugin-postcss

You have to import these lines:

// sass to css
import postcss from 'rollup-plugin-postcss'
//sass
import autoPreprocess from 'svelte-preprocess';

And in svelte({...}) add these lines:

preprocess: autoPreprocess(),
emitCss: true

Also add these lines after svelte({..}):

postcss({
    extract: true,
    minimize: true,
    use: [
        ['sass', {
            includePaths: [
                    './src'
                        ]
            }]
            ]
})

And that's all!

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

๐Ÿ‘‹ Kindness is contagious

Please leave a โค๏ธ or a friendly comment on this post if you found it helpful!

Okay