From 702407607abe56a0ac89cd9b4bb2caf7fce424ce Mon Sep 17 00:00:00 2001 From: Ian Macalinao Date: Mon, 30 Nov 2020 15:52:27 -0600 Subject: [PATCH 1/4] Add opts.noReactAutoImport --- src/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 044d3fb..eb30e0d 100644 --- a/src/index.js +++ b/src/index.js @@ -48,7 +48,7 @@ export default declare(({ if (typeof importPath !== 'string') { throw new TypeError('`applyPlugin` `importPath` must be a string'); } - const { ignorePattern, caseSensitive, filename: providedFilename } = state.opts; + const { ignorePattern, caseSensitive, filename: providedFilename, noReactAutoImport } = state.opts; const { file, filename } = state; if (ignorePattern) { // Only set the ignoreRegex once: @@ -130,7 +130,7 @@ export default declare(({ if (typeof filename === 'undefined' && typeof opts.filename !== 'string') { throw new TypeError('the "filename" option is required when transforming code'); } - if (!path.scope.hasBinding('React')) { + if (!opts.noReactAutoImport && !path.scope.hasBinding('React')) { const reactImportDeclaration = t.importDeclaration([ t.importDefaultSpecifier(t.identifier('React')), ], t.stringLiteral('react')); From 011f3099262592c803018b8cd0d06338c783b418 Mon Sep 17 00:00:00 2001 From: Ian Macalinao Date: Mon, 30 Nov 2020 21:46:26 -0800 Subject: [PATCH 2/4] Run transforms before other plugins do --- src/index.js | 62 +++++++++++++++++++++++++++------------------------- 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/src/index.js b/src/index.js index eb30e0d..317a8e1 100644 --- a/src/index.js +++ b/src/index.js @@ -48,8 +48,8 @@ export default declare(({ if (typeof importPath !== 'string') { throw new TypeError('`applyPlugin` `importPath` must be a string'); } - const { ignorePattern, caseSensitive, filename: providedFilename, noReactAutoImport } = state.opts; - const { file, filename } = state; + const { ignorePattern, caseSensitive, filename: providedFilename } = state.opts; + const { filename } = state; if (ignorePattern) { // Only set the ignoreRegex once: ignoreRegex = ignoreRegex || new RegExp(ignorePattern); @@ -115,57 +115,59 @@ export default declare(({ const svgReplacement = buildSvg(opts); path.replaceWith(svgReplacement); } - file.get('ensureReact')(); - file.set('ensureReact', () => {}); } } return { visitor: { Program: { - enter(path, { file, opts, filename }) { + enter(rootPath, state) { + const { opts, filename, file } = state; if (typeof filename === 'string' && typeof opts.filename !== 'undefined') { throw new TypeError('the "filename" option may only be provided when transforming code'); } if (typeof filename === 'undefined' && typeof opts.filename !== 'string') { throw new TypeError('the "filename" option is required when transforming code'); } - if (!opts.noReactAutoImport && !path.scope.hasBinding('React')) { + + if (!opts.noReactAutoImport && !rootPath.scope.hasBinding('React')) { const reactImportDeclaration = t.importDeclaration([ t.importDefaultSpecifier(t.identifier('React')), ], t.stringLiteral('react')); file.set('ensureReact', () => { - const [newPath] = path.unshiftContainer('body', reactImportDeclaration); - newPath.get('specifiers').forEach((specifier) => { path.scope.registerBinding('module', specifier); }); + const [newPath] = rootPath.unshiftContainer('body', reactImportDeclaration); + newPath.get('specifiers').forEach((specifier) => { rootPath.scope.registerBinding('module', specifier); }); }); } else { file.set('ensureReact', () => {}); } + + rootPath.traverse({ + CallExpression(path) { + const { node } = path; + const requireArg = node.arguments.length > 0 ? node.arguments[0] : null; + const filePath = t.isStringLiteral(requireArg) ? requireArg.value : null; + if (node.callee.name === 'require' && t.isVariableDeclarator(path.parent) && filePath) { + applyPlugin(path.parent.id, filePath, path.parentPath.parentPath, state); + } + }, + ImportDeclaration(path) { + const { node } = path; + if (node.specifiers.length > 0) { + applyPlugin(node.specifiers[0].local, node.source.value, path, state); + } + }, + ExportNamedDeclaration(path) { + const { node } = path; + if (node.specifiers.length > 0 && node.specifiers[0].local && node.specifiers[0].local.name === 'default') { + const exportName = node.specifiers[0].exported.name; + applyPlugin(exportName, node.source.value, path, state, true, filename); + } + }, + }); }, }, - CallExpression(path, state) { - const { node } = path; - const requireArg = node.arguments.length > 0 ? node.arguments[0] : null; - const filePath = t.isStringLiteral(requireArg) ? requireArg.value : null; - if (node.callee.name === 'require' && t.isVariableDeclarator(path.parent) && filePath) { - applyPlugin(path.parent.id, filePath, path.parentPath.parentPath, state); - } - }, - ImportDeclaration(path, state) { - const { node } = path; - if (node.specifiers.length > 0) { - applyPlugin(node.specifiers[0].local, node.source.value, path, state); - } - }, - ExportNamedDeclaration(path, state) { - const { node } = path; - if (node.specifiers.length > 0 && node.specifiers[0].local && node.specifiers[0].local.name === 'default') { - const exportName = node.specifiers[0].exported.name; - const filename = parseFilename(node.source.value).name; - applyPlugin(exportName, node.source.value, path, state, true, filename); - } - }, }, }; }); From 50ea24d23a83a6c48deb3dad2cd56a0f9a04cb85 Mon Sep 17 00:00:00 2001 From: Ian Macalinao Date: Mon, 30 Nov 2020 21:54:10 -0800 Subject: [PATCH 3/4] fix lint errors --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 317a8e1..29c68d5 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,4 @@ -import { extname, dirname, parse as parseFilename } from 'path'; +import { extname, dirname } from 'path'; import { readFileSync } from 'fs'; import { parse } from '@babel/parser'; import { declare } from '@babel/helper-plugin-utils'; From aeb870767ae16a033efeccb9901f6e7849bdf5f2 Mon Sep 17 00:00:00 2001 From: Ian Macalinao Date: Mon, 30 Nov 2020 22:01:38 -0800 Subject: [PATCH 4/4] Fix tests --- src/index.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 29c68d5..cb629b0 100644 --- a/src/index.js +++ b/src/index.js @@ -49,7 +49,7 @@ export default declare(({ throw new TypeError('`applyPlugin` `importPath` must be a string'); } const { ignorePattern, caseSensitive, filename: providedFilename } = state.opts; - const { filename } = state; + const { file, filename } = state; if (ignorePattern) { // Only set the ignoreRegex once: ignoreRegex = ignoreRegex || new RegExp(ignorePattern); @@ -115,6 +115,9 @@ export default declare(({ const svgReplacement = buildSvg(opts); path.replaceWith(svgReplacement); } + + file.get('ensureReact')(); + file.set('ensureReact', () => {}); } }