Синтаксическая ошибка Babel при сборке компонентов React (с типизацией потока) - PullRequest
0 голосов
/ 10 января 2020

Я написал набор компонентов React, которые я sh опубликую sh. Однако, когда я пытаюсь собрать их в общий формат js с babel перед публикацией, я получаю следующую ошибку, которую не может найти разрешение до.

Проект равен создан и извлечен из create-react-app

SyntaxError: src/packages/core/src/MyAppBar/MyAppBar.js: Unexpected token, expected ; (20:5)
  18 | import {isEmpty} from "lodash";
  19 | 
> 20 | type MyAppBarPropsType = {
     |      ^
  21 |     position?: string,
  22 |     commandBarActions?: Array<React$Element<React$ElementType>>,
  23 |     title?: string,

Строка сборки в моих npm скриптах:

    "build:commonjs": "cross-env NODE_ENV=production babel ./src/packages/core/src --out-dir ./lib --ignore spec.js"

Я бы подумал, что @babel/preset-flow было бы достаточно, чтобы иметь дело с типами потока в моем коде.

Предустановки из моего babel.config.js файла следующие:

presets: ['@babel/preset-flow', '@babel/preset-react', ['@babel/preset-env', {modules: 'commonjs'}]],

Для полноты моих полных package.json и babel.config.js

пакет. json

{
  "name": "react-ccl",
  "version": "0.2.0-alpha",
  "description": "Common component library based on xyz look and feel guidelines",
  "private": true,
  "dependencies": {
    "@babel/core": "7.6.0",
    "@material-ui/core": "3.9.2",
    "@material-ui/icons": "3.0.2",
    "@material-ui/lab": "3.0.0-alpha.30",
    "@material-ui/styles": "3.0.0-alpha.10",
    "@svgr/webpack": "4.3.2",
    "@typescript-eslint/eslint-plugin": "^2.2.0",
    "@typescript-eslint/parser": "^2.2.0",
    "babel-eslint": "10.0.3",
    "babel-jest": "^24.9.0",
    "babel-loader": "8.0.6",
    "babel-plugin-named-asset-import": "^0.3.4",
    "babel-preset-react-app": "^9.0.2",
    "camelcase": "^5.2.0",
    "case-sensitive-paths-webpack-plugin": "2.2.0",
    "css-loader": "2.1.1",
    "dotenv": "6.2.0",
    "dotenv-expand": "5.1.0",
    "eslint": "^6.6.0",
    "eslint-config-react-app": "^5.0.2",
    "eslint-loader": "3.0.0",
    "eslint-plugin-flowtype": "3.13.0",
    "eslint-plugin-import": "2.18.2",
    "eslint-plugin-jsx-a11y": "6.2.3",
    "eslint-plugin-react": "7.16.0",
    "eslint-plugin-react-hooks": "^1.6.1",
    "file-loader": "3.0.1",
    "fs-extra": "7.0.1",
    "highlight": "^0.2.4",
    "highlight.js": "^9.10.0",
    "html-webpack-plugin": "4.0.0-beta.5",
    "identity-obj-proxy": "3.0.0",
    "is-wsl": "^1.1.0",
    "lodash": "^4.17.15",
    "mini-css-extract-plugin": "0.8.0",
    "optimize-css-assets-webpack-plugin": "5.0.3",
    "pnp-webpack-plugin": "1.5.0",
    "postcss-flexbugs-fixes": "4.1.0",
    "postcss-loader": "3.0.0",
    "postcss-normalize": "7.0.1",
    "postcss-preset-env": "6.7.0",
    "postcss-safe-parser": "4.0.1",
    "react": "^16.10.0",
    "react-addons-test-utils": "15.5.1",
    "react-app-polyfill": "^1.0.3",
    "react-dev-utils": "^9.0.4",
    "react-dom": "^16.10.0",
    "react-test-renderer": "16.10.0",
    "resolve": "1.12.0",
    "resolve-url-loader": "3.1.0",
    "sass-loader": "7.2.0",
    "semver": "6.3.0",
    "style-loader": "1.0.0",
    "terser-webpack-plugin": "1.4.1",
    "ts-pnp": "1.1.4",
    "url-loader": "2.1.0",
    "webpack": "4.40.2",
    "webpack-dev-server": "3.2.1",
    "webpack-manifest-plugin": "2.0.4",
    "workbox-webpack-plugin": "4.3.1"
  },
  "devDependencies": {
    "@babel/cli": "7.7.7",
    "@babel/node": "7.7.7",
    "@babel/plugin-proposal-class-properties": "7.2.1",
    "@babel/plugin-syntax-dynamic-import": "7.2.0",
    "@babel/plugin-transform-object-assign": "7.2.0",
    "@babel/plugin-transform-react-constant-elements": "7.6.0",
    "@babel/plugin-transform-runtime": "7.6.2",
    "@babel/preset-env": "7.7.7",
    "@babel/preset-flow": "7.7.4",
    "@babel/preset-react": "7.7.4",
    "@babel/register": "7.0.0",
    "babel-cli": "^6.26.0",
    "chokidar": "1.6.1",
    "cpx": "^1.5.0",
    "cross-env": "^6.0.3",
    "enzyme": "^3.10.0",
    "enzyme-adapter-react-16": "^1.15.1",
    "enzyme-to-json": "^3.4.3",
    "flow-bin": "0.113.0",
    "flow-typed": "^2.6.2",
    "jest": "24.9.0",
    "jest-environment-jsdom-fourteen": "1.0.1",
    "jest-resolve": "24.9.0",
    "jest-watch-typeahead": "0.4.0",
    "lerna": "3.16.4",
    "npm-run-all": "4.0.2",
    "prop-types": "15.7.2",
    "rimraf": "3.0.0",
    "react-docgen": "3.0.0"
  },
  "scripts": {
    "prestart": "npm run gen:docs",
    "start": "npm-run-all --parallel start:docs gen:docs-watch",
    "start:docs": "node scripts/start.js",
    "gen:docs": "node scripts/generateComponentData.js",
    "gen:docs-watch": "npm run gen:docs -- --watch",
    "build:docs": "node scripts/build.js",
    "test": "node scripts/test.js",
    "predeploy:docs": "npm run build:docs",
    "deploy:docs": "gh-pages -d build",
    "flow": "flow",
    "build:commonjs": "cross-env NODE_ENV=production babel ./src/packages/core/src --out-dir ./lib --ignore spec.js"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "jest": {
    "roots": [
      "<rootDir>/src/packages/core"
    ],
    "collectCoverageFrom": [
      "src/**/*.{js,jsx,ts,tsx}",
      "!src/**/*.d.ts"
    ],
    "setupFiles": [
      "react-app-polyfill/jsdom"
    ],
    "setupFilesAfterEnv": [
      "<rootDir>/jest-test-setup.js"
    ],
    "testMatch": [
      "<rootDir>/src/**/__tests__/**/*.{js,jsx,ts,tsx}",
      "<rootDir>/src/**/*.{spec,test}.{js,jsx,ts,tsx}"
    ],
    "testEnvironment": "jest-environment-jsdom-fourteen",
    "transform": {
      "^.+\\.(js|jsx|ts|tsx)$": "<rootDir>/node_modules/babel-jest",
      "^.+\\.css$": "<rootDir>/config/jest/cssTransform.js",
      "^(?!.*\\.(js|jsx|ts|tsx|css|json)$)": "<rootDir>/config/jest/fileTransform.js"
    },
    "transformIgnorePatterns": [
      "[/\\\\]node_modules[/\\\\].+\\.(js|jsx|ts|tsx)$",
      "^.+\\.module\\.(css|sass|scss)$"
    ],
    "modulePaths": [],
    "moduleNameMapper": {
      "^react-native$": "react-native-web",
      "^.+\\.module\\.(css|sass|scss)$": "identity-obj-proxy"
    },
    "moduleFileExtensions": [
      "web.js",
      "js",
      "web.ts",
      "ts",
      "web.tsx",
      "tsx",
      "json",
      "web.jsx",
      "jsx",
      "node"
    ],
    "watchPlugins": [
      "jest-watch-typeahead/filename",
      "jest-watch-typeahead/testname"
    ]
  }
}

babel.config. js


const productionPlugins = [
    '@babel/plugin-transform-react-constant-elements',
    ['babel-plugin-react-remove-properties', { properties: ['data-mui-test'] }],
    [
        'babel-plugin-transform-react-remove-prop-types',
        {
            mode: 'unsafe-wrap',
        },
    ],
];

module.exports = {
    presets: ['@babel/preset-flow', '@babel/preset-react', ['@babel/preset-env', {modules: 'commonjs'}]],
    plugins: [
        ['@babel/plugin-proposal-class-properties', { loose: true }],
        ['@babel/plugin-proposal-object-rest-spread', { loose: true }],
        '@babel/plugin-transform-runtime',
        // for IE 11 support
        '@babel/plugin-transform-object-assign',
    ],
    ignore: [/@babel[\\|/]runtime/], // Fix a Windows issue.
    env: {
        cjs: {
            plugins: productionPlugins,
        },
    },
};

1 Ответ

0 голосов
/ 10 января 2020

Очевидно, preset-flow было недостаточно для того, чтобы babel передавал тип потока, определенный в файле JS. Когда я добавил следующую строку в devDependencies в моем package.json, сборка работала отлично.

    "babel-plugin-react-remove-properties": "^0.3.0",
...