Magento 2如何与CSS grunt-autoprefixer一起使用


9

通过查看全新安装的Magento 2(v2.1.8),我从他们的Gruntfile.jspackage.json文件中看到Magento正在使用grunt-autoprefixer

  1. 太好了,CSS自动前缀确实很有用。但是我看不到如何在Magento的Gruntfile中使用它,有人知道它是如何工作的吗?
  2. 此外,这在实际环境中的生产模式下将如何工作?Magento2不在生产模式下使用PHP LESS编译器,而Grunt仅用于进行开发。

Gruntfile.js

/**
 * Production preparation task.
 */
prod: function (component) {
    var tasks = [
        'less',
        'autoprefixer',
        'cssmin',
        'usebanner'
    ]

package.json

"devDependencies": {
    "glob": "^5.0.14",
    "grunt": "^0.4.5",
    "grunt-autoprefixer": "^2.0.0",

我跑了grunt autoprefixer,它似乎也不起作用。

$ grunt autoprefixer
Running "autoprefixer:setup" (autoprefixer) task
Autoprefixer's process() method is deprecated and will removed in next major release. Use postcss([autoprefixer]).process() instead
File setup/pub/styles/setup.css created.

Running "autoprefixer:updater" (autoprefixer) task
Warning: No source files were found. Use --force to continue.

Aborted due to warnings.


Execution Time (2017-10-29 11:12:01 UTC-0)
loading tasks               145ms  ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 30%
loading grunt-autoprefixer  118ms  ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 24%
autoprefixer:setup          216ms  ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 45%
autoprefixer:updater          5ms  ▇▇ 1%
Total 485ms

Answers:


5
  1. 定制dev/tools/grunt/configs/autoprefixer.json并运行grunt autoprefixer
  2. 您需要在部署管道中手动设置它,因为b / c无法从Magento运行Grunt任务。

正在grunt autoprefixer中止运行,并没有完成消息Autoprefixer's process() method is deprecated and will removed in next major release. Use postcss([autoprefixer]).process() instead。哪个表明Magento根本不支持自动前缀?
冬青

即使将Grunt设置为在部署管道中运行,如果管理员用户清除了CSS缓存,它也不是可靠的。
冬青

另外,如何在.json文件中设置grunt任务?不需要在.js文件中设置Grunt
Holly

1.它有效,这只是弃用警告。下面的几行中有关于已处理文件的信息。
igloczek

2.缓存不会删除pub主题目录中的文件,因此100%安全。
igloczek

5

请执行以上更改,希望它能正常运行。

package.json

{
  "name": "Project",
  "author": "Vendor",
  "description": "Node modules dependencies for local development",
  "version": "2.0.0",
  "license": "(OSL-3.0 OR AFL-3.0)",
  "repository": {
    "type": "git",
    "url": "https://github.com/magento/magento2.git"
  },
  "homepage": "http://magento.com/",
  "devDependencies": {
    "autoprefixer": "^7.1.1",
    "glob": "^5.0.14",
    "grunt": "^0.4.5",
    "grunt-banner": "^0.4.0",
    "grunt-contrib-clean": "^0.6.0",
    "grunt-contrib-connect": "^0.9.0",
    "grunt-contrib-cssmin": "^0.10.0",
    "grunt-contrib-imagemin": "^0.9.2",
    "grunt-contrib-jasmine": "^0.8.1",
    "grunt-contrib-less": "^0.12.0",
    "grunt-contrib-watch": "^0.6.1",
    "grunt-eslint": "17.3.1",
    "grunt-exec": "^0.4.6",
    "grunt-jscs": "2.2.0",
    "grunt-postcss": "^0.8.0",
    "grunt-replace": "^0.9.2",
    "grunt-styledocco": "^0.1.4",
    "grunt-template-jasmine-requirejs": "^0.2.3",
    "grunt-text-replace": "^0.4.0",
    "imagemin-svgo": "^4.0.1",
    "load-grunt-config": "^0.16.0",
    "morgan": "^1.5.0",
    "node-minify": "^1.0.1",
    "path": "^0.11.14",
    "serve-static": "^1.7.1",
    "strip-json-comments": "^1.0.2",
    "time-grunt": "^1.0.0",
    "underscore": "^1.7.0"
  },
  "engines": {
    "node": ">=0.10.0"
  }
}

postcss.js

/**
 * PostCSS autoprefixer initialisation
 *
 * Docs: https://github.com/postcss/autoprefixer
 * Config: dev/tools/grunt/configs/postcss.json
 * Usage: grunt autoprefixer:themename [--verbose] [--debug]
 * To do: load src from themes.js
 *
 * @param grunt
 */

module.exports = function(grunt) {
    grunt.loadNpmTasks('grunt-postcss');

    grunt.registerTask('autoprefixer', function (target) {
        var currentTarget = target || 'dist';

        /*** configuration tweaks ***/
        var config = grunt.config.get('postcss');

        // set 'processors' options (not possible to set function in json config)
        config['options'].processors = [require('autoprefixer')({browsers: ['last 2 versions']})];

        // apply theme source and destination dynamically
        if (target) {
            config[target] = {
                src: ['pub/static/frontend/*/'+target+'/*/css/*.css']
            };
        }
        grunt.config.set('postcss', config);

        grunt.option('force', true);
        grunt.task.run('postcss:'+currentTarget);
    });
};

postcss.json

{
    "options": {
        "map": true
    },
    "dist": {
      "src": ["pub/static/frontend/*/*/*/css/*.css"]
    }
}

设置完上述文件后,运行 grunt autoprefixer


您应该将此作为PR提交给核心,以替换当前过时的设置
igloczek

0

要对所有人真正清楚:postcss.js和postcss.json-进入/ dev / tools / grunt / configs package.json-在网站根目录中-只需检查是否在devDependencies下包含autoprefixer

完成后,以通常的方式运行exec / less / watch / setup。

然后在浏览器中访问您的网站以创建CSS缓存文件。

然后,运行:grunt autoprefixer

这将检查生成的CSS文件并添加-webkit-和其他浏览器支持

这对于开发来说是一个很好的解决方法,但是我还没有找到在生产中使用它的方法。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.