All Projects → bradlc → Tinker.macro

bradlc / Tinker.macro

Evaluate Laravel code at build-time, via Laravel Tinker

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Tinker.macro

Babel Plugin Css Modules Transform
Extract css class names from required css module files, so we can render it on server.
Stars: ✭ 318 (+467.86%)
Mutual labels:  babel, babel-plugin
Faster.js
faster.js is a Babel plugin that compiles idiomatic Javascript to faster, micro-optimized Javascript.
Stars: ✭ 429 (+666.07%)
Mutual labels:  babel, babel-plugin
Babel Plugin Tailwind Components
Use Tailwind with any CSS-in-JS library
Stars: ✭ 320 (+471.43%)
Mutual labels:  babel, babel-plugin
Babel Plugin Root Import
Add the opportunity to import modules by the root path
Stars: ✭ 1,084 (+1835.71%)
Mutual labels:  babel, babel-plugin
Babel Plugin Styled Components
Improve the debugging experience and add server-side rendering support to styled-components
Stars: ✭ 878 (+1467.86%)
Mutual labels:  babel, babel-plugin
Babel Plugin Module Resolver
Custom module resolver plugin for Babel
Stars: ✭ 3,134 (+5496.43%)
Mutual labels:  babel, babel-plugin
I18nize React
Internationalize react apps within a lunch break
Stars: ✭ 389 (+594.64%)
Mutual labels:  babel, babel-plugin
babel-plugin-syntax-pipeline
Allow parsing of pipeline operator |>
Stars: ✭ 23 (-58.93%)
Mutual labels:  babel, babel-plugin
Babel Plugin Transform React Remove Prop Types
Remove unnecessary React propTypes from the production build. 🎈
Stars: ✭ 890 (+1489.29%)
Mutual labels:  babel, babel-plugin
Htm
Hyperscript Tagged Markup: JSX alternative using standard tagged templates, with compiler support.
Stars: ✭ 7,299 (+12933.93%)
Mutual labels:  babel, babel-plugin
Babel Plugin Console
Babel Plugin that adds useful build time console functions 🎮
Stars: ✭ 278 (+396.43%)
Mutual labels:  babel, babel-plugin
Xwasm
[Work In Progress] WebAssembly Packager and WASM tooling for modern frontend
Stars: ✭ 45 (-19.64%)
Mutual labels:  babel, babel-plugin
Babel Blade
(under new management!) ⛸️Solve the Double Declaration problem with inline GraphQL. Babel plugin/macro that works with any GraphQL client!
Stars: ✭ 266 (+375%)
Mutual labels:  babel, babel-plugin
Effectfuljs
JavaScript embedded effects compiler
Stars: ✭ 287 (+412.5%)
Mutual labels:  babel, babel-plugin
Grafoo
A GraphQL Client and Toolkit
Stars: ✭ 264 (+371.43%)
Mutual labels:  babel, babel-plugin
Babel Plugin React Remove Properties
Babel plugin for removing React properties. 💨
Stars: ✭ 327 (+483.93%)
Mutual labels:  babel, babel-plugin
babel-plugin-transform-html-import-to-string
Turn HTML imports (and export from) into constant strings
Stars: ✭ 22 (-60.71%)
Mutual labels:  babel, babel-plugin
babel-plugin-remove-test-ids
🐠 Babel plugin to strip `data-test-id` HTML attributes
Stars: ✭ 40 (-28.57%)
Mutual labels:  babel, babel-plugin
Babel Plugin Sitrep
Log all assignments and the return value of a function with a simple comment
Stars: ✭ 442 (+689.29%)
Mutual labels:  babel, babel-plugin
Postjss
Use the power of PostCSS in compiling with JSS
Stars: ✭ 40 (-28.57%)
Mutual labels:  babel, babel-plugin

tinker.macro

Evaluate Laravel code at build-time, via Laravel Tinker

Installation

Install babel-plugin-macros (along with tinker.macro) and add it to your babel config:

npm install --save-dev babel-plugin-macros tinker.macro

.babelrc:

{
  "plugins": ["macros"]
}

That’s it!

Basic Usage

import tinker from 'tinker.macro'
let isDebug = tinker`config('app.debug')`

// ↓ ↓ ↓ ↓ ↓ ↓ ↓

let isDebug = true

If you are executing a single function call you can import the function like this:

import { config } from 'tinker.macro'
let isDebug = config('app.debug')

// ↓ ↓ ↓ ↓ ↓ ↓ ↓

let isDebug = true

There is also a convenient way to access App\* class methods and properties:

import { Article } from 'tinker.macro'
let article = Article.where('id', 7).first()

// ↓ ↓ ↓ ↓ ↓ ↓ ↓

let article = {
  id: 7,
  title: 'Hello, world',
  body: 'Lorem ipsum dolor sit amet.'
  // etc.
}

This is the same as:

import tinker from 'tinker.macro'
let articles = tinker`App\\Article::where('id', 7)->first()`

More Examples

route function

import tinker, { route } from 'tinker.macro'
// these are equivalent
let articleRoute1 = route('article', { id: 1 })
let articleRoute2 = tinker`route('article', ['id' => 1])`

// ↓ ↓ ↓ ↓ ↓ ↓ ↓

let articleRoute1 = 'http://localhost/article/1'
let articleRoute2 = 'http://localhost/article/1'

Retrieving a list of named routes

import tinker from 'tinker.macro'

let routes = tinker`
  $routes = [];
  foreach (app()->routes->getRoutes() as $route) {
    $name = $route->getName();
    if ($name !== null) {
      $uri = $route->uri;
      $routes[$name] = ($uri === '/' ? '' : '/') . $uri;
    }
  }
  $routes;
`

// ↓ ↓ ↓ ↓ ↓ ↓ ↓

let routes = {
  home: '/',
  blog: '/blog',
  post: '/blog/{slug}'
}

Re-evaluating on change

If you are using webpack you can ensure that expressions are re-evaluated when updating your Laravel app. Add the loader – tinker.macro/webpack – to your webpack configuration, like so:

module.exports = {
  // ...
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'tinker.macro/webpack'
      }
      // ...
    ]
  }
  // ...
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].