UNPKG

rollup-plugin-graphql

Version:
37 lines (27 loc) 843 B
import { EOL } from 'os'; export default function (source) { if (typeof source !== "string") { return source; } return replaceRequires(replaceModuleExports(source)); } function replaceModuleExports(source) { return source.replace('module.exports = doc', 'export default doc'); } function replaceRequires(source) { const imports = {}; let index = 0; // replace a require statement with a variable source = source.replace(/require\(([^)]+)\)/ig, (match, path) => { path = path.replace(/[\"\']+/g, ''); if (!imports[path]) { imports[path] = `frgmt${++index}`; } return imports[path]; }); // prepare import statements const importsOutput = Object.keys(imports) .map((path) => `import ${imports[path]} from "${path}";`) .join(EOL); return importsOutput + EOL + source; }