我对现代JS开发还比较陌生,我需要帮助或建议.

Situation:我们有一个支持IE8的React-Typescript Redux项目(React 0.14).现在我们正在升级到IE11和React 16,但IE8应该得到支持.

Requirement:通过 for each 版本使用不同的包和/或配置文件,减少浏览器版本之间的项目维护.

Problem:根据我目前的研究,似乎不可能使用不同的包装.同一个项目中的json文件和node_模块文件夹,使用选定的工具:npm、Webpack、React、Redux、Typescript.Yarn 似乎支持多种包装.json文件,但如果可能的话,我们希望避免从npmmigrations.

目前的项目 struct :

project_root/
  node_modules/
  src/
    components/
    utils/
    index.tsx
    components.css
  index.html
  package.json
  tsconfig.json
  webpack.config.json

我认为可能有效的方法是介绍IE8子文件夹及其软件包.json和node_模块文件夹,然后以某种方式引用该文件夹进行构建任务,但现在我不知道如何告诉npm在构建时引用它.

拟议项目 struct :

project_root/
  ie8/
   node_modules/
   package.json
  node_modules/
  src/
    components/
    utils/
    index.tsx
    components.css
  index.html
  package.json
  tsconfig.json
  webpack.config.json

我try 了在网上找到的不同的东西,包括resolve.modules: [__dirname + "/ie8/node_modules"]个,但它似乎不起作用,或者我误解了它的作用,因为我在几个文件上得到了Cannot find name 'require'个错误,而Typescript 2.8.3在终端输出中被引用,而不是2.3.4.如果没有它,项目将使用IE11的配置进行构建.

那么,有人能肯定地告诉我这是不可能的,或者提供一些指导吗?This是迄今为止我找到的最接近的答案,但听起来不是最终答案.或者,像这样的项目 struct 能支持所需的内容吗?或者将项目一分为二是最好的 Select ?

提前谢谢.

推荐答案

好吧,经过更多的研究,我偶然发现了Lerna个,这基本上可以让我做我想做的事(根据我目前所看到的).它需要特定的项目树设置,如下所示:

project_root/
  node_modules/
  packages/
    components/ // Components shared between projects
      components/
       MyComponent.jsx
      index.jsx

  legacy/
     output/
      build.js // React 0.14 build
     node_modules/
     package.json // project specific dependencies
     index.jsx // project specific entry
     .babelrc

  modern/
     output/
      build.js // React 16 build
     node_modules/
     package.json // project specific dependencies
     index.jsx // project specific entry
     .babelrc

  package.json // contains devDependencies shared between projects
  lerna.json
  webpack.config.js
  index.html

然后,在组件/索引中.jsx I指定了基于全局变量的不同版本需要的命令:

if(PROJECT_SRC == "legacy"){
    React = require('../legacy/node_modules/react');
    ReactDOM = require('../legacy/node_modules/react-dom');
} else {
    React = require('../modern/node_modules/react');
    ReactDOM = require('../modern/node_modules/react-dom');
}

注意:这可能是一种不好的做法,但目前我唯一可以在构建中包含不同版本的React.在整个项目对这个模型进行更改之后,我必须看看这种方法会出现什么问题.

在网页包里.配置.js I配置了两个导出——一个用于modern,一个用于legacy.每个都指向不同的条目索引.jsx文件,使用网页包.DefinePlugin将全局变量设置为"legacy"或"modern",并指定要解析的公共组件模块的路径:['node_modules', path.resolve(__dirname, 'components')]

网页包.单个项目输出的配置如下所示:

{
        entry: "./packages/legacy/index.jsx",
        target: "web", 
        output: 
        {
            filename: "build.js",
            path: __dirname + "/packages/legacy/dist/",
            libraryTarget: "var",
            library: "lib_name"
        },
        devtool: "source-map",
        resolve: {
            extensions: [".js", ".jsx", ".json"],
            modules: ['node_modules', path.resolve(__dirname, 'components')]
        },
        plugins: plugins_legacy,
        module: {
            loaders: [
                {
                    test: /\.jsx?$/,
                    loader: "babel-loader",
                    exclude: /node_modules/
                }
            ]
        }  
    }

请随意 comments 或指出问题,但我希望这将在future 对某人有所帮助!:)

Typescript相关问答推荐

Angular 17 -如何使用新的@if语法在对象中使用Deliverc值

泛型和数组:为什么我最终使用了`泛型T []`而不是`泛型T []`?<><>

在动态对话框中使用STEP组件实现布线

使用泛型keyof索引类型以在if-condition内类型推断

未在DIST中正确编译TypeScrip src模块导入

Material UI / MUI系统:我如何告诉TypeScript主题是由提供程序传递的?

分解对象时出现打字错误

类型TTextKey不能用于索引类型 ;TOption

MatTool提示在角垫工作台中不起作用

vue-tsc失败,错误引用项目可能无法禁用vue项目上的emit

如何创建由空格连接的多个字符串的类型

如何在Angular 服务中制作同步方法?

将接口映射到交叉口类型

如何定义这样一个TypeScript泛型,遍历路由配置树并累积路径

如何扩展RxJS?

Typescript .根据枚举输入参数返回不同的对象类型

为什么看起来相似的代码在打字时会产生不同的错误?

如何在TypeScript中声明逆变函数成员?

数据库中的表请求返回如下日期:2023-07-20T21:39:00.000Z 我需要将此数据格式化为 dd/MM/yyyy HH:mm

从 npm 切换到 pnpm 后出现Typescript 错误