Add import attributes support (#1388)

This commit is contained in:
Timofey Kachalov
2026-03-22 08:56:53 +04:00
committed by GitHub
parent 1aadbacc06
commit 560212c0c4
7 changed files with 214 additions and 7 deletions
@@ -1478,6 +1478,116 @@ describe('JavaScriptObfuscator', () => {
});
});
describe('Import attributes', () => {
describe('Variant #1: import with "with" syntax', () => {
const importAttributesRegExp: RegExp = /from\s*'\.\/config\.json'\s*with\s*\{\s*type\s*:\s*'json'\s*\}/;
let obfuscatedCode: string;
before(() => {
const code: string = `import config from './config.json' with { type: 'json' };\nconsole.log(config);`;
obfuscatedCode = JavaScriptObfuscator.obfuscate(code, {
...NO_ADDITIONAL_NODES_PRESET
}).getObfuscatedCode();
});
it('should preserve import attributes in obfuscated code', () => {
assert.match(obfuscatedCode, importAttributesRegExp);
});
});
describe('Variant #2: import with multiple attributes', () => {
const importAttributesRegExp: RegExp = /from\s*'\.\/data\.json'\s*with\s*\{\s*type\s*:\s*'json'\s*,\s*integrity\s*:\s*'sha384-abc'\s*\}/;
let obfuscatedCode: string;
before(() => {
const code: string = `import data from './data.json' with { type: 'json', integrity: 'sha384-abc' };\nconsole.log(data);`;
obfuscatedCode = JavaScriptObfuscator.obfuscate(code, {
...NO_ADDITIONAL_NODES_PRESET
}).getObfuscatedCode();
});
it('should preserve multiple import attributes in obfuscated code', () => {
assert.match(obfuscatedCode, importAttributesRegExp);
});
});
describe('Variant #3: import with "assert" syntax (deprecated)', () => {
const importAssertionsRegExp: RegExp = /from\s*'\.\/config\.json'\s*assert\s*\{\s*type\s*:\s*'json'\s*\}/;
let obfuscatedCode: string;
before(() => {
const code: string = `import config from './config.json' assert { type: 'json' };\nconsole.log(config);`;
obfuscatedCode = JavaScriptObfuscator.obfuscate(code, {
...NO_ADDITIONAL_NODES_PRESET
}).getObfuscatedCode();
});
it('should preserve import assertions in obfuscated code', () => {
assert.match(obfuscatedCode, importAssertionsRegExp);
});
});
describe('Variant #4: export with "with" syntax', () => {
const exportAttributesRegExp: RegExp = /from\s*'\.\/config\.json'\s*with\s*\{\s*type\s*:\s*'json'\s*\}/;
let obfuscatedCode: string;
before(() => {
const code: string = `export { default as config } from './config.json' with { type: 'json' };`;
obfuscatedCode = JavaScriptObfuscator.obfuscate(code, {
...NO_ADDITIONAL_NODES_PRESET
}).getObfuscatedCode();
});
it('should preserve export attributes in obfuscated code', () => {
assert.match(obfuscatedCode, exportAttributesRegExp);
});
});
describe('Variant #5: export all with "with" syntax', () => {
const exportAllAttributesRegExp: RegExp = /export\s*\*\s*from\s*'\.\/utils\.js'\s*with\s*\{\s*type\s*:\s*'javascript'\s*\}/;
let obfuscatedCode: string;
before(() => {
const code: string = `export * from './utils.js' with { type: 'javascript' };`;
obfuscatedCode = JavaScriptObfuscator.obfuscate(code, {
...NO_ADDITIONAL_NODES_PRESET
}).getObfuscatedCode();
});
it('should preserve export all attributes in obfuscated code', () => {
assert.match(obfuscatedCode, exportAllAttributesRegExp);
});
});
describe('Variant #6: side-effect import with "with" syntax', () => {
const sideEffectImportRegExp: RegExp = /import\s*'\.\/styles\.css'\s*with\s*\{\s*type\s*:\s*'css'\s*\}/;
let obfuscatedCode: string;
before(() => {
const code: string = `import './styles.css' with { type: 'css' };`;
obfuscatedCode = JavaScriptObfuscator.obfuscate(code, {
...NO_ADDITIONAL_NODES_PRESET
}).getObfuscatedCode();
});
it('should preserve side-effect import attributes in obfuscated code', () => {
assert.match(obfuscatedCode, sideEffectImportRegExp);
});
});
});
describe('getOptionsByPreset', () => {
describe('Variant #1: base behaviour', () => {
const optionsPresetName: TOptionsPreset = OptionsPreset.HighObfuscation;