Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@cyberalien/conditional-replacements

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@cyberalien/conditional-replacements

Replacements in code

  • 0.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

@cyberalien/conditional-replacements

This library is designed for build time import paths replacements.

It was created for Iconify Icon Finder build process, making it possible to substitute directories during build process. This makes it possible to use various language packs, icon packs, replace unused components with empty functions.

Replacements

Replacements class is for replacing imports.

Example of original code:

import { React } from 'react';
// @iconify-replacement: '/components/default/'
import { Input } from './components/default/input';
import { Button } from './components/default/button';
// @iconify-replacement: '/phrases/'
import { lang } from '../phrases/';

Parser:

import { readFileSync, writeFileSync } from 'fs';
import { Replacements } from '@cyberalien/conditional-replacements';

const replacements = new Replacements(
	{
		'/components/default/': '/components/figma/',
		'/phrases/': '/phrases_de/',
	},
	'@iconify-replacement'
);

// Parse ./lib/foo.js
let code = readFileSync('./lib/foo.js', 'utf8');
cost = replacements.parse(code);
writeFileSync('./lib/foo.js', code, 'utf8');

Parsed code:

import { React } from 'react';
import { Input } from './components/figma/input';
import { Button } from './components/figma/button';
import { lang } from '../phrases_de/';

In example above '/components/default/' was replaced with '/components/figma/', '/phrases/' was replaced with '/phrases_de/'.

Rules

  1. There can only be one replacement active at a time.

  2. Only lines following replacement comment are replaced. Any invalid line will break replacements.

Example:

// @iconify-replacement: '/components/default/'
import { Input } from './components/default/input';

import { Button } from './components/default/button';

will result in:

import { Input } from './components/figma/input';

import { Button } from './components/default/button';

Second import was not replaced because empty line above it broke pattern.

  1. Replacements are case sensitive.

License

This library is dual-licensed under Apache 2.0 and GPL 2.0 license. You may select, at your option, one of the above-listed licenses.

SPDX-License-Identifier: Apache-2.0 OR GPL-2.0

© 2020 Vjacheslav Trushkin

FAQs

Package last updated on 22 Jan 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc