Babel plugin that automatically adds 'use strict' directive to JavaScript files to enable strict mode
npx @tessl/cli install tessl/npm-babel--plugin-transform-strict-mode@7.27.0A Babel plugin that automatically adds the 'use strict' directive to JavaScript files to enable strict mode execution. This plugin ensures all code runs in strict mode by detecting missing directives and adding them at the beginning of program files.
npm install --save-dev @babel/plugin-transform-strict-modeES Module:
import strictModePlugin from "@babel/plugin-transform-strict-mode";CommonJS:
const strictModePlugin = require("@babel/plugin-transform-strict-mode");Note: The plugin internally uses:
declare from @babel/helper-plugin-utils for plugin creationtypes as t from @babel/core for AST node creationREQUIRED_VERSION utility for version compatibilityAdd the plugin to your Babel configuration:
{
"plugins": ["@babel/plugin-transform-strict-mode"]
}With Babel API:
import { transform } from "@babel/core";
import strictModePlugin from "@babel/plugin-transform-strict-mode";
const result = await transform(code, {
plugins: [strictModePlugin]
});Basic function (adds 'use strict'):
Input:
function greet(name) {
return "Hello, " + name;
}Output:
"use strict";
function greet(name) {
return "Hello, " + name;
}Preserves existing 'use strict' (no duplication):
Input:
"use strict";
foo();Output:
"use strict";
foo();Preserves leading comments:
Input:
// comments
module.exports = {};Output:
"use strict";
// comments
module.exports = {};Creates a Babel plugin instance that adds 'use strict' directives to JavaScript programs.
/**
* Default export function created via @babel/helper-plugin-utils declare wrapper
* Automatically handles Babel version compatibility and plugin initialization
*/
export default declare(api => {
api.assertVersion(REQUIRED_VERSION(7));
return {
name: "transform-strict-mode";
visitor: {
Program(path: NodePath<Program>): void;
};
};
});
interface BabelPlugin {
/** Plugin identifier used by Babel */
name: "transform-strict-mode";
/** AST visitor object containing transformation logic */
visitor: {
Program(path: NodePath<Program>): void;
};
}
interface BabelAPI {
/** Asserts minimum Babel version compatibility (requires version 7+) */
assertVersion(version: number): void;
}
/** Helper function from @babel/helper-plugin-utils for creating Babel plugins */
declare function declare(pluginFactory: (api: BabelAPI) => BabelPlugin): BabelPlugin;
/** Version compatibility utility function */
declare function REQUIRED_VERSION(version: number): number;The plugin operates as follows:
api.assertVersion(REQUIRED_VERSION(7))directive.value.value === "use strict")t.directive(t.directiveLiteral("use strict"))path.unshiftContainer("directives", directive) to prepend the directiveThe plugin handles various code patterns:
/** AST node representing a JavaScript directive like 'use strict' */
interface Directive {
type: "Directive";
value: DirectiveLiteral;
}
/** AST node representing the literal value of a directive */
interface DirectiveLiteral {
type: "DirectiveLiteral";
value: string;
}
/** AST node representing a JavaScript program (top-level scope) */
interface Program {
type: "Program";
directives: Directive[];
body: Statement[];
}
/** Babel NodePath wrapper providing transformation utilities */
interface NodePath<T> {
/** AST node being visited */
node: T;
/** Adds nodes to the beginning of a container (like directives array) */
unshiftContainer(listKey: "directives", nodes: Directive): void;
}
/** Babel AST types for creating directive nodes */
interface BabelTypes {
/** Creates a directive AST node */
directive(value: DirectiveLiteral): Directive;
/** Creates a directive literal AST node */
directiveLiteral(value: string): DirectiveLiteral;
}The plugin requires these peer and runtime dependencies:
declare function wrapper and REQUIRED_VERSION utilityThe plugin is designed to be fail-safe: