npm-pipeline-rails

Use npm as part of your Rails asset pipeline
授权协议 Readme
开发语言 JavaScript
所属分类 Web应用开发、 常用JavaScript包
软件类型 开源软件
地区 不详
投 递 者 卫深
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

End-of-life

July 2020: Rails 6 now recommends using Webpacker to manage frontend dependencies. Thank you to all the contributors who have made npm-pipeline-rails a successful experiment.


npm-pipeline-rails

Use npm as part of your Rails asset pipeline

npm-pipeline-rails allows you to use any toolchain to build your asset files in Rails 4.2+. This allows you to:


See § How it Works for an explanation of the diagram above.


⚠️ Notice ⚠️

Rails 5.1 will be adding official support for Webpack via webpacker. In contrast, npm-pipeline-rails is far less opinionated and more flexible than webpacker, but expect better support from using Rails's official integration.

Usage

Add this line below to your Gemfile. After that, proceed with an automated or manual setup.

gem 'npm-pipeline-rails'

Automated setup

Use the generators for your preferred build tool:

  • Brunch - ./bin/rails generate npm_pipeline:brunch
  • Gulp - ./bin/rails generate npm_pipeline:gulp
  • Webpack - ./bin/rails generate npm_pipeline:webpack

Manual setup

  • Put together a setup with Brunch, Broccoli, Gulp, Webpack or any other tool. It should:
    • Take source files from app/webpack/
    • Render CSS to vendor/assets/stylesheets/webpack/
    • Render JS to vendor/assets/javascripts/webpack/
    • (Replace webpack with whatever build tool you use.)
  • Create a package.json with start and build scripts to point to this setup. (See example)
    • start - Configure this script to run a development file watcher.
    • build - Configure this script to run a production compiler.
  • Add your expected compiled assets to .gitignore.

Set up support for tests

If you're using continuous integration for your tests, configure it to run this before your tests:

npm run build

For tests running in your development machine, ensure that asset files are available when running your tests. This means starting your dev server at least once before running tests, or invoking npm run build manually.

Disable some gems

You may also want to disable some gems, depending on your set up:

  • Disable uglifyjs if you already do minification in your npm tool.
  • Disable autoprefixer-rails if you already do autoprefixing in your npm tool.
  • Disable sprockets-es6 if you already do ES6 compiling in your npm tool.
  • and so on...

Heroku

When deploying to Heroku, use Node.js and Ruby buildpacks together. See: Using Multiple Buildpacks for an App (devcenter.heroku.com)

$ heroku buildpacks:set heroku/ruby
$ heroku buildpacks:add --index 1 heroku/nodejs

Buildpack added. Next release on my-app-name will use:
  1. heroku/nodejs
  2. heroku/ruby

It's recommended to turn off config.npm.install_on_asset_precompile to make deployments faster; see § Configuration.


Configuration

npm-pipeline-rails provides these configuration options below. Put them inside config/application.rb (not in an initializer!).

# These are defaults; in most cases, you don't need to configure anything.

Rails.application.configure do
  # Enables npm_pipeline_rails's invocation of `watch` commands. (v1.5.0+)
  # If `true`, watch commands will be ran alongside Rails's server.
  # Defaults to true in development.
  config.npm.enable_watch = Rails.env.development?

  # Command to install dependencies
  config.npm.install = ['npm install']

  # Command to build production assets
  config.npm.build = ['npm run build']

  # Command to start a file watcher
  config.npm.watch = ['npm run start']

  # The commands are arrays; you may add more commands as needed:
  config.npm.watch = [
    'npm run webpack:start',
    'npm run brunch:start'
  ]

  # If 'true', runs 'npm install' on 'rake assets:precompile'. (v1.6.0+)
  # If you disable this, you'll need to run `npm install` yourself.
  # This is generally desired, but you may set this to false when
  # deploying to Heroku to speed things up.
  config.npm.install_on_asset_precompile = true

  # If 'true', runs 'npm install' on 'rails server'. (v1.7.0+)
  # If you disable this, you'll need to run `npm install` yourself.
  config.npm.install_on_rails_server = true
end

How it works

npm-pipeline-rails allows you to hook certain commands, usually npm scripts, during the Rails app lifecycle. It assumes that your tool will build plain JS and CSS files into vendor/assets, allowing it to be picked up by Rails's asset pipeline.

It does not replace the Rails asset pipeline, but rather it works with it. The files you build with your npm pipeline will be available as regular files in the Rails asset pipeline.

In development

When starting a Rails development server (bundle exec rails s), it runs the install command. After that, it starts a background process that runs your watch command.

In production

When running rake assets:precompile, it will first run the install command then the build command.

More info

Consult railtie.rb for the actual code that makes all these happen.


Yarn

To use Yarn instead of npm, change config.npm.install as seen below in config/application.rb. See § Configuration for more details.

