Sails.js Linker Grunt任务和把手模板引擎

时间:2017-07-01 16:56:34

标签: node.js gruntjs sails.js handlebars.js

我正在构建一个Sails.js应用。我是Sails.js,Node.js环境和HTML模板引擎的新手。我想用Handlebars作为我的模板引擎。虽然基本呈现“有效”(即,从服务器传递的数据被正确呈现并且基本布局有效),但我无法让sails-linker将我的客户端脚本和样式注入到模板中。我搜索过,查看了Sail的文档,并尝试了各种设置但无济于事。

这是我的管道文件:

    /**
 * grunt/pipeline.js
 *
 * The order in which your css, javascript, and template files should be
 * compiled and linked from your views and static HTML files.
 *
 * (Note that you can take advantage of Grunt-style wildcard/glob/splat expressions
 * for matching multiple files, and ! in front of an expression to ignore files.)
 *
 * For more information see:
 *   https://github.com/balderdashy/sails-docs/blob/master/anatomy/myApp/tasks/pipeline.js.md
 */


// CSS files to inject in order
//
// (if you're using LESS with the built-in default config, you'll want
//  to change `assets/styles/importer.less` instead.)
var cssFilesToInject = [
    'styles/src/**/*.scss'
];


// Client-side javascript files to inject in order
// (uses Grunt-style wildcard/glob/splat expressions)
var jsFilesToInject = [

    // Load sails.io before everything else
    'js/dependencies/sails.io.js',

    // Dependencies like jQuery, or Angular are brought in here
    'js/dependencies/**/*.js',

    // All of the rest of your client-side js files
    // will be injected here in no particular order.
    'js/**/*.js'
];


// Client-side HTML templates are injected using the sources below
// The ordering of these templates shouldn't matter.
// (uses Grunt-style wildcard/glob/splat expressions)
//
// By default, Sails uses JST templates and precompiles them into
// functions for you.  If you want to use jade, handlebars, dust, etc.,
// with the linker, no problem-- you'll just want to make sure the precompiled
// templates get spit out to the same file.  Be sure and check out `tasks/README.md`
// for information on customizing and installing new tasks.
var templateFilesToInject = [
    '**/*.handlebars'
];

// Default path for public folder (see documentation for more information)
var tmpPath = '.tmp/public/';

// Prefix relative paths to source files so they point to the proper locations
// (i.e. where the other Grunt tasks spit them out, or in some cases, where
// they reside in the first place)
module.exports.cssFilesToInject = cssFilesToInject.map(function(cssPath) {
    // If we're ignoring the file, make sure the ! is at the beginning of the path
    if (cssPath[0] === '!') {
        return require('path').join('!.tmp/public/', cssPath.substr(1));
    }
    return require('path').join('.tmp/public/', cssPath);
});
module.exports.jsFilesToInject = jsFilesToInject.map(function(jsPath) {
    // If we're ignoring the file, make sure the ! is at the beginning of the path
    if (jsPath[0] === '!') {
        return require('path').join('!.tmp/public/', jsPath.substr(1));
    }
    return require('path').join('.tmp/public/', jsPath);
});
module.exports.templateFilesToInject = templateFilesToInject.map(function(tplPath) {
    // If we're ignoring the file, make sure the ! is at the beginning of the path

    if (tplPath[0] === '!') {
        return require('path').join('views/', tplPath.substr(1));
    }
    return require('path').join('views/', tplPath);
});

我在tasks / config

下添加了一个handlebars.js任务
    // tasks/config/handlebars.js
// --------------------------------
// handlebar task configuration.

module.exports = function(grunt) {

    // We use the grunt.config api's set method to configure an
    // object to the defined string. In this case the task
    // 'handlebars' will be configured based on the object below.
    grunt.config.set('handlebars', {
        dev: {
            // We will define which template files to inject
            // in tasks/pipeline.js
            files: {

                '.tmp/public/templates.js': require('../pipeline').templateFilesToInject
            }
        }
    });

    // load npm module for handlebars.

    grunt.loadNpmTasks('grunt-contrib-handlebars');
}

我的sails-linker.js文件(也位于tasks / config下面):

   /**
 * `sails-linker`
 *
 * ---------------------------------------------------------------
 *
 * Automatically inject <script> tags and <link> tags into the specified
 * specified HTML and/or EJS files.  The specified delimiters (`startTag`
 * and `endTag`) determine the insertion points.
 *
 * #### Development (default)
 * By default, tags will be injected for your app's client-side JavaScript files,
 * CSS stylesheets, and precompiled client-side HTML templates in the `templates/`
 * directory (see the `jst` task for more info on that).  In addition, if a LESS
 * stylesheet exists at `assets/styles/importer.less`, it will be compiled to CSS
 * and a `<link>` tag will be inserted for it.  Similarly, if any Coffeescript
 * files exists in `assets/js/`, they will be compiled into JavaScript and injected
 * as well.
 *
 * #### Production (`NODE_ENV=production`)
 * In production, all stylesheets are minified into a single `.css` file (see
 * `tasks/config/cssmin.js` task) and all client-side scripts are minified into
 * a single `.js` file (see `tasks/config/uglify.js` task).  Any client-side HTML
 * templates, CoffeeScript, or LESS files are bundled into these same two minified
 * files as well.
 *
 * For usage docs see:
 *   https://github.com/Zolmeister/grunt-sails-linker
 *
 */
