npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

xml-to-react-loader

v1.0.0

Published

Webpack loader designed to parse an svg file into a an abstract React component.

Downloads

8

Readme

XML to React Loader

A bundle loader intended to take XML files as input and produce abstract React components to render in React-style applications.

Unlike other loaders, this one does not assume the usage of React primitives for components. You can use your own Components or leave them as is. In addition, no transpiling should be required. This loader automatically generates valid JavaScript syntax that does not need to go through a jsx tool.

Setup

Add this loader as a rule for XML or XML derived files in your setup.

{
  module: {
    rules: [
      {
        test: /\.(svg|xml)$/,
        use: {
          loader: 'xml-to-react-loader'
        }
      }
    ]
  }
}

Alternatively you can manually invoke this loader on specific files

import Component from "!xml-to-react-loader!./path/to/file.xml";

Examples

If this is your xml file:

<note date="2020-07-07T23:04:29+00:00" author="John Doe">
  <heading>Reminder</heading>
  <body>Don't forget <bold>me</bold> this weekend!</body>
</note>

It will produce a JavaScript file roughly equivalent to the following:

import React from 'react';

export const Component = (
  getComponent: (tagname: string) => React.ReactNode,
  ...props: Record<string, unknown>
): React.FunctionComponent => {
  const Note = getComponent ? getComponent('node') : 'note';
  const Heading = getComponent ? getComponent('heading') : 'heading';
  const Body = getComponent ? getComponent('body') : 'body';
  const Bold = getComponent ? getComponent('bold') : 'body';

  return (
    <Note date="2020-07-07T23:04:29+00:00" author="John Doe" {...props}>
      <Heading>Reminder</Heading>
      <Body>Don&comma;t forget <Bold>me</Bold> this weekend!</Body>
    </Note>
  );
}

export default Component;

export const rootAttributes = {
  author: "John Doe",
  date: "2020-07-07T23:04:29+00:00",
};

Note: There is no need to transform the JSX syntax, this loader does that automatically. The JSX syntax in this example is preserved to enhance readability.

Usage

import NoteComponent from 'note.xml';

const tagMappings = {
  heading: 'h1',
  note: (props) => (
    <html>
      <body>
        <div {...props} />
      </body>
    </html>
  ),
  bold: 'b',
};

const Component = (props: {}) => (
  <NoteComponent
    getComponent={(tag) => tagMappings[tag] || 'p'}
    {...props}
  />
);

Because it is common that React primitive elements will not match your xml file 1:1, every component can take a getComponent prop to get your appropriate component for each tag.

It is simply a function that will be called with the tag string to get a component for, and should return something React can create, which means probably a string or Component.

Options

This loader accepts one option.

type Options = {
  reactPath?: string;
}

The option reactPath can be set if you have a unique path to react, the only required peer dependency. If this option is omitted the usual process of using require.context to get the path will be used. In most cases this should be sufficient.

TypeScript

This project is written and maintained in TypeScript. Because loaders are not directly a part of TypeScript, typings can be difficult. This module exports the appropriate types to expect from a file emitted from the loader, you will just need to hook up the definitions according to how you import them.

// your-typings-file.d.ts

import { XmlToReactLoaderExport } from 'xml-to-react-loader';

declare module '*.xml' {
  const _: XmlToReactLoaderExport;
  export = _;
}

// or

declare module '!xml-to-react-loader!*' {
  const _: XmlToReactLoaderExport;
  export = _;
}