Linux搭建(nodejs + grunt + compass) 开发环境

2024-04-01 17:32

本文主要是介绍Linux搭建(nodejs + grunt + compass) 开发环境,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  由于工作原因,需要写一些前端页面。有尝试用notepad++编辑,效率着实不高,感觉也挺low,于是网上查阅相关资料,结合一些自己的理解,动手搭建(nodejs + grunt + compass) 开发环境,从而简化提高开发效率。

工具安装

1.安装Nodejs

  Grunt需要Nodejs的支持,所以我们先安装Nodejs。直接进入Nodejs官网 中根据自己的系统选择下载。我这里是Centos,所以下载的是Linux 二进制包,解压后可直接使用。

# wget https://nodejs.org/dist/v10.9.0/node-v10.9.0-linux-x64.tar.xz    // 下载
# tar -xvf  node-v10.9.0-linux-x64.tar.xz -C    /usr/local     // 解压
# cd  /usr/local/node-v10.9.0-linux-x64/                  // 进入解压目录
# ./bin/node -v                               // 执行node命令 查看版本
v10.9.0

添加用户环境变量,cd到用户家目录:

# vim .bash_profile 
# export PATH=$PATH:/usr/local/node-v10.9.0-linux-x64//bin
# source .bash_profile 

配置国内npm镜像

# npm config set registry http://registry.npm.taobao.org/ 

2.安装grunt

通过npm安装grunt命令:

# npm install grunt-cli -g

3.安装Sass和Compass

Compass是Sass的样式框架,SASS是Ruby语言写的,不懂Ruby没关系,照样使用。只是必须先安装Ruby,然后再安装SASS。下载最新版的 Ruby 压缩文件。请点击这里下载。

# tar -xvf ruby-2.7.2.tar.gz   
# cd ruby-2.7.2
# ./configure
# make &  make install

gem 添加国内源

# gem sources -l
# gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/

gem安装Sass

# gem install sass
# sass -v

gem安装Compass

# gem install compass
# compass -v

gem安装bootstrap-sass

# gem install bootstrap-sass

创建项目

通过上面的步骤,项目环境基本准备好了,现在可以开始我们的项目。

1.创建项目目录,进到目录

# mkdir grunt-bootstrap
# cd grunt-bootstrap

2.目录中创建compass项目

# compass creat assert -r bootstrap-sass --using bootstrap
# mkdir config
# mv assert/config.rb config

3.修改config.rb中的资源路径

# vim config/config.rb
require 'bootstrap-sass'
require 'compass/import-once/activate'
# Require any additional compass plugins here.# Set this to the root of your project when deployed:
http_path = "./assert/"
css_dir = "./assert/stylesheets"
sass_dir = "./assert/sass"
images_dir = "./assert/images"
javascripts_dir = "./assert/javascripts"
fonts_dir = "./assert/fonts"# You can select your preferred output style here (can be overridden via the command line):
# output_style = :expanded or :nested or :compact or :compressed# To enable relative paths to assets via compass helper functions. Uncomment:
# relative_assets = true# To disable debugging comments that display the original location of your selectors. Uncomment:
# line_comments = false# If you prefer the indented syntax, you might want to regenerate this
# project again passing --syntax sass, or you can uncomment this:
# preferred_syntax = :sass
# and then run:
# sass-convert -R --from scss --to sass sass scss && rm -rf sass && mv scss sass

4.创建package.json和Gruntfile.js,文件可以通过npm init 创建,也可以通过grunt-init 通过模板创建。具体参考grunt官网

package.json

{"engines": {"node": ">= 0.10.0"},"devDependencies": {"grunt": "^1.3.0","grunt-contrib-compass": "^1.1.1","grunt-contrib-connect": "^3.0.0","grunt-contrib-watch": "^1.1.0"}
}

Gruntfile.js

