Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate dist folder for module use #63

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jspm_packages/
# dotenv environment variables file
.env

dist

package
*.tar.gz

Expand Down
1 change: 1 addition & 0 deletions dist/__tests__/composeSourceMaps.test.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
62 changes: 62 additions & 0 deletions dist/__tests__/composeSourceMaps.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var getMetroTransformer_1 = require("../getMetroTransformer");
var composeSourceMaps_1 = require("../composeSourceMaps");
var fs = require("fs");
var path = require("path");
var source_map_1 = require("source-map/source-map");
var getPositionOfSubstring_1 = require("./getPositionOfSubstring");
var ts = require("typescript");
var numberedLines = fs
.readFileSync(require.resolve("./files/numberedLines.js"))
.toString();
describe("convertMetroRawSourceMapToStandardSourceMap", function () {
it("takes a raw source map and converts it to a non-raw source map", function () {
var transformer = getMetroTransformer_1.getMetroTransformer(47);
var _a = transformer.transform({
filename: require.resolve("./files/numberedLines.js"),
src: numberedLines,
options: {
retainLines: false,
},
}), map = _a.map, code = _a.code;
if (typeof code !== "string") {
// use this rather than expect for typescript's sake
throw new Error("code must be a string");
}
expect(Array.isArray(map)).toBe(true);
expect(map).toMatchSnapshot();
var standardMap = composeSourceMaps_1.convertMetroRawSourceMapToStandardSourceMap(map, path.join(__dirname, "numberedLines.js"), numberedLines);
var standardMapConsumer = new source_map_1.SourceMapConsumer(standardMap); // upstream types are wrong
for (var _i = 0, _b = ["line1", "line2", "line3", "line5"]; _i < _b.length; _i++) {
var substring = _b[_i];
expect(standardMapConsumer.originalPositionFor(getPositionOfSubstring_1.getPositionOfSubstring(code, substring))).toMatchObject(getPositionOfSubstring_1.getPositionOfSubstring(numberedLines, substring));
}
expect(standardMap).toMatchSnapshot();
});
});
describe("composeSourceMaps", function () {
it("composes two source maps together", function () {
var filename = require.resolve("./files/hello.ts");
var hello = fs.readFileSync(filename).toString();
var tsTranspileResult = ts.transpileModule(hello, {
fileName: filename,
compilerOptions: {
sourceMap: true,
target: ts.ScriptTarget.ES2015,
},
});
expect(tsTranspileResult.outputText).toMatchSnapshot();
var upstreamTransformResult = getMetroTransformer_1.getMetroTransformer(47).transform({
filename: filename,
src: tsTranspileResult.outputText,
options: {
retainLines: false,
},
});
expect(upstreamTransformResult.code).toMatchSnapshot();
var composedMap = new source_map_1.SourceMapConsumer(composeSourceMaps_1.composeSourceMaps(tsTranspileResult.sourceMapText, composeSourceMaps_1.convertMetroRawSourceMapToStandardSourceMap(upstreamTransformResult.map, filename, hello), filename, hello)); // upstream types are wrong
expect(composedMap.originalPositionFor(getPositionOfSubstring_1.getPositionOfSubstring(upstreamTransformResult.code, "line6"))).toMatchObject({ line: 6 });
expect(composedMap.originalPositionFor(getPositionOfSubstring_1.getPositionOfSubstring(upstreamTransformResult.code, "line8"))).toMatchObject({ line: 8 });
});
});
1 change: 1 addition & 0 deletions dist/__tests__/extendFileExtension.test.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
10 changes: 10 additions & 0 deletions dist/__tests__/extendFileExtension.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var extendFileExtension_1 = require("../extendFileExtension");
describe("extendFileExtension", function () {
it("adds an extension part", function () {
expect(extendFileExtension_1.extendFileExtension("blah.js", "ext")).toBe("blah.ext.js");
expect(extendFileExtension_1.extendFileExtension("blah.tsx", "obfuscated")).toBe("blah.obfuscated.tsx");
expect(extendFileExtension_1.extendFileExtension("blah", "js")).toBe("blah.js");
});
});
5 changes: 5 additions & 0 deletions dist/__tests__/files/hello.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface Hello {
readonly line2: string;
line3: number;
}
export declare const line8: boolean;
4 changes: 4 additions & 0 deletions dist/__tests__/files/hello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
console.log("line6");
exports.line8 = true;
4 changes: 4 additions & 0 deletions dist/__tests__/getPositionOfSubstring.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export declare function getPositionOfSubstring(text: string, substring: string): {
line: number;
column: number;
} | null;
13 changes: 13 additions & 0 deletions dist/__tests__/getPositionOfSubstring.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function getPositionOfSubstring(text, substring) {
var lines = text.split(/\r?\n/);
for (var line = 0; line < lines.length; line++) {
var column = lines[line].indexOf(substring);
if (column >= 0) {
return { line: line + 1, column: column };
}
}
return null;
}
exports.getPositionOfSubstring = getPositionOfSubstring;
1 change: 1 addition & 0 deletions dist/__tests__/obfuscateCode.test.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
23 changes: 23 additions & 0 deletions dist/__tests__/obfuscateCode.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var fs = require("fs");
jest.mock("javascript-obfuscator", function () { return ({
obfuscate: function () {
return {
getObfuscatedCode: function () {
return "this code is obfuscated";
},
getSourceMap: function () {
return "";
},
};
},
}); });
var obfuscateCode_1 = require("../obfuscateCode");
describe("obfuscateCode", function () {
it("obfuscates code", function () {
var filename = require.resolve("./files/es5.js");
var es5code = fs.readFileSync(filename).toString();
expect(obfuscateCode_1.obfuscateCode(es5code, {})).toBe("this code is obfuscated");
});
});
5 changes: 5 additions & 0 deletions dist/composeSourceMaps.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { RawSourceMap } from "source-map";
export declare type MetroRawSourceMap = Array<[number, number, number, number, string | undefined]>;
export declare function convertMetroRawSourceMapToStandardSourceMap(map: MetroRawSourceMap, originalFileName: string, originalFileContent: string): string;
export declare function convertStandardSourceMapToMetroRawSourceMap(map: RawSourceMap | string): [number, number, number, number, string | undefined][];
export declare function composeSourceMaps(sourceMap: string | RawSourceMap, targetMap: string | RawSourceMap, sourceFileName: string, sourceContent: string): string;
70 changes: 70 additions & 0 deletions dist/composeSourceMaps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var source_map_1 = require("source-map");
function convertMetroRawSourceMapToStandardSourceMap(map, originalFileName, originalFileContent) {
var outputMap = new source_map_1.SourceMapGenerator();
outputMap.setSourceContent(originalFileName, originalFileContent);
map.forEach(function (args) {
var generatedLine = args[0], generatedColumn = args[1], originalLine = args[2], originalColumn = args[3];
outputMap.addMapping({
generated: {
line: generatedLine,
column: generatedColumn,
},
original: {
line: originalLine,
column: originalColumn,
},
source: originalFileName,
name: args.length === 5 ? args[4] : undefined,
});
});
return outputMap.toString();
}
exports.convertMetroRawSourceMapToStandardSourceMap = convertMetroRawSourceMapToStandardSourceMap;
function convertStandardSourceMapToMetroRawSourceMap(map) {
var consumer = new source_map_1.SourceMapConsumer(map); // upstream types are wrong
var outputMap = [];
consumer.eachMapping(function (mapping) {
outputMap.push([
mapping.generatedLine,
mapping.generatedColumn,
mapping.originalLine,
mapping.originalColumn,
mapping.name,
]);
});
return outputMap;
}
exports.convertStandardSourceMapToMetroRawSourceMap = convertStandardSourceMapToMetroRawSourceMap;
function composeSourceMaps(sourceMap, targetMap, sourceFileName, sourceContent) {
var tsConsumer = new source_map_1.SourceMapConsumer(sourceMap); // upstreeam types are wrong
var babelConsumer = new source_map_1.SourceMapConsumer(targetMap);
var map = new source_map_1.SourceMapGenerator();
map.setSourceContent(sourceFileName, sourceContent);
babelConsumer.eachMapping(function (_a) {
var generatedLine = _a.generatedLine, generatedColumn = _a.generatedColumn, originalLine = _a.originalLine, originalColumn = _a.originalColumn, name = _a.name;
if (originalLine) {
var original = tsConsumer.originalPositionFor({
line: originalLine,
column: originalColumn,
});
if (original.line) {
map.addMapping({
generated: {
line: generatedLine,
column: generatedColumn,
},
original: {
line: original.line,
column: original.column,
},
source: sourceFileName,
name: name,
});
}
}
});
return map.toString();
}
exports.composeSourceMaps = composeSourceMaps;
1 change: 1 addition & 0 deletions dist/extendFileExtension.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare function extendFileExtension(filename: string, extensionPart: string): string;
8 changes: 8 additions & 0 deletions dist/extendFileExtension.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function extendFileExtension(filename, extensionPart) {
var parts = filename.split(".");
parts.splice(1, 0, extensionPart);
return parts.join(".");
}
exports.extendFileExtension = extendFileExtension;
1 change: 1 addition & 0 deletions dist/getCallerFile.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare function getCallerFile(): any;
30 changes: 30 additions & 0 deletions dist/getCallerFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
// This code taken from https://stackoverflow.com/a/29581862/1382997
// Question by einstein https://stackoverflow.com/users/449132/einstein
// Answer by Rhionin https://stackoverflow.com/users/1751376/rhionin
function getCallerFile() {
var originalFunc = Error.prepareStackTrace;
var callerfile;
try {
var err = new Error();
var currentfile;
Error.prepareStackTrace = function (_, stack) {
return stack;
};
currentfile = err.stack.shift().getFileName();
// go up two places in the stack trace. i.e. out of this file, and out of index.ts
for (var i = 0; i < 2; i++) {
while (err.stack.length) {
callerfile = err.stack.shift().getFileName();
if (currentfile !== callerfile)
break;
}
currentfile = callerfile;
}
}
catch (e) { }
Error.prepareStackTrace = originalFunc;
return callerfile;
}
exports.getCallerFile = getCallerFile;
25 changes: 25 additions & 0 deletions dist/getMetroTransformer.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Node } from "babel-core";
import { RawSourceMap } from "source-map";
import { MetroRawSourceMap } from "./composeSourceMaps";
export interface MetroTransformerResult {
ast?: Node;
code?: string;
map?: string | RawSourceMap | MetroRawSourceMap;
}
export interface MetroTransformer {
transform(props: {
filename: string;
src: string;
options: {
dev?: boolean;
retainLines?: boolean;
};
}): MetroTransformerResult;
getCacheKey?(): string;
}
export declare function getMetroTransformer(reactNativeMinorVersion?: number): MetroTransformer;
export interface ReactNativeObfuscatingTransformerDefaultResult {
code: string;
map: string;
}
export declare function maybeTransformMetroResult(upstreamResult: MetroTransformerResult, { code, map }: ReactNativeObfuscatingTransformerDefaultResult, reactNativeMinorVersion?: number): MetroTransformerResult;
71 changes: 71 additions & 0 deletions dist/getMetroTransformer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var source_map_1 = require("source-map");
var semver = require("semver");
var composeSourceMaps_1 = require("./composeSourceMaps");
var babylon = require("babylon");
var babel_traverse_1 = require("babel-traverse");
function getReactNativeMinorVersion() {
var reactNativeVersionString = require("react-native/package.json").version;
var parseResult = semver.parse(reactNativeVersionString);
if (!parseResult) {
throw new Error("Can't parse react-native version string '" + reactNativeVersionString + "'");
}
return parseResult.minor;
}
function getMetroTransformer(reactNativeMinorVersion) {
if (reactNativeMinorVersion === void 0) { reactNativeMinorVersion = getReactNativeMinorVersion(); }
if (reactNativeMinorVersion >= 59) {
return require('metro-react-native-babel-transformer/src/index');
}
else if (reactNativeMinorVersion >= 56) {
return require("metro/src/reactNativeTransformer");
}
else if (reactNativeMinorVersion >= 52) {
return require("metro/src/transformer");
}
else if (reactNativeMinorVersion >= 0.47) {
return require("metro-bundler/src/transformer");
}
else if (reactNativeMinorVersion === 0.46) {
return require("metro-bundler/build/transformer");
}
else {
throw new Error("react-native-obfuscating-transformer requires react-native >= 0.46");
}
}
exports.getMetroTransformer = getMetroTransformer;
function maybeTransformMetroResult(upstreamResult, _a, reactNativeMinorVersion) {
var code = _a.code, map = _a.map;
if (reactNativeMinorVersion === void 0) { reactNativeMinorVersion = getReactNativeMinorVersion(); }
if (reactNativeMinorVersion >= 52) {
// convert code and map to ast
var ast = babylon.parse(code, {
sourceType: "module",
});
var mapConsumer_1 = new source_map_1.SourceMapConsumer(map) // upstream types are wrong
;
babel_traverse_1.default.cheap(ast, function (node) {
if (node.loc) {
var originalStart = mapConsumer_1.originalPositionFor(node.loc.start);
if (originalStart.line) {
node.loc.start.line = originalStart.line;
node.loc.start.column = originalStart.column;
}
var originalEnd = mapConsumer_1.originalPositionFor(node.loc.end);
if (originalEnd.line) {
node.loc.end.line = originalEnd.line;
node.loc.end.column = originalEnd.column;
}
}
});
return { ast: ast };
}
else if (Array.isArray(upstreamResult.map)) {
return { code: code, map: composeSourceMaps_1.convertStandardSourceMapToMetroRawSourceMap(map) };
}
else {
return { code: code, map: map };
}
}
exports.maybeTransformMetroResult = maybeTransformMetroResult;
2 changes: 2 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { obfuscatingTransformer } from "./obfuscatingTransformer";
export = obfuscatingTransformer;
3 changes: 3 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"use strict";
var obfuscatingTransformer_1 = require("./obfuscatingTransformer");
module.exports = obfuscatingTransformer_1.obfuscatingTransformer;
8 changes: 8 additions & 0 deletions dist/obfuscateCode.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as Obfuscator from "javascript-obfuscator";
import { MetroRawSourceMap } from "./composeSourceMaps";
import { RawSourceMap } from "source-map/source-map";
export declare function obfuscateCode(code: string, options: Obfuscator.Options): string;
export declare function obfuscateCodePreservingSourceMap(code: string, map: string | RawSourceMap | MetroRawSourceMap, originlFilename: string, originalSource: string, options: Obfuscator.Options): {
code: string;
map: string;
};
Loading