简体   繁体   English

如何自动完成在我的Node.js项目中编译Twitter Bootstrap等前端框架的任务?

[英]How do I automate the task of compiling front-end frameworks like Twitter Bootstrap in my Node.js project?

How do I automate the task of compiling Twitter Bootstrap in my Node.js project? 如何自动完成在Node.js项目中编译Twitter Bootstrap的任务?

I'm editing the LESS files that compile into a custom build of Bootstrap for my Node.js project, so I can't just use the online customizer or the pre-compiled JavaScript/CSS distribution. 我正在编辑LESS文件,这些文件编译成我的Node.js项目的自定义构建的Bootstrap,因此我不能只使用在线自定义程序或预编译的JavaScript / CSS分发。

How do I use something like Grunt or Bower to automate the process of building and compiling the Twitter Bootstrap front-end framework into my project from source? 我如何使用像Grunt或Bower这样的东西来自动化从源代码构建和编译Twitter Bootstrap前端框架到我的项目中的过程?

Is there a package manager for front-end libraries and frameworks? 是否有前端库和框架的包管理器?

I'm using Grunt to compile my LESS. 我正在使用Grunt来编译我的LESS。 Here are the dependencies which you have to add to your package.json: 以下是您必须添加到package.json的依赖项:

"devDependencies": {
    "grunt": "0.4.1",
    "grunt-contrib-concat": "0.3.0",
    "grunt-contrib-watch": "0.4.4",
    "assemble-less": "0.4.8"
}

And here is how my Gruntfile.js looks like: 以下是我的Gruntfile.js的样子:

module.exports = function(grunt) {

    grunt.initConfig({
        less: {
            project: {
                options: {
                    paths: ['src/css/less'],
                    yuicompress: true
                },
                src: ['src/css/less/index.less'],               
                dest: 'src/css/styles.css'
            }
        },
        watch: {
            css: {
                files: ['src/css/less/**/*.less'],
                tasks: ['less']
            }
        }
    });

    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('assemble-less');

    // grunt.registerTask('default', ['concat', 'less']);
    grunt.registerTask('default', ['less', 'watch']);

}

And I simply type grunt before to start working. 我只是打开grunt才开始工作。 It run a watcher and compiles my less files once something changes. 它会运行一个观察程序,并在发生变化时编译我的文件。

Edit: There is also https://github.com/emberfeather/less.js-middleware but you need to add the compilation to the app's flow. 编辑:还有https://github.com/emberfeather/less.js-middleware,但您需要将编译添加到应用程序的流程中。 This means that you will compile the less files during the run of the nodejs process. 这意味着您将在nodejs进程运行期间编译较少的文件。 This will happen only once and if you make changes in some of the files you will not see the result. 这只会发生一次,如果您在某些文件中进行更改,则无法看到结果。 Of course you may want to compile on every request, but this will decrease the performance of your app. 当然,您可能希望在每个请求上进行编译,但这会降低应用程序的性能。 So, you will end up with some kind of a watcher and compiler. 所以,你最终会得到某种观察者和编译器。 Exactly what Grunt is doing. 正是Grunt正在做的事情。 If you don't want to run grunt every time you may add it to your boot scripts (or startup things under windows). 如果你不想每次都将grunt添加到你的启动脚本(或在windows下启动),就会运行grunt

Depending on your arrangement, you may want to just look into less-middleware . 根据您的安排,您可能只想查看较少的中间件 It will compile your LESS into CSS on the fly in development, and in production will do it the first time the CSS is requested, and then serve up the CSS instead of recompiling it every time. 它将在开发中动态编译你的LESS到CSS中,并且在生产中将在第一次请求CSS时执行它,然后提供CSS而不是每次重新编译它。 Plenty of configuration examples at the included link. 包含链接中的大量配置示例。

I dont have the settings on hand for grunt-bootstrap and npmjs is offline right now for some reason, try this link for the settings https://npmjs.org/package/grunt-bootstrap 我没有grunt-bootstrap的设置,npmjs现在因为某些原因离线了,请尝试使用此链接进行设置https://npmjs.org/package/grunt-bootstrap

