Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
Size: Mime:
"use strict";

/**
 * !!!!!!!!!!!!!
 * @fileoverview
 *   this takes the variable name
 *   and then puts it into the `displayName` & `className` of the component
 *   and it caches the result
 *
 */
Object.defineProperty(exports, '__esModule', {
  value: true
}); // eslint-disable-next-line

exports.default = void 0;

const _path = require('path');

const _fs = require('fs');

const t = global._interopRequireWildcard(require('@babel/types'));

const _require = require("../utils/options"),
      useFileName = _require.useFileName,
      useDisplayName = _require.useDisplayName,
      useSSR = _require.useSSR;

const _getName = global._interopRequireDefault(require("../utils/getName"));

const _hash = global._interopRequireDefault(require("../utils/hash"));

const _require2 = require("../utils/detectors"),
      isStyled = _require2.isStyled; // this part is troublesome


function addConfig(path, displayName, componentId) {
  if (!displayName && !componentId) {
    return;
  }

  const withConfigProps = [];

  if (displayName) {
    withConfigProps.push(t.objectProperty(t.identifier('displayName'), t.stringLiteral(displayName)));
  } // Replace x`...` with x.withConfig({ })`...`


  if (componentId) {
    withConfigProps.push(t.objectProperty(t.identifier('componentId'), t.stringLiteral(componentId)));
  }

  if (path.node.tag && path.node.tag.property) {
    const property = path.node.tag.property.name; // console.log({ property })

    if (['css', 'global', 'cursorOnHover', 'local', 'once', 'hoist', 'auto', 'todo', 'keyframes', 'EMPTY', 'injectGlobal', 'toComponent'].includes(property)) {
      // console.log('IGNORED')
      return;
    }
  }

  path.node.tag = t.callExpression(t.memberExpression(path.node.tag, t.identifier('withConfig')), [t.objectExpression(withConfigProps)]);
}

function getBlockName(file) {
  const name = _path.basename(file.opts.filename, _path.extname(file.opts.filename));

  return name !== 'index' ? name : _path.basename(_path.dirname(file.opts.filename));
}

function getDisplayName(path, state) {
  const file = state.file;
  const componentName = (0, _getName.default)(path);

  if (file) {
    const blockName = getBlockName(file);

    if (blockName === componentName) {
      return componentName;
    }

    return componentName ? ''.concat(blockName, '__').concat(componentName) : blockName;
  } else {
    return componentName;
  }
}

function findModuleRoot(filename) {
  if (!filename) {
    return null;
  }

  const dir = _path.dirname(filename);

  if (_fs.existsSync(_path.join(dir, 'package.json'))) {
    return dir;
  } else if (dir !== filename) {
    // recursive tail call
    return findModuleRoot(dir);
  } else {
    return null;
  }
}

const FILE_HASH = 'styled-components-file-hash';
const COMPONENT_POSITION = 'styled-components-component-position';
const separatorRegExp = new RegExp('\\'.concat(_path.sep), 'g'); // eslint-disable-next-line

function getFileHash(state) {
  // hash calculation is costly due to fs operations, so we'll cache it per file.
  const file = state.file; // turning off cache
  // if (file.get(FILE_HASH)) {
  //   return file.get(FILE_HASH)
  // }
  // find module root directory

  const filename = file.opts.filename;
  const moduleRoot = findModuleRoot(filename);

  const filePath = moduleRoot && _path.relative(moduleRoot, filename).replace(separatorRegExp, '/');

  const moduleName = moduleRoot && JSON.parse(_fs.readFileSync(_path.join(moduleRoot, 'package.json'))).name;
  const code = file.code;
  const stuffToHash = [moduleName]; // console.log({ filePath })

  if (filePath) {
    stuffToHash.push(filePath); // if (filePath.includes('view-container')) {
    //   return
    // }
  } else {
    stuffToHash.push(code);
  }

  const fileHash = (0, _hash.default)(stuffToHash.join(''));
  file.set(FILE_HASH, fileHash);
  return fileHash;
}

function getNextId(state) {
  const id = state.file.get(COMPONENT_POSITION) || 0;
  state.file.set(COMPONENT_POSITION, id + 1);
  return id;
}

function getComponentId(state) {
  // Prefix the identifier with a character because CSS classes cannot start with a number
  return ''.concat(getFileHash(state).replace(/^(\d)/, 's$1'), '-').concat(getNextId(state));
}

function displayNameAndId(path, state) {
  if (isStyled(path.node.tag, state)) {
    const displayName = useDisplayName(state) && getDisplayName(path, useFileName(state) && state);
    const sanitized = displayName && displayName.replace(/[^_a-zA-Z0-9-]/g, ''); // console.log('is_styled: ', sanitized)

    const ssrIdentifier = useSSR(state) && getComponentId(state);
    addConfig(path, sanitized, ssrIdentifier);
  }
}

exports.default = displayNameAndId;
module.exports = displayNameAndId;