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

transform-dwayne-js

v5.0.8

Published

The module is used for transforming javascript code with embedded Dwayne HTML and Javascript.

Downloads

2

Readme

transform-dwayne-html

The module is used for transforming javascript code with embedded Dwayne HTML and Javascript.

It's supposed to be used in loaders, plugins for bundlers, build systems, task runners and etc.

Installation

npm install --save transform-dwayne-js

Usage

const transformDwayneJs = require('transform-dwayne-js');

const js = `import { Block } from 'dwayne';

export default class MyBlock extends Block {
  static html = html\`
    <div>
      {text}
    </div>
  \`;
}
`;

console.log(transformDwayneJs(js));

// {
//   code: `var _tmpl;
//
//   import { Block } from 'dwayne';
//
//   export default class MyBlock extends Block {
//     static html = (_tmpl = [
//       {
//         type: "div",
//         children: [
//           {
//             type: "#text",
//             value: function (_) {
//               return _.text;
//             }
//           }
//         ]
//       }
//     ], _tmpl.vars = ["text"], _tmpl);
//   }`,
//   map: { ... }
// }

API

transformDwayneJs(code: string, options?: {
  unscopables?: string[] = ['require'],
  transformEmbeddedHtml?: boolean = true,
  transformEmbeddedHtmlScopeless?: boolean = true,
  transformEmbeddedJs?: boolean = true,
  transformJsx?: boolean = false,
  taggedHtmlFuncName?: string = 'html',
  taggedHtmlScopelessFuncName?: string = 'htmlScopeless',
  taggedJsFuncName?: string = 'js',
  sourceMap?: boolean = true,
  inputSourceMap?: SourceMap | null = null,
  filename?: string = 'unknown',
  indent?: number | string = 2,
  useES6?: boolean = false
}): {
  code: string,
  map: SourceMap | null
}

There are two types of options: ones that are used by loaders, plugins and etc (not by the end Dwayne user) and ones that are usually passed through.

Plugins options:

  • options.inputSourceMap (default: null): input sourcemap.
  • options.filename (default: 'unknown'): used for sourcemaps and __source args (see options.addSource in transform-dwayne-html).

Dwayne user options:

  • options.transformEmbeddedHtml (default: true): whether to transform html tagged expressions (see the examples below).
  • options.transformEmbeddedHtmlScopeless (default: true): whether to transform scopeless html tagged expressions (see the examples below).
  • options.transformEmbeddedJs (default: true): whether to transform js tagged expressions (see the examples below).
  • options.transformJsx (default: false): whether to transform jsx expressions (see the examples below).
  • options.taggedHtmlFuncName (default: html): function name for tagged html expressions.
  • options.taggedHtmlScopelessFuncName (default: htmlScopeless): function name for tagged scopeless html expressions.
  • options.taggedJsFuncName (default: js): function name for tagged js expressions.
  • options.unscopables (default: ['require']): passed to transform-dwayne-html and transform-dwayne-js-expressions.
  • options.sourceMap (default: true): whether the sourcemap should be generated (also passed to transform-dwayne-html and transform-dwayne-js-expressions).
  • options.indent (default: 2): output indent string. Number means that many spaces. Also passed to transform-dwayne-html.
  • options.useES6 (default: false): whether ES6 should be used in the output rather than ES5: let instead of var, arrow functions instead of usual functions. It's recommended setting this option to true and leave transformations to babel. Also passed to transform-dwayne-html and transform-dwayne-js-expressions.

All other options are as well passed to transform-dwayne-html and transform-dwayne-js-expressions. See additional options there.

Returns an object with following properties:

  • code: the output js code.
  • map: the output sourcemap.

Examples

Html tagged expression

Input:

const tmpl = html`
  <div Class:active={active}>
    <Block>
      {text}
    </Block>
  </div>
`;

Output:

var _tmpl, _mixin;

const tmpl = (_tmpl = [
  {
    type: "div",
    args: {
      "Class:active": (_mixin = function (_) {
        return _.active;
      }, _mixin.mixin = Class, _mixin.__source = "source.js:2:8", _mixin)
    },
    children: [
      {
        type: Block,
        args: {
          __source: "source.js:3:6"
        },
        children: [
          {
            type: "#text",
            value: function (_) {
              return _.text;
            }
          }
        ]
      }
    ]
  }
], _tmpl.vars = ["active", "text"], _tmpl);

Scopeless html tagged expression

Input:

function getTemplate() {
  return htmlScopeless`
    <div Class:active="{active}">
      <Block>
        {this.value + text}
      </Block>
    </div>
  `;
}

Output:

function getTemplate() {
  var _mixin,
      _this = this;

  return [
    {
      type: "div",
      args: {
        "Class:active": (_mixin = function () {
          return active;
        }, _mixin.mixin = Class, _mixin.__source = "source.js:3:10", _mixin)
      },
      children: [
        {
          type: Block,
          args: {
            __source: "source.js:4:8"
          },
          children: [
            {
              type: "#text",
              value: function () {
                return _this.value + text;
              }
            }
          ]
        }
      ]
    }
  ];
}

Js tagged expression

Input:

const expression = js`a + b`;

Output:

const expression = function (_) {
  return _.a + _.b;
};

Jsx template

Input:

const tmpl = (
  <div Class:active={active}>
    <Block>
      {text}
    </Block>
  </div>
);

Output:

var _tmpl, _mixin;

const tmpl = (_tmpl = [
  {
    type: "div",
    args: {
      "Class:active": (_mixin = function (_) {
        return _.active;
      }, _mixin.mixin = Class, _mixin.__source = "source.js:2:8", _mixin)
    },
    children: [
      {
        type: Block,
        args: {
          __source: "source.js:3:6"
        },
        children: [
          {
            type: "#text",
            value: function (_) {
              return _.text;
            }
          }
        ]
      }
    ]
  }
], _tmpl.vars = ["active", "text"], _tmpl);