Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typescript “relative imports” are turned into “absolute imports” #1121

Closed
whimzyLive opened this issue Sep 11, 2019 · 1 comment
Closed

Comments

@whimzyLive
Copy link

🐞 bug report

Affected Rule

The issue is caused by the rule:

Possibly ts_library

Is this a regression?

Yes, the previous version in which this bug was not present was: ....

Not sure.
Tried with bazel/typescript 0.29 and 0.37, and both seems to have this issue.

Description

A clear and concise description of the problem...

typescript file with relative imports, turned into absolute import after compilation.

bootstrapper.ts:

import { Stack, App, StackProps } from '@aws-cdk/core';
import { StaticSite } from './resources/static-site';

class MyStaticSiteStack extends Stack {
  constructor(parent: App, name: string, props: StackProps) {
    super(parent, name, props);

    new StaticSite(this, 'StaticSite', {
      domainName: this.node.tryGetContext('domain'),
      siteSubDomain: this.node.tryGetContext('subdomain')
    });
  }
}

const app = new App();

new MyStaticSiteStack(app, 'MyStaticSite', { env: { region: 'us-east-1' } });

tsconfig:

{
  "compilerOptions": {
    "outDir": "./dist",
    "module": "commonjs",
    "moduleResolution": "node",
    "strict": true,
    "target": "es2015",
    "lib": ["es2015.promise", "es5", "es6", "es2017.object", "dom"],
    "noImplicitAny": false,
    "experimentalDecorators": true,
    "strictNullChecks": false,
    "baseUrl": "./",
    "rootDir": "./",
    "paths": {
      "@bhavnastitch/*": ["./*"],
      "@bh-orders-app/*": ["./bh-orders-app/*"]
    }
  },
  "exclude": ["node_modules"]
}

BUILD File:

package(default_visibility=["//visibility:public"])
load("@npm_bazel_typescript//:index.bzl", "ts_library")

alias(
    name = "tsconfig.json",
    actual = "//:tsconfig.json",
)
ts_library(
    name = "src",
    srcs = glob(["**/*.ts"]),
    tsconfig= ":tsconfig.json",
    deps= [
        "@npm//@aws-cdk/aws-s3",
        "@npm//@aws-cdk/core"
    ]
)

compiled file looks like this:

(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define("backend/bh-orders-app/bootstrapper", ["require", "exports", "@aws-cdk/core", "backend/bh-orders-app/resources/static-site"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    const core_1 = require("@aws-cdk/core");
    const static_site_1 = require("backend/bh-orders-app/resources/static-site");
    class MyStaticSiteStack extends core_1.Stack {
        constructor(parent, name, props) {
            super(parent, name, props);
            new static_site_1.StaticSite(this, 'StaticSite', {
                domainName: this.node.tryGetContext('domain'),
                siteSubDomain: this.node.tryGetContext('subdomain')
            });
        }
    }
    const app = new core_1.App();
    new MyStaticSiteStack(app, 'MyStaticSite', { env: { region: 'us-east-1' } });
});

This is what I get back but when I run it it wont work because it doesn't know what backend/bh-orders-app/resources/static-site refers to .

Directory structure:

backend--
         |--bh-order-app
            |--resources
               |--static-site.ts
            |--bootstrapper.ts <---- File that I have provided example for.
            |--BUILD
         |-WORKSPACE

🔬 Minimal Reproduction

Example Repo: https://github.com/wimzyLive/bhavnastitch/tree/master/backend

🔥 Exception or Error


No Errors or warnings.

🌍 Your Environment

Operating System:

  
macOS Mojave (version 10.14.4)
  

Output of bazel version:

  
Build label: 0.25.2
Build target: bazel-out/darwin-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Fri May 10 20:50:40 2019 (1557521440)
Build timestamp: 1557521440
Build timestamp as int: 1557521440
  
@alexeagle
Copy link
Collaborator

The "devmode" output from ts_library is intended to be used in concatjs bundler, so we always produce "named UMD" outputs.
You have two choices:

  • have a runtime layout that matches your sources. Choose a name for your workspace ("backend" in this case) that is resolvable at runtime (eg. tsickle does this by naming the workspace the same as the npm package that they publish)
  • use the module_name attribute on ts_library so that we name the module whatever you need at runtime. This is our monorepo support feature that lets you declare arbitrary packages in a subfolder in your tree

See https://github.com/bazelbuild/rules_nodejs/blob/master/docs/TypeScript.md#writing-typescript-code-for-bazel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants