简体   繁体   English

在Rails上使用Yarn安装Modernizr

[英]Installing Modernizr with Yarn on Rails

I am trying to install Modernizr with Yarn in Rails. 我正在尝试使用Rails中的Yarn安装Modernizr。 I do 'yarn add Modernizr' and it gets added to the .node-modules directory. 我做'yarn add Modernizr'并将其添加到.node-modules目录中。 However, I can't figure out how to reference it properly from Rails. 但是,我无法弄清楚如何从Rails中正确引用它。

I have added //= require modernizr/src/Modernizr to application.js. 我已经将//= require modernizr/src/Modernizr到application.js。 But I'm not sure that's the right reference because I get the following error: 但我不确定这是正确的参考,因为我收到以下错误:

Uncaught ReferenceError: define is not defined

Do I need to somehow build the Modernizr library. 我是否需要以某种方式构建Modernizr库。 Does yarn not do that? 纱线不这样做吗? I'm somewhat new to this and struggling to understand the relationship between webpacker, yarn, and rails and how to properly build and add libraries to rails with yarn. 我对此有些陌生,并且正在努力理解webpacker,yarn和rails之间的关系,以及如何正确地构建和添加库到带有纱线的轨道。 The tutorials all make it seem as though it's as simple as 'yarn add xxx' but I seem to be missing something. 这些教程都让它看起来像'yarn add xxx'一样简单,但我似乎错过了一些东西。 Thank you. 谢谢。

What you're looking for is the webpacker gem , which ships by default with Rails 5.1+ but can be used with Rails 4.2+. 您正在寻找的是webpacker gem ,它默认随Rails 5.1+发布,但可以与Rails 4.2+一起使用。 Get that set up, then follow these steps to get a custom Modernizr build running in your Rails app: 获取该设置,然后按照以下步骤在Rails应用程序中运行自定义Modernizr构建:

Loader 装载机

Install the loader for modernizr: 为modernizr安装加载器:

$ yarn install webpack-modernizr-loader

Modernizr Configuration Modernizr配置

Download your desired modernizr config from the modernizr site. 从modernizr站点下载所需的modernizr配置。 Visit a link like this: 访问这样的链接:

https://modernizr.com/download?-appearance-backgroundblendmode-backgroundcliptext-backgroundsize-bgpositionxy-borderradius-boxshadow-boxsizing-canvas-canvastext-cssgradients-csspointerevents-fontface-generatedcontent-inputtypes-lastchild-mediaqueries-multiplebgs-opacity-svg-touchevents-setclasses-dontmin-cssclassprefix:mod_ https://modernizr.com/download?-appearance-backgroundblendmode-backgroundcliptext-backgroundsize-bgpositionxy-borderradius-boxshadow-boxsizing-canvas-canvastext-cssgradients-csspointerevents-fontface-generatedcontent-inputtypes-lastchild-mediaqueries-multiplebgs-opacity-svg -touchevents-setclasses-dontmin-cssclassprefix:mod_个

Then configure your build, click "Build" in the upper right-hand corner, and download "Command Line Config". 然后配置您的构建,单击右上角的“构建”,然后下载“命令行配置”。

Then convert it from JSON to a module and save it as config/webpack/.modernizrrc.js (note the leading period in the filename), like so: 然后将它从JSON转换为模块并将其保存为config/webpack/.modernizrrc.js (注意文件名中的前导句点),如下所示:

"use strict";

module.exports = {
  minify: false,
  options: [
    "setClasses"
  ],
  "feature-detects": [
    "test/canvas",
    "test/canvastext",
    "test/inputtypes",
    "test/svg",
    "test/touchevents",
    "test/css/appearance",
    "test/css/backgroundblendmode",
    "test/css/backgroundcliptext",
    "test/css/backgroundposition-xy",
    "test/css/backgroundsize",
    "test/css/borderradius",
    "test/css/boxshadow",
    "test/css/boxsizing",
    "test/css/fontface",
    "test/css/generatedcontent",
    "test/css/gradients",
    "test/css/lastchild",
    "test/css/mediaqueries",
    "test/css/multiplebgs",
    "test/css/opacity",
    "test/css/pointerevents"
  ]
};

Custom Configuration 自定义配置

Next, create a custom webpack config file as config/webpack/custom.js : 接下来,创建一个自定义webpack配置文件作为config/webpack/custom.js

const path = require("path");

module.exports = {
  module: {
    rules: [
      {
        loader: "webpack-modernizr-loader",
        test: /\.modernizrrc\.js$/
      }
    ]
  },
  resolve: {
    alias: {
      modernizr$: path.resolve(__dirname, "./.modernizrrc.js")
    }
  }
};

Expose Configuration 公开配置

Make your config/webpack/environment.js look like this: 使您的config/webpack/environment.js看起来像这样:

const { environment } = require("@rails/webpacker");
const customConfig    = require('./custom');

environment.config.merge(customConfig);

module.exports = environment;

Import Modernizr 导入Modernizr

Add the following line to app/javascript/packs/application.js : 将以下行添加到app/javascript/packs/application.js

import modernizr from 'modernizr';

Load Your Pack 加载你的包

Add this to your layout: 将其添加到您的布局:

<%= javascript_pack_tag "application", defer: true %>

Voila

Load up your site in a browser, inspect, and confirm that (a) modernizr CSS classes have been added to the DOM, and (b) you aren't seeing any webpack compilation errors in the console. 在浏览器中加载您的站点,检查并确认(a)将现代化的CSS类添加到DOM中,以及(b)您没有在控制台中看到任何webpack编译错误。

Further Reading 进一步阅读

I came upon this question when I was looking to set up modernizr using webpacker in a Rails 5 app, saw the question was unanswered, and figured it out myself. 当我想在Rails 5应用程序中使用webpacker设置modernizr时,我遇到了这个问题,看到问题没有得到答复,并且自己想出来了。 If you want to know how all of this works, I suggest reading the docs for webpacker and webpack-modernizr-loader . 如果您想知道所有这些是如何工作的,我建议您阅读webpackerwebpack-modernizr-loader的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM