Babel.js 当汇总项目是另一个项目的依赖项时,如何绑定汇总项目

7gyucuyw  于 2023-01-15  发布在  Babel
关注(0)|答案(1)|浏览(181)

我试图创建一个React网站使用2项目:

  • React-kit用于存储组件列表
  • 运行React服务器的React应用程序(并且依赖React-kit)

我主要是按照this tutorial来使用Rollup(和babel)构建我的React套件。
我使用私有github存储库将React-Kit作为依赖项放入我的React-App中:(如果需要的话,我在问题的最后添加了我所有的构建文件(两个项目的rollup、babel和webpack)。)

    • 包. json**
{
  /* ... */
  "dependencies": {
    "react-kit": "github:myUser/react-Kit",  
    /* ... */
  }
}
    • 首先,**它在本地运行良好,这对我来说是一个大问题?谁做了这项工作?

在我的研究中,我注意到:

  • dist/存在于React-Kit github存储库中(但src/在这里)
  • dist/存在于我的本地React-App/node_modules/react-kit/上(只有package*.json没有src/
  • 当我在React-App上运行我的npm i时,React-kit被下载(这部分我理解^^),然后被他的汇总配置文件捆绑。

我的react-kit是如何捆绑的?当我在React-App上做npm i时,谁调用了启动汇总?

    • 第二个**我试图使用Jenkins在生产环境中部署React-App项目,但在本例中,我的react-kit/dist不在此处,并且React-App/node_modules/react-kit只有package*.json(因此我的react-App构建失败,因为无法导入react-kit)。

这是怎么回事?我尝试在本地和AWS上同时使用env(开发和生产),但结果总是一样的。
我想我漏掉了什么。

React试剂盒

    • 汇总配置js**
import babel from 'rollup-plugin-babel'
import commonjs from 'rollup-plugin-commonjs'
import external from 'rollup-plugin-peer-deps-external'
import postcss from 'rollup-plugin-postcss'
import resolve from 'rollup-plugin-node-resolve'
import url from 'rollup-plugin-url'
import svgr from '@svgr/rollup'
import { terser } from 'rollup-plugin-terser'

import pkg from './package.json'

export default {
  input: 'src/lib/index.js',
  output: [
    {
      file: pkg.main,
      format: 'cjs',
      sourcemap: true,
    },
    {
      file: pkg.module,
      format: 'es',
      sourcemap: true,
    },
  ],
  plugins: [
    external({
      // includeDependencies: true,
    }),
    postcss({
      plugins: [],
      minimize: true,
      sourceMap: 'inline',
    }),
    url(),
    svgr(),
    resolve(),
    babel({
      plugins: [
        '@babel/plugin-proposal-object-rest-spread',
        '@babel/plugin-proposal-optional-chaining',
        '@babel/plugin-syntax-dynamic-import',
        '@babel/plugin-proposal-class-properties',
        'transform-react-remove-prop-types',
      ],
      exclude: 'node_modules/**',
    }),
    commonjs(),
    // terser(),    // Activate to minimify
  ],
}
  • .巴伯尔中心*
{
  "presets": [
    [
      "@babel/preset-env",
      {
        "modules": false
      }
    ],
    "@babel/preset-react"
  ],
  "env": {
    "test": {
      "presets": [
        [
          "react-app"
        ]
      ]
    }
  },
  "plugins": [
    "@babel/plugin-proposal-object-rest-spread",
    "@babel/plugin-proposal-optional-chaining",
    "@babel/plugin-syntax-dynamic-import",
    "@babel/plugin-proposal-class-properties"
  ]
}
    • 包. json**
{
  "name": "react-kit",
  "version": "0.1.0",
  "description": "React components dictionary for Projects",
  "author": "",
  "license": "ISC",
  "main": "dist/index.js",
  "module": "dist/index.es.js",
  "jsnext:main": "dist/index.es.js",
  "engines": {
    "node": ">=8",
    "npm": ">=5"
  },
  "scripts": {
    "dev": "run-p build:watch start",
    "start": "styleguidist server --open",
    "styleguide:build": "styleguidist build",
    "build": "rollup -c",
    "build:watch": "rollup -c -w",
    "test": "jest",
    "test:coverage": "jest --coverage --forceExit --colors",
    "lint": "esw --ext .jsx --ext .js --color",
    "lint:fix": "npm run lint --fix",
    "prepare": "npm run build",
    "prerelease": "npm run lint:fix && npm run test:coverage && npm run build",
    "release": "npm publish",
    "predeploy": "npm run styleguide:build",
    "deploy": "gh-pages -d styleguide"
  },
  "dependencies": {
    "formik": "^1.5.8"
  },
  "peerDependencies": {
    "react": "^16.8.6",
    "react-dom": "^16.8.6",
    "react-router-dom": "^5.0.1"
  },
  "devDependencies": {
    "@babel/core": "^7.5.4",
    "@babel/plugin-proposal-class-properties": "^7.5.0",
    "@babel/plugin-proposal-object-rest-spread": "^7.5.4",
    "@babel/plugin-proposal-optional-chaining": "^7.2.0",
    "@babel/plugin-syntax-dynamic-import": "^7.2.0",
    "@babel/preset-env": "^7.5.4",
    "@babel/preset-react": "^7.0.0",
    "@svgr/rollup": "^4.3.1",
    "babel-core": "^7.0.0-bridge.0",
    "babel-eslint": "^10.0.2",
    "babel-jest": "^24.8.0",
    "babel-plugin-transform-react-remove-prop-types": "^0.4.24",
    "babel-preset-react-app": "^9.0.0",
    "cross-env": "^5.2.0",
    "enzyme": "^3.10.0",
    "enzyme-adapter-react-16": "^1.14.0",
    "eslint": "^6.0.1",
    "eslint-config-airbnb": "^17.1.1",
    "eslint-plugin-flowtype": "^3.11.1",
    "eslint-plugin-import": "^2.18.0",
    "eslint-plugin-jest": "^22.7.2",
    "eslint-plugin-jsx-a11y": "^6.2.3",
    "eslint-plugin-react": "^7.14.2",
    "eslint-plugin-react-hooks": "^1.6.1",
    "eslint-watch": "^5.1.2",
    "gh-pages": "^2.0.1",
    "jasmine-expect": "^4.0.2",
    "jest": "^24.8.0",
    "jest-pnp-resolver": "^1.2.1",
    "jest-resolve": "^24.8.0",
    "node-sass": "^4.12.0",
    "npm-run-all": "^4.1.5",
    "prop-types": "^15.7.2",
    "react": "^16.8.6",
    "react-app-polyfill": "^1.0.1",
    "react-dom": "^16.8.6",
    "react-router-dom": "^5.0.1",
    "react-styleguidist": "^9.1.11",
    "react-test-renderer": "^16.8.6",
    "rollup": "^1.16.7",
    "rollup-plugin-babel": "^4.3.3",
    "rollup-plugin-commonjs": "^10.0.1",
    "rollup-plugin-node-resolve": "^5.2.0",
    "rollup-plugin-peer-deps-external": "^2.2.0",
    "rollup-plugin-postcss": "^2.0.3",
    "rollup-plugin-terser": "^5.1.1",
    "rollup-plugin-url": "^2.2.2",
    "webpack": "^4.35.3",
    "webpack-blocks": "^2.0.1"
  },
  "files": [
    "dist"
  ],
  "jest": {
    "collectCoverageFrom": [
      "src/**/*.{js,jsx,ts,tsx}",
      "!src/**/*.d.ts",
      "!src/**/index.js"
    ],
    "resolver": "jest-pnp-resolver",
    "setupFiles": [
      "react-app-polyfill/jsdom",
      "<rootDir>/scripts/enzymeConfig.js"
    ],
    "testMatch": [
      "**/__tests__/**/*.[jt]s?(x)",
      "**/?(*.)+(spec|test).[jt]s?(x)"
    ],
    "testEnvironment": "jsdom",
    "testURL": "http://localhost",
    "transform": {
      "^.+\\.(js|jsx|ts|tsx)$": "<rootDir>/node_modules/babel-jest",
      "^.+\\.css$": "<rootDir>/scripts/cssTransform.js",
      "^(?!.*\\.(js|jsx|ts|tsx|css|json)$)": "<rootDir>/scripts/fileTransform.js"
    },
    "transformIgnorePatterns": [
      "[/\\\\]node_modules[/\\\\].+\\.(js|jsx|ts|tsx)$",
      "^.+\\.module\\.(css|sass|scss)$"
    ],
    "moduleDirectories": [
      "node_modules",
      "src"
    ],
    "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"
    ]
  }
}

