Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
cjs-esm-exports
Advanced tools
A WASM module to parse commonjs exports for ESM, powered by swc in rust.
npm install cjs-esm-exports
for yarn
users:
yarn add cjs-esm-exports
Types:
export function parse(
specifier: string,
code: string,
node_env?: 'development' | 'production',
call_mode?: boolean,
): {
exports: string[],
reexports: string[],
};
Example:
const { parse } = require('cjs-esm-exports');
// named exports
// exports: ['a', 'b', 'c', '__esModule', 'foo']
const { exports } = parse('index.cjs', `
/* exports.ignore = "not detected"; */
exports.a = "a";
module.exports.b = "b";
Object.defineProperty(exports, "c", { value: "c" });
Object.defineProperty(module.exports, "__esModule", { value: true })
const key = "foo"
Object.defineProperty(exports, key, { value: "e" });
`);
// reexports
// reexports: ['./lib']
const { reexports } = parse('index.cjs', `
module.exports = require("./lib");
`);
// object exports(spread supported)
// exports: ['foo', 'baz']
// reexports: ['./lib']
const { reexports } = parse('index.cjs', `
const foo = 'bar'
const obj = { baz: 123 }
module.exports = { foo, ...obj, ...require("./lib") };
`);
// condition
// exports: ['foo', 'cjs']
const { exports } = parse('index.cjs', `
module.exports.a = "a";
if (true) {
exports.foo = "bar";
}
const mtype = "cjs";
if (mtype === "cjs") {
exports.cjs = true;
} else {
exports.esm = true;
}
if (false) {
exports.ignore = "ignore";
}
`);
// block&IIFE
// exports: ['foo', 'baz', '__esModule']
const { exports } = parse('index.cjs', `
(function () {
exports.foo = "bar"
if (true) {
return
}
exports.ignore = '-'
})();
{
exports.baz = 123
}
exports.__esModule = true
`);
// condition with `process.env.NODE_ENV`
// reexports: ['./index.development']
const { reexports } = parse('index.cjs', `
if (process.env.NODE_ENV === "development") {
module.exports = require("./index.development")
} else {
module.exports = require("./index.production")
}
`, 'development');
// IIFE exports
// exports: ['foo']
const { exports } = parse('index.cjs', `
function Fn() {
return { foo: "bar" }
}
module.exports = Fn()
`);
// function reexports
// reexports: ['./lib()']
const { reexports } = parse('index.cjs', `
module.exports = require("./lib")()
`);
// apply function exports (call mode)
// exports: ['foo']
const { reexports } = parse('lib.cjs', `
module.exports = function() {
return { foo: 'bar' }
}
`, 'production', true);
You will need rust 1.30+ and wasm-pack.
wasm-pack build --target nodejs
cargo test --all
FAQs
A WASM module to parse commonjs exports for ESM.
The npm package cjs-esm-exports receives a total of 1 weekly downloads. As such, cjs-esm-exports popularity was classified as not popular.
We found that cjs-esm-exports demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.