Mirror: The magical sticky regex-based parser generator 🧙
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add option to Babel plugin to only minify tags

+53 -2
+5
src/babel/__snapshots__/plugin.test.js.snap
··· 32 32 };" 33 33 `; 34 34 35 + exports[`works while only minifying 1`] = ` 36 + "import match from 'reghex/macro'; 37 + const node = match('node')([\\"\\", \\"+|\\", \\"+(\\", \\"(\\", \\"?\\", \\"))*\\"], 1, 2, 3, 4, 5);" 38 + `; 39 + 35 40 exports[`works with local recursion 1`] = ` 36 41 "import { tag, _exec, _pattern } from 'reghex'; 37 42
+7 -2
src/babel/plugin.js
··· 1 1 import { makeHelpers } from './transform'; 2 2 3 - export default function reghexPlugin(babel) { 3 + export default function reghexPlugin(babel, opts = {}) { 4 4 let helpers; 5 5 6 6 return { ··· 10 10 helpers = makeHelpers(babel); 11 11 }, 12 12 ImportDeclaration(path) { 13 + if (opts.codegen === false) return; 13 14 helpers.updateImport(path); 14 15 }, 15 16 TaggedTemplateExpression(path) { 16 17 if (helpers.isMatch(path) && helpers.getMatchImport(path)) { 17 - helpers.transformMatch(path); 18 + if (opts.codegen === false) { 19 + helpers.minifyMatch(path); 20 + } else { 21 + helpers.transformMatch(path); 22 + } 18 23 } 19 24 }, 20 25 },
+18
src/babel/plugin.test.js
··· 16 16 ).toMatchSnapshot(); 17 17 }); 18 18 19 + it('works while only minifying', () => { 20 + const code = ` 21 + import match from 'reghex/macro'; 22 + 23 + const node = match('node')\` 24 + \${1}+ | \${2}+ (\${3} ( \${4}? \${5} ) )* 25 + \`; 26 + `; 27 + 28 + expect( 29 + transform(code, { 30 + babelrc: false, 31 + presets: [], 32 + plugins: [[reghexPlugin, { codegen: false }]], 33 + }).code 34 + ).toMatchSnapshot(); 35 + }); 36 + 19 37 it('works with local recursion', () => { 20 38 // NOTE: A different default name is allowed 21 39 const code = `
+23
src/babel/transform.js
··· 185 185 return id.name; 186 186 }, 187 187 188 + minifyMatch(path) { 189 + if (!path.node.tag.arguments.length) { 190 + throw path 191 + .get('tag') 192 + .buildCodeFrameError( 193 + 'match() must at least be called with a node name' 194 + ); 195 + } 196 + 197 + const quasis = path.node.quasi.quasis.map((x) => 198 + t.stringLiteral(x.value.cooked.replace(/\s*/g, '')) 199 + ); 200 + const expressions = path.node.quasi.expressions; 201 + const transform = this._prepareTransform(path); 202 + 203 + path.replaceWith( 204 + t.callExpression(path.node.tag, [ 205 + t.arrayExpression(quasis), 206 + ...expressions, 207 + ]) 208 + ); 209 + }, 210 + 188 211 transformMatch(path) { 189 212 if (!path.node.tag.arguments.length) { 190 213 throw path