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

Allow labelFormat option to be a function #1651

Merged
merged 2 commits into from Nov 25, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/large-snakes-ring.md
@@ -0,0 +1,6 @@
---
'babel-plugin-emotion': minor
'@emotion/babel-preset-css-prop': minor
---

Allow `labelFormat` option to be a function.
Expand Up @@ -66,6 +66,30 @@ _keyframes(process.env.NODE_ENV === \\"production\\" ? {
});"
`;

exports[`babel css inline label format function 1`] = `
"
import { css } from 'emotion'
let cls = css({color:'hotpink'})


↓ ↓ ↓ ↓ ↓ ↓

function _EMOTION_STRINGIFIED_CSS_ERROR__() { return \\"You have tried to stringify object returned from \`css\` function. It isn't supposed to be used directly (e.g. as value of the \`className\` prop), but rather handed to emotion so it can handle it (e.g. as value of \`css\` prop).\\"; }

import { css } from 'emotion';
let cls =
/*#__PURE__*/
css(process.env.NODE_ENV === \\"production\\" ? {
name: \\"1rm02zb-CLS_CSS-REQUIRES-OPTIONS\\",
styles: \\"color:hotpink;;label:CLS_CSS-REQUIRES-OPTIONS;\\"
} : {
name: \\"1rm02zb-CLS_CSS-REQUIRES-OPTIONS\\",
styles: \\"color:hotpink;;label:CLS_CSS-REQUIRES-OPTIONS;\\",
map: \\"/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImNzcy1yZXF1aXJlcy1vcHRpb25zLmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUVjIiwiZmlsZSI6ImNzcy1yZXF1aXJlcy1vcHRpb25zLmpzIiwic291cmNlc0NvbnRlbnQiOlsiXG4gICAgaW1wb3J0IHsgY3NzIH0gZnJvbSAnZW1vdGlvbidcbiAgICBsZXQgY2xzID0gY3NzKHtjb2xvcjonaG90cGluayd9KVxuICAgICJdfQ== */\\",
toString: _EMOTION_STRINGIFIED_CSS_ERROR__
});"
`;

exports[`babel css inline label format with dirname, filename, and local 1`] = `
"
import { css } from 'emotion'
Expand Down
22 changes: 22 additions & 0 deletions packages/babel-plugin-emotion/__tests__/css-requires-options.js
Expand Up @@ -2,6 +2,8 @@
import babelTester from 'babel-tester'
import plugin from 'babel-plugin-emotion'

const last = arr => arr[arr.length - 1]

const cases = {
'label format with only local': {
code: `
Expand Down Expand Up @@ -70,6 +72,26 @@ const cases = {
babelFileName: __filename
},

'label format function': {
code: `
import { css } from 'emotion'
let cls = css({color:'hotpink'})
`,
plugins: [
[
plugin,
{
labelFormat: ({ name, filename }) =>
`${name.toUpperCase()}_${last(
filename.replace(/\..+$/, '').split('/')
).toUpperCase()}`,
autoLabel: true
}
]
],
babelFileName: __filename
},

// this test has better readability for label alone than other ones which include source maps
'label on code transpiled by TS': {
code: `
Expand Down
23 changes: 20 additions & 3 deletions packages/babel-plugin-emotion/src/utils/label.js
@@ -1,6 +1,11 @@
// @flow
import nodePath from 'path'

type LabelFormatOptions = {
name: string,
filename: string
Andarist marked this conversation as resolved.
Show resolved Hide resolved
}

const invalidClassNameCharacters = /[!"#$%&'()*+,./:;<=>?@[\]^`|}~{]/g

const sanitizeLabelPart = (labelPart: string) =>
Expand All @@ -9,11 +14,23 @@ const sanitizeLabelPart = (labelPart: string) =>
function getLabel(
identifierName?: string,
autoLabel: boolean,
labelFormat?: string,
labelFormat?: string | (LabelFormatOptions => string),
filename: string
) {
if (!identifierName || !autoLabel) return null
if (!labelFormat) return sanitizeLabelPart(identifierName)

const sanitizedName = sanitizeLabelPart(identifierName)

if (!labelFormat) {
return sanitizedName
}

if (typeof labelFormat === 'function') {
return labelFormat({
name: sanitizedName,
filename
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we call this path to distinguish it from the filename used in the string form of labelFormat?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, changed.

})
}

const parsedPath = nodePath.parse(filename)
let localDirname = nodePath.basename(parsedPath.dir)
Expand All @@ -24,7 +41,7 @@ function getLabel(
}

return labelFormat
.replace(/\[local\]/gi, sanitizeLabelPart(identifierName))
.replace(/\[local\]/gi, sanitizedName)
.replace(/\[filename\]/gi, sanitizeLabelPart(localFilename))
.replace(/\[dirname\]/gi, sanitizeLabelPart(localDirname))
}
Expand Down