gpt4 book ai didi

node.js - 一个项目有多个 package.json 文件

转载 作者:IT老高 更新时间:2023-10-28 23:05:48 24 4
gpt4 key购买 nike

我对现代 JS 开发比较陌生,我需要有关我所处的这种情况的帮助或建议。

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

要求:通过为每个构建使用不同的包和/或配置文件来减少浏览器版本之间的项目维护。

问题:从我目前所做的研究来看,似乎不可能在同一个项目中使用不同的 package.json 文件和 node_modules 文件夹以及选定的工具:npm、Webpack、React、Redux、Typescript。 Yarn 似乎支持多个 package.json 文件,但我们希望尽可能避免从 npm 迁移。

当前项目结构:

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

我认为可能有用的是引入 IE8 子文件夹及其 package.json 和 node_modules 文件夹,然后以某种方式为构建任务引用该文件夹,但现在我忘记了如何告诉 npm 在构建时引用它。

建议的项目结构:

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

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

那么,任何人都可以肯定地告诉我这是不可能的或提供一些指导吗? This是迄今为止我找到的最接近的答案,但听起来不是最终答案。或者,这样的项目结构是否可以支持所需的内容,或者将项目分成两个是最好的选择?

提前致谢。

最佳答案

好的,经过更多研究后,我偶然发现了 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

然后,在 components/index.jsx 中,我根据全局变量为不同版本指定了 require 命令:

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 版本的唯一方法。在整个项目更改为这个模型之后,我将不得不看看这种方法会出现什么问题。

在 webpack.config.js 中,我配置了两个导出 - 一个用于现代,一个用于旧版。每个指向不同的入口 index.jsx 文件,使用 webpack.DefinePlugin 将全局变量设置为“legacy”或“modern”,并指定要解析的公共(public)组件模块的路径:['node_modules', path.resolve( __dirname, '组件')]

单个项目输出的 webpack.config 如下所示:

{
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/
}
]
}
}

请随意发表评论或指出问题,但我希望这对将来的人有所帮助! :)

关于node.js - 一个项目有多个 package.json 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50471757/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com