rollup-plugin-npm
Advanced tools
Comparing version 1.0.0 to 1.1.0
# rollup-plugin-npm changelog | ||
## 1.1.0 | ||
* Use node-resolve to handle various corner cases | ||
## 1.0.0 | ||
@@ -4,0 +8,0 @@ |
'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 @@ |
112
src/index.js
@@ -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 ) | ||
); | ||
}); | ||
} | ||
}; | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
1
6768
2
119
59
+ Addedresolve@^1.1.6
+ Addedfunction-bind@1.1.2(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedis-core-module@2.16.1(transitive)
+ Addedpath-parse@1.0.7(transitive)
+ Addedresolve@1.22.10(transitive)
+ Addedsupports-preserve-symlinks-flag@1.0.0(transitive)