/*global module:false*/
module.exports = function(grunt) {// Project configuration.grunt.initConfig({// Metadata.meta: {basePath: './',srcPath: './assert/sass/',deployPath: './assert/stylesheets/'},pkg: grunt.file.readJSON('package.json'),banner: '/*! <%= pkg.title || pkg.name %> - v<%= pkg.version %> - ' +'<%= grunt.template.today("yyyy-mm-dd") %>\n' +'<%= pkg.homepage ? "* " + pkg.homepage + "\\n" : "" %>' +'* Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author.name %>;' +' Licensed <%= _.pluck(pkg.licenses, "type").join(", ") %> */\n',// Task configuration.connect:{//这里为插件子刷新方式options: {port: 9000,hostname: 'localhost', //默认就是这个值,可配置为本机某个 IP,localhost 或域名livereload: 35729  //声明给 watch 监听的端口},server: {options: {open: true, //自动打开网页 http://base: ['.'  //主目录]}}},sass: {dist: {files: {'<%= meta.deployPath %>screen.css': '<%= meta.srcPath %>screen.scss'}}},compass: {dist: {options: {config: './config/config.rb', }}},watch: {scripts: {files: ['<%= meta.srcPath %>/**/*.scss'],tasks: ['compass']},livereload: {options: {livereload:'<%=connect.options.livereload%>'  //监听前面声明的端口  35729},files:[  //下面文件的改变就会实时刷新网页'*.html','./assert/stylesheets/styles.css']}}});// These plugins provide necessary tasks.//grunt.loadNpmTasks('grunt-contrib-sass');grunt.loadNpmTasks('grunt-contrib-compass');grunt.loadNpmTasks('grunt-contrib-watch');grunt.loadNpmTasks('grunt-contrib-connect');// Default task.grunt.registerTask('default', ['compass','connect','watch']);};

5.创建index.html

<!DOCTYPE html>
<html>
<head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Bootstrap Demo</title><link href="/assert/stylesheets/styles.css" rel="stylesheet" type="text/css" />
</head>
<body><div class="container"><p>This is a test page.</p></div><script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script><script src="/assert/javascripts/bootstrap.min.js"></script>
</body>
</html>

6.最后vscode中的项目目录结构
在这里插入图片描述
7.项目中运行grunt,至此我们编写sass就可自动编译成css文件,并自动刷新index页面。

这篇关于Linux搭建(nodejs + grunt + compass) 开发环境的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/867734

相关文章

VSCode开发中有哪些好用的插件和快捷键

《VSCode开发中有哪些好用的插件和快捷键》作为全球最受欢迎的编程工具,VSCode的快捷键体系是提升开发效率的核心密码,:本文主要介绍VSCode开发中有哪些好用的插件和快捷键的相关资料,文中... 目录前言1、vscode插件1.1 Live-server1.2 Auto Rename Tag1.3

Agent开发核心技术解析以及现代Agent架构设计

《Agent开发核心技术解析以及现代Agent架构设计》在人工智能领域,Agent并非一个全新的概念,但在大模型时代,它被赋予了全新的生命力,简单来说,Agent是一个能够自主感知环境、理解任务、制定... 目录一、回归本源:到底什么是Agent?二、核心链路拆解:Agent的"大脑"与"四肢"1. 规划模

Linux内核定时器使用及说明

《Linux内核定时器使用及说明》文章详细介绍了Linux内核定时器的特性、核心数据结构、时间相关转换函数以及操作API,通过示例展示了如何编写和使用定时器,包括按键消抖的应用... 目录1.linux内核定时器特征2.Linux内核定时器核心数据结构3.Linux内核时间相关转换函数4.Linux内核定时

Linux镜像文件制作方式

《Linux镜像文件制作方式》本文介绍了Linux镜像文件制作的过程,包括确定磁盘空间布局、制作空白镜像文件、分区与格式化、复制引导分区和其他分区... 目录1.确定磁盘空间布局2.制作空白镜像文件3.分区与格式化1) 分区2) 格式化4.复制引导分区5.复制其它分区1) 挂载2) 复制bootfs分区3)

python项目环境切换的几种实现方式

《python项目环境切换的几种实现方式》本文主要介绍了python项目环境切换的几种实现方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 如何在不同python项目中,安装不同的依赖2. 如何切换到不同项目的工作空间3.创建项目

Python+wxPython开发一个文件属性比对工具

《Python+wxPython开发一个文件属性比对工具》在日常的文件管理工作中,我们经常会遇到同一个文件存在多个版本,或者需要验证备份文件与源文件是否一致,下面我们就来看看如何使用wxPython模... 目录引言项目背景与需求应用场景核心需求运行结果技术选型程序设计界面布局核心功能模块关键代码解析文件大

C++多线程开发环境配置方法

《C++多线程开发环境配置方法》文章详细介绍了如何在Windows上安装MinGW-w64和VSCode,并配置环境变量和编译任务,使用VSCode创建一个C++多线程测试项目,并通过配置tasks.... 目录下载安装 MinGW-w64下载安装VS code创建测试项目配置编译任务创建 tasks.js

Linux服务器数据盘移除并重新挂载的全过程

《Linux服务器数据盘移除并重新挂载的全过程》:本文主要介绍在Linux服务器上移除并重新挂载数据盘的整个过程,分为三大步:卸载文件系统、分离磁盘和重新挂载,每一步都有详细的步骤和注意事项,确保... 目录引言第一步:卸载文件系统第二步:分离磁盘第三步:重新挂载引言在 linux 服务器上移除并重新挂p

Linux下屏幕亮度的调节方式

《Linux下屏幕亮度的调节方式》文章介绍了Linux下屏幕亮度调节的几种方法,包括图形界面、手动调节(使用ACPI内核模块)和外接显示屏调节,以及自动调节软件(CaliseRedshift和Reds... 目录1 概述2 手动调节http://www.chinasem.cn2.1 手动屏幕调节2.2 外接显

Linux(centos7)虚拟机没有IP问题及解决方案

《Linux(centos7)虚拟机没有IP问题及解决方案》文章介绍了在CentOS7中配置虚拟机网络并使用Xshell连接虚拟机的步骤,首先,检查并配置网卡ens33的ONBOOT属性为yes,然后... 目录输入查看ZFhrxIP命令:ip addr查看,没有虚拟机IP修改ens33配置文件重启网络Xh