The View layer of the basis of Sails - Assets

The assets directory is the directory where the front-end static resources are placed under the application of Sails, such as js, css, image, etc.

Dynamic injection of assert resources

Sails uses Grunt to complete the construction of Web applications. By default, sails uses tasks/register/defaults.js to complete the construction

module.exports = function (grunt) {
  grunt.registerTask('default', [

We found that there are three sub tasks: compileAssets, linkAssets and watch.
Among them, the task of linkAssets is to automatically inject the static resources in the assert into ejs or html when the application is started. In linkAssets, sails linker is used to complete the actual task:

grunt.registerTask('linkAssets', [

The task defines the injection of js, style and template resources.

    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,
        'views/**/*.ejs': require('../pipeline').cssFilesToInject

We can see that sails linker will look for startTag and endTag. If found, the content will be injected into ejs or html according to the actual resource situation. pipeline.js defines the injection methods of various resources. For example, the logic of style resource css is as follows:

var cssFilesToInject = [

  // Bring in `.css` files for themes and style guides (e.g. Bootstrap, Foundation)

  // All of the rest of your custom `.css` files will be injected here,
  // in no particular order.  To customize the ordering, add additional
  // items here, _above_ this one.
var tmpPath = '.tmp/public/';
module.exports.cssFilesToInject =>{
  // If we're ignoring the file, make sure the ! is at the beginning of the path
  if (cssPath[0] === '!') {
    return require('path').join('!' + tmpPath, cssPath.substr(1));
  return require('path').join(tmpPath, cssPath);

Integrated bower

Install bower

npm install bower -g

Configure bower

Create the. bowerrc file in the root directory of the sales project, and set the bower Library Directory in the file:


So, we can start using bower to install the relevant static resources.

Tags: Linker REST npm

Posted on Sat, 09 Nov 2019 10:31:10 -0500 by trg