React应用程序

    • 网络包配置js**
const webpack = require('webpack');
const path = require('path');
const Dotenv = require('dotenv-webpack');

const env = process.env.NODE_ENV || 'development';

const config = {
  entry: './src/index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js'
  },
  plugins: [
    new Dotenv({
      path: `.env.${env !== 'development' ? env : ''}`,
    })
  ],
  module: {
    rules: [
      {
        test: /\.m?js$/,
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env']
          }
        }
      },
      {
        test: /\.css$/,
        use: [
          'style-loader',
          'css-loader'
        ]
      },
      {
        test: /\.less$/,
        use: [
          'style-loader',
          'css-loader',
          'less-loader'
        ]
      }
    ]
  },
  resolve: {
    extensions: [
      '.js',
      '.jsx'
    ]
  }
}

module.exports = config;
    • 巴别塔配置js**
module.exports = function (api) {
  api.cache(true);
  return {
    "presets": [
      "@babel/preset-env",
      "@babel/preset-react"
    ],
    "plugins": [
      "@babel/plugin-proposal-class-properties"
    ]
  }
};
  • .巴伯尔中心*
{
  "presets": [
    [
      "@babel/preset-env",
      {
        "modules": false
      },
      "@babel/preset-react'"
    ],
  ],
  "plugins": [
    ["@babel/transform-runtime"]
  ]
}
ycl3bljg

ycl3bljg1#

NPM documentation中,我了解到prepare是在本地环境中用npm install调用的,没有参数。
prepare脚本调用build脚本,然后build脚本调用rollup命令

相关问题