From dc6d448b3c6e0f0b7f960d9c21db24543d8b4f4a Mon Sep 17 00:00:00 2001 From: Olivier Tassinari Date: Sun, 24 Jun 2018 00:43:40 +0200 Subject: [PATCH] [core] Upgrade preval (#11958) --- .babelrc | 4 +- docs/src/modules/utils/findPages.js | 2 +- docs/src/pages/style/typography/typography.md | 4 +- package.json | 2 +- scripts/get-replacement.js | 24 -- scripts/material-ui-babel-preval.js | 213 ------------------ yarn.lock | 16 +- 7 files changed, 14 insertions(+), 251 deletions(-) delete mode 100644 scripts/get-replacement.js delete mode 100644 scripts/material-ui-babel-preval.js diff --git a/.babelrc b/.babelrc index 1c831a3cb06f3d..1889fd4c7f5dfa 100644 --- a/.babelrc +++ b/.babelrc @@ -30,7 +30,7 @@ }, "docs-development": { "plugins": [ - "./scripts/material-ui-babel-preval", + "babel-plugin-preval", [ "module-resolver", { @@ -48,7 +48,7 @@ }, "docs-production": { "plugins": [ - "./scripts/material-ui-babel-preval", + "babel-plugin-preval", [ "module-resolver", { diff --git a/docs/src/modules/utils/findPages.js b/docs/src/modules/utils/findPages.js index 0cc02ff63a498f..d46b100f25e353 100644 --- a/docs/src/modules/utils/findPages.js +++ b/docs/src/modules/utils/findPages.js @@ -1,4 +1,4 @@ -import { findPages } from './find'; +import { findPages } from 'docs/src/modules/utils/find'; const pages = findPages({ front: true, diff --git a/docs/src/pages/style/typography/typography.md b/docs/src/pages/style/typography/typography.md index 6d7bf04591763c..470773e3d71ef8 100644 --- a/docs/src/pages/style/typography/typography.md +++ b/docs/src/pages/style/typography/typography.md @@ -4,6 +4,8 @@ components: Typography # Typography +Use typography to present your design and content as clearly and efficiently as possible. + Too many type sizes and styles at once can spoil any layout. A [typographic scale](https://material.io/design/typography/#type-scale) has a limited set of type sizes that work well together along with the layout grid. @@ -42,7 +44,7 @@ Material-UI default typography configuration only relies on 300, 400 and 500 fon {{"demo": "pages/style/typography/Types.js"}} -## CSS in JS +## Theme In some situation you might not be able to use the `Typography` component. Hopefully, you might be able to take advantage of the [`typography`](/customization/default-theme?expend-path=$.typography) keys of the theme. diff --git a/package.json b/package.json index 89809b416828ba..2024b020344245 100644 --- a/package.json +++ b/package.json @@ -75,7 +75,7 @@ "babel-loader": "8.0.0-beta.2", "babel-plugin-istanbul": "^4.1.5", "babel-plugin-module-resolver": "^3.0.0", - "babel-plugin-preval": "^1.6.4", + "babel-plugin-preval": "^2.0.0", "babel-plugin-react-remove-properties": "^0.2.5", "babel-plugin-transform-dev-warning": "^0.1.0", "babel-plugin-transform-react-constant-elements": "^6.23.0", diff --git a/scripts/get-replacement.js b/scripts/get-replacement.js deleted file mode 100644 index 527f1199a88312..00000000000000 --- a/scripts/get-replacement.js +++ /dev/null @@ -1,24 +0,0 @@ -/* eslint-disable */ -// Waiting for https://github.com/kentcdodds/babel-plugin-preval/issues/52 - -'use strict'; - -var requireFromString = require('require-from-string'); -var objectToAST = require('babel-plugin-preval/dist/object-to-ast'); - -module.exports = getReplacement; - -function getReplacement(_ref) { - var stringToPreval = _ref.string, - filename = _ref.filename, - babel = _ref.babel; - - var _babel$transform = babel.transform(stringToPreval, { - filename, - }), - code = _babel$transform.code; - - var transpiled = `require('@babel/register');\n${code}`; - var val = requireFromString(transpiled, filename); - return objectToAST(val); -} diff --git a/scripts/material-ui-babel-preval.js b/scripts/material-ui-babel-preval.js deleted file mode 100644 index bfc7839be0f3a2..00000000000000 --- a/scripts/material-ui-babel-preval.js +++ /dev/null @@ -1,213 +0,0 @@ -/* eslint-disable */ -// Waiting for https://github.com/kentcdodds/babel-plugin-preval/issues/52 - -'use strict'; - -function _toConsumableArray(arr) { - if (Array.isArray(arr)) { - for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { - arr2[i] = arr[i]; - } - return arr2; - } else { - return Array.from(arr); - } -} - -function _toArray(arr) { - return Array.isArray(arr) ? arr : Array.from(arr); -} - -var p = require('path'); -// const printAST = require('ast-pretty-print') -var getReplacement = require('./get-replacement'); -var objectToAST = require('babel-plugin-preval/dist/object-to-ast'); - -module.exports = prevalPlugin; - -function prevalPlugin(babel) { - var t = babel.types, - template = babel.template, - transformFromAst = babel.transformFromAst; - - var assignmentBuilder = template('const NAME = VALUE'); - return { - name: 'preval', - visitor: { - Program(path, _ref) { - var filename = _ref.file.opts.filename; - - var firstNode = path.node.body[0] || {}; - var comments = firstNode.leadingComments || []; - var isPreval = comments.some(isPrevalComment); - - if (!isPreval) { - return; - } - - comments.find(isPrevalComment).value = ' this file was prevaled'; - - var _transformFromAst = transformFromAst(path.node), - string = _transformFromAst.code; - - var replacement = getReplacement({ string, filename, babel }); - - var moduleExports = Object.assign( - {}, - t.expressionStatement( - t.assignmentExpression( - '=', - t.memberExpression(t.identifier('module'), t.identifier('exports')), - replacement, - ), - ), - { leadingComments: comments }, - ); - - path.replaceWith(t.program([moduleExports])); - }, - TaggedTemplateExpression(path, _ref2) { - var filename = _ref2.file.opts.filename; - - var isPreval = path.node.tag.name === 'preval'; - if (!isPreval) { - return; - } - var string = path.get('quasi').evaluate().value; - if (!string) { - throw new Error('Unable to determine the value of your preval string'); - } - var replacement = getReplacement({ string, filename, babel }); - path.replaceWith(replacement); - }, - ImportDeclaration(path, _ref3) { - var filename = _ref3.file.opts.filename; - - var isPreval = looksLike(path, { - node: { - source: { - leadingComments(comments) { - return comments && comments.some(isPrevalComment); - }, - }, - }, - }); - if (!isPreval) { - return; - } - var prevalComment = path.node.source.leadingComments.find(isPrevalComment).value.trim(); - var args = void 0; - if (prevalComment !== 'preval') { - args = prevalComment.replace(/preval\((.*)\)/, '$1').trim(); - } - - var replacement = getReplacement({ - string: ` - try { - // allow for transpilation of required modules - require('@babel/register') - } catch (e) { - // ignore error - } - var mod = require('${path.node.source.value}'); - mod = mod && mod.__esModule ? mod.default : mod - ${args ? `mod = mod(${args})` : ''} - module.exports = mod - `, - filename, - babel, - }); - path.replaceWith( - assignmentBuilder({ - NAME: t.identifier(path.node.specifiers[0].local.name), - VALUE: replacement, - }), - ); - }, - CallExpression(path, _ref4) { - var filename = _ref4.file.opts.filename; - - var isPreval = looksLike(path, { - node: { - callee: { - type: 'MemberExpression', - object: { name: 'preval' }, - property: { name: 'require' }, - }, - }, - }); - if (!isPreval) { - return; - } - - var _path$get = path.get('arguments'), - _path$get2 = _toArray(_path$get), - source = _path$get2[0], - args = _path$get2.slice(1); - - var argValues = args.map(function(a) { - var result = a.evaluate(); - if (!result.confident) { - throw new Error('preval cannot determine the value of an argument in preval.require'); - } - return result.value; - }); - var absolutePath = p.resolve(p.dirname(filename), source.node.value); - try { - // allow for transpilation of required modules - require('@babel/register'); - } catch (e) { - // ignore error - } - var mod = require(absolutePath); - if (argValues.length) { - if (typeof mod !== 'function') { - throw new Error( - `\`preval.require\`-ed module (${ - source.node.value - }) cannot accept arguments because it does not export a function. You passed the arguments: ${argValues.join( - ', ', - )}`, - ); - } - mod = mod.apply(undefined, _toConsumableArray(argValues)); - } - path.replaceWith(objectToAST(mod)); - }, - }, - }; -} - -function isPrevalComment(comment) { - var normalisedComment = comment.value - .trim() - .split(' ')[0] - .trim(); - return normalisedComment.startsWith('preval') || normalisedComment.startsWith('@preval'); -} - -function looksLike(a, b) { - return ( - a && - b && - Object.keys(b).every(function(bKey) { - var bVal = b[bKey]; - var aVal = a[bKey]; - if (typeof bVal === 'function') { - return bVal(aVal); - } - return isPrimitive(bVal) ? bVal === aVal : looksLike(aVal, bVal); - }) - ); -} - -function isPrimitive(val) { - // eslint-disable-next-line - return val == null || /^[sbn]/.test(typeof val); -} - -/* -eslint - import/no-unassigned-import:0 - import/no-dynamic-require:0 -*/ diff --git a/yarn.lock b/yarn.lock index e529110961aa69..145966419f3bdd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1879,7 +1879,7 @@ babel-plugin-istanbul@^4.1.5: istanbul-lib-instrument "^1.10.1" test-exclude "^4.2.1" -babel-plugin-macros@^2.0.0: +babel-plugin-macros@^2.2.2: version "2.2.2" resolved "https://registry.yarnpkg.com/babel-plugin-macros/-/babel-plugin-macros-2.2.2.tgz#049c93f4b934453688a6ec38bba529c55bf0fa1f" dependencies: @@ -1895,14 +1895,12 @@ babel-plugin-module-resolver@^3.0.0: reselect "^3.0.1" resolve "^1.4.0" -babel-plugin-preval@^1.6.4: - version "1.6.4" - resolved "https://registry.yarnpkg.com/babel-plugin-preval/-/babel-plugin-preval-1.6.4.tgz#96febe8172b3ca6c3d03ed96eeb0382ba4b18056" +babel-plugin-preval@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/babel-plugin-preval/-/babel-plugin-preval-2.0.0.tgz#7e30dce715d7e6d8e22e9ca81d029522f1fc4c8a" dependencies: - babel-plugin-macros "^2.0.0" - babel-register "^6.26.0" - babylon "^6.18.0" - require-from-string "^2.0.1" + babel-plugin-macros "^2.2.2" + require-from-string "^2.0.2" babel-plugin-react-remove-properties@^0.2.5: version "0.2.5" @@ -9285,7 +9283,7 @@ require-directory@^2.1.1: version "2.1.1" resolved "https://registry.yarnpkg.com/require-directory/-/require-directory-2.1.1.tgz#8c64ad5fd30dab1c976e2344ffe7f792a6a6df42" -require-from-string@^2.0.1: +require-from-string@^2.0.1, require-from-string@^2.0.2: version "2.0.2" resolved "https://registry.yarnpkg.com/require-from-string/-/require-from-string-2.0.2.tgz#89a7fdd938261267318eafe14f9c32e598c36909"