Sweet.js: Get Started

This course provides an introduction to Sweet.js, a macro engine on top of JavaScript which allows you to extend the language as you desire.
Course info
Rating
(14)
Level
Beginner
Updated
Jul 19, 2014
Duration
3h 22m
Table of contents
Overview of Sweet.js
Writing Your First Macro
Advanced Macros
Polyfilling ECMAScript6
Operators
Integrating Sweet.js
Description
Course info
Rating
(14)
Level
Beginner
Updated
Jul 19, 2014
Duration
3h 22m
Description

Compile-to-JavaScript languages are popular, but sometimes these new languages are introducing features that you don't need, or don't address the problems which you want to solve. Sweet.js comes at it from another angle: rather than having a new language, it gives you the building blocks to make the extensions you need to solve your problems. Learn how to use macros to create specialized syntax and polyfill language-level features without an entirely new language.

About the author
About the author

Aaron is a Senior Developer and Technical Web Specialist with Readify and Microsoft MVP for Internet Explorer Development. Aaron focuses mainly on front-end web development, with a passion for JavaScript.

More from the author
Play by Play: JavaScript Security
Beginner
1h 13m
May 2, 2018
Play by Play: Azure Beyond Websites
Beginner
1h 16m
Apr 14, 2017
Using ASP.NET MVC with Umbraco
Intermediate
1h 34m
Sep 30, 2015
More courses by Aaron Powell
Section Introduction Transcripts
Section Introduction Transcripts

Overview of Sweet.js
Welcome to the Pluralsight course Sweet. js: Get Started. In this module we'll have an overview of what Sweet. js is and why it could be useful in the projects that you're building. First off, what is Sweet. js? Compile to languages are becoming a lot more popular with Javascript. These are often referred to as transpilation languages or use of them is done by a transpile. Some popular variants of these are CoffeeScript and TypeScript, which are different languages that can compile down into resulting Javascript. The main reason that these types of languages are becoming popular is that they can be used to address issues or at least perceived issues with the Javascript programming language and different communities of Javascript developers have different approaches to the way that they think these problems should be solved. Sweet. js, on the other hand, is not a full compile to language like TypeScript or CoffeeScript could be considered. Instead, it gives you the building blocks to make your own specified languages for the problems that you try to solve. These are done through the use of hygienic macros. Under the hood Sweet. js is still just plain old Javascript. You just write your normal Javascript as you would do as though you were not using a transpile. The only time that Sweet. js would come in is when you actually want to use a macro with inside of what you're actually building and to do something specific to the problem you're trying to solve. Sweet. js was initially started as an open-source project by a Mozilla intern and it is still sponsored by them to a certain degree. A Sweet. js is available as a Node. js package, which we'll be using throughout the rest of this course.

Advanced Macros
Welcome back to our Sweet. js Get Started course. In this third module we're going to be having a look at some advanced macros that we could write with Sweet. js and some advanced concepts that we can combine with Sweet. js macros to make them more powerful and easier to work with. The first advanced concept that I want to introduce is adding restrictions to the patterns that we're creating, so that we can make sure that we only are matching _____ be more specifically on the things that we want to match with inside of our pattern usage. The order of this is that we can create classes for the tokens that we're going to be working with, so the tokens are the variables that we're capturing with inside of our Sweet. js patterns. The reason we might want to do this is that we don't want to treat every single token with inside of our pattern in the same manner. By default we can use any kind of token classification with inside of our pattern and they'll be parsed as valid by Sweet. js, but we might not want that to happen. We might want to be able to treat the usage of a variable with inside of our Sweet. js macro differently to the way we do an assignment statement with inside the macro itself, so this is what we would want to use token classes to make sure that we do some restrictions and their incorrect usages can be caught at compile time. Ultimately the goal is that we can be able to do different things based on the types of tokens that we're capturing with inside of our macro, so some of these that we don't have to create as many different named macros to do what are similar things, instead we can have different types of patterns and classes of those tokens with inside the patterns to make sure that we're capturing and performing the operations we want to on the correct part of our macro definition.