From 81c5715d0ba3396f8d72675563465595b66f2a56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ehrlich?= Date: Wed, 31 Oct 2018 23:49:09 +0100 Subject: [PATCH] fix: show hint for missing babel-core package --- packages/cli/src/api/compat.js | 45 +++++++++++++++++++ packages/cli/src/api/extractors/babel.js | 2 +- packages/cli/src/api/extractors/typescript.js | 2 +- 3 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 packages/cli/src/api/compat.js diff --git a/packages/cli/src/api/compat.js b/packages/cli/src/api/compat.js new file mode 100644 index 000000000..7c27089ef --- /dev/null +++ b/packages/cli/src/api/compat.js @@ -0,0 +1,45 @@ +import chalk from "chalk" +import * as core from "babel-core" + +function catchBabelVersionMismatch(fn) { + return function() { + try { + fn.apply(null, arguments) + } catch (e) { + if ( + e.message.startsWith( + "Plugin/Preset files are not allowed to export objects" + ) + ) { + const { makeInstall } = require("../lingui-init") + const install = makeInstall() + console.log(chalk.red("Please install missing Babel 6 core package:")) + console.log() + console.log(install("babel-core@^6.0.0", true)) + console.log() + + process.exit(1) + } else if ( + e.message.startsWith( + 'Requires Babel "^7.0.0-0", but was loaded with "6.26.3".' + ) + ) { + const { makeInstall } = require("../lingui-init") + const install = makeInstall() + console.log(chalk.red("Please install missing Babel 7 core packages:")) + console.log() + console.log(install("babel-core@^7.0.0-bridge.0 @babel/core", true)) + console.log() + + process.exit(1) + } else { + throw e + } + } + } +} + +export const transform = catchBabelVersionMismatch(core.transform) +export const transformFileSync = catchBabelVersionMismatch( + core.transformFileSync +) diff --git a/packages/cli/src/api/extractors/babel.js b/packages/cli/src/api/extractors/babel.js index 1a11c4747..c1e500471 100644 --- a/packages/cli/src/api/extractors/babel.js +++ b/packages/cli/src/api/extractors/babel.js @@ -1,5 +1,5 @@ // @flow -import { transformFileSync } from "babel-core" +import { transformFileSync } from "../compat" import linguiTransformJs from "@lingui/babel-plugin-transform-js" import linguiTransformReact from "@lingui/babel-plugin-transform-react" diff --git a/packages/cli/src/api/extractors/typescript.js b/packages/cli/src/api/extractors/typescript.js index 3242e4c2e..c055f149e 100644 --- a/packages/cli/src/api/extractors/typescript.js +++ b/packages/cli/src/api/extractors/typescript.js @@ -1,6 +1,6 @@ // @flow import fs from "fs" -import { transform } from "babel-core" +import { transform } from "../compat" import linguiTransformJs from "@lingui/babel-plugin-transform-js" import linguiTransformReact from "@lingui/babel-plugin-transform-react"