Rollup faces challenges when trying to bundle source code alongside Bazel and Typescript

I am attempting to create a bundle using rollup, typescript, and bazel environment. I am having trouble importing relative paths. Typescript builds correctly but rollup is unable to bundle the source.

WORKSPACE

# WORKSPACE
workspace(
    name = "WORKSPACE",
    # Map the @npm bazel workspace to the node_modules directory.
    # This allows Bazel to use the same node_modules as other local tooling.
    managed_directories = {"@npm": ["node_modules"]},
)

# Imports
load("//tools:dependencies.bzl", "fetch_dependencies")
fetch_dependencies()

load("@build_bazel_rules_nodejs//:index.bzl", "yarn_install")
yarn_install(
    name = "npm",
    package_json = "//:package.json",
    yarn_lock = "//:yarn.lock",
)

# Set up web testing, choose browsers we can test on
load("@io_bazel_rules_webtesting//web:repositories.bzl", "web_test_repositories")

web_test_repositories()

load("@io_bazel_rules_webtesting//web/versioned:browsers-0.3.2.bzl", "browser_repositories")

browser_repositories(
    chromium = True,
    firefox = True,
)

FOLDER STRUCTURE

apps
    mobile
        index.ts
        src
            index.tsx
            asd.ts

packages
    core
        index.ts
        src
            index.ts
        ...

CORE BUILD FILE

# Package Info
package(default_visibility = ['//visibility:public'])

# Imports
load('@npm//@bazel/typescript:index.bzl', 'ts_library')
load('@build_bazel_rules_nodejs//:index.bzl', 'pkg_npm')

ts_library(
    name = "core",
    module_name = '@app/core',
    srcs = glob(["**/*.ts"]),
    deps = [
        "@npm//reflect-metadata",
        "@npm//moment",
        "@npm//rxjs",
        "@npm//axios",
    ]
)

pkg_npm(
    name = "core-package",
    srcs = [],
    substitutions = {"//internal/": "//"},
    deps = [
        '//packages/core:core'
    ],
)

APP BUILD FILE

# Package Info
package(default_visibility = ['//visibility:public'])

# Imports
load('@npm//@bazel/typescript:index.bzl', 'ts_library')
load("@npm//http-server:index.bzl", "http_server")
load("@npm//@bazel/rollup:index.bzl", "rollup_bundle")

ts_library(
    name = "mobile",
    module_name = '@app/mobile',
    srcs = glob(["**/*.ts", "**/*.tsx"]),
    deps = [
        "@npm//react",
        "@npm//@types/react",
        "@npm//react-router-dom",
        "@npm//@types/react-router-dom",
        "@npm//rxjs",
        "@npm//moment",

        "//packages/core:core",
    ]
)

filegroup(
    name = "mobile-source",
    srcs = [":mobile"],
    output_group = "es6_sources",
)

rollup_bundle(
    config_file = "//:rollup.config.js",
    name = "bundle",
    entry_points = {
        "index.ts" : "bundle"
    },
    srcs = [],
    output_dir = True,
    deps = [
        ":mobile",
        "@npm//@rollup/plugin-node-resolve"
    ],
)

http_server(
    name = "devserver",
    data = [
        "public/index.html",
        "bundle",
    ],
    args = ["."],
)

rollup.config.js

import { nodeResolve } from '@rollup/plugin-node-resolve';

export default {
    input: 'index.ts',
    output: {
        name: 'bundle',
        format: 'umd',
        plugins: [
            nodeResolve({
                mainFields: ['browser', 'es2015', 'module', 'jsnext:main', 'main'],
            })
        ]
    },
    onwarn: function (warning) {
        if (warning.code === 'THIS_IS_UNDEFINED') { return; }
        console.warn(warning.message);
    }
};

my index file in apps/mobile

export * from '@apps/mobile/src';
import { A } from './src/as';

console.log(A.a());

When I run bazel build //apps/mobile:bundle, I receive the following error and the bundle file is not completed.

bazel-out/k8-fastbuild/bin/apps/mobile/index.mjs → bazel-out/k8-fastbuild/bin/apps/mobile/bundle...
'@app/mobile/src' is imported by bazel-out/k8-fastbuild/bin/apps/mobile/index.mjs, but could not be resolved – treating it as an external dependency
The "buildStart" hook used by the output plugin node-resolve is a build time hook and will not be run for that plugin. Either this plugin cannot be used as an output plugin, or it should have an option to configure it as an output plugin.
The "load" hook used by the output plugin node-resolve is a build time hook and will not be run for that plugin. Either this plugin cannot be used as an output plugin, or it should have an option to configure it as an output plugin.
The "resolveId" hook used by the output plugin node-resolve is a build time hook and will not be run for that plugin. Either this plugin cannot be used as an output plugin, or it should have an option to configure it as an output plugin.

GENRATED BUNDLE FILE

export * from '@apps/mobile/src';

class A {
    static a() {
        return 's';
    }
}

console.log(A.a());

Answer №1

After dedicating numerous hours to the problem, I finally discovered the solution. The ts_library rule includes parameters called devmode_target and devmode_module. When building in devmode, the ts_library does not reference configurations in the tsconfig file; instead, it looks at the values of devmode_target and devmode_module. By correctly setting these parameters (e.g. es2015), I was able to successfully build.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Transforming the Server-side model to the Client-side model within Angular 4

