Skip to content

Commit

Permalink
make changes
Browse files Browse the repository at this point in the history
  • Loading branch information
khanayan123 committed Dec 4, 2023
1 parent 2362ff1 commit 7287449
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 13 deletions.
5 changes: 4 additions & 1 deletion hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,11 @@ function createHook (meta) {
const iitmURL = new URL('lib/register.js', meta.url).toString()
async function getSource (url, context, parentGetSource) {
if (hasIitm(url)) {

const realUrl = deleteIitm(url)

const exportNames = await getExports(realUrl, context, parentGetSource)

return {
source: `
import { register } from '${iitmURL}'
Expand All @@ -162,7 +165,7 @@ register(${JSON.stringify(realUrl)}, namespace, set, ${JSON.stringify(specifiers
return parentGetSource(url, context, parentGetSource)
}
try {
const outPut = astParse(fileContents)
const outPut = astParse(fileContents, url)
fileContents = outPut.code
exportAlias = outPut.exportAlias
} catch (parseError) {
Expand Down
4 changes: 2 additions & 2 deletions lib/ast-parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

const getEsmExports = require('./get-esm-exports.js')

function astParse(fileContents) {
return getEsmExports(fileContents, true)
function astParse(fileContents, url) {
return getEsmExports(fileContents, true, url)
}

module.exports = astParse
25 changes: 17 additions & 8 deletions lib/get-esm-exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const recast = require('recast');
const { Parser } = require('acorn')
const { importAssertions } = require('acorn-import-assertions');

const TS_EXTENSION_RE = /\.(ts|mts|cts)$/

const acornOpts = {
ecmaVersion: 'latest',
sourceType: 'module'
Expand All @@ -15,14 +17,21 @@ function warn (txt) {
process.emitWarning(txt, 'get-esm-exports')
}

function getEsmExports(moduleStr, generate=false) {
function getEsmExports(moduleStr, generate=false, url=undefined) {
const exportSpecifierNames = new Set();
const exportAlias = {}
const ast = recast.parse(moduleStr, {parser: {
parse(source) {
return parser.parse(source, acornOpts);
}
}})
let ast

// if it's a typescript file, we need to parse it with recasts typescript parser
if (url && TS_EXTENSION_RE.test(url)) {
ast = recast.parse(moduleStr, {parser: require("recast/parsers/typescript")})
} else {
ast = recast.parse(moduleStr, {parser: {
parse(source) {
return parser.parse(source, acornOpts);
}
}})
}

recast.visit(ast, {
visitExportNamedDeclaration(path) {
Expand All @@ -41,7 +50,7 @@ function getEsmExports(moduleStr, generate=false) {
const node = path.node;
const iitmRenamedExport = 'iitmRenamedExport';

if (node.declaration.type === 'ObjectExpression' || node.declaration.type === 'ArrayExpression' || node.declaration.type === 'Literal') {
if ((node.declaration.type === 'ObjectExpression' || node.declaration.type === 'ArrayExpression' || node.declaration.type === 'Literal') && generate) {
const variableDeclaration = {
type: 'VariableDeclaration',
kind: 'let',
Expand Down Expand Up @@ -97,7 +106,7 @@ function getEsmExports(moduleStr, generate=false) {
},
});

if (exportSpecifierNames.size !== 0) {
if (exportSpecifierNames.size !== 0 && generate) {
convertExportSpecifierToLet(exportSpecifierNames, ast)
}

Expand Down
4 changes: 2 additions & 2 deletions lib/get-exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ async function getExports (url, context, parentLoad) {
}

if (format === 'module') {
return getEsmExports(source)
return getEsmExports(source, false, url)
}
if (format === 'commonjs') {
return addDefault(getCjsExports(source).exports)
}

// At this point our `format` is either undefined or not known by us. Fall
// back to parsing as ESM/CJS.
const esmExports = getEsmExports(source)
const esmExports = getEsmExports(source, false, url)
if (!esmExports.length) {
// TODO(bengl) it's might be possible to get here if somehow the format
// isn't set at first and yet we have an ESM module with no exports.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"typescript": "^4.7.4"
},
"dependencies": {
"@babel/parser": "^7.23.5",
"acorn": "^8.11.2",
"acorn-import-assertions": "^1.9.0",
"cjs-module-lexer": "^1.2.2",
Expand Down

0 comments on commit 7287449

Please sign in to comment.