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” when building with @bazel/typescript:0.37 #9350

Closed
whimzyLive opened this issue Sep 9, 2019 · 2 comments

Comments

@whimzyLive
Copy link

I am not really sure if this is a typescript issue or the way bazel compiles typescript, but in summary the issue is that if I compile a typescript file that contains "relative import" it turns into absolute import when compiled to javascript. I am using ts_library rule.

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

Bugs: what's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

Create a simple typescript project, and compile it with ts_library rule.

What operating system are you running Bazel on?

Mac OS Mojave

What's the output of bazel info release?

release 0.25.2

@irengrig
Copy link
Contributor

It looks like an issue to https://github.com/bazelbuild/rules_typescript.
Please file an issue to them.

/cc @alexeagle @gregmagolan

@whimzyLive
Copy link
Author

moved issue here as per @irengrig 's suggestions: bazel-contrib/rules_nodejs#1121
/cc @alexeagle @gregmagolan

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

No branches or pull requests

2 participants