-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: createRequire leaking into CommonJS (#3)
fixes #103
- Loading branch information
1 parent
4d5542e
commit 983a5eb
Showing
8 changed files
with
153 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import MagicString from 'magic-string'; | ||
import { attachScopes, type AttachedScope } from 'rollup-pluginutils'; | ||
import { walk } from 'estree-walker'; | ||
import type { Plugin } from 'rollup'; | ||
|
||
export const esmInjectCreateRequire = (): Plugin => { | ||
const createRequire = 'import{createRequire as _pkgrollCR}from"node:module";const require=_pkgrollCR(import.meta.url);'; | ||
|
||
return { | ||
name: 'esmInjectCreateRequire', | ||
renderChunk(code, _chunk, options) { | ||
if ( | ||
options.format !== 'es' | ||
|| !/\brequire\b/.test(code) | ||
) { | ||
return null; | ||
} | ||
|
||
const ast = this.parse(code); | ||
let currentScope = attachScopes(ast, 'scope'); | ||
let injectionNeeded = false; | ||
|
||
walk(ast, { | ||
enter(node) { | ||
// Not all nodes have scopes | ||
if (node.scope) { | ||
currentScope = node.scope as AttachedScope; | ||
} | ||
if ( | ||
node.type === 'Identifier' | ||
&& node.name === 'require' | ||
// If the current scope (or its parents) does not contain 'require' | ||
&& !currentScope.contains('require') | ||
) { | ||
injectionNeeded = true; | ||
|
||
// No need to continue if one instance is found | ||
this.skip(); | ||
} | ||
}, | ||
leave: (node) => { | ||
if (node.scope) { | ||
currentScope = currentScope.parent!; | ||
} | ||
}, | ||
}); | ||
|
||
if (!injectionNeeded) { | ||
return null; | ||
} | ||
|
||
const magicString = new MagicString(code); | ||
magicString.prepend(createRequire); | ||
return { | ||
code: magicString.toString(), | ||
map: magicString.generateMap({ hires: true }), | ||
}; | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters