borschik icon indicating copy to clipboard operation
borschik copied to clipboard

require-once flag

Open narqo opened this issue 13 years ago • 1 comments

We need an ability to prevent duplicates from processing twice.

Example for javascript tech:

// page.js
/* borschik:include('block1.js') */
/* borschik:include('block2.js') */
/* borschik:include('block3.js') */
// _page.js
(function() {
    this._ycssjs || (this._ycssjs = function(a, b) {
        return !(a in _ycssjs || _ycssjs[a]++)
    })
})();

if (_ycssjs('hash-sum-for-page.js-absolute-path') {
   if (_ycssjs('hash-sum-for-block1.js-absolute-path')) {
      // block1.js content
   }

   if (_ycssjs('hash-sum-for-block2.js-absolute-path')) {
      // block2.js content
   }

   if (_ycssjs('hash-sum-for-block3.js-absolute-path')) {
      // block3.js content
   }
}

The mechanism of guards could be more complex and wrap all blocks in lambdas, e.g. (function() { })() to prevent function/variables collapses.

narqo avatar Sep 27 '12 17:09 narqo

The similar technic could be implemented for CSS. This would be useful with dynamic CSS-bundles loading, to prevent from adding CSS-styles for blocks, that are already on the page.

/* page.css */
@import url(block-a.css);
@import url(block-b.css);
@import url(block-c.css);
/* _page.css */
._ycssjs_hash-sum-for-block-a.css { top:0 }
/* blocks-a.css content */

._ycssjs_hash-sum-for-block-b.css { top:0 }
/* blocks-b.css content */
...

The idea is to adding special class rule for every import. So we could check does block "A" is on the page with some JavaScript code:

// bundle-loader.js
var node = document.createElement('div')
node.className = '_ycssjs_hash-sum-for-block-a.css';

var root = document.documentElement;
root.insertBefore(node, root.firstChild);
if (window.getComputedStyle(document.body).getPropertyValue('top') !== "0") {
   // block isn't on the page
}

narqo avatar Sep 27 '12 18:09 narqo