UNPKG

export-loader

Version:

A webpack loader that allowes to write a Golang style exports.

35 lines (30 loc) 1.24 kB
const { generateExports } = require('./esprima') const d = require('debug')('norequire') const esprima = require('esprima') const { readFileSync } = require('fs') const path = require('path') // main loader func, calls generateExports with dependencies // which returns a func with a parser inside function exportLoader(content) { try{ // get current path from context or resolve when testing let currentPath = this.resourcePath || path.resolve("./test-utils/resourcePath.js") // get finalExports after parsing let { finalExports } = generateExports(esprima)(currentPath, readFileSync(currentPath,'utf8')) d(`finalExports is: ${finalExports}`) // replace all existing exports for duplicate error prevention content = content .replace(/export function/g,'function') .replace(/export (let|const|var)/g,'let') .replace(/export async function/g,'async function') .replace(/^export\s*{([\s\S]*?)}$/gm, '') .concat('\n') .concat(finalExports) d(content) }catch(e){ d(`Failed on export loader main func with error:${e}`) } return content } module.exports = exportLoader; module.exports.default = exportLoader;