Skip to content

Commit

Permalink
Fix babel plugin crashing when called programmatically without provid…
Browse files Browse the repository at this point in the history
…ing a filename (#1574)
  • Loading branch information
Andarist authored and emmatown committed Oct 26, 2019
1 parent 3927293 commit b3a0f14
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/slimy-dancers-wonder.md
@@ -0,0 +1,5 @@
---
'babel-plugin-emotion': patch
---

Fix babel plugin crashing when called programmatically without providing a filename
Expand Up @@ -15,7 +15,7 @@ exports[`styled inline babel 7 hash generation no file system 1`] = `
"import _styled from \\"@emotion/styled-base\\";
_styled(\\"h1\\", {
target: \\"e1i8mfxi0\\"
target: \\"e1vqloan0\\"
})({
name: \\"14ksm7b\\",
styles: \\"color:blue;\\"
Expand Down
Expand Up @@ -4,7 +4,7 @@ import { createInlineTests } from 'old-babel-tester'
const cases = {
'hash generation no file system': {
code: 'import styled from "@emotion/styled";\nstyled.h1`color:blue;`',
filename: ''
filename: undefined
}
}

Expand Down
Expand Up @@ -20,7 +20,9 @@ export function getTargetClassName(state: *, t: *) {
state.emotionTargetClassNameCount = 0
}

const filename = state.file.opts.filename
const hasFilepath =
state.file.opts.filename && state.file.opts.filename !== 'unknown'
const filename = hasFilepath ? state.file.opts.filename : ''
// normalize the file path to ignore folder structure
// outside the current node project and arch-specific delimiters
let moduleName = ''
Expand Down
5 changes: 4 additions & 1 deletion packages/babel-plugin-emotion/src/utils/source-maps.js
Expand Up @@ -27,7 +27,10 @@ export function getSourceMap(
): string {
const generator = makeSourceMapGenerator(state.file)
const generatorOpts = getGeneratorOpts(state.file)
if (generatorOpts.sourceFileName) {
if (
generatorOpts.sourceFileName &&
generatorOpts.sourceFileName !== 'unknown'
) {
generator.addMapping({
generated: {
line: 1,
Expand Down
2 changes: 1 addition & 1 deletion scripts/old-babel-tester/src/index.js
Expand Up @@ -38,7 +38,7 @@ const createInlineTester = babel => opts => {
}
]
],
filename: opts.filename !== undefined ? opts.filename : 'emotion.js',
filename: 'filename' in opts ? opts.filename : 'emotion.js',
babelrc: false,
ast: true,
...(isBabel7(babel)
Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Expand Up @@ -8758,7 +8758,7 @@ expand-template@^2.0.3:
resolved "https://registry.npmjs.org/expand-template/-/expand-template-2.0.3.tgz#6e14b3fcee0f3a6340ecb57d2e8918692052a47c"
integrity sha512-XYfuKMvj4O35f/pOXLObndIRvyQ+/+6AhODh+OKWj9S9498pHHn/IMszH+gt0fBCRWMNfk1ZSp5x3AifmnI2vg==

expand-tilde@^2.0.0:
expand-tilde@^2.0.0, expand-tilde@^2.0.2:
version "2.0.2"
resolved "https://registry.npmjs.org/expand-tilde/-/expand-tilde-2.0.2.tgz#97e801aa052df02454de46b02bf621642cdc8502"
integrity sha1-l+gBqgUt8CRU3kawK/YhZCzchQI=
Expand Down Expand Up @@ -23952,7 +23952,7 @@ which-pm-runs@^1.0.0:
resolved "https://registry.npmjs.org/which-pm-runs/-/which-pm-runs-1.0.0.tgz#670b3afbc552e0b55df6b7780ca74615f23ad1cb"
integrity sha1-Zws6+8VS4LVd9rd4DKdGFfI60cs=

which@1, which@^1.2.10, which@^1.2.12, which@^1.2.9, which@^1.3.0, which@^1.3.1:
which@1, which@^1.2.10, which@^1.2.12, which@^1.2.14, which@^1.2.9, which@^1.3.0, which@^1.3.1:
version "1.3.1"
resolved "https://registry.npmjs.org/which/-/which-1.3.1.tgz#a45043d54f5805316da8d62f9f50918d3da70b0a"
integrity sha512-HxJdYWq1MTIQbJ3nw0cqssHoTNU267KlrDuGZ1WYlxDStUtKUhOaJmh112/TZmHxxUfuJqPXSOm7tDyas0OSIQ==
Expand Down

0 comments on commit b3a0f14

Please sign in to comment.