简体   繁体   English

postcss-import grunt插件不起作用

[英]postcss-import grunt plugin does not work

Structure of my project is like this: 我的项目结构如下:

my_project
|-- css
|   -- main.css
|-- css-dev
|   -- main.css
|-- node_modules
|   -- bootstrap
|       -- dist
|           -- css
|               -- bootstrap.css
|-- package.json
`-- Gruntfile.js

And my Gruntfile.js is like this : 我的Gruntfile.js就像这样:

module.exports = function (grunt) {
    var processorArray = [
        require('postcss-import')(),
        require('cssnano')()
    ];
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        postcss: {
            options: {
                processors: processorArray
            },
            dist: {
                files: [{
                    expand: true,
                    cwd: 'css-dev/',
                    src: ['**/*.css'],
                    dest: 'css/'
                }]
            }
        },
        watch: {
            scripts: {
                files: ['css-dev/*.css'],
                tasks: ['postcss'],
                options: {
                    spawn: false
                }
            }
        }
    });

    grunt.loadNpmTasks('grunt-postcss');
    grunt.loadNpmTasks('grunt-contrib-watch');

};

As you see , I want to use postcss-import Grunt plugin to import bootstrap.css file into css-dev/main.css file and minifying result and place final file in css directory as a main.css file name. 如您所见,我想使用postcss-import Grunt插件将bootstrap.css文件导入css-dev/main.css文件并最小化结果,并将最终文件作为main.css文件名放置在css目录中。

Content of main.css file in css-dev directory is : css-dev目录中的main.css文件的内容为:

@import "bootstrap.css";

/* normalize selectors */
h1::before, h1:before {
    /* reduce shorthand even further */
    margin: 10px 20px 10px 20px;
    /* reduce color values */
    color: #ff0000;
    /* drop outdated vendor prefixes */
    -webkit-border-radius: 16px;
    border-radius: 16px;
    /* remove duplicated properties */
    font-weight: normal;
    font-weight: normal;
    /* reduce position values */
    background-position: bottom right;
}

/* correct invalid placement */
@charset "utf-8";

.test{
    font: 12px Calibri;
}

I think that all things are correct, but after running grunt tasks ,seems @import does not work properly and result file is like this : 我认为所有事情都是正确的,但是运行grunt任务后,@ @import似乎无法正常工作,结果文件如下所示:

@import "bootstrap.css";h1:before{margin:10px 20px;color:red;border-radius:16px;font-weight:400;background-position:100% 100%}.test{font:2px Calibri}

Means Unexpectedly, content of bootstrap file not imported to main.css file. 意味着,意外地,引导文件的内容未导入到main.css文件中。

what is problem,how can I solve that? 有什么问题,我该如何解决?

I don't think postcss-import is finding your bootstrap css file, you can specify exactly where to look for it using the path property. 我认为postcss-import找不到您的bootstrap css文件,您可以使用path属性指定要在哪里查找它的确切位置。

var processorArray = [
    require('postcss-import')({
        path: 'node_modules/bootstrap/dist/css'
    }),
    require('cssnano')()
];
grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    postcss: {
        options: {
            processors: processorArray
        },
        dist: {
            files: [{
                expand: true,
                cwd: 'css-dev/',
                src: ['**/*.css'],
                dest: 'css/'
            }]
        }
    },
    watch: {
        scripts: {
            files: ['css-dev/*.css'],
            tasks: ['postcss'],
            options: {
                spawn: false
            }
        }
    }
});

EDIT 编辑

It really depends how you're pulling your css libraries, i think your asking how would you set it up so they are found automatically. 这实际上取决于您如何拉出CSS库,我想您是在问如何设置它,以便自动找到它们。 According to the plugin 根据插件

This plugin can consume local files, node modules or web_modules. 该插件可以使用本地文件,节点模块或web_modules。 To resolve path of an @import rule, it can look into root directory (by default process.cwd()), web_modules, node_modules or local modules. 要解析@import规则的路径,它可以查看根目录(默认情况下为process.cwd()),web_modules,node_modules或本地模块。 When importing a module, it will looks for index.css or file referenced in package.json in the style or main fields. 导入模块时,它将在style或main字段中查找index.css或package.json中引用的文件。 You can also provide manually multiples paths where to look at. 您还可以手动提供要查看的倍数路径。

Meaning for your bootstrap css, if you leave the import as 对于引导CSS的含义,如果将导入保留为

@import "bootstrap.css";

The only place it looks automatically are 它自动显示的唯一位置是

web_modules/bootstrap.css
node_modules/bootstrap.css
local_modules/bootstrap.css

If you change your import to 如果将导入更改为

@import "bootstrap";

It will look in the following folders for index.css 它将在以下文件夹中查找index.css

web_modules/bootstrap/index.css
node_modules/bootstrap/index.css
local_modules/bootstrap/index.css

If you've pulled your css library using some kind of package manager and it has a package.json sat in the root folder of the library 如果您使用某种程序包管理器拉出了CSS库,并且该库的根文件夹中有package.json文件,

node_modules/bootstrap/package.json

The package.json can tell postcss where to find the library using either the main or style property package.json可以使用main或style属性告诉postcss在哪里找到库

{
    ...
    "main": "dist/css/bootstrap.css"
}

But like i said it really depends how you're pulling you're libraries, if you're just manually grabbing them and putting them in nested folders further than where the plugin looks, then you'll just have to add the direct paths to the file in the path object as shown in my original answer 但是就像我说的那样,这实际上取决于您如何拉出库,如果您只是手动获取它们并将它们放置在嵌套文件夹中,而不是插件看上去的位置,那么您只需要添加直接路径即可路径对象中的文件,如我的原始答案所示

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

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