<commit_before>/* @flow */

const { createFilter } = require('rollup-pluginutils');
const transform = require('./transform');
const slugify = require('./slugify');

/* ::
type RollupPluginOptions = {
  include?: string | string[],
  exclude?: string | string[],
  sourceMap?: boolean,
  evaluate?: boolean,
  displayName?: boolean,
}
*/

module.exports = function linaria({
  include,
  exclude,
  sourceMap,
  ...rest
} /* :RollupPluginOptions */ = {}) {
  const filter = createFilter(include, exclude);
  const cssLookup = {};

  return {
    name: 'linaria',
    load(id /* :string */) {
      return cssLookup[id];
    },
    /* eslint-disable-next-line consistent-return */
    resolveId(importee /* :string */) {
      if (importee in cssLookup) return importee;
    },
    transform(code /* :string */, id /* :string */) {
      if (!filter(id)) return;

      const result = transform(id, code, rest);

      if (!result.cssText) return;

      let { cssText } = result;

      const slug = slugify(id);
      const filename = `${id.replace(/\.js$/, '')}_${slug}.css`;

      if (sourceMap && result.cssSourceMapText) {
        const map = Buffer.from(result.cssSourceMapText).toString('base64');
        cssText += `/*# sourceMappingURL=data:application/json;base64,${map}*/`;
      }

      cssLookup[filename] = cssText;

      result.code += `\nimport ${JSON.stringify(filename)};\n`;

      /* eslint-disable-next-line consistent-return */
      return result.code;
    },
  };
};<commit_msg>feat: add a sourcemap to rollup output (#272)<commit_after>/* @flow */

const { createFilter } = require('rollup-pluginutils');
const transform = require('./transform');
const slugify = require('./slugify');

/* ::
type RollupPluginOptions = {
  include?: string | string[],
  exclude?: string | string[],
  sourceMap?: boolean,
  evaluate?: boolean,
  displayName?: boolean,
}
*/

module.exports = function linaria({
  include,
  exclude,
  sourceMap,
  ...rest
} /* :RollupPluginOptions */ = {}) {
  const filter = createFilter(include, exclude);
  const cssLookup = {};

  return {
    name: 'linaria',
    load(id /* :string */) {
      return cssLookup[id];
    },
    /* eslint-disable-next-line consistent-return */
    resolveId(importee /* :string */) {
      if (importee in cssLookup) return importee;
    },
    transform(code /* :string */, id /* :string */) {
      if (!filter(id)) return;

      const result = transform(id, code, rest);

      if (!result.cssText) return;

      let { cssText } = result;

      const slug = slugify(id);
      const filename = `${id.replace(/\.js$/, '')}_${slug}.css`;

      if (sourceMap && result.cssSourceMapText) {
        const map = Buffer.from(result.cssSourceMapText).toString('base64');
        cssText += `/*# sourceMappingURL=data:application/json;base64,${map}*/`;
      }

      cssLookup[filename] = cssText;

      result.code += `\nimport ${JSON.stringify(filename)};\n`;

      /* eslint-disable-next-line consistent-return */
      return { code: result.code, map: result.sourceMap };
    },
  };
};
{'fraction_non_alphanumeric': 0.17495934959349593, 'fraction_numerical': 0.0035772357723577236, 'mean_word_length': 2.9741602067183464, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4758438', 'n_tokens_mistral': 968, 'n_tokens_neox': 945, 'n_words': 206}