[英]Building monorepo babel-transpiled node JS application with dependencies
I am working on a project that is hosted as a monorepo. 我正在做一个作为monorepo托管的项目。 For simplification purposes let's say that inside there are three self-explanatory packages:
server
, a webapp
client and library
. 为了简化起见,假设内部有三个不言自明的软件包:
server
, webapp
client和library
。 The directory structure would be something like the following: 目录结构将类似于以下内容:
the-project
packages
server
src
webapp
src
library
src
All packages employ flow type notation, use a few >ES5 features and, for this reason, go through babel transpilation. 所有软件包都使用流类型表示法,使用一些> ES5功能,因此,需要进行babel转译。 The key difference is that transpilation of the
webapp
package is done via webpack, whereas server
employs a gulp task that triggers script transpilation through the gulp-babel
package. 关键区别在于,
webapp
软件包的转译是通过webpack进行的,而server
采用了gulp任务,该任务通过gulp gulp-babel
软件包触发了脚本的转译。 library
is transpiled automatically when web
is built. 建立
web
时, library
会自动转译。
Now, the problem I have is that for server
to build, babel requires library
to be built first and its package.json
to specify its (built) main JS source file so its transpiled artifacts can be included. 现在,我的问题是要构建
server
,babel要求首先构建library
,并要求其package.json
指定其(已构建)主JS源文件,以便可以包含其转译的工件。 As you can imagine, this would quickly become problematic if the project were to contain multiple libraries that are actively being developed (which it does), as all would require building, including any dependent packages (like server
in this simple case). 可以想象,如果项目包含多个正在积极开发的库(确实如此),这将很快成为问题,因为所有库都需要构建,包括任何依赖包(在这种简单情况下为
server
)。
As an attempt to overcome this annoyance, I initially thought of using webpack to build the server, which would take care of including whatever dependencies it requires into a bundle, but I ran into issues as apparently webpack is not meant to be used on node JS applications. 为了克服这种烦恼,我最初考虑使用webpack来构建服务器,它将负责将其所需的任何依赖项都包含到捆绑中,但是我遇到了一些问题,因为显然webpack不打算在节点JS上使用应用程序。
What strategies are available for building a node JS application requiring Babel transpilation, such that the application's source files as well as any dependencies are built transparently and contained in a single output directory? 有哪些策略可用于构建需要Babel转译的节点JS应用程序,从而透明地构建应用程序的源文件以及任何依赖项并将其包含在单个输出目录中?
Annex A 附件A
Simplified gulp task for transpilation of scripts, as employed by server
. server
使用的简化的gulp任务,用于脚本的翻译。
return gulp
.src([`src/**/*.js`], { allowEmpty: true })
.pipe(babel({ sourceMap: true }))
.pipe(gulp.dest('dist'));
As can be seen above, only server
's own source files are included in the task. 从上面可以看出,任务中仅包含
server
自己的源文件。 If src
were to be changed to also include library
, the task would emit the dependencies' artifacts in server
's own output directory and any require('library')
statements within would attempt to locate the built artifacts in packages/library
and not packages/server/dist
, thus resulting in import failures. 如果将
src
更改为也包括library
,则该任务将在server
自己的输出目录中发出依赖项的工件,并且其中的任何require('library')
语句将尝试在packages/library
而不是packages/server/dist
查找已构建的工件。 packages/server/dist
,从而导致导入失败。
First of all, I am not sure what your server is doing. 首先,我不确定您的服务器在做什么。 If it is doing a database connection or some calculations then I would not recommend it to be built by
webpack
. 如果它正在进行数据库连接或进行一些计算,那么我不建议由
webpack
构建webpack
。 Whereas If your server is just doing Server-Side Rendering and making some API calls to other servers then I would recommend it to be bundled using webpack
. 而如果您的服务器只是在进行服务器端渲染并向其他服务器进行一些API调用,那么我建议使用
webpack
将其捆绑在一起。
A lot of projects follow this philosophy. 许多项目都遵循这种理念。 For example, you can take a look at something similar, I have done in one of my personal projects [Blubus] .
例如,您可以看看我在一个个人项目[Blubus]中所做的类似事情。 Specifically, you might be interested in webpack-server-config .
具体来说,您可能对webpack-server-config感兴趣。 And also you can take a look at how big projects like spectrum does it.
另外,您还可以了解频谱等大型项目是如何做到的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.