Resolving paths in TypeScript outDir

自闭症网瘾萝莉.ら 提交于 2019-12-11 06:10:02

问题


I'm writing a UI page object model testing project in TypeScript and I'm trying my best to avoid hideous nested imports.

I found the paths object for the .tsconfig file that fixes the issue in .ts files, but, because I compile into an outDir, my compiled .js files cannot resolve the module path.

I develop in a model directory and compile into a src directory.

I've set up my .tsconfig file as follows

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": true,
    "noImplicitAny": false,
    "outDir": "src/",
    "baseUrl": "./model/",
    "paths": {
      "general.utils": [ "lib/general/general.utils" ],
      "pretest.setup": [ "lib/pretest.setup/pretest.setup" ],
      "tpapi": [ "lib/tpapi/tpapi" ],
      "*": [ "*" ]
    }
  },
  "exclude": [
    "node_modules"
  ],
  "compileOnSave": false
}

That paths allows me to

import { chooseRandomValueFrom } from 'general.utils';

from a .ts file that is in model, but won't resolve in a .js file in src.

This is an npm type project that is run with npm test. That command executes a script that compiles, runs tests from, and deletes the src directory.

#!/bin/bash

npm run compile:ts
node_modules/webdriverio/bin/wdio
npm run clean:ts

回答1:


The best way I found to deal with this is to have a collector/exporter file that exports everything from the files that it collects from.

For examples if I have 5 files that have different types of utilities in them, I would name them appropriately, then collect them all in a collector/exporter file called utilities.

In the utilities file, it's just lines that look like

export * from 'path/to/file1
export * from 'path/to/file2
export * from 'path/to/file3

Then I can import only from the collector/exporter file.

It's not great, but it's a little better than doing nothing at all.



来源:https://stackoverflow.com/questions/40407342/resolving-paths-in-typescript-outdir

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!