New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

rollup-plugin-npm

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rollup-plugin-npm - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

4

CHANGELOG.md
# rollup-plugin-npm changelog
## 1.1.0
* Use node-resolve to handle various corner cases
## 1.0.0

@@ -4,0 +8,0 @@

109

dist/rollup-plugin-npm.cjs.js
'use strict';
var path = require('path');
var fs = require('fs');
var builtins = require('builtin-modules');
builtins = 'default' in builtins ? builtins['default'] : builtins;
var resolve = require('resolve');
resolve = 'default' in resolve ? resolve['default'] : resolve;
var babelHelpers = {};
babelHelpers.toConsumableArray = function (arr) {
if (Array.isArray(arr)) {
for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];
return arr2;
} else {
return Array.from(arr);
}
};
var absolutePath = /^(?:\/|(?:[A-Za-z]:)?[\\|\/])/;
function dirExists(dir) {
try {
fs.readdirSync(dir);
return true;
} catch (err) {
return false;
}
}
function npm(options) {

@@ -33,74 +13,35 @@ options = options || {};

var skip = options.skip || [];
var useMain = options.main !== false;
return {
resolveId: function resolveId(importee, importer) {
// disregard relative paths, absolute paths, and entry modules
if (importee[0] === '.' || absolutePath.test(importee) || !importer) return null;
// lop off trailing slash to handle bizarro cases like
// https://github.com/nodejs/readable-stream/blob/077681f08e04094f087f11431dc64ca147dda20f/lib/_stream_readable.js#L125
if (importee.slice(-1) === '/') importee = importee.slice(0, -1);
var parts = importee.split(/[\/\\]/);
var id = parts.shift();
// npm scoped packages – @user/package
if (id[0] === '@' && parts[0]) {
id += '/' + parts.shift();
}
if (~skip.indexOf(id)) return null;
// exclude skipped modules
if (~skip.indexOf(id)) return;
// disregard entry modules and builtins
if (!importer || ~builtins.indexOf(importee)) return null;
var root = absolutePath.exec(importer)[0];
var dir = path.dirname(importer);
var modulePath;
while (dir !== root && dir !== '.') {
modulePath = path.resolve(dir, 'node_modules', id);
if (dirExists(modulePath)) {
// `foo/src/bar`
if (parts.length) {
return path.resolve.apply(undefined, [modulePath].concat(babelHelpers.toConsumableArray(parts))).replace(/\.js$/, '') + '.js';
}
if (!options.jsnext && !options.main) {
throw new Error('To import from a package in node_modules (' + id + '), either options.jsnext or options.main must be true');
}
// `foo`
var pkgPath = path.resolve(modulePath, 'package.json');
var pkg = undefined;
try {
pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
} catch (err) {
throw new Error('Missing or malformed package.json: ' + modulePath);
}
if (options.jsnext) {
var main = pkg['jsnext:main'];
if (main) return path.resolve(path.dirname(pkgPath), main).replace(/\.js$/, '') + '.js';
if (!options.main) {
throw new Error('Package ' + id + ' (imported by ' + importer + ') does not have a jsnext:main field. You should either allow legacy modules with options.main, or skip it with options.skip = [\'' + id + '\'])');
return new Promise(function (accept, reject) {
resolve(importee, {
basedir: path.dirname(importer),
packageFilter: function packageFilter(pkg) {
var id = pkg['name'];
if (options.jsnext) {
var main = pkg['jsnext:main'];
if (main) {
pkg['main'] = main;
} else if (!useMain) {
throw new Error('Package ' + id + ' (imported by ' + importer + ') does not have a jsnext:main field. You should either allow legacy modules with options.main, or skip it with options.skip = [\'' + id + '\'])');
}
} else if (!useMain) {
throw new Error('To import from a package in node_modules (' + id + '), either options.jsnext or options.main must be true');
}
return pkg;
}
if (options.main) {
var main = pkg['main'] || 'index.js';
if (main) return path.resolve(path.dirname(pkgPath), main).replace(/\.js$/, '') + '.js';
}
if (~builtins.indexOf(id)) return false;
throw new Error('Could not import module ' + id + ' (imported by ' + importer + ')');
}
dir = path.dirname(dir);
}
return null;
}, function (err, resolved) {
return err ? reject(err) : accept(resolved);
});
});
}

@@ -107,0 +48,0 @@ };

@@ -1,26 +0,5 @@

import { dirname, resolve } from 'path';
import { readdirSync, readFileSync } from 'fs';
import { dirname } from 'path';
import builtins from 'builtin-modules';
import resolve from 'resolve';
var babelHelpers = {};
babelHelpers.toConsumableArray = function (arr) {
if (Array.isArray(arr)) {
for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];
return arr2;
} else {
return Array.from(arr);
}
};
var absolutePath = /^(?:\/|(?:[A-Za-z]:)?[\\|\/])/;
function dirExists(dir) {
try {
readdirSync(dir);
return true;
} catch (err) {
return false;
}
}
function npm(options) {

@@ -30,74 +9,35 @@ options = options || {};

var skip = options.skip || [];
var useMain = options.main !== false;
return {
resolveId: function resolveId(importee, importer) {
// disregard relative paths, absolute paths, and entry modules
if (importee[0] === '.' || absolutePath.test(importee) || !importer) return null;
// lop off trailing slash to handle bizarro cases like
// https://github.com/nodejs/readable-stream/blob/077681f08e04094f087f11431dc64ca147dda20f/lib/_stream_readable.js#L125
if (importee.slice(-1) === '/') importee = importee.slice(0, -1);
var parts = importee.split(/[\/\\]/);
var id = parts.shift();
// npm scoped packages – @user/package
if (id[0] === '@' && parts[0]) {
id += '/' + parts.shift();
}
if (~skip.indexOf(id)) return null;
// exclude skipped modules
if (~skip.indexOf(id)) return;
// disregard entry modules and builtins
if (!importer || ~builtins.indexOf(importee)) return null;
var root = absolutePath.exec(importer)[0];
var dir = dirname(importer);
var modulePath;
while (dir !== root && dir !== '.') {
modulePath = resolve(dir, 'node_modules', id);
if (dirExists(modulePath)) {
// `foo/src/bar`
if (parts.length) {
return resolve.apply(undefined, [modulePath].concat(babelHelpers.toConsumableArray(parts))).replace(/\.js$/, '') + '.js';
}
if (!options.jsnext && !options.main) {
throw new Error('To import from a package in node_modules (' + id + '), either options.jsnext or options.main must be true');
}
// `foo`
var pkgPath = resolve(modulePath, 'package.json');
var pkg = undefined;
try {
pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));
} catch (err) {
throw new Error('Missing or malformed package.json: ' + modulePath);
}
if (options.jsnext) {
var main = pkg['jsnext:main'];
if (main) return resolve(dirname(pkgPath), main).replace(/\.js$/, '') + '.js';
if (!options.main) {
throw new Error('Package ' + id + ' (imported by ' + importer + ') does not have a jsnext:main field. You should either allow legacy modules with options.main, or skip it with options.skip = [\'' + id + '\'])');
return new Promise(function (accept, reject) {
resolve(importee, {
basedir: dirname(importer),
packageFilter: function packageFilter(pkg) {
var id = pkg['name'];
if (options.jsnext) {
var main = pkg['jsnext:main'];
if (main) {
pkg['main'] = main;
} else if (!useMain) {
throw new Error('Package ' + id + ' (imported by ' + importer + ') does not have a jsnext:main field. You should either allow legacy modules with options.main, or skip it with options.skip = [\'' + id + '\'])');
}
} else if (!useMain) {
throw new Error('To import from a package in node_modules (' + id + '), either options.jsnext or options.main must be true');
}
return pkg;
}
if (options.main) {
var main = pkg['main'] || 'index.js';
if (main) return resolve(dirname(pkgPath), main).replace(/\.js$/, '') + '.js';
}
if (~builtins.indexOf(id)) return false;
throw new Error('Could not import module ' + id + ' (imported by ' + importer + ')');
}
dir = dirname(dir);
}
return null;
}, function (err, resolved) {
return err ? reject(err) : accept(resolved);
});
});
}

@@ -104,0 +44,0 @@ };

{
"name": "rollup-plugin-npm",
"description": "Bundle third-party dependencies in node_modules",
"version": "1.0.0",
"version": "1.1.0",
"devDependencies": {

@@ -31,4 +31,5 @@ "eslint": "^1.7.3",

"dependencies": {
"builtin-modules": "^1.1.0"
"builtin-modules": "^1.1.0",
"resolve": "^1.1.6"
}
}
# rollup-plugin-npm
**experimental, depends on unreleased version of Rollup**
Find third party modules in `node_modules`, so that they can be included in a Rollup bundle.

@@ -6,0 +4,0 @@

@@ -1,94 +0,46 @@

import { dirname, resolve } from 'path';
import { readdirSync, readFileSync } from 'fs';
import { dirname } from 'path';
import builtins from 'builtin-modules';
import resolve from 'resolve';
var absolutePath = /^(?:\/|(?:[A-Za-z]:)?[\\|\/])/;
function dirExists ( dir ) {
try {
readdirSync( dir );
return true;
} catch ( err ) {
return false;
}
}
export default function npm ( options ) {
options = options || {};
var skip = options.skip || [];
const skip = options.skip || [];
const useMain = options.main !== false;
return {
resolveId: function ( importee, importer ) {
// disregard relative paths, absolute paths, and entry modules
if ( importee[0] === '.' || absolutePath.test( importee ) || !importer ) return null;
resolveId( importee, importer ) {
const parts = importee.split( /[\/\\]/ );
const id = parts.shift();
// lop off trailing slash to handle bizarro cases like
// https://github.com/nodejs/readable-stream/blob/077681f08e04094f087f11431dc64ca147dda20f/lib/_stream_readable.js#L125
if ( importee.slice( -1 ) === '/' ) importee = importee.slice( 0, -1 );
if ( ~skip.indexOf(id) ) return null;
var parts = importee.split( /[\/\\]/ );
var id = parts.shift();
// disregard entry modules and builtins
if ( !importer || ~builtins.indexOf( importee ) ) return null;
// npm scoped packages – @user/package
if ( id[0] === '@' && parts[0] ) {
id += '/' + parts.shift();
}
// exclude skipped modules
if ( ~skip.indexOf( id ) ) return;
var root = absolutePath.exec( importer )[0];
var dir = dirname( importer );
var modulePath;
while ( dir !== root && dir !== '.' ) {
modulePath = resolve( dir, 'node_modules', id );
if ( dirExists( modulePath ) ) {
// `foo/src/bar`
if ( parts.length ) {
return resolve( modulePath, ...parts ).replace( /\.js$/, '' ) + '.js';
}
if ( !options.jsnext && !options.main ) {
throw new Error( `To import from a package in node_modules (${id}), either options.jsnext or options.main must be true` );
}
// `foo`
const pkgPath = resolve( modulePath, 'package.json' );
let pkg;
try {
pkg = JSON.parse( readFileSync( pkgPath, 'utf-8' ) );
} catch ( err ) {
throw new Error( `Missing or malformed package.json: ${modulePath}` );
}
if ( options.jsnext ) {
const main = pkg[ 'jsnext:main' ];
if ( main ) return resolve( dirname( pkgPath ), main ).replace( /\.js$/, '' ) + '.js';
if ( !options.main ) {
throw new Error( `Package ${id} (imported by ${importer}) does not have a jsnext:main field. You should either allow legacy modules with options.main, or skip it with options.skip = ['${id}'])` );
return new Promise( ( accept, reject ) => {
resolve(
importee,
{
basedir: dirname( importer ),
packageFilter( pkg ) {
const id = pkg[ 'name' ];
if ( options.jsnext ) {
const main = pkg[ 'jsnext:main' ];
if ( main ) {
pkg[ 'main' ] = main;
} else if ( !useMain ) {
throw new Error( `Package ${id} (imported by ${importer}) does not have a jsnext:main field. You should either allow legacy modules with options.main, or skip it with options.skip = ['${id}'])` );
}
} else if ( !useMain ) {
throw new Error( `To import from a package in node_modules (${id}), either options.jsnext or options.main must be true` );
}
return pkg;
}
}
if ( options.main ) {
const main = pkg[ 'main' ] || 'index.js';
if ( main ) return resolve( dirname( pkgPath ), main ).replace( /\.js$/, '' ) + '.js';
}
if ( ~builtins.indexOf( id ) ) return false;
throw new Error( `Could not import module ${id} (imported by ${importer})` );
}
dir = dirname( dir );
}
return null;
},
( err, resolved ) => err ? reject( err ) : accept( resolved )
);
});
}
};
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc