简体   繁体   English

使用依赖项构建monorepo babel编译的节点JS应用程序

[英]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 . 为了简化起见,假设内部有三个不言自明的软件包: serverwebapp 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.

相关问题 什么决定了babel-transiled脚本中`require`调用的顺序? - What determines the order of `require` calls in babel-transpiled scripts? 无法用节点运行babel转换文件 - Can't run babel transpiled files with node Babel成功地转译了ES6代码(node.js),但是当“ npm start”执行时,它会抛出“ SyntaxError:意外的令牌导入” - Babel transpiled ES6 code (node.js) successfully, but when do “npm start” it throws “SyntaxError: Unexpected token import ” 使用 lerna 为 react 和 node js web 应用程序实现 monorepo - implementing monorepo for react and node js web application using lerna 运行 Babel 转译代码时需要 JSON 文件引发 Node.js 加载程序错误“错误:找不到模块'example.json'” - require a JSON file throws a Node.js loader error "Error: Cannot find module 'example.json'" when running Babel transpiled code 是否可以使用Babel npm包用于node.js服务器应用程序 - Is it ok to use Babel npm package for node.js server application 使用Babel为Koa / Node / js应用程序进行WebStorm调试配置 - WebStorm Debug configuration for Koa/Node/js application using babel 使用node-inspector或babel-node-debug调试es6转换代码 - Debug es6 transpiled code using node-inspector or babel-node-debug 没有为nodeJS编译的babel定义regeneratorRuntime - regeneratorRuntime is not defined for nodeJS transpiled babel 如何在 monorepo 中成功锁定节点模块依赖项? - How can I successfully lock down node module dependencies in a monorepo?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM