- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试在我的 TypeScript 应用程序中导入 package.json
:
import packageJson from '../package.json';
我的 tsconfig.json
包含以下内容:
{
"compilerOptions": {
"rootDir": "./src/"
"outDir": "./dist/",
"baseUrl": ".",
"resolveJsonModule": true
}
}
问题是当我编译这个的时候,我得到了
error TS6059: File '/path/to/package.json' is not under 'rootDir' '/path/to/app/src/'. 'rootDir' is expected to contain all source files.
我不确定我是否理解这个问题,因为 ./src/
和 /.dist
具有相同的父级 ..
,因此 TypeScript 可以不理会 import '../package.json'
并且 它可以从 rootDir
或 outDir 中工作
。
无论如何,我已经尝试了以下方法,但结果并不令人满意:
rootDir
- 编译有效,但 dist
将包含 dist/src
,这是我不想要的outDir
- 然后 src
被 .js
文件(和 .js.map
污染,如果 sourceMap
是真的)@ts-ignore
- 编译停止导入 ../package.json
此限制的解决方法是什么,将生成的文件保存在 dist
中,并允许从 rootDir
的父目录导入?
最佳答案
这是可能的,事实证明,不难。
解决方案不明显的原因是因为 typescript 依赖于 rootDir
来决定输出的目录结构(参见 this comment from Typescript's bossman ),并且只包含代码可以导入输出或包依赖项。
rootDir
设置为项目的根目录,package.json
将发送到 outDir
的根目录并可以导入。但是随后您编译的 src
文件将写入 outDir/src
。rootDir
设置为src
,那里的文件将编译到outDir
的根目录。但是现在the compiler won't have a place to emit package.json
,所以它发出“一个错误,因为项目似乎配置错误”(老板的话)。Typescript 项目 由tsconfig 文件定义,是独立的,并且有效地 受其rootDir< 限制
。这是一件非常好的事情,因为它符合封装原则。
您可以有多个项目(例如,一个主项目和一组库),每个项目都在它们自己的目录中,并且有它们自己的 tsconfig。它们之间的依赖关系使用 Typescript 在 tsconfig 文件中声明 Project References .
I admit, the term "projects" is a poor one, as intuitively it refers to the whole shebang, but "modules" and "packages" are already taken in this context. Think of them as "subprojects" and it will make more sense.
我们会将 src
目录和包含 package.json
的根目录视为单独的项目。每个都有自己的 tsconfig
文件。
为 src
目录提供自己的项目。
./src/tsconfig.json
:
{
"compilerOptions": {
"rootDir": ".",
"outDir": "../dist/",
"resolveJsonModule": true
},
"references": [ // this is how we declare a dependency from
{ "path": "../" } // this project to the one at the root dir`
]
}
给根目录它自己的项目。
./tsconfig.json
:
{
"compilerOptions": {
"rootDir": ".",
"outDir": ".", // if out path for a file is same as its src path, nothing will be emitted
"resolveJsonModule": true,
"composite": true // required on the dependency project for references to work
},
"files": [ // by whitelisting the files to include, TS won't automatically
"package.json" // include all source below root, which is the default.
]
}
运行 tsc --build src
瞧瞧!
这将构建src
项目。因为它声明了对根项目的引用,所以它也会构建那个项目,但前提是它已过时。因为根 tsconfig 与 outDir
具有相同的目录,所以 tsc 将不会对 package.json
做任何事情,它被配置为编译的一个文件。
您可以通过将模块/库/子项目放在它们自己的子目录中并为它们提供自己的 tsconfig 来隔离模块/库/子项目。
您可以使用 Project References 显式管理依赖项,以及模块化构建:
来自链接文档:
您可以大大缩短构建时间
A long-awaited feature is smart incremental builds for TypeScript projects. In 3.0 you can use the
--build
flag withtsc
. This is effectively a new entry point fortsc
that behaves more like a build orchestrator than a simple compiler.Running
tsc --build
(tsc -b
for short) will do the following:
- Find all referenced projects
- Detect if they are up-to-date
- Build out-of-date projects in the correct order
Don’t worry about ordering the files you pass on the commandline -
tsc
will re-order them if needed so that dependencies are always built first.
强制组件之间的逻辑分离
以新的更好的方式组织您的代码。
这也很简单:
用于共享选项和构建所有内容的根 tsconfig
使用简单的 tsc --build
命令创建子项目(使用 --force
从头开始构建它们)
src/tsconfig.json
{
"compilerOptions": {
"outDir": ".", // prevents this tsconfig from compiling any files
// we want subprojects to inherit these options:
"target": "ES2019",
"module": "es2020",
"strict": true,
...
},
// configure this project to build all of the following:
"references": [
{ "path": "./common" }
{ "path": "./projectA" }
]
}
一个“公共(public)”库被阻止从其他子项目,因为它没有项目引用
src/common/tsconfig.json
{
"extends": "../tsconfig.json", //inherit from root tsconfig
// but override these:
"compilerOptions": {
"rootDir": ".",
"outDir": "../../build/common",
"resolveJsonModule": true,
"composite": true
}
}
一个子项目可以导入 common 因为声明了引用。src/projectA/tsconfig.json
{
"extends": "../tsconfig.json", //inherit from root tsconfig
// but override these:
"compilerOptions": {
"rootDir": ".",
"outDir": "../../build/libA",
"resolveJsonModule": true,
"composite": true
},
"references": [
{ "path": "../common" }
]
}
关于javascript - 'package.json' 不在 'rootDir' 之下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55753163/
我有一个 html 格式的表单: 我需要得到 JavaScript在value input 字段执行,但只能通过表单的 submit .原因是页面是一个模板所以我不控制它(不能有
我管理的论坛是托管软件,因此我无法访问源代码,我只能向页面添加 JavaScript 来实现我需要完成的任务。 我正在尝试用超链接替换所有页面上某些文本关键字的第一个实例。我还根据国家/地区代码对这些
我正在使用 JS 打开新页面并将 HTML 代码写入其中,但是当我尝试使用 document.write() 在新页面中编写 JS 时功能不起作用。显然,一旦看到 ,主 JS 就会关闭。用于即将打开的
提问不是为了解决问题,提问是为了更好地理解系统 专家!我知道每当你将 javascript 代码输入 javascript 引擎时,它会立即由 javascript 引擎执行。由于没有看过Engi
我在一个文件夹中有两个 javascript 文件。我想将一个变量的 javascript 文件传递到另一个。我应该使用什么程序? 最佳答案 window.postMessage用于跨文档消息。使
我有一个练习,我需要输入两个输入并检查它们是否都等于一个。 如果是 console.log 正则 console.log false 我试过这样的事情: function isPositive(fir
我正在做一个Web应用程序,计划允许其他网站(客户端)在其页面上嵌入以下javascript: 我的网络应用程序位于 http://example.org 。 我不能假设客户端网站的页面有 JQue
目前我正在使用三个外部 JS 文件。 我喜欢将所有三个 JS 文件合而为一。 尽一切可能。我创建 aio.js 并在 aio.js 中 src="https://code.jquery.com/
我有例如像这样的数组: var myArray = []; var item1 = { start: '08:00', end: '09:30' } var item2 = {
所以我正在制作一个 Chrome 扩展,它使用我制作的一些 TamperMonkey 脚本。我想要一个“主”javascript 文件,您可以在其中包含并执行其他脚本。我很擅长使用以下行将其他 jav
我有 A、B html 和 A、B javascript 文件。 并且,如何将 A JavaScript 中使用的全局变量直接移动到 B JavaScript 中? 示例 JavaScript) va
我需要将以下整个代码放入名为 activate.js 的 JavaScript 中。你能告诉我怎么做吗? var int = new int({ seconds: 30, mark
我已经为我的 .net Web 应用程序创建了母版页 EXAMPLE1.Master。他们的 I 将值存储在 JavaScript 变量中。我想在另一个 JS 文件中检索该变量。 示例1.大师:-
是否有任何库可以用来转换这样的代码: function () { var a = 1; } 像这样的代码: function () { var a = 1; } 在我的浏览器中。因为我在 Gi
我收到语法缺失 ) 错误 $(document).ready(function changeText() { var p = document.getElementById('bidp
我正在制作进度条。它有一个标签。我想调整某个脚本完成的标签。在找到可能的解决方案的一些答案后,我想出了以下脚本。第一个启动并按预期工作。然而,第二个却没有。它出什么问题了?代码如下: HTML:
这里有一个很简单的问题,我简单的头脑无法回答:为什么我在外部库中加载时,下面的匿名和onload函数没有运行?我错过了一些非常非常基本的东西。 Library.js 只有一行:console.log(
我知道 javascript 是一种客户端语言,但如果实际代码中嵌入的 javascript 代码以某种方式与在控制台上运行的代码不同,我会尝试找到答案。让我用一个例子来解释它: 我想创建一个像 Mi
我如何将这个内联 javascript 更改为 Unobtrusive JavaScript? 谢谢! 感谢您的回答,但它不起作用。我的代码是: PHP js文件 document.getElem
我正在寻找将简单的 JavaScript 对象“转储”到动态生成的 JavaScript 源代码中的最优雅的方法。 目的:假设我们有 node.js 服务器生成 HTML。我们在服务器端有一个对象x。
我是一名优秀的程序员,十分优秀!