<commit_before>var through = require("through2");
var jsdom = require("jsdom");
var path = require("path");
var fs = require("fs");
var Moon = require("moonjs");
var defaultConfig = require("./src/config.js");
var util = require("./src/util.js");
var generateId = require("./src/id.js");
var addClass = require("./src/addClass.js");
var scopeStyle = require("./src/scopeStyle.js");

module.exports = function(file) {
    var base = path.basename(file);

    if(!/\.moon$/i.test(base)) {
      return through();
    }

    var input = "";
    var code = "";
    var componentName = base.replace(/\.[^/.]+$/, "");
    var id = generateId(componentName);
    var scopeClass = `m-scope-${id}`;
    var configPath = path.join(process.cwd(), "moonfile.js");
    var config = defaultConfig;

    // There is a config file
    if(fs.existsSync(configPath)) {
      util.extend(config, require(configPath));
    }

    var main = function(buf, enc, next) {
      input += buf;
      next();
    }

    var flush = function(next) {
      var stream = this;
      code += `var Moon; var componentName = "${componentName}"; var scopeId = "${scopeClass}";\nvar __moon__options__ = {};\n`;

      jsdom.env(input, function(err, window) {
        var template = window.document.querySelector("template");
        var script = window.document.querySelector("script");
        var style = window.document.querySelector("style");
        var scoped = null;

        if(style) {
          var lang = style.getAttribute("lang");
          scoped = style.getAttribute("scoped") !== null;

          if(lang) {
            style.innerHTML = config.compilers[lang](style.innerHTML);
          }

          style.innerHTML = style.innerHTML.replace(/\n/g, "");

          if(scoped) {
            style.innerHTML = scopeStyle(style.innerHTML, scopeClass);
          }

          code += `var insert = require('moonify/src/insert');\nvar removeStyle = insert(scopeId, ${JSON.stringify(style.innerHTML)});\n`;
        }

        if(script) {
          var lang = script.getAttribute("lang");
          if(lang) {
            script.innerHTML = config.compilers[lang](script.innerHTML);
          }
          code += `(function(exports) {${script.innerHTML}})(__moon__options__);`;
        }

        if(template) {
          var lang = template.getAttribute("lang");
          if(lang) {
            template.innerHTML = config.compilers[lang](template.innerHTML);
          }

          if(style && scoped) {
            addClass(template.content.childNodes, scopeClass);
          }

          var render = Moon.compile(`<div>${template.innerHTML}</div>`).toString();
          render.replace(/^function (anonymous)/, function(match, name) {
            render = render.replace(name, "");<commit_msg>better way of exporting and setting options via script<commit_after>var through = require("through2");
var jsdom = require("jsdom");
var path = require("path");
var fs = require("fs");
var Moon = require("moonjs");
var defaultConfig = require("./src/config.js");
var util = require("./src/util.js");
var generateId = require("./src/id.js");
var addClass = require("./src/addClass.js");
var scopeStyle = require("./src/scopeStyle.js");

module.exports = function(file) {
    var base = path.basename(file);

    if(!/\.moon$/i.test(base)) {
      return through();
    }

    var input = "";
    var code = "";
    var componentName = base.replace(/\.[^/.]+$/, "");
    var id = generateId(componentName);
    var scopeClass = `m-scope-${id}`;
    var configPath = path.join(process.cwd(), "moonfile.js");
    var config = defaultConfig;

    // There is a config file
    if(fs.existsSync(configPath)) {
      util.extend(config, require(configPath));
    }

    var main = function(buf, enc, next) {
      input += buf;
      next();
    }

    var flush = function(next) {
      var stream = this;
      code += `var Moon; var componentName = "${componentName}"; var scopeId = "${scopeClass}";\nvar __moon__options__ = {};\n`;

      jsdom.env(input, function(err, window) {
        var template = window.document.querySelector("template");
        var script = window.document.querySelector("script");
        var style = window.document.querySelector("style");
        var scoped = null;

        if(style) {
          var lang = style.getAttribute("lang");
          scoped = style.getAttribute("scoped") !== null;

          if(lang) {
            style.innerHTML = config.compilers[lang](style.innerHTML);
          }

          style.innerHTML = style.innerHTML.replace(/\n/g, "");

          if(scoped) {
            style.innerHTML = scopeStyle(style.innerHTML, scopeClass);
          }

          code += `var insert = require('moonify/src/insert');\nvar removeStyle = insert(scopeId, ${JSON.stringify(style.innerHTML)});\n`;
        }

        if(script) {
          var lang = script.getAttribute("lang");
          if(lang) {
            script.innerHTML = config.compilers[lang](script.innerHTML);
          }
          code += `__moon__options__ = (function(exports) {${script.innerHTML} return exports;})({});`;
        }

        if(template) {
          var lang = template.getAttribute("lang");
          if(lang) {
            template.innerHTML = config.compilers[lang](template.innerHTML);
          }

          if(style && scoped) {
            addClass(template.content.childNodes, scopeClass);
          }

          var render = Moon.compile(`<div>${template.innerHTML}</div>`).toString();
          render.replace(/^function (anonymous)/, function(match, name) {
            render = render.replace(name, "");
{'fraction_non_alphanumeric': 0.1638760242251514, 'fraction_numerical': 0.0003562522265764161, 'mean_word_length': 2.5560481317289425, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2901535', 'n_tokens_mistral': 1631, 'n_tokens_neox': 1612, 'n_words': 329}