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

Pass filename to PostCSS correctly #243

Merged
merged 1 commit into from Aug 8, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 15 additions & 7 deletions src/ast-object.js
@@ -1,7 +1,10 @@
import postcssJs from 'postcss-js'
import Input from 'postcss/lib/input'
import { expandCSSFallbacks, prefixer } from './parser'
import { forEach, reduce } from './utils'
import { getFilename } from './babel'

function prefixAst(args, t) {
function prefixAst(args, t, path) {
function isLiteral(value) {
return (
t.isStringLiteral(value) ||
Expand All @@ -11,7 +14,7 @@ function prefixAst(args, t) {
}

if (Array.isArray(args)) {
return args.map(element => prefixAst(element, t))
return args.map(element => prefixAst(element, t, path))
}

if (t.isObjectExpression(args)) {
Expand All @@ -38,7 +41,7 @@ function prefixAst(args, t) {
]
}

const prefixedValue = prefixAst(property.value, t)
const prefixedValue = prefixAst(property.value, t, path)

if (!property.computed) {
if (prefixedPseudoSelectors[key.value]) {
Expand Down Expand Up @@ -75,7 +78,12 @@ function prefixAst(args, t) {
: property.value.value

const style = { [property.key.name]: propertyValue }
const prefixedStyle = expandCSSFallbacks(prefixer(style))
const parsedStyle = postcssJs.parse(style)
parsedStyle.source = {}
parsedStyle.source.input = new Input(parsedStyle.toString(), {
Copy link
Member

Choose a reason for hiding this comment

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

Clever!

from: getFilename(path)
})
const prefixedStyle = expandCSSFallbacks(prefixer(parsedStyle))

for (let k in prefixedStyle) {
const key = t.isStringLiteral(property.key)
Expand Down Expand Up @@ -105,7 +113,7 @@ function prefixAst(args, t) {
}

if (t.isArrayExpression(args)) {
return t.arrayExpression(prefixAst(args.elements, t))
return t.arrayExpression(prefixAst(args.elements, t, path))
}

return args
Expand Down Expand Up @@ -288,7 +296,7 @@ export default class ASTObject {
return new ASTObject(props, [], composesCount, t)
}

static fromAST(astObj, t) {
static fromAST(astObj, t, path) {
function isLiteral(value) {
return (
t.isStringLiteral(value) ||
Expand Down Expand Up @@ -376,7 +384,7 @@ export default class ASTObject {
return props
}

const objectProperties = convertAstToObj(prefixAst(astObj, t))
const objectProperties = convertAstToObj(prefixAst(astObj, t, path))
return new ASTObject(
objectProperties,
expressions,
Expand Down
4 changes: 2 additions & 2 deletions src/babel.js
Expand Up @@ -16,7 +16,7 @@ import { hashArray } from './hash'
import cssProps from './css-prop'
import ASTObject from './ast-object'

function getFilename(path) {
export function getFilename(path) {
return path.hub.file.opts.filename === 'unknown'
? ''
: path.hub.file.opts.filename
Expand Down Expand Up @@ -203,7 +203,7 @@ export function buildStyledObjectCallExpression(path, state, identifier, t) {

function buildProcessedStylesFromObjectAST(objectAST, path, t) {
if (t.isObjectExpression(objectAST)) {
return ASTObject.fromAST(objectAST, t).toAST()
return ASTObject.fromAST(objectAST, t, path).toAST()
}
if (t.isArrayExpression(objectAST)) {
return t.arrayExpression(
Expand Down
2 changes: 1 addition & 1 deletion src/parser.js
Expand Up @@ -55,7 +55,7 @@ export function parseCSS(
}
})

const styles = expandCSSFallbacks(prefixer(postcssJs.objectify(root)))
const styles = expandCSSFallbacks(prefixer(root))

return {
styles,
Expand Down
6 changes: 0 additions & 6 deletions test/__snapshots__/css.test.js.snap
Expand Up @@ -17,8 +17,6 @@ exports[`css composes 1`] = `
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
}

Expand All @@ -35,8 +33,6 @@ exports[`css composes with objects 1`] = `
display: block;
width: 30px;
height: calc(40vw - 50px);
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
}

Expand All @@ -62,8 +58,6 @@ exports[`css composes with objects 1`] = `

exports[`css composes with undefined values 1`] = `
.glamor-0 {
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
}

Expand Down
2 changes: 0 additions & 2 deletions test/babel/__snapshots__/css.test.js.snap
Expand Up @@ -58,8 +58,6 @@ const cls1 = css({
const cls2 = /*#__PURE__*/css([cls1], [], function createEmotionStyledRules() {
return [{
'height': '20',
'WebkitBoxPack': 'center',
'msFlexPack': 'center',
'justifyContent': 'center'
}];
});"
Expand Down
1 change: 1 addition & 0 deletions test/babel/css-prop.test.js
Expand Up @@ -5,6 +5,7 @@ import * as fs from 'fs'
jest.mock('fs')

fs.existsSync.mockReturnValue(true)
fs.statSync.mockReturnValue({ isFile: () => false })

describe('babel css prop', () => {
test('basic with extractStatic', () => {
Expand Down
1 change: 1 addition & 0 deletions test/babel/css.test.js
Expand Up @@ -4,6 +4,7 @@ import * as fs from 'fs'
jest.mock('fs')

fs.existsSync.mockReturnValue(true)
fs.statSync.mockReturnValue({ isFile: () => false })

describe('babel css', () => {
describe('inline', () => {
Expand Down
1 change: 1 addition & 0 deletions test/babel/font-face.test.js
Expand Up @@ -4,6 +4,7 @@ import * as fs from 'fs'
jest.mock('fs')

fs.existsSync.mockReturnValue(true)
fs.statSync.mockReturnValue({ isFile: () => false })

describe('fontFace babel', () => {
describe('inline', () => {
Expand Down
1 change: 1 addition & 0 deletions test/babel/fs.test.js
Expand Up @@ -4,6 +4,7 @@ import touch from 'touch'
import emotionPlugin from '../../src/babel'

jest.mock('fs').mock('touch')
fs.statSync.mockReturnValue({ isFile: () => false })

const basic = `
css\`
Expand Down
1 change: 1 addition & 0 deletions test/babel/inject-global.test.js
Expand Up @@ -4,6 +4,7 @@ import * as fs from 'fs'
jest.mock('fs')

fs.existsSync.mockReturnValue(true)
fs.statSync.mockReturnValue({ isFile: () => false })

describe('babel injectGlobal', () => {
describe('inline', () => {
Expand Down
1 change: 1 addition & 0 deletions test/babel/keyframes.test.js
Expand Up @@ -4,6 +4,7 @@ import * as fs from 'fs'
jest.mock('fs')

fs.existsSync.mockReturnValue(true)
fs.statSync.mockReturnValue({ isFile: () => false })

describe('babel keyframes', () => {
describe('inline', () => {
Expand Down
1 change: 1 addition & 0 deletions test/babel/styled.test.js
Expand Up @@ -5,6 +5,7 @@ import * as fs from 'fs'
jest.mock('fs')

fs.existsSync.mockReturnValue(true)
fs.statSync.mockReturnValue({ isFile: () => false })

describe('babel styled component', () => {
describe('inline', () => {
Expand Down
9 changes: 8 additions & 1 deletion test/browserslist/__snapshots__/babel.test.js.snap
Expand Up @@ -4,7 +4,14 @@ exports[`babel css inline css basic 1`] = `
"
/*#__PURE__*/css([], [], function createEmotionStyledRules() {
return [{
\\"display\\": \\"-webkit-box; display: -ms-flexbox; display: flex\\"
\\"display\\": \\"flex\\"
}];
});"
`;

exports[`babel css inline css object 1`] = `
"
css({
'display': 'flex'
});"
`;
16 changes: 12 additions & 4 deletions test/browserslist/__snapshots__/css.test.js.snap
Expand Up @@ -2,8 +2,18 @@

exports[`prefixing css 1`] = `
.glamor-0 {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
}

<div
className="glamor-0"
>
hello world
</div>
`;

exports[`prefixing object 1`] = `
.glamor-0 {
display: flex;
}

Expand All @@ -16,8 +26,6 @@ exports[`prefixing css 1`] = `

exports[`prefixing styled 1`] = `
.glamor-1 {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
}

Expand Down
12 changes: 12 additions & 0 deletions test/browserslist/babel.test.js
Expand Up @@ -15,5 +15,17 @@ describe('babel css', () => {
})
expect(code).toMatchSnapshot()
})
test('css object', () => {
const basic = `
css({
display: 'flex'
})`
const { code } = babel.transform(basic, {
plugins: [[plugin]],
filename: __filename,
babelrc: false
})
expect(code).toMatchSnapshot()
})
})
})
10 changes: 10 additions & 0 deletions test/browserslist/css.test.js
Expand Up @@ -23,4 +23,14 @@ describe('prefixing', () => {

expect(tree).toMatchSnapshot()
})
test('object', () => {
const cls1 = css({
display: 'flex'
})
const tree = renderer
.create(<div className={cls1}>hello world</div>)
.toJSON()

expect(tree).toMatchSnapshot()
})
})
6 changes: 0 additions & 6 deletions test/macro/__snapshots__/css.test.js.snap
Expand Up @@ -5,8 +5,6 @@ exports[`css macro composes 1`] = `
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
}

Expand All @@ -23,8 +21,6 @@ exports[`css macro composes with objects 1`] = `
display: block;
width: 30px;
height: calc(40vw - 50px);
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
}

Expand All @@ -50,8 +46,6 @@ exports[`css macro composes with objects 1`] = `

exports[`css macro composes with undefined values 1`] = `
.glamor-0 {
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
}

Expand Down