module.exports = function(grunt) {

    grunt.config.set('sails-linker', {
        devJs: {
            options: {
                startTag: '<!--SCRIPTS-->',
                endTag: '<!--SCRIPTS END-->',
                fileTmpl: '<script src="%s"></script>',
                appRoot: '.tmp/public'
            },
            files: {
                '.tmp/public/**/*.html': require('../pipeline').jsFilesToInject,
                'views/**/*.html': require('../pipeline').jsFilesToInject,
                '.tmp/public/templates.js': require('../pipeline').jsFilesToInject,
                'views/**/*.handlebars': require('../pipeline').jsFilesToInject
            }
        },

        devJsRelative: {
            options: {
                startTag: '<!--SCRIPTS-->',
                endTag: '<!--SCRIPTS END-->',
                fileTmpl: '<script src="%s"></script>',
                appRoot: '.tmp/public',
                relative: true
            },
            files: {
                '.tmp/public/**/*.html': require('../pipeline').jsFilesToInject,
                'views/**/*.html': require('../pipeline').jsFilesToInject,
                '.tmp/public/templates.js': require('../pipeline').jsFilesToInject,
                'views/**/*.handlebars': require('../pipeline').jsFilesToInject
            }
        },

        prodJs: {
            options: {
                startTag: '<!--SCRIPTS-->',
                endTag: '<!--SCRIPTS END-->',
                fileTmpl: '<script src="%s"></script>',
                appRoot: '.tmp/public'
            },
            files: {
                '.tmp/public/**/*.html': ['.tmp/public/min/production.min.js'],
                'views/**/*.html': ['.tmp/public/min/production.min.js'],
                'views/**/*.handlebars': ['.tmp/public/min/production.min.js']
            }
        },

        prodJsRelative: {
            options: {
                startTag: '<!--SCRIPTS-->',
                endTag: '<!--SCRIPTS END-->',
                fileTmpl: '<script src="%s"></script>',
                appRoot: '.tmp/public',
                relative: true
            },
            files: {
                '.tmp/public/**/*.html': ['.tmp/public/min/production.min.js'],
                'views/**/*.html': ['.tmp/public/min/production.min.js'],
                'views/**/*.handlebars': ['.tmp/public/min/production.min.js']
            }
        },

        devStyles: {
            options: {
                startTag: '<!--STYLES-->',
                endTag: '<!--STYLES END-->',
                fileTmpl: '<link rel="stylesheet" href="%s">',
                appRoot: '.tmp/public'
            },

            files: {
                '.tmp/public/**/*.html': require('../pipeline').cssFilesToInject,
                'views/**/*.html': require('../pipeline').cssFilesToInject,
                '.tmp/public/templates.js': require('../pipeline').jsFilesToInject,
                'views/**/*.handlebars': require('../pipeline').cssFilesToInject
            }
        },

        devStylesRelative: {
            options: {
                startTag: '<!--STYLES-->',
                endTag: '<!--STYLES END-->',
                fileTmpl: '<link rel="stylesheet" href="%s">',
                appRoot: '.tmp/public',
                relative: true
            },

            files: {
                '.tmp/public/**/*.html': require('../pipeline').cssFilesToInject,
                'views/**/*.html': require('../pipeline').cssFilesToInject,
                '.tmp/public/templates.js': require('../pipeline').jsFilesToInject,
                'views/**/*.handlebars': require('../pipeline').cssFilesToInject
            }
        },

        prodStyles: {
            options: {
                startTag: '<!--STYLES-->',
                endTag: '<!--STYLES END-->',
                fileTmpl: '<link rel="stylesheet" href="%s">',
                appRoot: '.tmp/public'
            },
            files: {
                '.tmp/public/index.html': ['.tmp/public/min/production.min.css'],
                'views/**/*.html': ['.tmp/public/min/production.min.css'],
                'views/**/*.handlebars': ['.tmp/public/min/production.min.css']
            }
        },

        prodStylesRelative: {
            options: {
                startTag: '<!--STYLES-->',
                endTag: '<!--STYLES END-->',
                fileTmpl: '<link rel="stylesheet" href="%s">',
                appRoot: '.tmp/public',
                relative: true
            },
            files: {
                '.tmp/public/index.html': ['.tmp/public/min/production.min.css'],
                'views/**/*.html': ['.tmp/public/min/production.min.css'],
                'views/**/*.handlebars': ['.tmp/public/min/production.min.css']
            }
        },

        // Bring in JST template object
        devTpl: {
            options: {
                startTag: '<!--TEMPLATES-->',
                endTag: '<!--TEMPLATES END-->',
                fileTmpl: '<script type="text/javascript" src="%s"></script>',
                appRoot: '.tmp/public'
            },
            files: {
                '.tmp/public/index.html': ['.tmp/public/jst.js'],
                'views/**/*.html': ['.tmp/public/jst.js'],
                'views/**/*.handlebars': ['.tmp/public/jst.js']
            }
        },

        devJsJade: {
            options: {
                startTag: '// SCRIPTS',
                endTag: '// SCRIPTS END',
                fileTmpl: 'script(src="%s")',
                appRoot: '.tmp/public'
            },
            files: {
                'views/**/*.jade': require('../pipeline').jsFilesToInject
            }
        },

        devJsRelativeJade: {
            options: {
                startTag: '// SCRIPTS',
                endTag: '// SCRIPTS END',
                fileTmpl: 'script(src="%s")',
                appRoot: '.tmp/public',
                relative: true
            },
            files: {
                'views/**/*.jade': require('../pipeline').jsFilesToInject
            }
        },

        prodJsJade: {
            options: {
                startTag: '// SCRIPTS',
                endTag: '// SCRIPTS END',
                fileTmpl: 'script(src="%s")',
                appRoot: '.tmp/public'
            },
            files: {
                'views/**/*.jade': ['.tmp/public/min/production.min.js']
            }
        },

        prodJsRelativeJade: {
            options: {
                startTag: '// SCRIPTS',
                endTag: '// SCRIPTS END',
                fileTmpl: 'script(src="%s")',
                appRoot: '.tmp/public',
                relative: true
            },
            files: {
                'views/**/*.jade': ['.tmp/public/min/production.min.js']
            }
        },

        devStylesJade: {
            options: {
                startTag: '// STYLES',
                endTag: '// STYLES END',
                fileTmpl: 'link(rel="stylesheet", href="%s")',
                appRoot: '.tmp/public'
            },

            files: {
                'views/**/*.jade': require('../pipeline').cssFilesToInject
            }
        },

        devStylesRelativeJade: {
            options: {
                startTag: '// STYLES',
                endTag: '// STYLES END',
                fileTmpl: 'link(rel="stylesheet", href="%s")',
                appRoot: '.tmp/public',
                relative: true
            },

            files: {
                'views/**/*.jade': require('../pipeline').cssFilesToInject
            }
        },

        prodStylesJade: {
            options: {
                startTag: '// STYLES',
                endTag: '// STYLES END',
                fileTmpl: 'link(rel="stylesheet", href="%s")',
                appRoot: '.tmp/public'
            },
            files: {
                'views/**/*.jade': ['.tmp/public/min/production.min.css']
            }
        },

        prodStylesRelativeJade: {
            options: {
                startTag: '// STYLES',
                endTag: '// STYLES END',
                fileTmpl: 'link(rel="stylesheet", href="%s")',
                appRoot: '.tmp/public',
                relative: true
            },
            files: {
                'views/**/*.jade': ['.tmp/public/min/production.min.css']
            }
        },

        // Bring in JST template object
        devTplJade: {
            options: {
                startTag: '// TEMPLATES',
                endTag: '// TEMPLATES END',
                fileTmpl: 'script(type="text/javascript", src="%s")',
                appRoot: '.tmp/public'
            },
            files: {
                'views/**/*.jade': ['.tmp/public/jst.js']
            }
        }
    });

    grunt.loadNpmTasks('grunt-sails-linker');
};

最后,我的compileAssets.js文件

   /**
 * `compileAssets`
 *
 * ---------------------------------------------------------------
 *
 * This Grunt tasklist is not designed to be used directly-- rather
 * it is a helper called by the `default`, `prod`, `build`, and
 * `buildProd` tasklists.
 *
 * For more information see:
 *   http://sailsjs.org/documentation/anatomy/my-app/tasks/register/compile-assets-js
 *
 */
module.exports = function(grunt) {
    grunt.registerTask('compileAssets', [
        'clean:dev',
        'copy:dev',
        "handlebars:dev",
        "sass:dev",
        "ts:dev",

    ]);
};

我在views目录下有几个.handlebars文件,包括layout.handlebars。内容正在正确呈现;我的样式和脚本标签根本没有被注入。

非常感谢任何指导。

1 个答案:

答案 0 :(得分:0)

要使sails链接器正常工作,您需要在手柄布局中进行一些更改

在正文的头部添加<!--STYLES--><!--STYLES END-->个人this

并在正文末尾添加<!--SCRIPTS--><!--SCRIPTS END-->

然后sails链接器将能够将样式和脚本注入到您的布局中。