Giter VIP home page Giter VIP logo

mopt's Introduction

mopt NPM Version NPM License

A babel plugin to statically optimize mithril hyperscript function invocations.

// This hyperscript function invocation
m(".fooga");

// Gets optimized into
m.vnode("div",undefined,{className:"fooga"},undefined,undefined,undefined);

Please file an issue if you come across any cases that this doesn't handle, I'd love to improve the number of structures I can rewrite!

Mithril Version Warning

mopt only works with [email protected].

For optimizing [email protected] see mithril-objectify.

Installation

Install with npm

npm i mopt

Usage with Babel

.babelrc

// .babelrc
{
    "plugins": [ "mopt" ]
}

CLI

$ babel --plugins mopt script.js

API

require("babel-core").transform("<code>", {
  plugins: [ "mopt" ]
});

Usage with a bundler

Browserify - babelify

WebPack - babel-loader

mopt's People

Contributors

greenkeeperio-bot avatar tivac avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

mopt's Issues

Doesn't work with ES6 imports?

I'm trying to use this as a browserify transform along with babelify but I'm running into some issues with ES6 imports.

This example works as expected:

const m = require('mithril')
const tags = m('.hello', m('a.world', {href: '#'}, 'Hello!'))
console.log(tags)

But transforms aren't applied for this example:

import m from 'mithril'
const tags = m('.hello', m('a.world', {href: '#'}, 'Hello!'))
console.log(tags)

Optimize m.component as first arg

Much like the just-added support for knowing that m.trust() is optimizable we should know that m.component() is optimizable as well. Not gonna get a ton of wins w/ that one, but should be pretty easy to add.

Stats

Would be neat to see stats output,

converted / total = % converted

for each file/thing being processed. Would need to be an option you can plumb through browserify/API/CLI.

use as browserify transform results in error

Create an empty file empty.js

$ browserify --version
5.11.0

$ browserify empty.js
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){

},{}]},{},[1]);

$ browserify -t mithril-objectify empty.js
/Users/sh/Work/tutory/webapp/node_modules/mithril-objectify/node_modules/falafel/node_modules/acorn/dist/acorn.js:1748
  throw err;
  ^

SyntaxError: Invalid regular expression flag (1:1)
    at Parser.pp.raise (/Users/sh/Work/tutory/webapp/node_modules/mithril-objectify/node_modules/falafel/node_modules/acorn/dist/acorn.js:1745:13)
    at Parser.pp.readRegexp (/Users/sh/Work/tutory/webapp/node_modules/mithril-objectify/node_modules/falafel/node_modules/acorn/dist/acorn.js:3527:38)
    at Parser.pp.readToken_slash (/Users/sh/Work/tutory/webapp/node_modules/mithril-objectify/node_modules/falafel/node_modules/acorn/dist/acorn.js:3318:28)
    at Parser.pp.getTokenFromCode (/Users/sh/Work/tutory/webapp/node_modules/mithril-objectify/node_modules/falafel/node_modules/acorn/dist/acorn.js:3456:19)
    at Parser.pp.readToken (/Users/sh/Work/tutory/webapp/node_modules/mithril-objectify/node_modules/falafel/node_modules/acorn/dist/acorn.js:3190:15)
    at Parser.pp.nextToken (/Users/sh/Work/tutory/webapp/node_modules/mithril-objectify/node_modules/falafel/node_modules/acorn/dist/acorn.js:3182:71)
    at parse (/Users/sh/Work/tutory/webapp/node_modules/mithril-objectify/node_modules/falafel/node_modules/acorn/dist/acorn.js:100:5)
    at module.exports (/Users/sh/Work/tutory/webapp/node_modules/mithril-objectify/node_modules/falafel/index.js:22:15)
    at transform (/Users/sh/Work/tutory/webapp/node_modules/mithril-objectify/index.js:9:12)
    at nr (/usr/local/lib/node_modules/browserify/node_modules/module-deps/index.js:230:23)

Wrong output with classes and ternary operator

Hello, I noticed a weird behaviour in my app after running mopt, which seems to involve classes determined by ternary operators. Here's a quick example to reproduce the issue:

Source code:

const root = document.body;
const states = [true, false, null];
const state = states[Math.floor(Math.random() * states.length)];

m.render(root, m('span', {
        className: state === true ? 'text-success' : state === false ? 'text-danger' : 'text-muted'
}, 'status'));

Transformed result:

var root = document.body;
var states = [true, false, null];
var state = states[Math.floor(Math.random() * states.length)];

m.render(root, m.vnode('span', undefined, undefined, undefined, 'status', undefined));

CLI error in Windows

Hi tivac,
I had some issues running mithril-objectify via CLI in Windows.

First I had to install it globally via npm install -g mithril-objectify to make it being recognized as command (is there another way to run it only locally inside the project?).

Then to make it work I had to change the mithril-objectify.cmd content from:

"%~dp0\node_modules\mithril-objectify\bin\cli.js"   %*

to

@IF EXIST "%~dp0\node.exe" (
  "%~dp0\node.exe"  "%~dp0\node_modules\mithril-objectify\bin\cli.js" %*
) ELSE (
  node  "%~dp0\node_modules\mithril-objectify\bin\cli.js" %*
)

Now it's working like a charm, I hope this could be useful.

String variables not correctly resolved

I have the following component which works without mopt enabled, but is broken with mopt:

const NEWEST = 'Newest first';
const VOTES = 'Most votes';
const SortToggle = {
    view({ attrs }) {
        return (
            m('.SortToggle',
                m('.SortToggle-option', { onclick() { attrs.sortBy(NEWEST); } }, NEWEST),
                m('.SortToggle-option', { onclick() { attrs.sortBy(MOST_VOTES); } }, MOST_VOTES)
            )
        );

    }
};

It works fine using mopt if I inline the string constants, like this:

const NEWEST = 'Newest first';
const VOTES = 'Most votes';
const SortToggle = {
    view({ attrs }) {
        return (
            m('.SortToggle',
                m('.SortToggle-option', { onclick() { attrs.sortBy(NEWEST); } }, 'Newest first'),
                m('.SortToggle-option', { onclick() { attrs.sortBy(MOST_VOTES); } }, 'Most votes')
            )
        );

    }
};

Allow to use it without babel

Hi Tivac.

Loved you module so far. Now as it requires babel to run not so much.

Would love allow to use it as plain browserify transform again.

PSA: I've added a bunch of default community health files org-wide

Community health files are stuff like CONTRIBUTING.md, FUNDING.yml, and so on, stuff that normally lives in .github, docs/, or the repo's root. I've added default files for everything on this list of supported file types except for a CONTRIBUTING.md file, as that process varies pretty greatly across repos. For issue templates, there's two templates: one for bugs and one for feature requests. The pull request template and issue templates are each derived from the core project's own core templates but with core-specific stuff omitted.

No action is required on your part, but you may wish to customize these appropriately and/or take other related action in light of this like adding/removing issue templates.


If you have any questions, comments, or concerns, please file an issue and I'd be more than willing to address them.

Wrong output with classes with template string and ternary operator

Version: 5.0.2

code:

m(
    "p",
    {
        className: `mithril-Pager-page-arrow ${vnode.attrs.currentPage > 1 ? "" : "mithril-Pager-page-arrow--disabled"}`
    },
    "test"
)

In the output className is not included.

For repo: https://github.com/gyandeeps/mithril-hn
File: https://github.com/gyandeeps/mithril-hn/blob/82879b862da659491d441837f9953f40337ed033/src/js/components/Pager.js

Notes:

  • If I don't use template string then it works.
    className: "mithril-Pager-page-arrow " + (vnode.attrs.currentPage > 1 ? "" : "mithril-Pager-page-arrow--disabled")

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.