ttypescript-browser-like-import-transformer

A custom transformer that can be used with ttypescript to transform ts imports to browser style imports


Project maintained by Jack-Works Hosted on GitHub Pages — Theme by mattgraham

Home > config > PluginConfigs > safeAccess

PluginConfigs.safeAccess property

Use property access syntax to access UMD variable

Signature:

safeAccess?: string

Remarks

By turning this off, this transformer will emit dangerous code. This might be useful in some cases: e.g. you want a limited code generation (before: import “a(‘b’)”, out: globalThis.a(‘b’)).

After opening this option, the code above will become globalThis["a('b')"] which is safe.

Example

Filename: safeAccess-default.ts

/// { rules: { '/(.+)/': { type: 'umd', target: '$1' } }, globalObject: 'globalThis.__deps__' }
import x from '__proto__.constructor.constructor.call(null, "eval(\'alert()\')")'
console.log(x)

Filename: safeAccess-default.js

// CompilerOptions: {"module":"ESNext"}
// PluginConfig: {"rules":{"/(.+)/":{"type":"umd","target":"$1"}},"globalObject":"globalThis.__deps__"}
const x = __UMDBindCheck(globalThis.__deps__["__proto__.constructor.constructor.call(null, \"eval('alert()')\")"], ["default"], "__proto__.constructor.constructor.call(null, \"eval('alert()')\")", "globalThis.__deps__.__proto__.constructor.constructor.call(null, \"eval('alert()')\")", false).default;
console.log(x);
import { __UMDBindCheck as __UMDBindCheck } from "https://cdn.jsdelivr.net/npm/@magic-works/ttypescript-browser-like-import-transformer@2.1.2/es/ttsclib.min.js";

Filename: safeAccess-true.js

// CompilerOptions: {"module":"ESNext"}
// PluginConfig: {"safeAccess":true,"rules":{"/(.+)/":{"type":"umd","target":"$1"}},"globalObject":"globalThis.__deps__"}
const x = __UMDBindCheck(globalThis.__deps__["__proto__.constructor.constructor.call(null, \"eval('alert()')\")"], ["default"], "__proto__.constructor.constructor.call(null, \"eval('alert()')\")", "globalThis.__deps__.__proto__.constructor.constructor.call(null, \"eval('alert()')\")", false).default;
console.log(x);
import { __UMDBindCheck as __UMDBindCheck } from "https://cdn.jsdelivr.net/npm/@magic-works/ttypescript-browser-like-import-transformer@2.1.2/es/ttsclib.min.js";

Filename: safeAccess-false.js

// CompilerOptions: {"module":"ESNext"}
// PluginConfig: {"safeAccess":false,"rules":{"/(.+)/":{"type":"umd","target":"$1"}},"globalObject":"globalThis.__deps__"}
const x = __UMDBindCheck(globalThis.__deps__.__proto__.constructor.constructor.call(null, "eval('alert()')"), ["default"], "__proto__.constructor.constructor.call(null, \"eval('alert()')\")", "globalThis.__deps__.__proto__.constructor.constructor.call(null, \"eval('alert()')\")", false).default;
console.log(x);
import { __UMDBindCheck as __UMDBindCheck } from "https://cdn.jsdelivr.net/npm/@magic-works/ttypescript-browser-like-import-transformer@2.1.2/es/ttsclib.min.js";