Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bento jss: transform the CSS and useStyles exports. (#29777)
* build-system: teach buildExtension how to build jss * remove style-loader from main deps * make work with watch mode * add babel transform * delete jss build, perform at babel time * lint * dd todo * also convert the module.exports into named exports * copyright * Thank you justin, path.evaluate is gold. * Update with smarter xform * extract classes var, no longer jss required export * another test * remove rest operator * fix, bad interpolation * improve two comments * lint * updates, tanks justin * lint * Update build-system/babel-plugins/babel-plugin-transform-jss/index.js Co-authored-by: Justin Ridgewell <justin@ridgewell.name> * Update build-system/babel-plugins/babel-plugin-transform-jss/index.js Co-authored-by: Justin Ridgewell <justin@ridgewell.name> * remove outdated option * lint * remove computer properties for browserify * Allow JSON.parse in jss files for conformance-config * test code also needs transform Co-authored-by: Justin Ridgewell <justin@ridgewell.name>
- Loading branch information
1 parent
cea6c61
commit 6f97154
Showing
26 changed files
with
485 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
build-system/babel-plugins/babel-plugin-transform-jss/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/** | ||
* Copyright 2020 The AMP HTML Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* Takes a .jss.js file and transforms the `useStyles` export to remove side effects | ||
* and directly return the classes map. Also includes special key 'CSS' in the classes | ||
* object with the entire CSS string. | ||
* | ||
* @example | ||
* In: | ||
* ``` | ||
* import {createUseStyles} from 'react-jss' | ||
* | ||
* const jss = { button: { fontSize: 12 }} | ||
* export const useStyles = createUseStyles(jss); | ||
* ``` | ||
* | ||
* Out: | ||
* ``` | ||
* const jss = { button: { fontSize: 12 }} | ||
* const _classes = {button: 'button-1', CSS: 'button-1 { font-size: 12 }'} | ||
* export const useStyles = () => _classes; | ||
* ``` | ||
*/ | ||
|
||
const {create} = require('jss'); | ||
const {default: preset} = require('jss-preset-default'); | ||
|
||
module.exports = function ({types: t, template}) { | ||
function isJssFile(filename) { | ||
return filename.endsWith('.jss.js'); | ||
} | ||
|
||
function compileJss(JSS) { | ||
const jss = create(); | ||
jss.setup(preset()); | ||
return jss.createStyleSheet(JSS); | ||
} | ||
|
||
return { | ||
visitor: { | ||
CallExpression(path, state) { | ||
// TODO: Can I skip the whole file if not jss? | ||
const {filename} = state.file.opts; | ||
if (!isJssFile(filename)) { | ||
return; | ||
} | ||
|
||
const callee = path.get('callee'); | ||
if (!callee.isIdentifier({name: 'createUseStyles'})) { | ||
return; | ||
} | ||
|
||
const {confident, value: JSS} = path.get('arguments.0').evaluate(); | ||
if (!confident) { | ||
throw path.buildCodeFrameError( | ||
`First argument to createUseStyles must be statically evaluatable.` | ||
); | ||
} | ||
const sheet = compileJss(JSS); | ||
if ('CSS' in sheet.classes) { | ||
throw path.buildCodeFrameError( | ||
'Cannot have class named CSS in your JSS object.' | ||
); | ||
} | ||
|
||
const id = path.scope.generateUidIdentifier('classes'); | ||
path.scope.push({ | ||
id, | ||
init: template.expression.ast`JSON.parse(${t.stringLiteral( | ||
JSON.stringify({ | ||
...sheet.classes, | ||
'CSS': sheet.toString(), | ||
}) | ||
)})`, | ||
}); | ||
|
||
path.replaceWith(template.expression.ast`(() => ${id})`); | ||
}, | ||
|
||
// Remove the import for react-jss | ||
ImportDeclaration(path, state) { | ||
const {filename} = state.file.opts; | ||
if (!isJssFile(filename)) { | ||
return; | ||
} | ||
|
||
if (path.node.source.value === 'react-jss') { | ||
path.remove(); | ||
} | ||
}, | ||
}, | ||
}; | ||
}; |
19 changes: 19 additions & 0 deletions
19
...abel-plugin-transform-jss/test/fixtures/transform-assertions/ignore-non-jss-file/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* Copyright 2020 The AMP HTML Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import {createUseStyles} from 'react-jss'; | ||
const JSS = {button: {fontSize: 12}}; | ||
export const useStyles = createUseStyles(JSS); |
5 changes: 5 additions & 0 deletions
5
...-plugin-transform-jss/test/fixtures/transform-assertions/ignore-non-jss-file/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"plugins": ["../../../.."], | ||
"sourceType": "module", | ||
"filename": "component.js" | ||
} |
22 changes: 22 additions & 0 deletions
22
...el-plugin-transform-jss/test/fixtures/transform-assertions/ignore-non-jss-file/output.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Copyright 2020 The AMP HTML Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { createUseStyles } from 'react-jss'; | ||
const JSS = { | ||
button: { | ||
fontSize: 12 | ||
} | ||
}; | ||
export const useStyles = createUseStyles(JSS); |
19 changes: 19 additions & 0 deletions
19
...l-plugin-transform-jss/test/fixtures/transform-assertions/should-throw-classname/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* Copyright 2020 The AMP HTML Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// Throws because cant use classname "CSS", as it conflicts with this transform. | ||
import {createUseStyles} from 'react-jss'; | ||
export const useStyles = createUseStyles({CSS: {fontSize: 12}}); |
6 changes: 6 additions & 0 deletions
6
...ugin-transform-jss/test/fixtures/transform-assertions/should-throw-classname/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"plugins": ["../../../.."], | ||
"sourceType": "module", | ||
"filename": "component.jss.js", | ||
"throws": "CSS" | ||
} |
21 changes: 21 additions & 0 deletions
21
...-plugin-transform-jss/test/fixtures/transform-assertions/should-throw-confidence/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* Copyright 2020 The AMP HTML Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// Throws because object spread is not statically evaluable. | ||
import {createUseStyles} from 'react-jss'; | ||
const foo = {foo: 7} | ||
const JSS = {...foo} | ||
export const useStyles = createUseStyles(JSS); |
6 changes: 6 additions & 0 deletions
6
...gin-transform-jss/test/fixtures/transform-assertions/should-throw-confidence/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"plugins": ["../../../.."], | ||
"sourceType": "module", | ||
"filename": "component.jss.js", | ||
"throws": "statically evaluatable" | ||
} |
20 changes: 20 additions & 0 deletions
20
...plugin-transform-jss/test/fixtures/transform-assertions/should-transform-jss-var/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* Copyright 2020 The AMP HTML Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import {createUseStyles} from 'react-jss' | ||
|
||
const JSS = {button: {fontSize: 12}}; | ||
|
||
export const useStyles = createUseStyles(JSS); |
5 changes: 5 additions & 0 deletions
5
...in-transform-jss/test/fixtures/transform-assertions/should-transform-jss-var/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"plugins": ["../../../.."], | ||
"sourceType": "module", | ||
"filename": "component.jss.js" | ||
} |
23 changes: 23 additions & 0 deletions
23
...ugin-transform-jss/test/fixtures/transform-assertions/should-transform-jss-var/output.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
var _classes = JSON.parse("{\"button\":\"button-0-2-1\",\"CSS\":\".button-0-2-1 {\\n font-size: 12px;\\n}\"}"); | ||
|
||
/** | ||
* Copyright 2020 The AMP HTML Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
const JSS = { | ||
button: { | ||
fontSize: 12 | ||
} | ||
}; | ||
export const useStyles = () => _classes; |
19 changes: 19 additions & 0 deletions
19
...plugin-transform-jss/test/fixtures/transform-assertions/should-transform-literal/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* Copyright 2020 The AMP HTML Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import {createUseStyles} from 'react-jss'; | ||
|
||
export const useStyles = createUseStyles({button: {fontSize: 12}}); |
5 changes: 5 additions & 0 deletions
5
...in-transform-jss/test/fixtures/transform-assertions/should-transform-literal/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"plugins": ["../../../.."], | ||
"sourceType": "module", | ||
"filename": "component.jss.js" | ||
} |
18 changes: 18 additions & 0 deletions
18
...ugin-transform-jss/test/fixtures/transform-assertions/should-transform-literal/output.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
var _classes = JSON.parse("{\"button\":\"button-0-3-1\",\"CSS\":\".button-0-3-1 {\\n font-size: 12px;\\n}\"}"); | ||
|
||
/** | ||
* Copyright 2020 The AMP HTML Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
export const useStyles = () => _classes; |
19 changes: 19 additions & 0 deletions
19
build-system/babel-plugins/babel-plugin-transform-jss/test/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* Copyright 2020 The AMP HTML Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
const runner = require('@babel/helper-plugin-test-runner').default; | ||
|
||
runner(__dirname); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,10 @@ module.exports = { | |
], | ||
}, | ||
}, | ||
{ | ||
test: /\.css$/i, | ||
use: ['style-loader', 'css-loader'], | ||
}, | ||
], | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.