From a3883f833033d747fef3e0b6cc68233c8bd68975 Mon Sep 17 00:00:00 2001 From: Conrad Buck Date: Wed, 25 Nov 2020 10:23:33 -0500 Subject: [PATCH] Allow macros to end in .cjs --- other/docs/author.md | 8 +++++--- src/index.js | 4 ++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/other/docs/author.md b/other/docs/author.md index b1467b1..d324b5b 100644 --- a/other/docs/author.md +++ b/other/docs/author.md @@ -56,15 +56,17 @@ There are two parts to the `babel-plugin-macros` API: The way that `babel-plugin-macros` determines whether to run a macro is based on the source string of the `import` or `require` statement. It must match this -regex: `/[./]macro(\.js)?$/` for example: +regex: `/[./]macro(\.c?js)?$/` for example: _matches_: ``` 'my.macro' 'my.macro.js' +'my.macro.cjs' 'my/macro' 'my/macro.js' +'my/macro.cjs' ``` _does not match_: @@ -305,8 +307,8 @@ Contributions to improve this experience are definitely welcome! ## Async logic -Unfortunately, babel plugins are synchronous so you can't do anything asynchronous -with `babel-plugin-macros`. However, you can cheat a bit by running +Unfortunately, babel plugins are synchronous so you can't do anything +asynchronous with `babel-plugin-macros`. However, you can cheat a bit by running `child_process`'s `spawnSync` to synchronously execute a file. It's definitely a hack and is not great for performance, but in most cases it's fast enough™️. diff --git a/src/index.js b/src/index.js index dbf8a0d..ffe3f6f 100644 --- a/src/index.js +++ b/src/index.js @@ -1,9 +1,9 @@ const p = require('path') const resolve = require('resolve') -const traverse = require("@babel/traverse").default +const traverse = require('@babel/traverse').default // const printAST = require('ast-pretty-print') -const macrosRegex = /[./]macro(\.js)?$/ +const macrosRegex = /[./]macro(\.c?js)?$/ const testMacrosRegex = v => macrosRegex.test(v) // https://stackoverflow.com/a/32749533/971592