CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-babel--helper-module-imports

Babel helper functions for inserting module loads

Overall
score

99%

Overview
Eval results
Files

task.mdevals/scenario-4/

Environment-Aware Import Injector

Create a Babel plugin that intelligently injects utility imports based on the target environment's module system. The plugin should detect when specific utility function calls are used and automatically inject the appropriate imports with correct module system settings.

Requirements

Your plugin should:

  1. Detect calls to a utility function named safeStringify in the code
  2. Inject an import for this function from a package called safe-json-utils
  3. Automatically detect whether the file is an ES6 module or CommonJS
  4. For ES6 modules: inject as a named import with ES6 module type
  5. For CommonJS files: inject as CommonJS require with appropriate interop settings
  6. Handle multiple calls to the same function without creating duplicate imports
  7. Ensure the generated identifier is properly scoped and collision-free

Test Cases

  • In an ES6 module file, when safeStringify(data) is called, the plugin injects a proper ES6 named import at the top of the file @test
  • In a CommonJS file, when safeStringify(data) is called, the plugin injects a proper CommonJS require @test
  • When safeStringify is called multiple times in the same file, only one import is injected @test

Implementation

@generates

API

import type { PluginObj } from "@babel/core";

export default function environmentAwareImportPlugin(): PluginObj;

Dependencies { .dependencies }

@babel/helper-module-imports { .dependency }

Provides helper functions for injecting module imports with environment-aware settings.

@babel/core { .dependency }

Provides the core Babel plugin API and types.

@babel/traverse { .dependency }

Provides types for AST traversal and NodePath.

Install with Tessl CLI

npx tessl i tessl/npm-babel--helper-module-imports

tile.json