Dynamic import and import.meta support

This commit is contained in:
sanex3339
2020-01-19 01:03:04 +03:00
parent 9b00584808
commit b50c48a683
12 changed files with 75 additions and 10 deletions
+1
View File
@@ -3,6 +3,7 @@ Change Log
v0.24.0
---
* **Internal refactoring:** completely new mechanism to rename variable names
* Dynamic import and `import.meta` support. Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/505
* Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/496
v0.23.2
+6 -6
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
File diff suppressed because one or more lines are too long
+1
View File
@@ -24,6 +24,7 @@
"@gradecam/tsenum": "1.2.0",
"@nuxtjs/opencollective": "0.2.2",
"acorn": "7.1.0",
"acorn-import-meta": "1.0.0",
"chalk": "3.0.0",
"chance": "1.1.4",
"class-validator": "0.11.0",
+6 -1
View File
@@ -1,4 +1,5 @@
import * as acorn from 'acorn';
import acornImportMeta from 'acorn-import-meta';
import * as ESTree from 'estree';
import chalk, { Chalk } from 'chalk';
@@ -70,7 +71,11 @@ export class ASTParserFacade {
sourceType
};
const program: ESTree.Program = <any>acorn.parse(input, config);
const program: ESTree.Program = (
<any>acorn
.Parser.extend(acornImportMeta)
.parse(input, config)
);
if (comments.length) {
program.comments = comments;
+7
View File
@@ -0,0 +1,7 @@
declare module 'acorn-import-meta' {
import * as acorn from 'acorn';
function acornImportMeta (BaseParser: typeof acorn.Parser): typeof acorn.Parser;
export = acornImportMeta;
}
@@ -551,6 +551,27 @@ describe('JavaScriptObfuscator', () => {
});
});
describe('import.meta support', () => {
const regExp: RegExp = /console\['log']\(import\.meta\['url']\);/;
let obfuscatedCode: string;
beforeEach(() => {
const code: string = readFileAsString(__dirname + '/fixtures/import-meta.js');
obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET
}
).getObfuscatedCode();
});
it('should support `import.meta`', () => {
assert.match(obfuscatedCode, regExp);
});
});
describe('mangled identifier names generator', () => {
const regExp: RegExp = /var *c *= *0x1/;
@@ -0,0 +1 @@
console.log(import.meta.url);
@@ -173,5 +173,27 @@ describe('ScopeIdentifiersTransformer ImportDeclaration identifiers', () => {
assert.match(obfuscatedCode, variableDeclarationIdentifierRegExp);
});
});
describe('Variant #6: dynamic import support', () => {
const dynamicImportRegExp: RegExp = /import\(_0x[a-f0-9]{4,6}\);/;
let obfuscatedCode: string;
before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/dynamic-import.js');
obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
renameGlobals: true
}
).getObfuscatedCode();
});
it('should support dynamic import', () => {
assert.match(obfuscatedCode, dynamicImportRegExp);
});
});
});
});
+6 -1
View File
@@ -196,7 +196,7 @@
resolved "https://registry.yarnpkg.com/@types/escodegen/-/escodegen-0.0.6.tgz#5230a9ce796e042cda6f086dbf19f22ea330659c"
integrity sha1-UjCpznluBCzabwhtvxnyLqMwZZw=
"@types/eslint-scope@^3.7.0":
"@types/eslint-scope@3.7.0":
version "3.7.0"
resolved "https://registry.yarnpkg.com/@types/eslint-scope/-/eslint-scope-3.7.0.tgz#4792816e31119ebd506902a482caec4951fabd86"
integrity sha512-O/ql2+rrCUe2W2rs7wMR+GqPRcgB6UiqN5RhrR5xruFlY7l9YLMn0ZkDzjoHLeiFkR8MCQZVudUuuvQ2BLC9Qw==
@@ -468,6 +468,11 @@
resolved "https://registry.yarnpkg.com/@xtuc/long/-/long-4.2.2.tgz#d291c6a4e97989b5c61d9acf396ae4fe133a718d"
integrity sha512-NuHqBY1PB/D8xU6s/thBgOAiAP7HOYDQ32+BFZILJ8ivkUkAHQnWfn6WhL79Owj1qmUnoN/YPhktdIoucipkAQ==
acorn-import-meta@1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/acorn-import-meta/-/acorn-import-meta-1.0.0.tgz#6cff1f01db3b60148934823d3d2dd0c08354aead"
integrity sha512-yX652u86bKzuM+mzEHV84T0R+srQwTOmprUiFC3zlhlc02lBQzqxkB/H/7jexX9vlz/TRuQiZs9mKEDK3bbmhw==
acorn@7.1.0:
version "7.1.0"
resolved "https://registry.yarnpkg.com/acorn/-/acorn-7.1.0.tgz#949d36f2c292535da602283586c2477c57eb2d6c"