Example #1
0
    visitProgram: function(path: IPath<ESTree.Program>): boolean {
      if (path.scope.lookup('Buffer') === null) {
        const bufferPath = getModulePath('.', 'buffer', context);
        const bufferIndex = getModuleIndex(bufferPath);

        const body = path.get<ESTree.Statement[]>('body');
        body.get(body.value.length - 1).insertBefore(
          b.variableDeclaration(
            'var',
            [
              b.variableDeclarator(
                b.identifier('Buffer'),
                b.memberExpression(
                  b.memberExpression(
                    b.callExpression(
                      b.identifier('__paeckchen_require__'),
                      [
                        b.literal(bufferIndex)
                      ]
                    ),
                    b.identifier('exports'),
                    false
                  ),
                  b.identifier('Buffer'),
                  false
                )
              )
            ]
          )
        );
        enqueueModule(bufferPath);
      }
      return false;
    }
Example #2
0
function wrapModule(modulePath: string, modules: (ESTree.Expression | ESTree.SpreadElement)[],
    context: IPaeckchenContext, detectedGlobals: IDetectedGlobals, plugins: any): void {
  // Prefill module indices
  getModuleIndex(modulePath);
  if (wrappedModules[modulePath].ast !== undefined) {
    // Module is already up to date
    return;
  }

  try {
    let moduleAst: ESTree.Program;
    if (Object.keys(context.config.externals).indexOf(modulePath) !== -1) {
      moduleAst = wrapExternalModule(modulePath, context);
    } else if (!context.host.fileExists(modulePath)) {
      moduleAst = b.program([
        b.throwStatement(
          b.literal(`Module ${modulePath} not found`)
        )
      ]);
    } else {
      moduleAst = processModule(modulePath, context, detectedGlobals, plugins);
    }

    const wrappedModule = createModuleWrapper(modulePath, moduleAst);
    wrappedModules[wrappedModule.name] = wrappedModule;
    modules[wrappedModule.index] = wrappedModule.ast;
  } catch (e) {
    console.error(`Failed to process module '${modulePath}'`);
    throw e;
  }
}
Example #3
0
export function rewriteRequireStatements(program: ESTree.Program, currentModule: string,
    context: IPaeckchenContext): void {
  visit(program, {
    visitCallExpression(path: IPath<ESTree.CallExpression>): boolean {
      const callee = path.node.callee;
      // TODO: check binding here, not name
      if (n.Identifier.check(callee) && callee.name === 'require') {
        const importPath = path.node.arguments[0];
        if (n.Literal.check(importPath)) {
          const modulePath = getModulePath(currentModule, (importPath as ESTree.Literal).value.toString(), context);
          const moduleIndex = getModuleIndex(modulePath);

          path.replace(
            b.memberExpression(
              b.callExpression(
                b.identifier('__paeckchen_require__'),
                [
                  b.literal(moduleIndex)
                ]
              ),
              b.identifier('exports'),
              false
            )
          );

          enqueueModule(modulePath);
        }
        return false;
      }
      this.traverse(path);
    }
  });
}
Example #4
0
 .map(specifier =>
   b.expressionStatement(
     b.assignmentExpression(
       '=',
       moduleExportsExpression(specifier.exported.name),
       b.literal(specifier.local.name)
     )
   )
Example #5
0
function moduleExportsExpression(id: string): any {
  return b.memberExpression(
    b.memberExpression(
      b.identifier('module'),
      b.identifier('exports'),
      false
    ),
    b.literal(id),
    true
  );
}
Example #6
0
/**
 * var identifier = modules[moduleIndex]();
 */
function importModule(identifier: ESTree.Identifier, moduleIndex: number): ESTree.VariableDeclaration {
  return b.variableDeclaration(
    'var',
    [
      b.variableDeclarator(
        identifier,
        b.callExpression(
          b.identifier('__paeckchen_require__'),
          [
            b.literal(moduleIndex)
          ]
        )
      )
    ]
  );
}
Example #7
0
 visitProgram: function(path: IPath<ESTree.Program>): boolean {
   if (path.scope.lookup('global') === null) {
     const body = path.get<ESTree.Statement[]>('body');
     body.get(body.value.length - 1).insertBefore(
       b.variableDeclaration(
         'var',
         [
           b.variableDeclarator(
             b.identifier('global'),
             b.thisExpression()
           )
         ]
       )
     );
   }
   return false;
 }
Example #8
0
function createModuleWrapper(name: string, moduleAst: ESTree.Program): IWrappedModule {
  const index = getModuleIndex(name);
  return {
    index,
    name,
    ast: b.functionExpression(
      b.identifier(`_${index}`),
      [
        b.identifier('module'),
        b.identifier('exports')
      ],
      b.blockStatement(
        moduleAst.body
      )
    )
  };
}
Example #9
0
    visitExportAllDeclaration: function(path: IPath<ESTree.ExportAllDeclaration>): boolean {
      // e.g. export * from './a';
      const reexportModuleName = getModulePath(currentModule, path.node.source.value as string, context);
      const reexportModuleIndex = getModuleIndex(reexportModuleName);

      const loc = (pos: ESTree.Position) => `${pos.line}_${pos.column}`;
      const tempIdentifier = b.identifier(`__export${reexportModuleIndex}_${loc(path.node.loc.start)}`);

      path.replace(
        importModule(tempIdentifier, reexportModuleIndex),
        exportAllKeys(tempIdentifier)
      );

      enqueueModule(reexportModuleName);
      return false;
    },
Example #10
0
function wrapExternalModule(modulePath: string, context: IPaeckchenContext): ESTree.Program {
  const external = context.config.externals[modulePath] === false
    ? b.objectExpression([])
    : b.identifier(context.config.externals[modulePath] as string);
  return b.program([
    b.expressionStatement(
      b.assignmentExpression(
        '=',
        b.memberExpression(
          b.identifier('module'),
          b.identifier('exports'),
          false
        ),
        external
      )
    )
  ]);
}