im using the latest version of grunt-bootstrap and grunt-contrib-less like this; 即时通讯使用最新版本的grunt-bootstrap和grunt-contrib-less这样的;

package.json 的package.json

"devDependencies": {
"grunt": "~0.4.2",
"grunt-cli": "~0.1.11",
"grunt-bootstrap": "~0.1.0",
"grunt-contrib-jade": "~0.8.0",
"grunt-contrib-less": "~0.8.2",
"grunt-contrib-jshint": "~0.7.2",
"grunt-contrib-uglify": "~0.2.7",
"grunt-contrib-watch": "~0.5.3"
//others here

} }

Gruntfile.JS less: entry Gruntfile.JS less:entry

module.exports = function(grunt) {

    // Project configuration.
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        jshint: {
            options: {
                curly: true,
                eqeqeq: true,
                immed: true,
                latedef: true,
                newcap: true,
                noarg: true,
                sub: true,
                undef: true,
                boss: true,
                eqnull: true,
                browser: true,
                globals: {
                    require: true,
                    define: true,
                    requirejs: true,
                    describe: true,
                    expect: true,
                    it: true
                },

                // https://leanpub.com/grunt/read #see 'module' is not defined
                node: true
            },
            // https://leanpub.com/grunt/read #see 'module' is not defined
            all: [
                'Gruntfile.js',
                'src/app/*.js',
                'src/config.js',
                'tests/app/*.js'
            ]
        },
        uglify: {
            options: {
                banner: '*//*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> *//*\n'
            },
            build: {
                src: 'src/app/<%= pkg.name %>.js',
                dest: 'build/app/<%= pkg.name %>.min.js'
            }
        },

        // Run jshint any time a file is added
        watch: {
            scripts: {
                files: [
                    'Gruntfile.js',
                    'src/app/*.js',
                    'tests/app/*.js'
                ],
                tasks: ['jshint'],
                options: {
                    /**
                     * If you need to dynamically modify your config, the spawn option must be disabled to keep the watch
                     * running under the same context.
                     */
                    spawn: false
                }
            },
            css: {
                files: ['src/assets/css/*.less'],
                tasks: ['less']
            }
        },
        less: {
            development: {
                options: {
                    paths: ["src/assets/css"],
                    compress: true,
                    report: 'gzip',
                    cleancss: true,
                    ieCompat: true,
                    strictImports: true
                    //dumpLineNumbers
                },
                files: [{
                    expand: true,
                    cwd: "src/assets/css",
                    src:  ['*.less'],
                    dest: 'build/assets/css',
                    ext: '.css'
                }]
            }
            /*
            production: {
                options: {
                    cleancss: true
                },
                files: {
                }

            }*/
        },
        jade: {
            compile: {
                options: {
                    //client: false,
                    pretty: true,
                    data: {
                        debug: false
                    }
                },
                files: [ {
                    cwd: "src/app/views",
                    src: "**/*.jade",
                    dest: "build/templates",
                    expand: true,
                    ext: ".html"
                } ]
            }
        }
    });

    // Load task(s)
    grunt.loadNpmTasks('grunt-contrib-less');
    grunt.loadNpmTasks('grunt-contrib-jade');
    grunt.loadNpmTasks('grunt-bootstrap');
    grunt.loadNpmTasks('grunt-contrib-jshint');
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-watch');

    // Register task(s).
    grunt.registerTask('default', [
            'jshint',
            'uglify',
            'less',
            'jade',
            'bootstrap'
        ]);

    grunt.registerTask('dev', [
            'watch'
    ]);
};

Edit: i found this once again, https://github.com/jgallen23/grunt-bootstrap i knew it was somewhere out there. 编辑:我再次发现了这个, https://github.com/jgallen23/grunt-bootstrap我知道它在那里的某个地方。 there is some of your bootstrap config options your probably looking for that you need to add to the gruntfile.js to complete your task.. Enjoy 你可能正在寻找一些你需要添加到gruntfile.js的bootstrap配置选项来完成你的任务..享受

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

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