at-import / node-sass-import-once

Eyeglass style Import Once, but for all the things!
Other
94 stars 23 forks source link

Add 'transformContent' option #32

Open antixrist opened 8 years ago

antixrist commented 8 years ago

I use it for custom url resolver. Example:

  // ...
    .pipe(gulpSass({
      precision: 10,
      importer: require('node-sass-import-once'),
      importOnce: {
        index: true,
        css: true,
        bower: false,

        /**
         * It's new option
         * 
         * @param {string} filename
         * @param {string} contents
         * @returns {string}
         */
        transformContent: function (filename, contents) {
          return [
            '$__filepath: unquote("'+ filename +'");',
            '@function url($url: null, $args...) {',
            '  @return __url($__filepath, $url);',
            '}',
            contents
          ].join('\n');
        }
      },
      functions: {
        '__url($filepath, $url)': function(filepath, url, done) {
          url = url.getValue();
          filepath = filepath.getValue();

          var resolvedUrl = url;
          /* ...custom url resolve here... */
          done(new sass.types.String('url('+ resolvedUrl +')'));
        },
      }
    }))
  // ...