From c74437c1aa9a8f9b4d639285e81c991111030a8a Mon Sep 17 00:00:00 2001 From: Evan Jacobs Date: Fri, 25 Feb 2022 09:03:53 -0500 Subject: [PATCH] Revert "one more tweak for babel macro" This reverts commit ae36dfe568d4648e7f843838104c92eb5544fc6b. --- src/utils/detectors.js | 7 ++++--- src/visitors/transpileCssProp.js | 6 ++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/utils/detectors.js b/src/utils/detectors.js index e918470..834b77f 100644 --- a/src/utils/detectors.js +++ b/src/utils/detectors.js @@ -21,10 +21,11 @@ export const importLocalName = (name, state, options = {}) => { const cacheKeyAffix = cacheIdentifier ? `|${cacheIdentifier}` : '' const cacheKey = name + state.file.opts.filename + cacheKeyAffix - if (state.customImportName && !localNameCache[cacheKey]) { - return state.customImportName.name - } else if (!bypassCache && localNameCache[cacheKey]) { + if (!bypassCache && cacheKey in localNameCache) { return localNameCache[cacheKey] + // state.customImportName is injected by the babel macro + } else if (state.customImportName) { + return state.customImportName.name } let localName = state.styledRequired diff --git a/src/visitors/transpileCssProp.js b/src/visitors/transpileCssProp.js index 6e4053e..930fcf4 100644 --- a/src/visitors/transpileCssProp.js +++ b/src/visitors/transpileCssProp.js @@ -44,12 +44,14 @@ export default t => (path, state) => { const program = state.file.path - let importName = importLocalName('default', state) + // state.customImportName is passed through from styled-components/macro if it's used + // since the macro also inserts the import + let importName = state.customImportName || importLocalName('default', state) const { bindings } = program.scope // Insert import if it doesn't exist yet - if (!importName) { + if (!importName || !bindings[importName.name] || !bindings[importName]) { addDefault(path, 'styled-components', { nameHint: 'styled', })