npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

kisphp-assets

v0.8.1

Published

Compile assets dependencies with gulp in a simple way

Downloads

218

Readme

Kisphp assets management with GULP

This tool will allow you to compile javascripts, css and copy assets file to your public directory in a very easy way.

Requirements

You need to have NodeJS 16 installed and GulpJS

npm install --global gulp

Install

Follow the next steps to create the necessary file for the tool integration

If you want to have a quick demo or a quick setup and you have an empty directory, run the following command to install and add the files automatically:

curl -s https://gitlab.com/kisphp/assets/raw/main/install.sh | bash -

package.json file

Create the package.json script in the root directory of the project with the following content:

{
  "scripts": {
    "build": "gulp --gulpfile app/gulp/gulpfile.js --cwd ."
  }
}

For the moment, the package.json file will only have the gulp execution script defined. We also need some dependencies defined in this file, but we'll add them with the following command.

Run the following command to install the dependencies and save them into package.json file:

npm install --save kisphp-assets jquery bootstrap

After the command finishes, you'll see that inside the package.json file will appear a list of dependencies that we'll need for our project.

Create gulp assets directory:

mkdir -p app/gulp/assets/{css,images,js}
mkdir -p app/gulp/assets/js/modules
touch app/gulp/assets/css/main.css

Copy gulpfile.js in app/gulp/ directory

cp node_modules/kisphp-assets/gulpfile.js app/gulp/gulpfile.js

Copy gulp-config.dist.js in your application root directory and remove dist from its name

cp node_modules/kisphp-assets/gulp-config.dist.js ./gulp-config.js

Create the app/gulp/assets/js/modules/demo.js file with the content:

cat << EOF > app/gulp/assets/js/modules/demo.js
module.exports = {
    init: function() {
        console.log('file loaded');
    }
}
EOF

In the app/gulp/assets/js/modules/ directory you'll store all javascript modules for your website. A best practice is to minimize the dependencies between those files as much as possible. Every file will export an object with the init() function that will be called in the following file.

Create the app/gulp/assets/js/app.js with the following content:

cat << EOF > app/gulp/assets/js/app.js
$(document).ready(function(){
    require('./modules/demo').init();
    // add here more files that do one thing (Single Responsibility Principle)
});
EOF

This is file where you load all your javascript modules.

Run gulp script to compile all assets file

npm run build

When the command will finish you'll see new directories and files inside the public directory.

public
|-- css
|   |-- app.css
|   `-- external.css
`-- js
    |-- app.js
    `-- external.js

Load generated assets files into your html code

To load the generated files into your html template, you need to add the css files inside the <head>...</head> tags:

<link rel="stylesheet" href="/css/external.css" />
<link rel="stylesheet" href="/css/app.css" />

And then add the javascript files right before the </body> closing tag:

<script src="/js/external.js"></script>
<script src="/js/app.js"></script>

Examples of configuration

Here is a fully functional example of how to use this library.

Create the following files in a new directory

app/gulp/assets/css/main.css

.alfa {
  background: #369;
  color: #fff;
}
.beta {
  background: #6f42c1;
  color: #fff;
}
.gama {
  background #a52834;
  color: #fff;
}

Load assets file in your public/index.html or public/index.php file

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Example</title>
    <link rel="stylesheet" href="/css/external.css" />
    <link rel="stylesheet" href="/css/app.css" />
</head>
<body>

<div class="container">
    <div class="row">
        <div class="col alfa">
            1 of 3
        </div>
        <div class="col-6 beta">
            2 of 3 (wider)
        </div>
        <div class="col gama">
            3 of 3
        </div>
    </div>
    <div class="row">
        <div class="col-sm-3 clickme">Click me</div>
        <div class="col"><span id="counter">0</span></div>
    </div>
</div>

<script src="/js/external.js"></script>
<script src="/js/app.js"></script>
</body>
</html>

Create the counter js file in app/gulp/assets/js/modules/counter.js with the following content:

module.exports = {
    init: function (){
        $('.clickme').on('click', function (ev){
            ev.preventDefault();

            let counter = $('#counter');
            let counterValue = counter.html();

            counter.html(parseInt(counterValue) + 1);
        });
    }
};

Then, you need to register the new javascript module to the application

Open the file app/gulp/assets/js/app.js and add your new module:

$(document).ready(function(){
    require('./modules/demo').init();
    require('./modules/counter').init();
});

Once you have all these files run the following commands to compile the dependencies

npm install

npm run build

If you have php installed, run the following command:

php -S localhost:8000 -t public

And open the url http://localhost:8000/ in your browser

SCSS support

The tool has a script to compile scss files, but the dependencies are not installed. If you use scss, run the following command to install the necessary dependencies:

npm i -S gulp-sass
npm i -S sass