Rails.application.configure do
  # ...

  config.npm.install = ['yarn']
end

Skipping Rails asset pipeline

The recommended setup renders files to vendor/assets/stylesheets/webpack/ and vendor/assets/javascripts/webpack/. (Replace webpack with whatever build tool you use.) You may opt to output to public/assets/stylesheets/ and public/assets/javascripts/ instead.

This is not recommended since you will miss out on automatic asset fingerprinting, among other nice integrations.

If you do this, you will need to run npm run build as part of your deploy script and CI test script.


Motivation

Rails's asset pipeline was a great step forward for Rails 3. For today's requirements however, it doesn't always come with all the tools you need. npm-pipeline-rails lets you outsource asset building complexities to Node.js-based tools.

Read more →


Also see

Thanks

npm-pipeline-rails © 2016+, Rico Sta. Cruz. Released under the MIT License.
Authored and maintained by Rico Sta. Cruz with help from contributors (list).

ricostacruz.com  · GitHub @rstacruz  · Twitter @rstacruz

  • 前言 如果您曾經閱讀過小弟的vue + webpack 起手式一文,裡面我曾提到關於容易與傳統 MVC 框架(Rails, ASP.NET MVC)等結合。這篇文章主要就是用來介紹其作法與為什麼我會這麼說。 我先承認吧!我不總是需要使用 SPA 的架構 雖然它很好,但對於很多小型專案,或者我們應該說 UI/UX 設計本身非常單純的專案 - 殺雞焉用牛刀。所以這邊文章的作法針對的是那些專案不是使用

  • 使用Capybara进行JS的集成测试 谈论驱动 让测试通过 Webpack in Development Mode Js设计 是用户在网页上有好的体验的重要因素。 尽管如此,许多网页不测试JS。 部分原因是js难以测试。交互和逻辑是依靠用户在网页上的交互动作,这就难了。 Js框架也不支持简单的单元测试。同时工具变化也非常快。b本章使用的工具看起来有点老,如果你读的时候没有被彻底的antiquat

  • http://clarkdave.net/2015/01/how-to-use-webpack-with-rails/ http://m.oschina.net/blog/464093   webpack 是一个很强大的打包工具,主要用于前端开发,可以和 bower,npm 很好的配合工作。 它比 Rails 原本的前端管理模式要好很多,不过还是可以和 Sprockets 以及 asset pip

  • 最近研究了下Koa2框架,喜爱其中间件的思想。但是发现实在是太简洁了,只有基本功能,虽然可以方便搭各种服务,但是离可以适应快速开发的网站框架还是有点距离。于是参考Rails的大致框架搭建了个网站框架kails, 配合postgres和redis, 实现了MVC架构,前端webpack,react前后端同构等网站开发基本框架。本文主要介绍kails搭建中的各种技术栈和思想。 本文首发于Blog of

 相关资料
  • Banzai Pipeline, or simply Pipeline is a tabletop reef break located in Hawaii, Oahu's North Shore. The most famous and infamous reef in the universe is the benchmark by which all other waves are meas

  • 当Item在Spider中被收集之后,它将会被传递到Item Pipeline,一些组件会按照一定的顺序执行对Item的处理。 每个item pipeline组件(有时称之为“Item Pipeline”)是实现了简单方法的Python类。他们接收到Item并通过它执行一些行为,同时也决定此Item是否继续通过pipeline,或是被丢弃而不再进行处理。 以下是item pipeline的一些典型

  • Asset Pipeline 本文介绍 Asset Pipeline。 读完本文,你将学到: Asset Pipeline 是什么以及其作用; 如何合理组织程序的静态资源; Asset Pipeline 的优势; 如何向 Asset Pipeline 中添加预处理器; 如何在 gem 中打包静态资源; 1 Asset Pipeline 是什么? Asset Pipeline 提供了一个框架,用于连

  • HTML-Pipeline 是一个轻量级框架,可用于将用户的内容转换为 HTML 格式。 HTML-Pipeline 提供了一些可链接的、基于 DOM 的 HTML 过滤器,且支持将过滤器组成管道。 用法 过滤器采用 HTML 字符串或 Nokogiri::HTML::DocumentFragment,可以对内容进行操作,然后输出对应的结果。 如,将 Markdown 源代码转换为 Markdow

  • ember-pipeline Railway oriented programming in Ember. To install: ember install ember-pipeline Philosophy ember-pipeline allows you to compose a pipeline of (promise aware) methods on an object using

  • Mojito-pipeline 是 Mojito 扩展,只要数据有效,即可允许应用程序呈现 mojits。它管理 mojit 所有的执行阶段且能够在用户代理上展示内容。 当数据到达时及时显示 mojits,此过程中通过及时显示页面分页大大的提高了前端性能。 Features 减少了第一个字节到最后一个字节的时间 支持客户端禁用 JavaScript 支持 Shaker (automatically