CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-angular-devkit--build-ng-packagr

Angular Build Architect builder for ng-packagr library packaging (deprecated)

89

1.00x
Overview
Eval results
Files

task.mdevals/scenario-4/

Dynamic Package Loader

Overview

Create a package loader utility that dynamically imports npm packages at runtime with robust error handling. The loader should distinguish between different types of import failures and provide helpful error messages to guide users.

Requirements

Your task is to implement a PackageLoader class with the following functionality:

Core Functionality

  1. Dynamic Package Loading

    • Accept a package name as a parameter
    • Use dynamic imports to load the package at runtime
    • Return the loaded module to the caller
  2. Error Handling

    • Detect when a package is not installed (MODULE_NOT_FOUND)
    • Distinguish between "package not found" and other import errors
    • Provide helpful error messages that include installation instructions
    • Re-throw non-MODULE_NOT_FOUND errors for proper error propagation
  3. User-Friendly Messages

    • When a package is missing, return an error message that:
      • Clearly states which package is missing
      • Provides the exact npm install command to fix the issue
      • Explains why the package is needed

Implementation Details

Implement a class named PackageLoader with a method loadPackage(packageName: string) that:

  • Attempts to dynamically import the specified package
  • Returns a successful result object with the loaded module if import succeeds
  • Returns a failure result object with a helpful error message if the package is not found
  • Throws other errors for proper error handling

The result should be an object with the following structure:

  • Success case: { success: true, module: any }
  • Failure case: { success: false, error: string }

Dependencies { .dependencies }

Node.js { .dependency }

Provides the JavaScript runtime environment and module system.

Test Cases

Test 1: Successfully load an installed package { .test }

File: src/package-loader.test.ts

import { PackageLoader } from './package-loader';

describe('PackageLoader', () => {
  it('should successfully load an installed package', async () => {
    const loader = new PackageLoader();
    const result = await loader.loadPackage('path');

    expect(result.success).toBe(true);
    expect(result.module).toBeDefined();
    expect(result.module.join).toBeDefined(); // path module has join function
  });
});

Test 2: Handle missing package gracefully { .test }

File: src/package-loader.test.ts

import { PackageLoader } from './package-loader';

describe('PackageLoader', () => {
  it('should return helpful error for missing package', async () => {
    const loader = new PackageLoader();
    const result = await loader.loadPackage('non-existent-package-xyz');

    expect(result.success).toBe(false);
    expect(result.error).toContain('non-existent-package-xyz');
    expect(result.error).toContain('npm install');
  });
});

Test 3: Error code checking { .test }

File: src/package-loader.test.ts

import { PackageLoader } from './package-loader';

describe('PackageLoader', () => {
  it('should check error codes correctly', async () => {
    const loader = new PackageLoader();

    // Verify that the loader handles error codes appropriately
    // by attempting to load a package that definitely doesn't exist
    const result = await loader.loadPackage('definitely-not-a-real-package-12345');

    expect(result.success).toBe(false);
    expect(result.error).toBeDefined();
  });
});

Deliverables

  1. A PackageLoader class in src/package-loader.ts
  2. Test file src/package-loader.test.ts with all test cases passing
  3. The implementation should use dynamic imports (import())
  4. Proper TypeScript types for the return values

Install with Tessl CLI

npx tessl i tessl/npm-angular-devkit--build-ng-packagr

tile.json