Skip to content
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
18 changes: 9 additions & 9 deletions functions/packager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ async function getContents(
{},
);

// Hardcoded deletion of some modules that are not used but added by accident
deleteHardcodedRequires(
contents,
"/node_modules/react/cjs/react.production.min.js",
);
deleteHardcodedRequires(
contents,
"/node_modules/react-dom/cjs/react-dom.production.min.js",
);
// // Hardcoded deletion of some modules that are not used but added by accident
// deleteHardcodedRequires(
// contents,
// "/node_modules/react/cjs/react.production.min.js",
// );
// deleteHardcodedRequires(
// contents,
// "/node_modules/react-dom/cjs/react-dom.production.min.js",
// );

return { ...contents, ...packageJSONFiles };
}
Expand Down
8 changes: 3 additions & 5 deletions functions/packager/packages/find-requires.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fs } from "mz";
import { dirname, join } from "path";
import { join } from "path";

import { IPackage } from "./find-package-infos";
import resolveRequiredFiles from "./resolve-required-files";
Expand All @@ -21,9 +21,6 @@ export interface IFileData {
}

function rewritePath(path: string, currentPath: string, packagePath: string) {
const relativePath = nodeResolvePath(join(dirname(currentPath), path));
const isDependency = /^(\w|@\w)/.test(path);

return browserResolve.sync(path, { filename: currentPath });
}

Expand All @@ -50,6 +47,7 @@ function buildRequireObject(
try {
extractedRequires = extractRequires(fileData.content);
} catch (e) {
console.error(e);
return existingContents;
}

Expand All @@ -60,7 +58,7 @@ function buildRequireObject(
try {
newPath = rewritePath(requirePath, filePath, packagePath);
} catch (e) {
console.warn(`Couldn't find ${requirePath}`);
// console.warn(`Couldn't find ${requirePath}`);
return;
}

Expand Down
20 changes: 20 additions & 0 deletions functions/packager/packages/utils/extract-requires.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ describe("extractRequires", () => {
expect(extractRequires(code)).toEqual(["react"]);
});

it("can eliminate requires statements", () => {
const code = `
if (process.env.NODE_ENV === 'development') {
module.exports = require('react');
} else {
module.exports = require('blaat');
}
`;

expect(extractRequires(code)).toEqual(["react"]);
});

it("can find dynamic require statements", () => {
const code = `
const react = import('react');
Expand Down Expand Up @@ -51,4 +63,12 @@ describe("extractRequires", () => {

expect(extractRequires(code)).toEqual(["react-dom"]);
});

it("can find re-exports", () => {
const code = `
export react from './react';
`;

expect(extractRequires(code)).toEqual(["./react"]);
});
});
71 changes: 17 additions & 54 deletions functions/packager/packages/utils/extract-requires.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import * as acorn from "acorn";
import {
CallExpression,
ImportDeclaration,
Literal,
MemberExpression,
} from "estree";
/* tslint:disable */
const walk = require("acorn/dist/walk");
import { CallExpression, ImportDeclaration, Literal } from "estree";
import * as Babel from "babel-core";
import { babylonAstDependencies } from "./get-deps";

require("acorn-dynamic-import/lib/inject").default(acorn);
/* tslint:enable */
Expand All @@ -33,56 +28,24 @@ type NewCallExpression = CallExpression & {
};

export default function exportRequires(code: string) {
const ast = acorn.parse(code, {
ecmaVersion: ECMA_VERSION,
locations: true,
plugins: {
dynamicImport: true,
},
ranges: true,
sourceType: "module",
const plugins: any = [
"transform-node-env-inline",
"minify-dead-code-elimination",
];
const presets = ["stage-0"];

process.env.NODE_ENV = "development";
let { ast } = Babel.transform(code, {
plugins,
presets,
});
process.env.NODE_ENV = "production";

const requires: string[] = [];

walk.simple(
ast,
{
ImportDeclaration(node: ImportDeclaration) {
if (typeof node.source.value === "string") {
requires.push(node.source.value);
}
},
CallExpression(node: NewCallExpression) {
if (
/* require() */ (node.callee.type === "Identifier" &&
node.callee.name === "require") ||
node.callee.type === "Import" ||
/* require.resolve */ (node.callee.type === "MemberExpression" &&
node.callee.object.name &&
node.callee.object.name === "require" &&
node.callee.property.name &&
node.callee.property.name === "resolve")
) {
if (
node.arguments.length === 1 &&
node.arguments[0].type === "Literal"
) {
const literalArgument = node.arguments[0] as Literal;
if (typeof literalArgument.value === "string") {
requires.push(literalArgument.value);
}
}
}
},
},
{
...walk.base,
Import(node: any, st: any, c: any) {
// Do nothing
},
},
);
const deps = babylonAstDependencies(ast);

deps.forEach((dep: any) => requires.push(dep.source));

return requires;
}
59 changes: 59 additions & 0 deletions functions/packager/packages/utils/get-deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import traverse from "babel-traverse";
import * as types from "babel-types";

export function babylonAstDependencies(ast: any) {
const dependencies: any[] = [];

function addDependency(source: any) {
// Ensure that dependencies are only identified once
if (!dependencies.some(dep => dep.source === source)) {
dependencies.push({ source });
}
}

traverse(ast, {
// `import ... from '...';
ImportDeclaration(node: any) {
addDependency(node.node.source.value);
},
// `export ... from '...';
ExportDeclaration(node: any) {
if (node.node.source) {
addDependency(node.node.source.value);
}
},
// `require('...');
CallExpression(node: any) {
const callNode = node.node;
if (
callNode.callee.name === "require" ||
callNode.callee.type === "Import"
) {
const arg = callNode.arguments[0];
if (types.isLiteral(arg)) {
const anyArg: any = arg;
addDependency(anyArg.value);
} else {
if (!arg.loc || !arg.loc.start) {
throw new Error("Require expression cannot be statically analyzed");
}

const err: any = new Error(
`Require expression at line ${arg.loc.start.line}, column ${
arg.loc.start.column
} cannot be statically analyzed`,
);

err.loc = {
line: arg.loc.start.line,
column: arg.loc.start.column,
};

throw err;
}
}
},
});

return dependencies;
}
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
"acorn": "^5.1.2",
"acorn-dynamic-import": "^2.0.2",
"aws-lambda": "^0.1.2",
"babel-core": "^6.26.3",
"babel-plugin-minify-dead-code-elimination": "^0.4.3",
"babel-plugin-transform-node-env-inline": "^0.4.3",
"babel-preset-stage-0": "^6.24.1",
"babylon-ast-dependencies": "^1.0.2",
"browser-resolve": "^1.11.2",
"json5": "^1.0.1",
"lambda-git": "^0.1.1",
Expand All @@ -33,6 +38,7 @@
"devDependencies": {
"@types/acorn": "^4.0.2",
"@types/aws-lambda": "^0.0.16",
"@types/babel-core": "^6.25.5",
"@types/browser-resolve": "^0.0.5",
"@types/express": "^4.0.37",
"@types/jest": "^21.1.1",
Expand Down
Loading