简体   繁体   English

使用webpack ou npm在多个包中拆分javascript Javascript项目

[英]Split a javascript Javascript project in multiple package using webpack ou npm

I'm trying to build an application that have to run on Windows(PC), Android and iOS. 我正在尝试构建一个必须在Windows(PC),Android和iOS上运行的应用程序。

For this, I will use Electron (for Windows) and React Native (for mobile plateforms). 为此,我将使用Electron(用于Windows)和React Native(用于移动平台)。 Both will be built with React and Redux. 两者都将使用React和Redux构建。 This way, I will be able to implement the logic in Redux reducers and middlewares and use them in both projects. 这样,我将能够在Redux Reducer和中间件中实现逻辑,并在两个项目中使用它们。

From now, I have done a POC for the Electron app using webpack. 从现在开始,我使用webpack为Electron app做了POC。 The reducers are currently directly in the app code. Reducer目前直接在应用程序代码中。 I want to extract the Redux relative code in a seperate package to allow me using it in a the React Native project. 我想在一个单独的包中提取Redux相关代码,以允许我在React Native项目中使用它。

I can make an npm package for my Redux module containing the reducers and the middlwares but this way, when I will compile my application, webpack will not compile my seperate package, it will use the precompiled version. 我可以为包含reducers和middlware的Redux模块制作一个npm包,但这样,当我编译我的应用程序时,webpack将不会编译我的单独包,它将使用预编译版本。

So What I want is to be able to make a separate package but still compile it at application compile time (because it is still in developpement and the dev is very closely related to main application dev). 所以我想要的是能够创建一个单独的包,但仍然在应用程序编译时编译它(因为它仍处于开发阶段,并且dev与主应用程序开发人员密切相关)。

Do I have to do it only with npm or with webpack as well ? 我是否只能使用npm或webpack进行此操作?

I'm pretty new to the Javascript dev stack. 我是Javascript dev堆栈的新手。

I think you have different ways to handle that problem. 我认为你有不同的方法来处理这个问题。

  1. You can use NPM package. 您可以使用NPM包。 But in code of package, you will store not only original source code, but compiled code too. 但是在包的代码中,您不仅会存储原始源代码,还会存储编译代码。 I mean before publish that package, you'll need to compile it in normal ES5 code. 我的意思是在发布该包之前,您需要在普通的ES5代码中编译它。
  2. I think you can use submodule system provided by Git. 我想你可以使用Git提供的子模块系统。 You should have separate repository with common code of your reducers. 您应该有一个单独的存储库,其中包含reducers的公共代码。 And in each project (Electron, RN, etc.), you will have a directory with git submodule. 在每个项目(电子,RN等)中,您将拥有一个带有git子模块的目录。 Webpack should compile code in that directory normally without any problems. Webpack应该正常编译该目录中的代码而没有任何问题。

UPD: UPD:

About submodules you can read nice article here: https://git-scm.com/book/en/v2/Git-Tools-Submodules#Starting-with-Submodules 关于子模块你可以在这里阅读好文章: https//git-scm.com/book/en/v2/Git-Tools-Submodules#Starting-with-Submodules

In few words, in project it will looks like: 简而言之,在项目中它看起来像:

cd yourProjectFolder
git submodule add https://github.com/TalAter/awesome-service-workers submoduleDirectoryName

It will clone repository to your project, and create .gitmodules file. 它将克隆存储库到您的项目,并创建.gitmodules文件。 Code from submodule will not exists in current project repository. 子模块中的代码在当前项目存储库中不存在。 In remote repository it will contain only link to submodule, but on your machine, you will have full code and you will be able to compile it. 在远程存储库中,它只包含子模块的链接,但在您的机器上,您将拥有完整的代码,您将能够编译它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM