Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support pre-transpiled code (#379) #380

Merged
merged 1 commit into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/utils/detectors.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import getSequenceExpressionValue from './getSequenceExpressionValue'
import { useTopLevelImportPathMatchers } from './options'

const VALID_TOP_LEVEL_IMPORT_PATH_MATCHERS = [
Expand Down Expand Up @@ -81,6 +82,15 @@ export const isStyled = t => (tag, state) => {
) {
// styled.something()
return isStyled(t)(tag.callee.object, state)
} else if (
t.isCallExpression(tag) &&
t.isSequenceExpression(tag.callee) &&
t.isMemberExpression(getSequenceExpressionValue(tag.callee)) &&
getSequenceExpressionValue(tag.callee).property.name !==
'default' /** ignore default for #93 below */
) {
// (..., styled).something()
return isStyled(t)(getSequenceExpressionValue(tag.callee), state)
} else {
return (
(t.isMemberExpression(tag) &&
Expand All @@ -94,6 +104,12 @@ export const isStyled = t => (tag, state) => {
importLocalName('default', state, {
cacheIdentifier: tag.callee.name,
})) ||
(t.isCallExpression(tag) &&
t.isSequenceExpression(tag.callee) &&
getSequenceExpressionValue(tag.callee).name ===
importLocalName('default', state, {
cacheIdentifier: getSequenceExpressionValue(tag.callee).name,
})) ||
/**
* #93 Support require()
* styled-components might be imported using a require()
Expand All @@ -111,6 +127,13 @@ export const isStyled = t => (tag, state) => {
t.isMemberExpression(tag.callee) &&
tag.callee.property.name === 'default' &&
tag.callee.object.name === state.styledRequired) ||
(state.styledRequired &&
t.isCallExpression(tag) &&
t.isSequenceExpression(tag.callee) &&
t.isMemberExpression(getSequenceExpressionValue(tag.callee)) &&
getSequenceExpressionValue(tag.callee).property.name === 'default' &&
getSequenceExpressionValue(tag.callee).object.name ===
state.styledRequired) ||
(importLocalName('default', state) &&
t.isMemberExpression(tag) &&
t.isMemberExpression(tag.object) &&
Expand Down
4 changes: 4 additions & 0 deletions src/utils/getSequenceExpressionValue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default path =>
path.expressions && path.expressions.length
? path.expressions[path.expressions.length - 1]
: undefined
23 changes: 18 additions & 5 deletions src/visitors/assignStyledRequired.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
import { isValidTopLevelImport } from '../utils/detectors'

export default t => (path, state) => {
let init = path.node.init;

if (
t.isCallExpression(path.node.init) &&
t.isIdentifier(path.node.init.callee) &&
path.node.init.callee.name === 'require' &&
path.node.init.arguments &&
path.node.init.arguments[0] &&
t.isLiteral(path.node.init.arguments[0]) &&
isValidTopLevelImport(path.node.init.arguments[0].value, state)
init.callee.name === '_interopRequireDefault' &&
init.arguments &&
init.arguments[0]
) {
// _interopRequireDefault(require())
init = path.node.init.arguments[0];
}

if (
t.isCallExpression(init) &&
t.isIdentifier(init.callee) &&
init.callee.name === 'require' &&
init.arguments &&
init.arguments[0] &&
t.isLiteral(init.arguments[0]) &&
isValidTopLevelImport(init.arguments[0].value, state)
) {
state.styledRequired = path.node.id.name
}
Expand Down
13 changes: 13 additions & 0 deletions test/fixtures/pre-transpiled/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"plugins": [
[
"../../../src",
{
"fileName": false,
"transpileTemplateLiterals": false,
"ssr": true
}
],
["@babel/plugin-proposal-class-properties", { "loose": true }]
]
}
5 changes: 5 additions & 0 deletions test/fixtures/pre-transpiled/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var _styled = _interopRequireDefault(require("styled-components"));

const Test = (0, _styled.default)('div')({
width: '100%',
});
8 changes: 8 additions & 0 deletions test/fixtures/pre-transpiled/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var _styled = _interopRequireDefault(require("styled-components"));

const Test = (0, _styled.default)('div').withConfig({
displayName: "Test",
componentId: "sc-1m3e07f-0"
})({
width: '100%'
});