在Go中进行SASS渲染

huangapple go评论100阅读模式
英文:

SASS Rendering in Go

问题

我开始使用Go进行Web开发,但在资源管理方面遇到了问题。我希望有一个像Rails的Asset Pipeline一样的工具来管理(和压缩)css/js文件(以及SASS),但我仍然可以使用css和js文件。

虽然我可以使用css和js,但无法使用SASS。在Go项目中有没有一种使用SASS的方法?我没有使用框架。

谢谢!

英文:

I am beginning to use Go for web development, but I am having issues with asset management. I would prefer to have a tool like Rails' Asset Pipeline for managing (and compressing) css/js files (as well as SASS), but I am still able to work with css and js files.

While I am able to work with css and js, I am not able to work with SASS. Is there a way to use SASS in a Golang project? I am not using a framework.

Thank you!

答案1

得分: 1

我对Ruby on Rails不太熟悉,但我猜想Ruby on Rails提供了一些工具来管理源代码到分发客户端的资源转换(如polyfills、转译、压缩、将SASS/SCSS编译为CSS、将XScript编译为JavaScript等)。

虽然Web开发框架可能会尝试通过这种方式快速帮助开发人员(我猜Rails是这样做的,而不是Ruby),但这并不是Go的工作方式。

Go是一种语言,不是一个框架+语言,只是一个编译器、一些构建工具和一套编写、测试、文档和缩进规范(缩进、测试和文档部分是可选的)。

至少我用Go构建服务器的方式与客户端有些解耦。当需要时,它会提供静态资源(例如,提供压缩的JavaScript、样式表和HTML,以及来自数据库的JSON等),但它并不关心这些资源是什么,它只是一个服务器。Go工具链用于构建Go应用程序(例如上述的服务器),但它不适用于构建客户端Web应用程序(由JavaScript、CSS和HTML组成)。

现在,你可以使用类似于Rails的Go框架来帮助“打包”CSS、JS和HTML。但我不知道是否有这样的框架。

你可以使用一个将Go转换为客户端代码(即JavaScript)的编译器,比如https://github.com/gopherjs/gopherjs,如果你喜欢Go工具链并希望将其用于客户端开发。但是,这并不能提供类似Go的性能,并且你只能使用Go的一个子集。它实际上只是一种不同的编写JavaScript的方式。

然而,在你的情况下,你最可能需要的是一个用于客户端的“构建链”。在2016年,有三个工具(在我看来)非常出色:

npm
webpack
bower

我可以写一篇关于使用这些工具的文章,但以下是摘要:

Webpack用于创建代码的“流水线”,可以执行诸如在JavaScript上调用Babel、将Sass编译为CSS、压缩资源、允许使用import语法编写JavaScript等操作。实际上,它是你JavaScript开发工具中的瑞士军刀,可能与你之前使用的工具的功能相匹配。

Npm是Node的包管理器,即使你不使用Node作为服务器,它也可以用于跟踪构建应用程序的依赖关系(如Webpack)和下载模块。它还可以用于运行各种脚本和部署,但同时使用npm和webpack可能有点过度,如果你有一个package.json(npm的配置文件)与你的项目一起,那么设置webpack环境可能会更容易一些。

Bower是我在小项目中实际上不使用的工具。但它基本上是一个JavaScript库的存储库(还有其他功能),所以你可以轻松地运行"bower install jquery",就可以为当前项目下载jQuery。

当然,还有许多其他工具,这只是我喜欢的一些工具,但你可以尝试一些其他工具。它们可以帮助你替代之前的工作流程。不要认为客户端和服务器端的代码是相同的,它们是解耦的,强烈的分离可能会对你有很大帮助。

英文:

I'm not familiar with Ruby on Rails but, I assume, that ruby on rails gave you some sort of tools for managing the source to distribution client-side asset transition (polyfills, transpiling, minification, compiling of SASS/SCSS to CSS, compiling of XScript to JavaScript ... etc).

While a web development framework might do that to try and ease in developers quickly (I assume rails does that, not ruby) its not exactly the way Go does stuff.

Go is a language, not a framework + language, just a compiler, a few build tools and a set of standards for how to write, test, document and indent stuff (with the indent,test and document part being optional).

A go server, at least the way I built servers with go, is somewhat decoupled from the client. It server static assets when they are needed (e.g. it serves the minified JavaScript and the stylsheets and the html, and jsons with info from the databases... etc), but it doesn't really care about what those are, its a server. The go toolchain is made for building golang applications (e.g. said server), but its not made for building client-side web applications (those consisting of js, css and html).

Now, you may use a framework similar to rails written in go that helps "pack up" css, js, html. But I'm unaware if there are any.

You may use a compiler which turns go into client-side code (i.e. javascript) https://github.com/gopherjs/gopherjs , if you enjoy the go toolchian and want to use it for client-side development. But, go-like performance isn't something this gives you AND you are working with a subset of go. Its really just a different way to write javascript.

However, what you most likely need in your case is a "build-chain" for your client side. Here there are 3 tools which (in my opinion) stand out in 2016:

npm
webpack
bower

I could write an essay about using this tools but here's the summary:

Webpack is used to create a "pipeline" for your code which does thing like, calling babel on javascript, compiling sass to css, minifying assets, allowing js to be written with import syntax... etc, really, its a swis army knife in your js development arsenal and probably matches the functionality of whatever you were using before.

Npm is the node package manager BUT even if you are not using node for your server. It can be useful to keep tracks of dependencies for building your application (like webpack) and for downloading modules. Its also useful for running various scripts and deployment, its a bit of an overkill to use both npm and weback though you will probably have an easier time setting up the webpack enviornment if you have a package.json (config file for npm) with each of your project.

Bower is one I actually don't use for small projects. But its basically a repository for javascript libraries (among other things), so you can easily say, write "bower install jquery" and you've downloaded jQuery for your current project.

Again, there are many other tools out there, these are just some of the ones I like, but, check some of them out. They can help you replaces your previous pipeline. Don't think of client and server side code as being the same, they are decoupled and having a strong separation between them might help you a lot.

huangapple
  • 本文由 发表于 2016年11月19日 04:27:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/40685427.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定