ㅤ
0. Overview
- Advanced Webflow Development with Visual Studio Code by Veer Manhas
- Using VS Code and GitHub with Webflow by Ben Parker
ㅤ
🤔 The why:
If you’ve done any kind of custom coding using Webflow you’ve likely encountered some or all of the following pain points:
- Poor syntax highlighting
- No autocomplete
- Endless clicking through menus to edit/publish your code
- Waiting for the site to re-publish every time you make any change to the code
- Character limits
- Having to upload code to a CDN
- Can’t use NPM to easily install libraries
- Can’t easily segment code into individual files, leading to a bloated, endless file or lots of script-src to keep track of
- No integration with GitHub or robust version control/collaboration tool
This article will show you how to tackle all of these, allowing you to efficiently inject advanced functionalities and effects into your Webflow site. Let’s get into the How!
ㅤ
1. Prerequisites
- Visual Studio Code
- Webflow Core/Growth Seat (for custom code)
- GitHub Account
- Make sure you’ve gone through the GitHub Quickstart
- Node.js and NPM
ㅤ
2. Setting up LocalHost + Webflow using Parcel.js
- Create a new GitHub Repository and make it public
- Clone your Repository to your desktop
- Open your Repository in VS Code and create a new app.js file
- Open the integrated terminal (Ctrl+Shift) and make sure you have downloaded & installed node & npm
- Initialize NPM, by running npm init in the terminal
- Install Parcel.js, by running npm i parcel in the terminal
- Before you commit to your Git Repo, make sure to create a .gitignore with the following so that we don’t track our cache or node_modules
# dependencies /node_modules # misc .parcel-cache/** .parcel-cache/data.mdb
ㅤ
- A package.json was created in step 5 let’s add a run command to it so we can simply run npm start in the terminal to package our app and serve it on localhost.
"scripts": { "start": "parcel app.js --hmr-port 50619" }
ㅤ
{ "name": "repo-name", "version": "1.0.0", "description": "repo description", "main": "app.js", "scripts": { "start": "parcel app.js --hmr-port 50619" }, "repository": { "type": "git", "url": "git+https://github.com/psychoactive-studios/repo-name.git" }, "author": "Author Name", "license": "ISC", "bugs": { "url": "https://github.com/psychoactive-studios/repo-name/issues" }, "homepage": "https://github.com/psychoactive-studios/repo-name#readme", "dependencies": { "parcel": "^2.7.0", }, }
ㅤ
- Run npm start we’ll see that our parcel server is running at http://localhost:1234/
- We can now add the following to the Webflow project’s head custom code section and publish our Webflow site:
<script src="http://localhost:1234/app.js"></script>
- Now as long as your parcel.js localhost server is running, any saved change to app.js will be reflected when you reload your Webflow site in your browser - no need to re-publish your project every time.
To check this is working try adding an alert to app.js:alert('Hello World')
ㅤ
- You can now use NPM to install libraries, split your code into multiple files and import files into your code:
import {Curtains, Plane, RenderTarget, PingPongPlane, ShaderPass} from 'curtainsjs'; import {TextTexture} from './TextTexture'; import sliderShader from './sliderShader'; import textShader from './textShader'; import ThreeD from './3d'; import svg from './svg.glb' import glb from './icon.glb'
- You may want to import files which parcel doesn’t know how to parse/transform:
@parcel/core: No transformers found for icon.glb.
ㅤ
If so you’ll have to add a .parcelrc file with something like the following:
{ "extends": "@parcel/config-default", "transformers": { "*.glb": ["@parcel/transformer-raw"] } }
ㅤ
Importing assets this way is likely fine while developing or for small files like simple .svg and .glb with no textures - for production, you likely want a proper CDN to handle 100s or 1000s of kbㅤ
3. Hosting our Code using GitHub Pages
This setup relies on a public repository, make sure you are not handling any sensitive data in your code.
ㅤ
- Got to your github.com and navigate to your repository settings > general
ㅤ
- Make sure the repository visibility is set to public if it isn’t already - look at the bottom of the settings page settings > general > Danger Zone
ㅤ
- In settings > pages create a site from your main branch /root
ㅤ
- Once your site is live, your parceljs-bundled app.js (and every other tracked file) will be hosted under your page domain, something like:
https://psychoactive-studios.github.io/repo-name/dist/app.jsㅤ
- At the top of your app.js define the following variable - we will use this to check if our parceled app is loaded
const parceled = true
ㅤ
- In our Webflow project, we can now add some logic so that if localhost:1234/app.js isn’t available the site will pull the address with the bundled app.js from GitHub Pages:
<script src="http://localhost:1234/app.js"></script> <!-- tries to load from local host --> <script> if(typeof parceled === 'undefined') { let script = document.createElement('script') script.type = 'text/javascript' script.src = 'https://psychoactive-studios.github.io/repo-name/dist/app.js'; document.head.appendChild(script) } //checks if the parceled variable from app.js is undefined - meaning local host isn't up and running //if not loads from the github pages </script>
ㅤ
This allows us to work locally from localhost and make changes without temporarily breaking the site for users, or having to make any edits to the Webflow project’s Head Code.
ㅤ
- Every time we commit our code to main (or whatever branch we set our GitHub page to publish from in 3.) our app.js will update and so will our Webflow site.Ensure you’ve run npm start before committing so that dist/app.js is up to date
ㅤ