diff --git a/README.md b/README.md index 134248e..c1d7c17 100644 --- a/README.md +++ b/README.md @@ -142,3 +142,32 @@ module.exports = { } ``` + +for customized exported(re-exported) styled + +```ts +interface CustomModule { + // module name used in import statement + moduleName: string + // `true` if you may import libs from 'my-emotion/subpath' + includesSubPath?: boolean + // all available names exported from custom module + exportedNames: string[] + // we may do some additional work on styled function, so if styled is reexport, you should specify it here + styledName?: string + // has default export + hasDefaultExport?: boolean +} + +createEmotionPlugin({ + ...otherConfig, + customModules: [ + { + moduleName: 'my-emotion', + includesSubPath: true, + exportedNames: ['myStyled', 'myCss'] + styledName: 'myStyled', + } + ] +}) +``` \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 72417a9..77d60aa 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,23 +7,43 @@ import findRoot from 'find-root' const hashString = require('@emotion/hash').default +interface ModuleConfig { + moduleName: string + includesSubPath?: boolean + exportedNames: string[] + styledName?: string + hasDefaultExport?: boolean +} + +interface ImportInfo extends ModuleConfig { + name: string + type: 'namedImport' | 'namespaceImport' | 'defaultImport' +} + export interface Options { sourcemap?: boolean autoLabel?: boolean labelFormat?: string autoInject?: boolean - customStyledModuleName?: string + customModules?: ModuleConfig[] } -export interface ImportInfos { - name: string - type: 'namedImport' | 'namespaceImport' | 'defaultImport' - moduleName: string -} - -const hasDefaultExports = ['@emotion/styled'] -const libraries = ['@emotion/styled', 'emotion', '@emotion/core'] -const functions = ['css', 'keyframes', 'injectGlobal', 'merge'] +const defaultEmotionModules: ModuleConfig[] = [ + { + moduleName: 'emotion', + exportedNames: ['css', 'keyframes', 'injectGlobal', 'cx', 'merge'], + }, + { + moduleName: '@emotion/styled', + exportedNames: ['styled'], + hasDefaultExport: true, + styledName: 'styled', + }, + { + moduleName: '@emotion/core', + exportedNames: ['css'], + }, +] const defaultOptions: Options = { sourcemap: true, @@ -57,12 +77,85 @@ const createImportJSXAst = memoize((propertyName: string | undefined) => { ) }) -export const createEmotionPlugin = (options?: Options) => { - const notNullOptions = options - ? { ...defaultOptions, ...options } - : { ...defaultOptions } +export const createEmotionPlugin = (pluginOptions?: Options) => { + const options = { ...defaultOptions, ...pluginOptions } + const modules = new Map( + defaultEmotionModules + .concat(options.customModules || []) + .map((m) => [m.moduleName, m]), + ) + + function getImportCalls( + importDeclarationNode: ts.ImportDeclaration, + compilerOptions: ts.CompilerOptions, + ) { + const importCalls: ImportInfo[] = [] + const moduleName = (importDeclarationNode.moduleSpecifier) + .text + if (!importDeclarationNode.importClause) { + return importCalls + } + const { name, namedBindings } = importDeclarationNode.importClause! + for (const moduleInfo of modules.values()) { + if ( + moduleInfo.moduleName === moduleName || + (moduleInfo.includesSubPath && + moduleName.includes(moduleInfo.moduleName + '/')) + ) { + // import lib from 'lib' + if (name) { + if (moduleInfo.hasDefaultExport) { + importCalls.push({ + name: name.text, + type: 'defaultImport', + ...moduleInfo, + }) + } else if (compilerOptions.allowSyntheticDefaultImports) { + // treat it as import * as emotion from 'emotion' + importCalls.push({ + name: name.text, + type: 'namespaceImport', + ...moduleInfo, + }) + } + } + + if (namedBindings) { + // import { xxx } from 'lib' + if (ts.isNamedImports(namedBindings)) { + namedBindings.elements.forEach((node) => { + // import { default as lib, a as alias, b } from 'lib' + if ( + // propertyName's existence means alias + node.propertyName + ? moduleInfo.exportedNames.includes(node.propertyName.text) || + (node.propertyName.text === 'default' && + moduleInfo.hasDefaultExport) + : moduleInfo.exportedNames.includes(node.name.text) + ) { + importCalls.push({ + name: node.name.text, + type: 'namedImport', + ...moduleInfo, + }) + } + }) + } else if (ts.isNamespaceImport(namedBindings)) { + // import * as xxx from 'lib' + importCalls.push({ + name: namedBindings.name!.text, + type: 'namespaceImport', + ...moduleInfo, + }) + } + } + } + } + return importCalls + } + const transformer: ts.TransformerFactory = (context) => { - let importCalls: ImportInfos[] = [] + let importCalls: ImportInfo[] = [] const compilerOptions = context.getCompilerOptions() let sourcemapGenerator: SourceMapGenerator let emotionTargetClassNameCount = 0 @@ -78,7 +171,7 @@ export const createEmotionPlugin = (options?: Options) => { // insert import { jsx [as jsxFactory] } from '@emotion/core' behind the react import declaration if ( !inserted && - notNullOptions.autoInject && + options.autoInject && (node.moduleSpecifier).text === 'react' ) { inserted = true @@ -87,7 +180,7 @@ export const createEmotionPlugin = (options?: Options) => { return node } - if (notNullOptions.autoLabel || notNullOptions.sourcemap) { + if (options.autoLabel || options.sourcemap) { if (ts.isCallExpression(node)) { let { expression } = node if ( @@ -100,19 +193,21 @@ export const createEmotionPlugin = (options?: Options) => { : (expression as ts.CallExpression | ts.PropertyAccessExpression) let transformedNode = node let updateCallFunction = () => transformedNode + // styled.div({}) => styled('div')({}) if (ts.isPropertyAccessExpression(expression)) { - const info = importCalls.find( - (importInfo) => - importInfo.name === - ((expression as ts.PropertyAccessExpression) - .expression as ts.Identifier).text, - ) - if ( - info && - (info.moduleName === '@emotion/styled' || - (notNullOptions.customStyledModuleName != undefined && - notNullOptions.customStyledModuleName === info.moduleName)) - ) { + const info = importCalls.find((importInfo) => { + const expr = (expression as ts.PropertyAccessExpression) + .expression + return ( + importInfo.styledName === + (ts.isIdentifier(expr) + ? expr.text + : ts.isPropertyAccessExpression(expr) + ? expr.name.text + : '') + ) + }) + if (info) { expression = ts.createCall( expression.expression, [], @@ -123,7 +218,7 @@ export const createEmotionPlugin = (options?: Options) => { const exp = ts.isCallExpression(expression) ? expression : null if (exp) { updateCallFunction = () => { - if (exp.arguments.length === 1) { + if (exp.arguments.length >= 1) { const filename = sourceFile.fileName let moduleName = '' let rootPath = filename @@ -152,22 +247,28 @@ export const createEmotionPlugin = (options?: Options) => { const stableClassName = `e${hashArray( stuffToHash, )}${positionInFile}` + const [el, opts] = exp.arguments + const targetAssignment = ts.createPropertyAssignment( + ts.createIdentifier('target'), + ts.createStringLiteral(stableClassName), + ) + const args = [el] + args.push( + ts.createObjectLiteral( + opts && ts.isObjectLiteralExpression(opts) + ? opts.properties.concat(targetAssignment) + : [targetAssignment], + true, + ), + ) + const updatedCall = ts.updateCall( exp, exp.expression, exp.typeArguments, - exp.arguments.concat([ - ts.createObjectLiteral( - [ - ts.createPropertyAssignment( - ts.createIdentifier('target'), - ts.createStringLiteral(stableClassName), - ), - ], - true, - ), - ]), + args, ) + return ts.updateCall( transformedNode, updatedCall, @@ -192,88 +293,73 @@ export const createEmotionPlugin = (options?: Options) => { (subExpression.expression as ts.Identifier).text, ) if (importedInfo) { - const propertyToAccess = - importedInfo.type === 'namespaceImport' - ? ( - (expression as ts.PropertyAccessExpression).name || - (subExpression as ts.PropertyAccessExpression).name - ).text - : '' - const isEmotionCall = - (importedInfo.type === 'namespaceImport' && - (propertyToAccess === 'default' || - functions.includes(propertyToAccess))) || - importedInfo.type !== 'namespaceImport' - - if (isEmotionCall) { - if (notNullOptions.autoLabel) { - const rawPath = sourceFile.fileName - const localNameNode = (node.parent as ts.VariableDeclaration) - .name - if (localNameNode && ts.isIdentifier(localNameNode)) { - const local = localNameNode.text - const fileName = basename(rawPath, extname(rawPath)) - transformedNode = ts.updateCall( - transformedNode, - transformedNode.expression, - transformedNode.typeArguments, - transformedNode.arguments.concat([ - ts.createStringLiteral( - `label:${notNullOptions - .labelFormat!.replace('[local]', local) - .replace('[filename]', fileName)};`, - ), - ]), - ) - } - } - if ( - notNullOptions.sourcemap && - process.env.NODE_ENV !== 'production' - ) { - const sourceFileNode = node.getSourceFile() - const lineAndCharacter = ts.getLineAndCharacterOfPosition( - sourceFileNode, - node.pos, - ) - const sourceFileName = relative( - process.cwd(), - sourceFileNode.fileName, - ) - sourcemapGenerator.addMapping({ - generated: { - line: 1, - column: 0, - }, - source: sourceFileName, - original: { - line: lineAndCharacter.line + 1, - column: lineAndCharacter.character + 1, - }, - }) - sourcemapGenerator.setSourceContent( - sourceFileName, - sourceFileNode.text, - ) - const comment = convert - .fromObject(sourcemapGenerator) - .toComment({ multiline: true }) + if (options.autoLabel) { + const rawPath = sourceFile.fileName + const localNameNode = (node.parent as ts.VariableDeclaration) + .name + if (localNameNode && ts.isIdentifier(localNameNode)) { + const local = localNameNode.text + const fileName = basename(rawPath, extname(rawPath)) transformedNode = ts.updateCall( transformedNode, transformedNode.expression, transformedNode.typeArguments, transformedNode.arguments.concat([ - ts.createStringLiteral(comment), + ts.createStringLiteral( + `label:${options + .labelFormat!.replace('[local]', local) + .replace('[filename]', fileName)};`, + ), ]), ) } - transformedNode = ts.addSyntheticLeadingComment( + } + if ( + options.sourcemap && + process.env.NODE_ENV !== 'production' + ) { + const sourceFileNode = node.getSourceFile() + const lineAndCharacter = ts.getLineAndCharacterOfPosition( + sourceFileNode, + node.pos, + ) + const sourceFileName = relative( + process.cwd(), + sourceFileNode.fileName, + ) + sourcemapGenerator.addMapping({ + generated: { + line: 1, + column: 0, + }, + source: sourceFileName, + original: { + line: lineAndCharacter.line + 1, + column: lineAndCharacter.character + 1, + }, + }) + sourcemapGenerator.setSourceContent( + sourceFileName, + sourceFileNode.text, + ) + const comment = convert + .fromObject(sourcemapGenerator) + .toComment({ multiline: true }) + transformedNode = ts.updateCall( transformedNode, - ts.SyntaxKind.MultiLineCommentTrivia, - '#__PURE__', + transformedNode.expression, + transformedNode.typeArguments, + transformedNode.arguments.concat([ + ts.createStringLiteral(comment), + ]), ) - return updateCallFunction() } + transformedNode = ts.addSyntheticLeadingComment( + transformedNode, + ts.SyntaxKind.MultiLineCommentTrivia, + '#__PURE__', + ) + return updateCallFunction() } } } @@ -295,88 +381,3 @@ export const createEmotionPlugin = (options?: Options) => { } return transformer } - -function getImportCalls( - importDeclarationNode: ts.ImportDeclaration, - compilerOptions: ts.CompilerOptions, -) { - const importCalls: ImportInfos[] = [] - const moduleName = (importDeclarationNode.moduleSpecifier) - .text - if (!importDeclarationNode.importClause) { - return importCalls - } - const { name, namedBindings } = importDeclarationNode.importClause! - if (libraries.includes(moduleName)) { - if (name) { - // import emotion from 'emotion' - // treat it as import * as emotion from 'emotion' - if ( - moduleName === 'emotion' && - compilerOptions.allowSyntheticDefaultImports - ) { - importCalls.push({ - name: name.text, - type: 'namespaceImport', - moduleName, - }) - } else if (hasDefaultExports.includes(moduleName)) { - importCalls.push({ - name: name.text, - type: 'defaultImport', - moduleName, - }) - } - } - if (namedBindings) { - if (ts.isNamedImports(namedBindings)) { - namedBindings.forEachChild((node) => { - // import { default as styled } from '@emotion/styled' - // push styled into importCalls - if ( - hasDefaultExports.includes(moduleName) && - (node as ts.ImportSpecifier).propertyName && - (node as ts.ImportSpecifier).propertyName!.text === 'default' - ) { - importCalls.push({ - name: (node as ts.ImportSpecifier).name!.text, - type: 'namedImport', - moduleName, - }) - } - // import { css as emotionCss } from 'lib in libraries' - // push emotionCss into importCalls - if ( - (node as ts.ImportSpecifier).propertyName && - functions.includes((node as ts.ImportSpecifier).propertyName!.text) - ) { - importCalls.push({ - name: (node as ts.ImportSpecifier).name!.text, - type: 'namedImport', - moduleName, - }) - } - // import { css } from 'lib in libraries' - // push css into importCalls - if ( - !(node as ts.ImportSpecifier).propertyName && - functions.includes((node as ts.ImportSpecifier).name!.text) - ) { - importCalls.push({ - name: (node as ts.ImportSpecifier).name!.text, - type: 'namedImport', - moduleName, - }) - } - }) - } else { - importCalls.push({ - name: namedBindings.name!.text, - type: 'namespaceImport', - moduleName, - }) - } - } - } - return importCalls -} diff --git a/tests/__snapshots__/component-as-selector.tsx.shot b/tests/__snapshots__/component-as-selector.tsx.shot index c4796a4..6f3f70b 100644 --- a/tests/__snapshots__/component-as-selector.tsx.shot +++ b/tests/__snapshots__/component-as-selector.tsx.shot @@ -216,6 +216,115 @@ TypeScript after transform: ReactDOM.render(, document.querySelector('#app')); +`; + +exports[`should transform component-as-selector.tsx with custom module 1`] = ` + +File: component-as-selector.tsx +TypeScript before transform: + import React from 'react' + import ReactDOM from 'react-dom' + import styled from '@emotion/styled' + + interface Props { + backgroundColor: string + } + + const Wrapper = styled('div')( + { + color: 'red', + }, + (props) => ({ + backgroundColor: props.backgroundColor, + }), + ) + + const WrapperPropertyAccess = styled.div( + { + color: 'yellow', + }, + (props) => ({ + backgroundColor: props.backgroundColor, + }), + ) + + const Parent = styled('div')({ + [\`\${Wrapper}\`]: { + fontSize: '100px', + }, + [\`\${WrapperPropertyAccess}\`]: { + fontSize: '80px', + } + }) + + export class SimpleComponent extends React.PureComponent { + render() { + return ( + + + hello + + + world + + + ) + } + } + + ReactDOM.render(, document.querySelector('#app')) + + + + ↓ ↓ ↓ ↓ ↓ ↓ + +TypeScript after transform: + import { jsx } from "@emotion/core"; + import React from 'react'; + import ReactDOM from 'react-dom'; + import styled from '@emotion/styled'; + interface Props { + backgroundColor: string; + } + const Wrapper = /*#__PURE__*/ styled('div', { + target: "ersjv9n0" + })({ + color: 'red', + }, (props) => ({ + backgroundColor: props.backgroundColor, + }), "label:Wrapper;", "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3RzL2ZpeHR1cmVzL2NvbXBvbmVudC1hcy1zZWxlY3Rvci50c3giXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBUWdCIiwiZmlsZSI6ImNvbXBvbmVudC1hcy1zZWxlY3Rvci50c3giLCJzb3VyY2VSb290IjoiIiwic291cmNlc0NvbnRlbnQiOlsiaW1wb3J0IFJlYWN0IGZyb20gJ3JlYWN0J1xuaW1wb3J0IFJlYWN0RE9NIGZyb20gJ3JlYWN0LWRvbSdcbmltcG9ydCBzdHlsZWQgZnJvbSAnQGVtb3Rpb24vc3R5bGVkJ1xuXG5pbnRlcmZhY2UgUHJvcHMge1xuICBiYWNrZ3JvdW5kQ29sb3I6IHN0cmluZ1xufVxuXG5jb25zdCBXcmFwcGVyID0gc3R5bGVkKCdkaXYnKTxQcm9wcz4oXG4gIHtcbiAgICBjb2xvcjogJ3JlZCcsXG4gIH0sXG4gIChwcm9wcykgPT4gKHtcbiAgICBiYWNrZ3JvdW5kQ29sb3I6IHByb3BzLmJhY2tncm91bmRDb2xvcixcbiAgfSksXG4pXG5cbmNvbnN0IFdyYXBwZXJQcm9wZXJ0eUFjY2VzcyA9IHN0eWxlZC5kaXY8UHJvcHM+KFxuICB7XG4gICAgY29sb3I6ICd5ZWxsb3cnLFxuICB9LFxuICAocHJvcHMpID0+ICh7XG4gICAgYmFja2dyb3VuZENvbG9yOiBwcm9wcy5iYWNrZ3JvdW5kQ29sb3IsXG4gIH0pLFxuKVxuXG5jb25zdCBQYXJlbnQgPSBzdHlsZWQoJ2RpdicpKHtcbiAgW2Ake1dyYXBwZXJ9YF06IHtcbiAgICBmb250U2l6ZTogJzEwMHB4JyxcbiAgfSxcbiAgW2Ake1dyYXBwZXJQcm9wZXJ0eUFjY2Vzc31gXToge1xuICAgIGZvbnRTaXplOiAnODBweCcsXG4gIH1cbn0pXG5cbmV4cG9ydCBjbGFzcyBTaW1wbGVDb21wb25lbnQgZXh0ZW5kcyBSZWFjdC5QdXJlQ29tcG9uZW50IHtcbiAgcmVuZGVyKCkge1xuICAgIHJldHVybiAoXG4gICAgICA8UGFyZW50PlxuICAgICAgICA8V3JhcHBlciBiYWNrZ3JvdW5kQ29sb3I9XCJibHVlXCI+XG4gICAgICAgICAgPHNwYW4+aGVsbG88L3NwYW4+XG4gICAgICAgIDwvV3JhcHBlcj5cbiAgICAgICAgPFdyYXBwZXJQcm9wZXJ0eUFjY2VzcyBiYWNrZ3JvdW5kQ29sb3I9XCJjeWFuXCI+XG4gICAgICAgICAgPHNwYW4+d29ybGQ8L3NwYW4+XG4gICAgICAgIDwvV3JhcHBlclByb3BlcnR5QWNjZXNzPlxuICAgICAgPC9QYXJlbnQ+XG4gICAgKVxuICB9XG59XG5cblJlYWN0RE9NLnJlbmRlcig8U2ltcGxlQ29tcG9uZW50IC8+LCBkb2N1bWVudC5xdWVyeVNlbGVjdG9yKCcjYXBwJykpXG4iXX0= */"); + const WrapperPropertyAccess = /*#__PURE__*/ styled("div", { + target: "ersjv9n1" + })({ + color: 'yellow', + }, (props) => ({ + backgroundColor: props.backgroundColor, + }), "label:WrapperPropertyAccess;", "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3RzL2ZpeHR1cmVzL2NvbXBvbmVudC1hcy1zZWxlY3Rvci50c3giXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBUWdCLEFBU2MiLCJmaWxlIjoiY29tcG9uZW50LWFzLXNlbGVjdG9yLnRzeCIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzQ29udGVudCI6WyJpbXBvcnQgUmVhY3QgZnJvbSAncmVhY3QnXG5pbXBvcnQgUmVhY3RET00gZnJvbSAncmVhY3QtZG9tJ1xuaW1wb3J0IHN0eWxlZCBmcm9tICdAZW1vdGlvbi9zdHlsZWQnXG5cbmludGVyZmFjZSBQcm9wcyB7XG4gIGJhY2tncm91bmRDb2xvcjogc3RyaW5nXG59XG5cbmNvbnN0IFdyYXBwZXIgPSBzdHlsZWQoJ2RpdicpPFByb3BzPihcbiAge1xuICAgIGNvbG9yOiAncmVkJyxcbiAgfSxcbiAgKHByb3BzKSA9PiAoe1xuICAgIGJhY2tncm91bmRDb2xvcjogcHJvcHMuYmFja2dyb3VuZENvbG9yLFxuICB9KSxcbilcblxuY29uc3QgV3JhcHBlclByb3BlcnR5QWNjZXNzID0gc3R5bGVkLmRpdjxQcm9wcz4oXG4gIHtcbiAgICBjb2xvcjogJ3llbGxvdycsXG4gIH0sXG4gIChwcm9wcykgPT4gKHtcbiAgICBiYWNrZ3JvdW5kQ29sb3I6IHByb3BzLmJhY2tncm91bmRDb2xvcixcbiAgfSksXG4pXG5cbmNvbnN0IFBhcmVudCA9IHN0eWxlZCgnZGl2Jykoe1xuICBbYCR7V3JhcHBlcn1gXToge1xuICAgIGZvbnRTaXplOiAnMTAwcHgnLFxuICB9LFxuICBbYCR7V3JhcHBlclByb3BlcnR5QWNjZXNzfWBdOiB7XG4gICAgZm9udFNpemU6ICc4MHB4JyxcbiAgfVxufSlcblxuZXhwb3J0IGNsYXNzIFNpbXBsZUNvbXBvbmVudCBleHRlbmRzIFJlYWN0LlB1cmVDb21wb25lbnQge1xuICByZW5kZXIoKSB7XG4gICAgcmV0dXJuIChcbiAgICAgIDxQYXJlbnQ+XG4gICAgICAgIDxXcmFwcGVyIGJhY2tncm91bmRDb2xvcj1cImJsdWVcIj5cbiAgICAgICAgICA8c3Bhbj5oZWxsbzwvc3Bhbj5cbiAgICAgICAgPC9XcmFwcGVyPlxuICAgICAgICA8V3JhcHBlclByb3BlcnR5QWNjZXNzIGJhY2tncm91bmRDb2xvcj1cImN5YW5cIj5cbiAgICAgICAgICA8c3Bhbj53b3JsZDwvc3Bhbj5cbiAgICAgICAgPC9XcmFwcGVyUHJvcGVydHlBY2Nlc3M+XG4gICAgICA8L1BhcmVudD5cbiAgICApXG4gIH1cbn1cblxuUmVhY3RET00ucmVuZGVyKDxTaW1wbGVDb21wb25lbnQgLz4sIGRvY3VtZW50LnF1ZXJ5U2VsZWN0b3IoJyNhcHAnKSlcbiJdfQ== */"); + const Parent = /*#__PURE__*/ styled('div', { + target: "ersjv9n2" + })({ + [\`\${Wrapper}\`]: { + fontSize: '100px', + }, + [\`\${WrapperPropertyAccess}\`]: { + fontSize: '80px', + } + }, "label:Parent;", "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3RzL2ZpeHR1cmVzL2NvbXBvbmVudC1hcy1zZWxlY3Rvci50c3giXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBUWdCLEFBU2MsQUFTZiIsImZpbGUiOiJjb21wb25lbnQtYXMtc2VsZWN0b3IudHN4Iiwic291cmNlUm9vdCI6IiIsInNvdXJjZXNDb250ZW50IjpbImltcG9ydCBSZWFjdCBmcm9tICdyZWFjdCdcbmltcG9ydCBSZWFjdERPTSBmcm9tICdyZWFjdC1kb20nXG5pbXBvcnQgc3R5bGVkIGZyb20gJ0BlbW90aW9uL3N0eWxlZCdcblxuaW50ZXJmYWNlIFByb3BzIHtcbiAgYmFja2dyb3VuZENvbG9yOiBzdHJpbmdcbn1cblxuY29uc3QgV3JhcHBlciA9IHN0eWxlZCgnZGl2Jyk8UHJvcHM+KFxuICB7XG4gICAgY29sb3I6ICdyZWQnLFxuICB9LFxuICAocHJvcHMpID0+ICh7XG4gICAgYmFja2dyb3VuZENvbG9yOiBwcm9wcy5iYWNrZ3JvdW5kQ29sb3IsXG4gIH0pLFxuKVxuXG5jb25zdCBXcmFwcGVyUHJvcGVydHlBY2Nlc3MgPSBzdHlsZWQuZGl2PFByb3BzPihcbiAge1xuICAgIGNvbG9yOiAneWVsbG93JyxcbiAgfSxcbiAgKHByb3BzKSA9PiAoe1xuICAgIGJhY2tncm91bmRDb2xvcjogcHJvcHMuYmFja2dyb3VuZENvbG9yLFxuICB9KSxcbilcblxuY29uc3QgUGFyZW50ID0gc3R5bGVkKCdkaXYnKSh7XG4gIFtgJHtXcmFwcGVyfWBdOiB7XG4gICAgZm9udFNpemU6ICcxMDBweCcsXG4gIH0sXG4gIFtgJHtXcmFwcGVyUHJvcGVydHlBY2Nlc3N9YF06IHtcbiAgICBmb250U2l6ZTogJzgwcHgnLFxuICB9XG59KVxuXG5leHBvcnQgY2xhc3MgU2ltcGxlQ29tcG9uZW50IGV4dGVuZHMgUmVhY3QuUHVyZUNvbXBvbmVudCB7XG4gIHJlbmRlcigpIHtcbiAgICByZXR1cm4gKFxuICAgICAgPFBhcmVudD5cbiAgICAgICAgPFdyYXBwZXIgYmFja2dyb3VuZENvbG9yPVwiYmx1ZVwiPlxuICAgICAgICAgIDxzcGFuPmhlbGxvPC9zcGFuPlxuICAgICAgICA8L1dyYXBwZXI+XG4gICAgICAgIDxXcmFwcGVyUHJvcGVydHlBY2Nlc3MgYmFja2dyb3VuZENvbG9yPVwiY3lhblwiPlxuICAgICAgICAgIDxzcGFuPndvcmxkPC9zcGFuPlxuICAgICAgICA8L1dyYXBwZXJQcm9wZXJ0eUFjY2Vzcz5cbiAgICAgIDwvUGFyZW50PlxuICAgIClcbiAgfVxufVxuXG5SZWFjdERPTS5yZW5kZXIoPFNpbXBsZUNvbXBvbmVudCAvPiwgZG9jdW1lbnQucXVlcnlTZWxlY3RvcignI2FwcCcpKVxuIl19 */"); + export class SimpleComponent extends React.PureComponent { + render() { + return ( + + hello + + + world + + ); + } + } + ReactDOM.render(, document.querySelector('#app')); + + `; exports[`should transform component-as-selector.tsx with default configs 1`] = ` diff --git a/tests/__snapshots__/css-in-callback.tsx.shot b/tests/__snapshots__/css-in-callback.tsx.shot index de71e4b..ec67517 100644 --- a/tests/__snapshots__/css-in-callback.tsx.shot +++ b/tests/__snapshots__/css-in-callback.tsx.shot @@ -103,6 +103,73 @@ TypeScript before transform: + ↓ ↓ ↓ ↓ ↓ ↓ + +TypeScript after transform: + import { jsx } from "@emotion/core"; + import React from 'react'; + import ReactDOM from 'react-dom'; + import { css } from 'emotion'; + import styled from '@emotion/styled'; + const styles = (props: any) => /*#__PURE__*/ css({ + color: 'red', + background: 'yellow', + width: \`\${props.scale * 100}px\`, + }, "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3RzL2ZpeHR1cmVzL2Nzcy1pbi1jYWxsYmFjay50c3giXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBSytCIiwiZmlsZSI6ImNzcy1pbi1jYWxsYmFjay50c3giLCJzb3VyY2VSb290IjoiIiwic291cmNlc0NvbnRlbnQiOlsiaW1wb3J0IFJlYWN0IGZyb20gJ3JlYWN0J1xuaW1wb3J0IFJlYWN0RE9NIGZyb20gJ3JlYWN0LWRvbSdcbmltcG9ydCB7IGNzcyB9IGZyb20gJ2Vtb3Rpb24nXG5pbXBvcnQgc3R5bGVkIGZyb20gJ0BlbW90aW9uL3N0eWxlZCdcblxuY29uc3Qgc3R5bGVzID0gKHByb3BzOiBhbnkpID0+XG4gIGNzcyh7XG4gICAgY29sb3I6ICdyZWQnLFxuICAgIGJhY2tncm91bmQ6ICd5ZWxsb3cnLFxuICAgIHdpZHRoOiBgJHtwcm9wcy5zY2FsZSAqIDEwMH1weGAsXG4gIH0pXG5cbmNvbnN0IENvbnRhaW5lciA9IHN0eWxlZCgnYnV0dG9uJylgXG4gICR7c3R5bGVzfVxuICAkeygpID0+IGNzcyh7XG4gICAgYmFja2dyb3VuZDogJ3JlZCcsXG4gIH0pfVxuYFxuZXhwb3J0IGNsYXNzIFNpbXBsZUNvbXBvbmVudCBleHRlbmRzIFJlYWN0LlB1cmVDb21wb25lbnQge1xuICByZW5kZXIoKSB7XG4gICAgcmV0dXJuIChcbiAgICAgIDxDb250YWluZXI+XG4gICAgICAgIDxzcGFuPmhlbGxvPC9zcGFuPlxuICAgICAgPC9Db250YWluZXI+XG4gICAgKVxuICB9XG59XG5cblJlYWN0RE9NLnJlbmRlcig8U2ltcGxlQ29tcG9uZW50IC8+LCBkb2N1bWVudC5xdWVyeVNlbGVjdG9yKCcjYXBwJykpXG4iXX0= */"); + const Container = /*#__PURE__*/ styled('button', "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3RzL2ZpeHR1cmVzL2Nzcy1pbi1jYWxsYmFjay50c3giXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBSytCLEFBT2IiLCJmaWxlIjoiY3NzLWluLWNhbGxiYWNrLnRzeCIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzQ29udGVudCI6WyJpbXBvcnQgUmVhY3QgZnJvbSAncmVhY3QnXG5pbXBvcnQgUmVhY3RET00gZnJvbSAncmVhY3QtZG9tJ1xuaW1wb3J0IHsgY3NzIH0gZnJvbSAnZW1vdGlvbidcbmltcG9ydCBzdHlsZWQgZnJvbSAnQGVtb3Rpb24vc3R5bGVkJ1xuXG5jb25zdCBzdHlsZXMgPSAocHJvcHM6IGFueSkgPT5cbiAgY3NzKHtcbiAgICBjb2xvcjogJ3JlZCcsXG4gICAgYmFja2dyb3VuZDogJ3llbGxvdycsXG4gICAgd2lkdGg6IGAke3Byb3BzLnNjYWxlICogMTAwfXB4YCxcbiAgfSlcblxuY29uc3QgQ29udGFpbmVyID0gc3R5bGVkKCdidXR0b24nKWBcbiAgJHtzdHlsZXN9XG4gICR7KCkgPT4gY3NzKHtcbiAgICBiYWNrZ3JvdW5kOiAncmVkJyxcbiAgfSl9XG5gXG5leHBvcnQgY2xhc3MgU2ltcGxlQ29tcG9uZW50IGV4dGVuZHMgUmVhY3QuUHVyZUNvbXBvbmVudCB7XG4gIHJlbmRlcigpIHtcbiAgICByZXR1cm4gKFxuICAgICAgPENvbnRhaW5lcj5cbiAgICAgICAgPHNwYW4+aGVsbG88L3NwYW4+XG4gICAgICA8L0NvbnRhaW5lcj5cbiAgICApXG4gIH1cbn1cblxuUmVhY3RET00ucmVuZGVyKDxTaW1wbGVDb21wb25lbnQgLz4sIGRvY3VtZW50LnF1ZXJ5U2VsZWN0b3IoJyNhcHAnKSlcbiJdfQ== */") \` + \${styles} + \${() => /*#__PURE__*/ css({ + background: 'red', + }, "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3RzL2ZpeHR1cmVzL2Nzcy1pbi1jYWxsYmFjay50c3giXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBSytCLEFBT2IsQUFFUiIsImZpbGUiOiJjc3MtaW4tY2FsbGJhY2sudHN4Iiwic291cmNlUm9vdCI6IiIsInNvdXJjZXNDb250ZW50IjpbImltcG9ydCBSZWFjdCBmcm9tICdyZWFjdCdcbmltcG9ydCBSZWFjdERPTSBmcm9tICdyZWFjdC1kb20nXG5pbXBvcnQgeyBjc3MgfSBmcm9tICdlbW90aW9uJ1xuaW1wb3J0IHN0eWxlZCBmcm9tICdAZW1vdGlvbi9zdHlsZWQnXG5cbmNvbnN0IHN0eWxlcyA9IChwcm9wczogYW55KSA9PlxuICBjc3Moe1xuICAgIGNvbG9yOiAncmVkJyxcbiAgICBiYWNrZ3JvdW5kOiAneWVsbG93JyxcbiAgICB3aWR0aDogYCR7cHJvcHMuc2NhbGUgKiAxMDB9cHhgLFxuICB9KVxuXG5jb25zdCBDb250YWluZXIgPSBzdHlsZWQoJ2J1dHRvbicpYFxuICAke3N0eWxlc31cbiAgJHsoKSA9PiBjc3Moe1xuICAgIGJhY2tncm91bmQ6ICdyZWQnLFxuICB9KX1cbmBcbmV4cG9ydCBjbGFzcyBTaW1wbGVDb21wb25lbnQgZXh0ZW5kcyBSZWFjdC5QdXJlQ29tcG9uZW50IHtcbiAgcmVuZGVyKCkge1xuICAgIHJldHVybiAoXG4gICAgICA8Q29udGFpbmVyPlxuICAgICAgICA8c3Bhbj5oZWxsbzwvc3Bhbj5cbiAgICAgIDwvQ29udGFpbmVyPlxuICAgIClcbiAgfVxufVxuXG5SZWFjdERPTS5yZW5kZXIoPFNpbXBsZUNvbXBvbmVudCAvPiwgZG9jdW1lbnQucXVlcnlTZWxlY3RvcignI2FwcCcpKVxuIl19 */")} + \`; + export class SimpleComponent extends React.PureComponent { + render() { + return ( + hello + ); + } + } + ReactDOM.render(, document.querySelector('#app')); + + +`; + +exports[`should transform css-in-callback.tsx with custom module 1`] = ` + +File: css-in-callback.tsx +TypeScript before transform: + import React from 'react' + import ReactDOM from 'react-dom' + import { css } from 'emotion' + import styled from '@emotion/styled' + + const styles = (props: any) => + css({ + color: 'red', + background: 'yellow', + width: \`\${props.scale * 100}px\`, + }) + + const Container = styled('button')\` + \${styles} + \${() => css({ + background: 'red', + })} + \` + export class SimpleComponent extends React.PureComponent { + render() { + return ( + + hello + + ) + } + } + + ReactDOM.render(, document.querySelector('#app')) + + + ↓ ↓ ↓ ↓ ↓ ↓ TypeScript after transform: diff --git a/tests/__snapshots__/custom-module.tsx.shot b/tests/__snapshots__/custom-module.tsx.shot new file mode 100644 index 0000000..588f646 --- /dev/null +++ b/tests/__snapshots__/custom-module.tsx.shot @@ -0,0 +1,163 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`should not add sourcemap to custom-module.tsx if NODE_ENV === 'production' 1`] = ` + +File: custom-module.tsx +TypeScript before transform: + import { myStyled, myCss } from 'my-emotion' + + export const className = myCss({ + color: 'red', + background: 'yellow', + }) + + export const StyledComponent = myStyled.div({ + color: 'blue', + }) + + + + ↓ ↓ ↓ ↓ ↓ ↓ + +TypeScript after transform: + import { myStyled, myCss } from 'my-emotion'; + export const className = myCss({ + color: 'red', + background: 'yellow', + }); + export const StyledComponent = myStyled.div({ + color: 'blue', + }); + + +`; + +exports[`should transform custom-module.tsx with autoLabel false 1`] = ` + +File: custom-module.tsx +TypeScript before transform: + import { myStyled, myCss } from 'my-emotion' + + export const className = myCss({ + color: 'red', + background: 'yellow', + }) + + export const StyledComponent = myStyled.div({ + color: 'blue', + }) + + + + ↓ ↓ ↓ ↓ ↓ ↓ + +TypeScript after transform: + import { myStyled, myCss } from 'my-emotion'; + export const className = myCss({ + color: 'red', + background: 'yellow', + }); + export const StyledComponent = myStyled.div({ + color: 'blue', + }); + + +`; + +exports[`should transform custom-module.tsx with custom module 1`] = ` + +File: custom-module.tsx +TypeScript before transform: + import { myStyled, myCss } from 'my-emotion' + + export const className = myCss({ + color: 'red', + background: 'yellow', + }) + + export const StyledComponent = myStyled.div({ + color: 'blue', + }) + + + + ↓ ↓ ↓ ↓ ↓ ↓ + +TypeScript after transform: + import { myStyled, myCss } from 'my-emotion'; + export const className = /*#__PURE__*/ myCss({ + color: 'red', + background: 'yellow', + }, "label:className;", "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3RzL2ZpeHR1cmVzL2N1c3RvbS1tb2R1bGUudHN4Il0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUV5QiIsImZpbGUiOiJjdXN0b20tbW9kdWxlLnRzeCIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzQ29udGVudCI6WyJpbXBvcnQgeyBteVN0eWxlZCwgbXlDc3MgfSBmcm9tICdteS1lbW90aW9uJ1xuXG5leHBvcnQgY29uc3QgY2xhc3NOYW1lID0gbXlDc3Moe1xuICBjb2xvcjogJ3JlZCcsXG4gIGJhY2tncm91bmQ6ICd5ZWxsb3cnLFxufSlcblxuZXhwb3J0IGNvbnN0IFN0eWxlZENvbXBvbmVudCA9IG15U3R5bGVkLmRpdih7XG4gIGNvbG9yOiAnYmx1ZScsXG59KVxuIl19 */"); + export const StyledComponent = /*#__PURE__*/ myStyled("div", { + target: "eiefrsv0" + })({ + color: 'blue', + }, "label:StyledComponent;", "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3RzL2ZpeHR1cmVzL2N1c3RvbS1tb2R1bGUudHN4Il0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUV5QixBQUtNIiwiZmlsZSI6ImN1c3RvbS1tb2R1bGUudHN4Iiwic291cmNlUm9vdCI6IiIsInNvdXJjZXNDb250ZW50IjpbImltcG9ydCB7IG15U3R5bGVkLCBteUNzcyB9IGZyb20gJ215LWVtb3Rpb24nXG5cbmV4cG9ydCBjb25zdCBjbGFzc05hbWUgPSBteUNzcyh7XG4gIGNvbG9yOiAncmVkJyxcbiAgYmFja2dyb3VuZDogJ3llbGxvdycsXG59KVxuXG5leHBvcnQgY29uc3QgU3R5bGVkQ29tcG9uZW50ID0gbXlTdHlsZWQuZGl2KHtcbiAgY29sb3I6ICdibHVlJyxcbn0pXG4iXX0= */"); + + +`; + +exports[`should transform custom-module.tsx with default configs 1`] = ` + +File: custom-module.tsx +TypeScript before transform: + import { myStyled, myCss } from 'my-emotion' + + export const className = myCss({ + color: 'red', + background: 'yellow', + }) + + export const StyledComponent = myStyled.div({ + color: 'blue', + }) + + + + ↓ ↓ ↓ ↓ ↓ ↓ + +TypeScript after transform: + import { myStyled, myCss } from 'my-emotion'; + export const className = myCss({ + color: 'red', + background: 'yellow', + }); + export const StyledComponent = myStyled.div({ + color: 'blue', + }); + + +`; + +exports[`should transform custom-module.tsx with sourcemap false 1`] = ` + +File: custom-module.tsx +TypeScript before transform: + import { myStyled, myCss } from 'my-emotion' + + export const className = myCss({ + color: 'red', + background: 'yellow', + }) + + export const StyledComponent = myStyled.div({ + color: 'blue', + }) + + + + ↓ ↓ ↓ ↓ ↓ ↓ + +TypeScript after transform: + import { myStyled, myCss } from 'my-emotion'; + export const className = myCss({ + color: 'red', + background: 'yellow', + }); + export const StyledComponent = myStyled.div({ + color: 'blue', + }); + + +`; diff --git a/tests/__snapshots__/emotion-core-complex.tsx.shot b/tests/__snapshots__/emotion-core-complex.tsx.shot index bc60a30..f03a066 100644 --- a/tests/__snapshots__/emotion-core-complex.tsx.shot +++ b/tests/__snapshots__/emotion-core-complex.tsx.shot @@ -110,6 +110,62 @@ TypeScript after transform: ReactDOM.render(, document.querySelector('#app')); +`; + +exports[`should transform emotion-core-complex.tsx with custom module 1`] = ` + +File: emotion-core-complex.tsx +TypeScript before transform: + import React from 'react' + import { PureComponent } from 'react' + import ReactDOM from 'react-dom' + import { css } from '@emotion/core' + + const className = css({ + color: 'red', + background: 'yellow', + }) + + export class SimpleComponent extends PureComponent { + render() { + return ( + +
+ hello +
+
+ ) + } + } + + ReactDOM.render(, document.querySelector('#app')) + + + + ↓ ↓ ↓ ↓ ↓ ↓ + +TypeScript after transform: + import { jsx } from "@emotion/core"; + import React from 'react'; + import { PureComponent } from 'react'; + import ReactDOM from 'react-dom'; + import { css } from '@emotion/core'; + const className = /*#__PURE__*/ css({ + color: 'red', + background: 'yellow', + }, "label:className;", "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3RzL2ZpeHR1cmVzL2Vtb3Rpb24tY29yZS1jb21wbGV4LnRzeCJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFLa0IiLCJmaWxlIjoiZW1vdGlvbi1jb3JlLWNvbXBsZXgudHN4Iiwic291cmNlUm9vdCI6IiIsInNvdXJjZXNDb250ZW50IjpbImltcG9ydCBSZWFjdCBmcm9tICdyZWFjdCdcbmltcG9ydCB7IFB1cmVDb21wb25lbnQgfSBmcm9tICdyZWFjdCdcbmltcG9ydCBSZWFjdERPTSBmcm9tICdyZWFjdC1kb20nXG5pbXBvcnQgeyBjc3MgfSBmcm9tICdAZW1vdGlvbi9jb3JlJ1xuXG5jb25zdCBjbGFzc05hbWUgPSBjc3Moe1xuICBjb2xvcjogJ3JlZCcsXG4gIGJhY2tncm91bmQ6ICd5ZWxsb3cnLFxufSlcblxuZXhwb3J0IGNsYXNzIFNpbXBsZUNvbXBvbmVudCBleHRlbmRzIFB1cmVDb21wb25lbnQge1xuICByZW5kZXIoKSB7XG4gICAgcmV0dXJuIChcbiAgICAgIDxSZWFjdC5GcmFnbWVudD5cbiAgICAgICAgPGRpdiBjc3M9e2NsYXNzTmFtZX0+XG4gICAgICAgICAgPHNwYW4+aGVsbG88L3NwYW4+XG4gICAgICAgIDwvZGl2PlxuICAgICAgPC9SZWFjdC5GcmFnbWVudD5cbiAgICApXG4gIH1cbn1cblxuUmVhY3RET00ucmVuZGVyKDxTaW1wbGVDb21wb25lbnQgLz4sIGRvY3VtZW50LnF1ZXJ5U2VsZWN0b3IoJyNhcHAnKSlcbiJdfQ== */"); + export class SimpleComponent extends PureComponent { + render() { + return ( +
+ hello +
+
); + } + } + ReactDOM.render(, document.querySelector('#app')); + + `; exports[`should transform emotion-core-complex.tsx with default configs 1`] = ` diff --git a/tests/__snapshots__/emotion-core-simple.tsx.shot b/tests/__snapshots__/emotion-core-simple.tsx.shot index 408c36a..fb51195 100644 --- a/tests/__snapshots__/emotion-core-simple.tsx.shot +++ b/tests/__snapshots__/emotion-core-simple.tsx.shot @@ -98,6 +98,56 @@ TypeScript after transform: ReactDOM.render(, document.querySelector('#app')); +`; + +exports[`should transform emotion-core-simple.tsx with custom module 1`] = ` + +File: emotion-core-simple.tsx +TypeScript before transform: + import React from 'react' + import ReactDOM from 'react-dom' + import { css } from '@emotion/core' + + const className = css({ + color: 'red', + background: 'yellow', + }) + + export class SimpleComponent extends React.PureComponent { + render() { + return ( +
+ hello +
+ ) + } + } + + ReactDOM.render(, document.querySelector('#app')) + + + + ↓ ↓ ↓ ↓ ↓ ↓ + +TypeScript after transform: + import { jsx } from "@emotion/core"; + import React from 'react'; + import ReactDOM from 'react-dom'; + import { css } from '@emotion/core'; + const className = /*#__PURE__*/ css({ + color: 'red', + background: 'yellow', + }, "label:className;", "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3RzL2ZpeHR1cmVzL2Vtb3Rpb24tY29yZS1zaW1wbGUudHN4Il0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUlrQiIsImZpbGUiOiJlbW90aW9uLWNvcmUtc2ltcGxlLnRzeCIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzQ29udGVudCI6WyJpbXBvcnQgUmVhY3QgZnJvbSAncmVhY3QnXG5pbXBvcnQgUmVhY3RET00gZnJvbSAncmVhY3QtZG9tJ1xuaW1wb3J0IHsgY3NzIH0gZnJvbSAnQGVtb3Rpb24vY29yZSdcblxuY29uc3QgY2xhc3NOYW1lID0gY3NzKHtcbiAgY29sb3I6ICdyZWQnLFxuICBiYWNrZ3JvdW5kOiAneWVsbG93Jyxcbn0pXG5cbmV4cG9ydCBjbGFzcyBTaW1wbGVDb21wb25lbnQgZXh0ZW5kcyBSZWFjdC5QdXJlQ29tcG9uZW50IHtcbiAgcmVuZGVyKCkge1xuICAgIHJldHVybiAoXG4gICAgICA8ZGl2IGNzcz17Y2xhc3NOYW1lfT5cbiAgICAgICAgPHNwYW4+aGVsbG88L3NwYW4+XG4gICAgICA8L2Rpdj5cbiAgICApXG4gIH1cbn1cblxuUmVhY3RET00ucmVuZGVyKDxTaW1wbGVDb21wb25lbnQgLz4sIGRvY3VtZW50LnF1ZXJ5U2VsZWN0b3IoJyNhcHAnKSlcbiJdfQ== */"); + export class SimpleComponent extends React.PureComponent { + render() { + return (
+ hello +
); + } + } + ReactDOM.render(, document.querySelector('#app')); + + `; exports[`should transform emotion-core-simple.tsx with default configs 1`] = ` diff --git a/tests/__snapshots__/import-as.tsx.shot b/tests/__snapshots__/import-as.tsx.shot index 05baeb1..f1e943a 100644 --- a/tests/__snapshots__/import-as.tsx.shot +++ b/tests/__snapshots__/import-as.tsx.shot @@ -130,6 +130,72 @@ TypeScript after transform: ReactDOM.render(, document.querySelector('#app')); +`; + +exports[`should transform import-as.tsx with custom module 1`] = ` + +File: import-as.tsx +TypeScript before transform: + import 'somepolyfill' + import { default as emotion } from '@emotion/styled' + import React from 'react' + import ReactDOM from 'react-dom' + + interface Props { + backgroundColor: string + } + + const Wrapper = emotion('div')( + { + color: 'red', + }, + (props: Props) => ({ + backgroundColor: props.backgroundColor, + }), + ) + + export class SimpleComponent extends React.PureComponent { + render() { + return ( + + hello + + ) + } + } + + ReactDOM.render(, document.querySelector('#app')) + + + + ↓ ↓ ↓ ↓ ↓ ↓ + +TypeScript after transform: + import 'somepolyfill'; + import { default as emotion } from '@emotion/styled'; + import { jsx } from "@emotion/core"; + import React from 'react'; + import ReactDOM from 'react-dom'; + interface Props { + backgroundColor: string; + } + const Wrapper = /*#__PURE__*/ emotion('div', { + target: "ezbh3f00" + })({ + color: 'red', + }, (props: Props) => ({ + backgroundColor: props.backgroundColor, + }), "label:Wrapper;", "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3RzL2ZpeHR1cmVzL2ltcG9ydC1hcy50c3giXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBU2dCIiwiZmlsZSI6ImltcG9ydC1hcy50c3giLCJzb3VyY2VSb290IjoiIiwic291cmNlc0NvbnRlbnQiOlsiaW1wb3J0ICdzb21lcG9seWZpbGwnXG5pbXBvcnQgeyBkZWZhdWx0IGFzIGVtb3Rpb24gfSBmcm9tICdAZW1vdGlvbi9zdHlsZWQnXG5pbXBvcnQgUmVhY3QgZnJvbSAncmVhY3QnXG5pbXBvcnQgUmVhY3RET00gZnJvbSAncmVhY3QtZG9tJ1xuXG5pbnRlcmZhY2UgUHJvcHMge1xuICBiYWNrZ3JvdW5kQ29sb3I6IHN0cmluZ1xufVxuXG5jb25zdCBXcmFwcGVyID0gZW1vdGlvbignZGl2JykoXG4gIHtcbiAgICBjb2xvcjogJ3JlZCcsXG4gIH0sXG4gIChwcm9wczogUHJvcHMpID0+ICh7XG4gICAgYmFja2dyb3VuZENvbG9yOiBwcm9wcy5iYWNrZ3JvdW5kQ29sb3IsXG4gIH0pLFxuKVxuXG5leHBvcnQgY2xhc3MgU2ltcGxlQ29tcG9uZW50IGV4dGVuZHMgUmVhY3QuUHVyZUNvbXBvbmVudCB7XG4gIHJlbmRlcigpIHtcbiAgICByZXR1cm4gKFxuICAgICAgPFdyYXBwZXIgYmFja2dyb3VuZENvbG9yPVwiYmx1ZVwiPlxuICAgICAgICA8c3Bhbj5oZWxsbzwvc3Bhbj5cbiAgICAgIDwvV3JhcHBlcj5cbiAgICApXG4gIH1cbn1cblxuUmVhY3RET00ucmVuZGVyKDxTaW1wbGVDb21wb25lbnQgLz4sIGRvY3VtZW50LnF1ZXJ5U2VsZWN0b3IoJyNhcHAnKSlcbiJdfQ== */"); + export class SimpleComponent extends React.PureComponent { + render() { + return ( + hello + ); + } + } + ReactDOM.render(, document.querySelector('#app')); + + `; exports[`should transform import-as.tsx with default configs 1`] = ` diff --git a/tests/__snapshots__/import-css-as.tsx.shot b/tests/__snapshots__/import-css-as.tsx.shot index a58ab26..e98426a 100644 --- a/tests/__snapshots__/import-css-as.tsx.shot +++ b/tests/__snapshots__/import-css-as.tsx.shot @@ -98,6 +98,56 @@ TypeScript after transform: ReactDOM.render(, document.querySelector('#app')); +`; + +exports[`should transform import-css-as.tsx with custom module 1`] = ` + +File: import-css-as.tsx +TypeScript before transform: + import React from 'react' + import ReactDOM from 'react-dom' + import { css as emotionCss } from 'emotion' + + const className = emotionCss({ + color: 'red', + background: 'yellow', + }) + + export class SimpleComponent extends React.PureComponent { + render() { + return ( +
+ hello +
+ ) + } + } + + ReactDOM.render(, document.querySelector('#app')) + + + + ↓ ↓ ↓ ↓ ↓ ↓ + +TypeScript after transform: + import { jsx } from "@emotion/core"; + import React from 'react'; + import ReactDOM from 'react-dom'; + import { css as emotionCss } from 'emotion'; + const className = /*#__PURE__*/ emotionCss({ + color: 'red', + background: 'yellow', + }, "label:className;", "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3RzL2ZpeHR1cmVzL2ltcG9ydC1jc3MtYXMudHN4Il0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUlrQiIsImZpbGUiOiJpbXBvcnQtY3NzLWFzLnRzeCIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzQ29udGVudCI6WyJpbXBvcnQgUmVhY3QgZnJvbSAncmVhY3QnXG5pbXBvcnQgUmVhY3RET00gZnJvbSAncmVhY3QtZG9tJ1xuaW1wb3J0IHsgY3NzIGFzIGVtb3Rpb25Dc3MgfSBmcm9tICdlbW90aW9uJ1xuXG5jb25zdCBjbGFzc05hbWUgPSBlbW90aW9uQ3NzKHtcbiAgY29sb3I6ICdyZWQnLFxuICBiYWNrZ3JvdW5kOiAneWVsbG93Jyxcbn0pXG5cbmV4cG9ydCBjbGFzcyBTaW1wbGVDb21wb25lbnQgZXh0ZW5kcyBSZWFjdC5QdXJlQ29tcG9uZW50IHtcbiAgcmVuZGVyKCkge1xuICAgIHJldHVybiAoXG4gICAgICA8ZGl2IGNsYXNzTmFtZT17Y2xhc3NOYW1lfT5cbiAgICAgICAgPHNwYW4+aGVsbG88L3NwYW4+XG4gICAgICA8L2Rpdj5cbiAgICApXG4gIH1cbn1cblxuUmVhY3RET00ucmVuZGVyKDxTaW1wbGVDb21wb25lbnQgLz4sIGRvY3VtZW50LnF1ZXJ5U2VsZWN0b3IoJyNhcHAnKSlcbiJdfQ== */"); + export class SimpleComponent extends React.PureComponent { + render() { + return (
+ hello +
); + } + } + ReactDOM.render(, document.querySelector('#app')); + + `; exports[`should transform import-css-as.tsx with default configs 1`] = ` diff --git a/tests/__snapshots__/import-css.tsx.shot b/tests/__snapshots__/import-css.tsx.shot index 726d726..eb78b3a 100644 --- a/tests/__snapshots__/import-css.tsx.shot +++ b/tests/__snapshots__/import-css.tsx.shot @@ -98,6 +98,56 @@ TypeScript after transform: ReactDOM.render(, document.querySelector('#app')); +`; + +exports[`should transform import-css.tsx with custom module 1`] = ` + +File: import-css.tsx +TypeScript before transform: + import React from 'react' + import ReactDOM from 'react-dom' + import { css } from 'emotion' + + const className = css({ + color: 'red', + background: 'yellow', + }) + + export class SimpleComponent extends React.PureComponent { + render() { + return ( +
+ hello +
+ ) + } + } + + ReactDOM.render(, document.querySelector('#app')) + + + + ↓ ↓ ↓ ↓ ↓ ↓ + +TypeScript after transform: + import { jsx } from "@emotion/core"; + import React from 'react'; + import ReactDOM from 'react-dom'; + import { css } from 'emotion'; + const className = /*#__PURE__*/ css({ + color: 'red', + background: 'yellow', + }, "label:className;", "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3RzL2ZpeHR1cmVzL2ltcG9ydC1jc3MudHN4Il0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUlrQiIsImZpbGUiOiJpbXBvcnQtY3NzLnRzeCIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzQ29udGVudCI6WyJpbXBvcnQgUmVhY3QgZnJvbSAncmVhY3QnXG5pbXBvcnQgUmVhY3RET00gZnJvbSAncmVhY3QtZG9tJ1xuaW1wb3J0IHsgY3NzIH0gZnJvbSAnZW1vdGlvbidcblxuY29uc3QgY2xhc3NOYW1lID0gY3NzKHtcbiAgY29sb3I6ICdyZWQnLFxuICBiYWNrZ3JvdW5kOiAneWVsbG93Jyxcbn0pXG5cbmV4cG9ydCBjbGFzcyBTaW1wbGVDb21wb25lbnQgZXh0ZW5kcyBSZWFjdC5QdXJlQ29tcG9uZW50IHtcbiAgcmVuZGVyKCkge1xuICAgIHJldHVybiAoXG4gICAgICA8ZGl2IGNsYXNzTmFtZT17Y2xhc3NOYW1lfT5cbiAgICAgICAgPHNwYW4+aGVsbG88L3NwYW4+XG4gICAgICA8L2Rpdj5cbiAgICApXG4gIH1cbn1cblxuUmVhY3RET00ucmVuZGVyKDxTaW1wbGVDb21wb25lbnQgLz4sIGRvY3VtZW50LnF1ZXJ5U2VsZWN0b3IoJyNhcHAnKSlcbiJdfQ== */"); + export class SimpleComponent extends React.PureComponent { + render() { + return (
+ hello +
); + } + } + ReactDOM.render(, document.querySelector('#app')); + + `; exports[`should transform import-css.tsx with default configs 1`] = ` diff --git a/tests/__snapshots__/import-default-emotion.tsx.shot b/tests/__snapshots__/import-default-emotion.tsx.shot index 09ebd6b..783d32d 100644 --- a/tests/__snapshots__/import-default-emotion.tsx.shot +++ b/tests/__snapshots__/import-default-emotion.tsx.shot @@ -98,6 +98,56 @@ TypeScript after transform: ReactDOM.render(, document.querySelector('#app')); +`; + +exports[`should transform import-default-emotion.tsx with custom module 1`] = ` + +File: import-default-emotion.tsx +TypeScript before transform: + import React from 'react' + import ReactDOM from 'react-dom' + import emotion from 'emotion' + + const className = emotion.css({ + color: 'red', + background: 'yellow', + }) + + export class SimpleComponent extends React.PureComponent { + render() { + return ( +
+ hello +
+ ) + } + } + + ReactDOM.render(, document.querySelector('#app')) + + + + ↓ ↓ ↓ ↓ ↓ ↓ + +TypeScript after transform: + import { jsx } from "@emotion/core"; + import React from 'react'; + import ReactDOM from 'react-dom'; + import emotion from 'emotion'; + const className = /*#__PURE__*/ emotion.css({ + color: 'red', + background: 'yellow', + }, "label:className;", "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3RzL2ZpeHR1cmVzL2ltcG9ydC1kZWZhdWx0LWVtb3Rpb24udHN4Il0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUlrQiIsImZpbGUiOiJpbXBvcnQtZGVmYXVsdC1lbW90aW9uLnRzeCIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzQ29udGVudCI6WyJpbXBvcnQgUmVhY3QgZnJvbSAncmVhY3QnXG5pbXBvcnQgUmVhY3RET00gZnJvbSAncmVhY3QtZG9tJ1xuaW1wb3J0IGVtb3Rpb24gZnJvbSAnZW1vdGlvbidcblxuY29uc3QgY2xhc3NOYW1lID0gZW1vdGlvbi5jc3Moe1xuICBjb2xvcjogJ3JlZCcsXG4gIGJhY2tncm91bmQ6ICd5ZWxsb3cnLFxufSlcblxuZXhwb3J0IGNsYXNzIFNpbXBsZUNvbXBvbmVudCBleHRlbmRzIFJlYWN0LlB1cmVDb21wb25lbnQge1xuICByZW5kZXIoKSB7XG4gICAgcmV0dXJuIChcbiAgICAgIDxkaXYgY2xhc3NOYW1lPXtjbGFzc05hbWV9PlxuICAgICAgICA8c3Bhbj5oZWxsbzwvc3Bhbj5cbiAgICAgIDwvZGl2PlxuICAgIClcbiAgfVxufVxuXG5SZWFjdERPTS5yZW5kZXIoPFNpbXBsZUNvbXBvbmVudCAvPiwgZG9jdW1lbnQucXVlcnlTZWxlY3RvcignI2FwcCcpKVxuIl19 */"); + export class SimpleComponent extends React.PureComponent { + render() { + return (
+ hello +
); + } + } + ReactDOM.render(, document.querySelector('#app')); + + `; exports[`should transform import-default-emotion.tsx with default configs 1`] = ` diff --git a/tests/__snapshots__/namespace-import.tsx.shot b/tests/__snapshots__/namespace-import.tsx.shot index 7927a33..56a1f67 100644 --- a/tests/__snapshots__/namespace-import.tsx.shot +++ b/tests/__snapshots__/namespace-import.tsx.shot @@ -126,6 +126,70 @@ TypeScript after transform: ReactDOM.render(, document.querySelector('#app')); +`; + +exports[`should transform namespace-import.tsx with custom module 1`] = ` + +File: namespace-import.tsx +TypeScript before transform: + import React from 'react' + import ReactDOM from 'react-dom' + import * as emotion from '@emotion/styled' + + interface Props { + backgroundColor: string + } + + const Wrapper = emotion.default('div')( + { + color: 'red', + }, + (props: Props) => ({ + backgroundColor: props.backgroundColor, + }), + ) + + export class SimpleComponent extends React.PureComponent { + render() { + return ( + + hello + + ) + } + } + + ReactDOM.render(, document.querySelector('#app')) + + + + ↓ ↓ ↓ ↓ ↓ ↓ + +TypeScript after transform: + import { jsx } from "@emotion/core"; + import React from 'react'; + import ReactDOM from 'react-dom'; + import * as emotion from '@emotion/styled'; + interface Props { + backgroundColor: string; + } + const Wrapper = /*#__PURE__*/ emotion.default('div', { + target: "e1pgislt0" + })({ + color: 'red', + }, (props: Props) => ({ + backgroundColor: props.backgroundColor, + }), "label:Wrapper;", "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3RzL2ZpeHR1cmVzL25hbWVzcGFjZS1pbXBvcnQudHN4Il0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQVFnQiIsImZpbGUiOiJuYW1lc3BhY2UtaW1wb3J0LnRzeCIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzQ29udGVudCI6WyJpbXBvcnQgUmVhY3QgZnJvbSAncmVhY3QnXG5pbXBvcnQgUmVhY3RET00gZnJvbSAncmVhY3QtZG9tJ1xuaW1wb3J0ICogYXMgZW1vdGlvbiBmcm9tICdAZW1vdGlvbi9zdHlsZWQnXG5cbmludGVyZmFjZSBQcm9wcyB7XG4gIGJhY2tncm91bmRDb2xvcjogc3RyaW5nXG59XG5cbmNvbnN0IFdyYXBwZXIgPSBlbW90aW9uLmRlZmF1bHQoJ2RpdicpKFxuICB7XG4gICAgY29sb3I6ICdyZWQnLFxuICB9LFxuICAocHJvcHM6IFByb3BzKSA9PiAoe1xuICAgIGJhY2tncm91bmRDb2xvcjogcHJvcHMuYmFja2dyb3VuZENvbG9yLFxuICB9KSxcbilcblxuZXhwb3J0IGNsYXNzIFNpbXBsZUNvbXBvbmVudCBleHRlbmRzIFJlYWN0LlB1cmVDb21wb25lbnQge1xuICByZW5kZXIoKSB7XG4gICAgcmV0dXJuIChcbiAgICAgIDxXcmFwcGVyIGJhY2tncm91bmRDb2xvcj1cImJsdWVcIj5cbiAgICAgICAgPHNwYW4+aGVsbG88L3NwYW4+XG4gICAgICA8L1dyYXBwZXI+XG4gICAgKVxuICB9XG59XG5cblJlYWN0RE9NLnJlbmRlcig8U2ltcGxlQ29tcG9uZW50IC8+LCBkb2N1bWVudC5xdWVyeVNlbGVjdG9yKCcjYXBwJykpXG4iXX0= */"); + export class SimpleComponent extends React.PureComponent { + render() { + return ( + hello + ); + } + } + ReactDOM.render(, document.querySelector('#app')); + + `; exports[`should transform namespace-import.tsx with default configs 1`] = ` diff --git a/tests/__snapshots__/simple.tsx.shot b/tests/__snapshots__/simple.tsx.shot index fd4d13c..17ee33e 100644 --- a/tests/__snapshots__/simple.tsx.shot +++ b/tests/__snapshots__/simple.tsx.shot @@ -126,6 +126,70 @@ TypeScript after transform: ReactDOM.render(, document.querySelector('#app')); +`; + +exports[`should transform simple.tsx with custom module 1`] = ` + +File: simple.tsx +TypeScript before transform: + import React from 'react' + import ReactDOM from 'react-dom' + import styled from '@emotion/styled' + + interface Props { + backgroundColor: string + } + + const Wrapper = styled('div')( + { + color: 'red', + }, + (props: Props) => ({ + backgroundColor: props.backgroundColor, + }), + ) + + export class SimpleComponent extends React.PureComponent { + render() { + return ( + + hello + + ) + } + } + + ReactDOM.render(, document.querySelector('#app')) + + + + ↓ ↓ ↓ ↓ ↓ ↓ + +TypeScript after transform: + import { jsx } from "@emotion/core"; + import React from 'react'; + import ReactDOM from 'react-dom'; + import styled from '@emotion/styled'; + interface Props { + backgroundColor: string; + } + const Wrapper = /*#__PURE__*/ styled('div', { + target: "e4zdufa0" + })({ + color: 'red', + }, (props: Props) => ({ + backgroundColor: props.backgroundColor, + }), "label:Wrapper;", "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3RzL2ZpeHR1cmVzL3NpbXBsZS50c3giXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBUWdCIiwiZmlsZSI6InNpbXBsZS50c3giLCJzb3VyY2VSb290IjoiIiwic291cmNlc0NvbnRlbnQiOlsiaW1wb3J0IFJlYWN0IGZyb20gJ3JlYWN0J1xuaW1wb3J0IFJlYWN0RE9NIGZyb20gJ3JlYWN0LWRvbSdcbmltcG9ydCBzdHlsZWQgZnJvbSAnQGVtb3Rpb24vc3R5bGVkJ1xuXG5pbnRlcmZhY2UgUHJvcHMge1xuICBiYWNrZ3JvdW5kQ29sb3I6IHN0cmluZ1xufVxuXG5jb25zdCBXcmFwcGVyID0gc3R5bGVkKCdkaXYnKShcbiAge1xuICAgIGNvbG9yOiAncmVkJyxcbiAgfSxcbiAgKHByb3BzOiBQcm9wcykgPT4gKHtcbiAgICBiYWNrZ3JvdW5kQ29sb3I6IHByb3BzLmJhY2tncm91bmRDb2xvcixcbiAgfSksXG4pXG5cbmV4cG9ydCBjbGFzcyBTaW1wbGVDb21wb25lbnQgZXh0ZW5kcyBSZWFjdC5QdXJlQ29tcG9uZW50IHtcbiAgcmVuZGVyKCkge1xuICAgIHJldHVybiAoXG4gICAgICA8V3JhcHBlciBiYWNrZ3JvdW5kQ29sb3I9XCJibHVlXCI+XG4gICAgICAgIDxzcGFuPmhlbGxvPC9zcGFuPlxuICAgICAgPC9XcmFwcGVyPlxuICAgIClcbiAgfVxufVxuXG5SZWFjdERPTS5yZW5kZXIoPFNpbXBsZUNvbXBvbmVudCAvPiwgZG9jdW1lbnQucXVlcnlTZWxlY3RvcignI2FwcCcpKVxuIl19 */"); + export class SimpleComponent extends React.PureComponent { + render() { + return ( + hello + ); + } + } + ReactDOM.render(, document.querySelector('#app')); + + `; exports[`should transform simple.tsx with default configs 1`] = ` diff --git a/tests/__snapshots__/styled-with-options.tsx.shot b/tests/__snapshots__/styled-with-options.tsx.shot index c524840..260722e 100644 --- a/tests/__snapshots__/styled-with-options.tsx.shot +++ b/tests/__snapshots__/styled-with-options.tsx.shot @@ -49,6 +49,7 @@ TypeScript after transform: } const Wrapper = /*#__PURE__*/ emotion('div', { label: 'Yes-This-Is-A-Label', + target: "erqq4pg0" })({ color: 'red', }, (props: Props) => ({ @@ -115,6 +116,7 @@ TypeScript after transform: } const Wrapper = /*#__PURE__*/ emotion('div', { label: 'Yes-This-Is-A-Label', + target: "erqq4pg0" })({ color: 'red', }, (props: Props) => ({ @@ -130,6 +132,73 @@ TypeScript after transform: ReactDOM.render(, document.querySelector('#app')); +`; + +exports[`should transform styled-with-options.tsx with custom module 1`] = ` + +File: styled-with-options.tsx +TypeScript before transform: + import React from 'react' + import ReactDOM from 'react-dom' + import emotion from '@emotion/styled' + + interface Props { + backgroundColor: string + } + + const Wrapper = emotion('div', { + label: 'Yes-This-Is-A-Label', + })( + { + color: 'red', + }, + (props: Props) => ({ + backgroundColor: props.backgroundColor, + }), + ) + + export class SimpleComponent extends React.PureComponent { + render() { + return ( + + hello + + ) + } + } + + ReactDOM.render(, document.querySelector('#app')) + + + + ↓ ↓ ↓ ↓ ↓ ↓ + +TypeScript after transform: + import { jsx } from "@emotion/core"; + import React from 'react'; + import ReactDOM from 'react-dom'; + import emotion from '@emotion/styled'; + interface Props { + backgroundColor: string; + } + const Wrapper = /*#__PURE__*/ emotion('div', { + label: 'Yes-This-Is-A-Label', + target: "erqq4pg0" + })({ + color: 'red', + }, (props: Props) => ({ + backgroundColor: props.backgroundColor, + }), "label:Wrapper;", "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3RzL2ZpeHR1cmVzL3N0eWxlZC13aXRoLW9wdGlvbnMudHN4Il0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQVFnQiIsImZpbGUiOiJzdHlsZWQtd2l0aC1vcHRpb25zLnRzeCIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzQ29udGVudCI6WyJpbXBvcnQgUmVhY3QgZnJvbSAncmVhY3QnXG5pbXBvcnQgUmVhY3RET00gZnJvbSAncmVhY3QtZG9tJ1xuaW1wb3J0IGVtb3Rpb24gZnJvbSAnQGVtb3Rpb24vc3R5bGVkJ1xuXG5pbnRlcmZhY2UgUHJvcHMge1xuICBiYWNrZ3JvdW5kQ29sb3I6IHN0cmluZ1xufVxuXG5jb25zdCBXcmFwcGVyID0gZW1vdGlvbignZGl2Jywge1xuICBsYWJlbDogJ1llcy1UaGlzLUlzLUEtTGFiZWwnLFxufSkoXG4gIHtcbiAgICBjb2xvcjogJ3JlZCcsXG4gIH0sXG4gIChwcm9wczogUHJvcHMpID0+ICh7XG4gICAgYmFja2dyb3VuZENvbG9yOiBwcm9wcy5iYWNrZ3JvdW5kQ29sb3IsXG4gIH0pLFxuKVxuXG5leHBvcnQgY2xhc3MgU2ltcGxlQ29tcG9uZW50IGV4dGVuZHMgUmVhY3QuUHVyZUNvbXBvbmVudCB7XG4gIHJlbmRlcigpIHtcbiAgICByZXR1cm4gKFxuICAgICAgPFdyYXBwZXIgYmFja2dyb3VuZENvbG9yPVwiYmx1ZVwiPlxuICAgICAgICA8c3Bhbj5oZWxsbzwvc3Bhbj5cbiAgICAgIDwvV3JhcHBlcj5cbiAgICApXG4gIH1cbn1cblxuUmVhY3RET00ucmVuZGVyKDxTaW1wbGVDb21wb25lbnQgLz4sIGRvY3VtZW50LnF1ZXJ5U2VsZWN0b3IoJyNhcHAnKSlcbiJdfQ== */"); + export class SimpleComponent extends React.PureComponent { + render() { + return ( + hello + ); + } + } + ReactDOM.render(, document.querySelector('#app')); + + `; exports[`should transform styled-with-options.tsx with default configs 1`] = ` @@ -181,6 +250,7 @@ TypeScript after transform: } const Wrapper = /*#__PURE__*/ emotion('div', { label: 'Yes-This-Is-A-Label', + target: "erqq4pg0" })({ color: 'red', }, (props: Props) => ({ @@ -247,6 +317,7 @@ TypeScript after transform: } const Wrapper = /*#__PURE__*/ emotion('div', { label: 'Yes-This-Is-A-Label', + target: "erqq4pg0" })({ color: 'red', }, (props: Props) => ({ diff --git a/tests/fixtures/custom-module.tsx b/tests/fixtures/custom-module.tsx new file mode 100644 index 0000000..c398531 --- /dev/null +++ b/tests/fixtures/custom-module.tsx @@ -0,0 +1,10 @@ +import { myStyled, myCss } from 'my-emotion' + +export const className = myCss({ + color: 'red', + background: 'yellow', +}) + +export const StyledComponent = myStyled.div({ + color: 'blue', +}) diff --git a/tests/jest.d.ts b/tests/jest.d.ts index 828162e..b7fd07a 100644 --- a/tests/jest.d.ts +++ b/tests/jest.d.ts @@ -5,3 +5,5 @@ declare namespace jest { } declare module 'jest-specific-snapshot' + +declare module 'my-emotion' diff --git a/tests/unit.spec.ts b/tests/unit.spec.ts index c633f2c..e9a250e 100644 --- a/tests/unit.spec.ts +++ b/tests/unit.spec.ts @@ -116,4 +116,17 @@ fixtures expect(result).toMatchSpecificSnapshot(pathToSnap) process.env.NODE_ENV = originalEnv }) + + it(`should transform ${filename} with custom module`, () => { + const result = transform({ + customModules: [ + { + moduleName: 'my-emotion', + exportedNames: ['myStyled', 'myCss'], + styledName: 'myStyled', + }, + ], + }) + expect(result).toMatchSpecificSnapshot(pathToSnap) + }) })