My Server-side C# model public class Instructor:Entity { public string Name { get; set; } public string PhoneNo { get; set; } } Client-side TypeScript model export class Instructor extends Entity { public name:string; public address ...

Error alert! A token error has been detected while executing Angular tests under <Jasmine>

I've been navigating the world of Angular testing, but I've hit a roadblock that I can't seem to bypass. Despite my efforts to consult the documentation and scour Google for answers, I remain stuck with a cryptic error message stating "Inval ...

Why doesn't WebStorm display TypeScript inspection errors in real-time?

I'm currently utilizing WebStorm 2017.2.4 in conjunction with Angular 4.3 - I am facing an issue where TypeScript errors are not being displayed: Query How can I enable real-time inspections to occur immediately? (I've already attempted invali ...

Encountered an error while trying to generate the Component class for the ColorlibStepIcon from Material UI in TypeScript

I am trying to convert the ColorlibStepIcon functional component into a class component for my Stepper. Unfortunately, I have not been successful and keep encountering errors. I have attempted some changes but it is still not working as expected. You can ...

The callback function `(err: any, data: any) => void` does not share any properties with the type `RequestInit`

Inspired by the tutorial at , I am working on a time-based visualization. I am currently using version "d3": "^5.4.0". Here is the code snippet: d3.json('http://127.0.0.1:5000', function (err, data) { if (err) throw err; // Cre ...

Is there a way to include values in the body of an HTTP GET request using Angular?

I've created a function in my service that looks like this: /** * Retrieve all data * @param sendSelectedValues string */ getAllActPlanBalanceYearData(sendSelectedValues: any): Observable<any> { const url = `/yearlyvalues/act-and ...

Attempting to design an interface in Angular 2 to align with the anticipated API response structure

I have a specific JSON response in mind that I am expecting from an API. My goal is to create a user interface for it. { items: [ { identifier: 1, details: [ { id: 1001, perishable: 0 }, { ...

Updating and Preserving Content in Angular

I've encountered an issue while working on a code that allows users to edit and save a paragraph on the screen. Currently, the editing functionality is working fine and the save() operation is successful. However, after saving, the edited paragraph do ...

A Fresh Approach for Generating Unique UUIDs without Bitwise Operators

To generate UUIDs in TypeScript, I have implemented a method inspired by the solution provided on Stack Overflow. The code effectively converts JavaScript to TypeScript. generateUUID(): string { let date = new Date().getTime(); if (window.performa ...

Unlocking the potential: passing designated text values with Javascript

In my current React code, I am retrieving the value from cookies like this: initialTrafficSource: Cookies.get("initialTrafficSource") || null, Mapping for API const body = {Source: formValue.initialTrafficSource} Desired Output: utmcsr=(direct)|utmcmd=(n ...

Is it possible for a conditional type in TypeScript to be based on its own value?

Is it possible to use this type in TypeScript? type Person = { who: string; } type Person = Person.who === "me" ? Person & Me : Person; ...

Trouble with querying NG elements using "queryAll(By.css)" in Angular and Jasmin unit testing

I've encountered an unusual problem that needs to be resolved for me to successfully complete a unit test for a project I'm currently engaged in. Here is what my unit test currently looks like: it('should display the navbar list', ...

Utilizing *ngFor to display elements with odd indices

Within my Angular application, I have successfully used a loop to populate the 4 employeeList components. Here is the code snippet: <div *ngFor="let record of records"> <p-panel> <div comp-employeeList [listFilter]="record.Filte ...

Angular: "btn" class vanishes when the button is toggled

I am facing an issue with the button's class change functionality. I am using the [ngClass] directive to switch between Bootstrap classes for the button style. However, when I click the button, the "btn" class seems to disappear from the code. Instead ...

What is the best way to incorporate this in a callback function?

Utilizing a third-party component requires creating an object for configuration, such as itemMovementOptions in the given code sample. export class AppComponent implements OnInit { readonly itemMovementOptions = { threshold: { horizontal: ...

Angular RxJS: The never-ending reduction

I have developed a component that contains two buttons (searchButton, lazyButton). The ngOnDestroy method is defined as follows: public ngOnDestroy() { this.unsubscribe$.next(); this.unsubscribe$.complete(); } I have created two observables from ...

Issue with React-Toastify not displaying on the screen

After updating from React-Toastify version 7.0.3 to 9.0.3, I encountered an issue where notifications are not rendering at all. Here are the steps I followed: yarn add [email protected] Modified Notification file import React from "react" ...

The type mismatch issue occurs when using keyof with Typescript generics

One of the challenges I am facing is related to an interface that stores a key of another interface (modelKey) and the corresponding value of that key (value): interface ValueHolder<T, H extends keyof T> { modelKey: H; value: T[H]; } My objectiv ...

Using selectors and mappers in Typescript generics

I am looking to create a versatile selector and mapper method. interface State { user: { name: string; age: number; } } const pickName = (state: State) => state.user.name; const selectAge = (state: State) => state.user.age; ...

The FileSaver application generates a unique file with content that diverges from the original document

I'm attempting to utilize the Blob function to generate a file from information transmitted via a server call, encoded in base64. The initial file is an Excel spreadsheet with a length of 5,507 bytes. After applying base64 encoding (including newlines ...