Exemplo n.º 1
0
  var images = attrs.srcset.trim().split(/\s*,\s*/).map(function(src){
    var parts = src.split(/\s+/);
    var reference = parts.shift();
    var uri = resolveResourceUri(reference);

    if (uri)
    {
      var imageFile = resolveImage(flow, uri.filename, node, null, {
        file: file,
        loc: node.loc,
        token: atHtml.translate(node)
      });

      if (imageFile)
      {
        imageFile.cssResource = true;
        return {
          file: imageFile,
          hash: uri.hash,
          settings: parts
        };
      }
      else
      {
        flow.warn({
          file: file.relpath,
          message: 'Image reference `' + reference + '` is not resolved (ignored)'
        });
      }
    }

    return {
      static: src
    };
  });
Exemplo n.º 2
0
(module.exports = function(flow){
  var fconsole = flow.console;
  var queue = flow.files.queue;

  for (var i = 0, file; file = queue[i]; i++)
    if (file.type == 'script' && file.htmlNode)
    {
      fconsole.log(file.relpath);
      if (file.outputFilename)
      {
        html_at.replaceToken(file.htmlNode, {
          type: 'script',
          name: 'script',
          attribs: {
            src: file.htmlRef || file.outputFilename
          },
          children: []
        });
      }
      else
      {
        if (!file.outputContent)
        {
          file.htmlFile.unlink(file, file.htmlNode);
          html_at.removeToken(file.htmlNode, true);
        }
        else
          html_at.replaceToken(file.htmlNode, {
            type: 'script',
            name: 'script',
            attribs: {},
            children: [
              {
                type: 'text',
                data: file.outputContent
              }
            ]
          });
      }
    }
}).handlerName = '[js] Modify <script> entry in html file';
Exemplo n.º 3
0
            pkg.map(function(file){
              if (file.htmlNode)
              {
                if (!htmlNode)
                {
                  htmlFile = file.htmlFile;
                  htmlNode = file.htmlNode;
                  htmlFile.unlink(file, htmlNode);
                }
                else
                {
                  file.htmlFile.unlink(file, file.htmlNode);
                  html_at.removeToken(file.htmlNode, true);
                }

                delete file.htmlNode;
              }

              return '// [' + file.relpath + ']\n' + file.outputContent;
            }).join(';\n\n') + ';'
Exemplo n.º 4
0
(module.exports = function(flow){
  var queue = flow.files.queue;
  var fconsole = flow.console;

  flow.resLinks = [];

  for (var i = 0, file; file = queue[i]; i++)
  {
    switch (file.type)
    {
      case 'html':
        fconsole.start('Scan (' + file.type + ') ' + file.relpath);

        if (!file.ast)
          html.processFile(file, flow);

        atHtml.walk(file.ast, {
          'tag': function(node){
            var attrs = atHtml.getAttrs(node);

            switch (node.name)
            {
              case 'source':
                if (attrs.srcset)
                  htmlSrcset(flow, file, node, attrs);
                break;

              case 'img':
                if (attrs.srcset)
                  htmlSrcset(flow, file, node, attrs);

                // ignore if no src value
                if (!attrs.src)
                  return;

                var uri = resolveResourceUri(attrs.src);

                if (uri)
                {
                  fconsole.log('Found <img src="' + uri.filename + uri.hash + '"/>');

                  node.loc = file.location(node.info.start);

                  var imageFile = resolveImage(flow, uri.filename, node, null, {
                    file: file,
                    loc: node.loc,
                    token: atHtml.translate(node)
                  });

                  if (imageFile)
                    //attrs.src = imageFile.fileRef + uri.hash;
                    flow.resLinks.push({
                      type: 'img-src',
                      sourceFile: file,
                      file: imageFile,
                      hash: uri.hash,
                      host: attrs
                    });
                  else
                    flow.warn({
                      file: file.relpath,
                      message: 'Image reference is not resolved (ignored)'
                    });
                }

                break;

              case 'link':
                if (/^image\//.test(attrs.type) ||
                    atHtml.rel(node, 'icon') ||
                    atHtml.rel(node, 'apple-touch-icon') ||
                    atHtml.rel(node, 'apple-touch-icon-precomposed') ||
                    atHtml.rel(node, 'apple-touch-startup-image') ||
                    atHtml.rel(node, 'image_src'))
                {
                  var uri = resolveResourceUri(attrs.href);

                  if (uri)
                  {
                    fconsole.log('Found <link rel="' + atHtml.rel(node).join(' ') + '" href="' + uri.filename + uri.hash + '"/>');

                    node.loc = file.location(node.info.start);

                    var imageFile = resolveImage(flow, uri.filename, node, null, {
                      file: file,
                      loc: node.loc,
                      token: atHtml.translate(node)
                    });

                    if (imageFile)
                      //attrs.href = imageFile.fileRef + uri.hash;
                      flow.resLinks.push({
                        type: 'link-href',
                        sourceFile: file,
                        file: imageFile,
                        hash: uri.hash,
                        host: attrs
                      });
                    else
                      flow.warn({
                        file: file.relpath,
                        message: 'Image reference is not resolved (ignored)'
                      });
                  }
                }

                break;

              case 'meta':
                if (attrs.name == 'msapplication-TileImage' ||
                    attrs.name == 'msapplication-square70x70logo' ||
                    attrs.name == 'msapplication-square150x150logo' ||
                    attrs.name == 'msapplication-wide310x150logo' ||
                    attrs.name == 'msapplication-square310x310logo')
                {
                  var uri = resolveResourceUri(attrs.content);

                  if (uri)
                  {
                    fconsole.log('Found <meta name="' + attrs.name + '" content="' + uri.filename + uri.hash + '"/>');

                    node.loc = file.location(node.info.start);

                    var imageFile = resolveImage(flow, uri.filename, node, null, {
                      file: file,
                      loc: node.loc,
                      token: atHtml.translate(node)
                    });

                    if (imageFile)
                      flow.resLinks.push({
                        type: 'meta-content',
                        sourceFile: file,
                        file: imageFile,
                        hash: uri.hash,
                        host: attrs
                      });
                    else
                      flow.warn({
                        file: file.relpath,
                        message: 'Image reference is not resolved (ignored)'
                      });
                  }
                }

                break;
            }
          }
        });

        fconsole.endl();
      break;

      case 'style':
      case 'style-block':
        fconsole.start('Scan (' + file.type + ') ' + file.relpath);

        atCss.walk(file.ast, {
          'uri': function(token){
            fconsole.log('Found ' + atCss.translate(token));

            var uri = resolveResourceUri(atCss.unpackUri(token));

            if (uri)
            {
              token.loc = file.location(token[0]);
              var imageFile = resolveImage(flow, uri.filename, token, null, {
                file: file,
                loc: token.loc,
                token: atCss.translate(token)
              });

              if (imageFile)
              {
                imageFile.cssResource = true;
                //atCss.packUri(imageFile.fileRef + uri.hash, token);
                flow.resLinks.push({
                  type: 'css-url',
                  sourceFile: file,
                  file: imageFile,
                  hash: uri.hash,
                  token: token
                });
              }
              else
                flow.warn({
                  file: file.relpath,
                  message: 'Image reference is not resolved (ignored)'
                });
            }
          }
        });

        fconsole.endl();
      break;

      case 'template':
        fconsole.start('Scan (' + file.type + ') ' + file.relpath);

        if (file.ast)
        {
          atTmpl.walk(file.ast, flow.js.basis.template, {
            'attr': function(token, parentToken){
              var attributeName = this.tokenName(token);
              var attributeValue = this.tokenValue(token);

              switch (attributeName)
              {
                case 'src':
                  var tagName = this.tokenName(parentToken);

                  if (tagName == 'img')
                  {
                    fconsole.log('Found <' + tagName + ' src="' + attributeValue + '"/>');

                    if (this.hasBindings(token))
                    {
                      fconsole.log('[i] Ignored, token has bindings on `src` attribute');
                      return;
                    }

                    var uri = resolveResourceUri(attributeValue);

                    if (uri)
                    {
                      var imageFile = resolveImage(flow, uri.filename, token, flow.indexFile, {
                        file: file,
                        loc: token.loc
                      });

                      if (imageFile)
                      {
                        imageFile.cssResource = true;
                        //this.tokenValue(token, imageFile.fileRef + uri.hash);
                        flow.resLinks.push({
                          type: 'tmpl-' + attributeName,
                          sourceFile: file,
                          file: imageFile,
                          hash: uri.hash,
                          token: token,
                          context: this
                        });
                      }
                      else
                      {
                        flow.warn({
                          file: file.relpath,
                          message: 'Image reference `' + attributeValue + '` is not resolved (ignored)'
                        });
                      }
                    }
                  }
                  break;

                case 'srcset':
                  var tagName = this.tokenName(parentToken);

                  if (tagName == 'img' || tagName == 'source')
                  {
                    fconsole.log('Found <' + tagName + ' srcset="' + attributeValue + '"/>');

                    if (this.hasBindings(token))
                    {
                      fconsole.log('[i] Ignored, token has bindings on `srcset` attribute');
                      return;
                    }

                    var images = attributeValue.trim().split(/\s*,\s*/).map(function(src){
                      var parts = src.split(/\s+/);
                      var reference = parts.shift();
                      var uri = resolveResourceUri(reference);

                      if (uri)
                      {
                        var imageFile = resolveImage(flow, uri.filename, token, flow.indexFile, {
                          file: file,
                          loc: token.loc
                        });

                        if (imageFile)
                        {
                          imageFile.cssResource = true;
                          return {
                            file: imageFile,
                            hash: uri.hash,
                            settings: parts
                          };
                        }
                        else
                        {
                          flow.warn({
                            file: file.relpath,
                            message: 'Image reference `' + reference + '` is not resolved (ignored)'
                          });
                        }
                      }

                      return {
                        static: src
                      };
                    });

                    flow.resLinks.push({
                      type: 'tmpl-' + attributeName,
                      sourceFile: file,
                      tag: tagName,
                      images: images,
                      token: token,
                      context: this
                    });
                  }
                  break;

                case 'style':
                  if (!/^b:/.test(this.tokenName(parentToken)) && attributeValue)
                  {
                    fconsole.log('Style attribute found');
                    file.link(flow.files.add({
                      type: 'style-block',
                      inline: true,
                      tmplFile: file,
                      tmplToken: token,
                      tmplContext: this,
                      baseURI: flow.indexFile.baseURI,
                      content: this.tokenValue(token),
                      ast: atCss.parse(this.tokenValue(token), true)
                    }), token);
                  }
                  break;
              }
            }
          });
        }

        fconsole.endl();
      break;
    }
  }
}).handlerName = '[res] Extract';
Exemplo n.º 5
0
          'tag': function(node){
            var attrs = atHtml.getAttrs(node);

            switch (node.name)
            {
              case 'source':
                if (attrs.srcset)
                  htmlSrcset(flow, file, node, attrs);
                break;

              case 'img':
                if (attrs.srcset)
                  htmlSrcset(flow, file, node, attrs);

                // ignore if no src value
                if (!attrs.src)
                  return;

                var uri = resolveResourceUri(attrs.src);

                if (uri)
                {
                  fconsole.log('Found <img src="' + uri.filename + uri.hash + '"/>');

                  node.loc = file.location(node.info.start);

                  var imageFile = resolveImage(flow, uri.filename, node, null, {
                    file: file,
                    loc: node.loc,
                    token: atHtml.translate(node)
                  });

                  if (imageFile)
                    //attrs.src = imageFile.fileRef + uri.hash;
                    flow.resLinks.push({
                      type: 'img-src',
                      sourceFile: file,
                      file: imageFile,
                      hash: uri.hash,
                      host: attrs
                    });
                  else
                    flow.warn({
                      file: file.relpath,
                      message: 'Image reference is not resolved (ignored)'
                    });
                }

                break;

              case 'link':
                if (/^image\//.test(attrs.type) ||
                    atHtml.rel(node, 'icon') ||
                    atHtml.rel(node, 'apple-touch-icon') ||
                    atHtml.rel(node, 'apple-touch-icon-precomposed') ||
                    atHtml.rel(node, 'apple-touch-startup-image') ||
                    atHtml.rel(node, 'image_src'))
                {
                  var uri = resolveResourceUri(attrs.href);

                  if (uri)
                  {
                    fconsole.log('Found <link rel="' + atHtml.rel(node).join(' ') + '" href="' + uri.filename + uri.hash + '"/>');

                    node.loc = file.location(node.info.start);

                    var imageFile = resolveImage(flow, uri.filename, node, null, {
                      file: file,
                      loc: node.loc,
                      token: atHtml.translate(node)
                    });

                    if (imageFile)
                      //attrs.href = imageFile.fileRef + uri.hash;
                      flow.resLinks.push({
                        type: 'link-href',
                        sourceFile: file,
                        file: imageFile,
                        hash: uri.hash,
                        host: attrs
                      });
                    else
                      flow.warn({
                        file: file.relpath,
                        message: 'Image reference is not resolved (ignored)'
                      });
                  }
                }

                break;

              case 'meta':
                if (attrs.name == 'msapplication-TileImage' ||
                    attrs.name == 'msapplication-square70x70logo' ||
                    attrs.name == 'msapplication-square150x150logo' ||
                    attrs.name == 'msapplication-wide310x150logo' ||
                    attrs.name == 'msapplication-square310x310logo')
                {
                  var uri = resolveResourceUri(attrs.content);

                  if (uri)
                  {
                    fconsole.log('Found <meta name="' + attrs.name + '" content="' + uri.filename + uri.hash + '"/>');

                    node.loc = file.location(node.info.start);

                    var imageFile = resolveImage(flow, uri.filename, node, null, {
                      file: file,
                      loc: node.loc,
                      token: atHtml.translate(node)
                    });

                    if (imageFile)
                      flow.resLinks.push({
                        type: 'meta-content',
                        sourceFile: file,
                        file: imageFile,
                        hash: uri.hash,
                        host: attrs
                      });
                    else
                      flow.warn({
                        file: file.relpath,
                        message: 'Image reference is not resolved (ignored)'
                      });
                  }
                }

                break;
            }
          }
Exemplo n.º 6
0
module.exports = function(flow){
  //
  // build generic style file (style from js & tmpl)
  //

  var fconsole = flow.console;
  var queue = flow.files.queue;
  var htmlFile;
  var styleFileMap = {};
  var targetMap = {};
  var packages = [];
  var multipleThemeBuild = false;

  function putStyle(pkg, file){
    if (!styleFileMap[pkg])
      styleFileMap[pkg] = [];

    styleFileMap[pkg].push(file);
  }

  //
  // split generic files by package
  //

  for (var i = 0, file; file = queue[i]; i++)
    if (file.type == 'style' && file.isResource)
      if (file.themes && (file.themes.length > 1 || (file.themes.length == 1 && file.themes[0] != 'base')))
      {
        multipleThemeBuild = true;
        break;
      }

  fconsole.log('Style package mode: ' + (multipleThemeBuild ? 'Multiple themes' : 'Single theme') + '\n');

  fconsole.start('Split style by packages');
  for (var i = 0, file; file = queue[i]; i++)
  {
    if (file.type == 'style' && file.isResource && !file.conditional)
    {
      // as we add file to some package mark it as non-resource anymore
      file.isResource = false;

      if (!multipleThemeBuild || !file.themes)
      {
        fconsole.log(file.relpath, '(all)');
        putStyle('style', file);
      }
      else
      {
        fconsole.log(file.relpath, '[' + file.themes.join(', ') + ']');
        file.themes.forEach(function(themeName){
          putStyle('theme-' + themeName, file);
        });
      }

      continue;
    }

    if (file.type == 'html' && file.ast && !htmlFile)
      htmlFile = file;
  }
  fconsole.endl();

  //
  // generate package files
  //

  fconsole.start('Create generic files');
  for (var name in styleFileMap)
  {
    fconsole.start(name + '.css');

    var genericStyle = createGenericFile(flow, name, styleFileMap[name]);

    targetMap[name] = true;
    packages.push(genericStyle);

    if (name == 'style' && htmlFile && !flow.options.singleFile)
    {
      fconsole.log('Inject generic file link into html');

      genericStyle.htmlFile = htmlFile;
      genericStyle.htmlNode = {
        type: 'tag',
        name: 'link',
        children: [],
        attribs: {
          rel: 'stylesheet',
          type: 'text/css',
          media: 'all'
        }
      };

      html_at.injectToHead(htmlFile.ast, genericStyle.htmlNode);
      htmlFile.link(genericStyle, genericStyle.htmlNode);
    }

    fconsole.endl();
  }

  if (multipleThemeBuild && flow.tmpl.module)
  {
    var themeUrlsMap = false;

    packages.forEach(function(file){
      if (file.theme)
      {
        if (!themeUrlsMap)
          themeUrlsMap = {};

        file.themeUrlsMap = themeUrlsMap;
        file.themeName = file.theme.replace(/^theme-/, '');
      }
    });

    if (themeUrlsMap)
      flow.files.add({
        jsRef: '_theme_css_',
        generated: true,
        type: 'json',
        isResource: true,
        jsResourceContent: themeUrlsMap
      });

    js_at.append(flow.tmpl.themeModule.ast, js_at.parse('(' + function(themes){
      /*global document, basis, getTheme, onThemeChange */
      var linkEl = document.getElementById('theme-style');
      var storage = global.localStorage || {};
      var inDom = !!linkEl;
      var head;

      var themeName = linkEl && linkEl.startupTheme;
      try { themeName = themeName || storage._basisjs_theme_; } catch(e) {}

      if (themes.indexOf(themeName) != -1)
        getTheme(themeName).apply();

      onThemeChange(function(name){
        var path = basis.resource('./_theme_css_').fetch()[name];
        try { storage._basisjs_theme_ = name; } catch(e) {}
        if (path)
        {
          if (!linkEl)
          {
            linkEl = document.createElement('link');
            linkEl.rel = 'stylesheet';
            linkEl.type = 'text/css';
            linkEl.media = 'all';
            (basis.doc ? basis.doc.head.ready : basis.ready)(function(){
              head = document.head || document.getElementByTagName('head')[0];
              head.appendChild(linkEl);
            });
          }

          if (path != linkEl.href.substr(linkEl.href.length - path.length))
            linkEl.href = path;
          if (head && !inDom)
            head.appendChild(linkEl);
        }
        else
        {
          if (inDom && linkEl && linkEl.parentNode)
            linkEl.parentNode.removeChild(linkEl);
        }
      }, null, true);
    } + ')(' + JSON.stringify(Object.keys(flow.tmpl.themes)) + ')'));
  }

  //
  // output files
  //

  flow.css.packages = queue.filter(function(file){
    if (file.type == 'style' &&
        file.htmlNode &&
        !file.outputFilename &&
        !file.conditional)
    {
      if (!file.inline || !file.htmlId)
        setOutputFilename(file, this);

      fconsole.log(file.relOutputFilename);

      return file;
    }
  }, targetMap).concat(packages);
};