Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

Commit

Permalink
Merge pull request #53 from rezzza/jail
Browse files Browse the repository at this point in the history
Add a jail feature
  • Loading branch information
Rich-Harris authored Mar 30, 2017
2 parents eff6465 + 2a37695 commit 48127ab
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ rollup({

// if there's something your bundle requires that you DON'T
// want to include, add it to 'skip'. Local and relative imports
// can be skipped by giving the full filepath. E.g.,
// can be skipped by giving the full filepath. E.g.,
// `path.resolve('src/relative-dependency.js')`
skip: [ 'some-big-dependency' ], // Default: []

Expand All @@ -50,7 +50,11 @@ rollup({
// whether to prefer built-in modules (e.g. `fs`, `path`) or
// local ones with the same names
preferBuiltins: false // Default: true


// Lock the module search in this path (like a chroot). Module defined
// outside this path will be mark has external
jail: '/my/jail/path' // Default: '/'

})
]
}).then( bundle => bundle.write({ dest: 'bundle.js', format: 'iife' }) );
Expand All @@ -64,10 +68,10 @@ rollup({
nodeResolve({ jsnext: true, main: true }),
commonjs()
]
}).then(bundle => bundle.write({
dest: 'bundle.js',
}).then(bundle => bundle.write({
dest: 'bundle.js',
moduleName: 'MyModule',
format: 'iife'
format: 'iife'
})).catch(err => console.log(err.stack));
```

Expand Down
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { dirname, resolve, normalize } from 'path';
import { dirname, resolve, normalize, sep } from 'path';
import builtins from 'builtin-modules';
import _nodeResolve from 'resolve';
import browserResolve from 'browser-resolve';
Expand All @@ -14,6 +14,7 @@ export default function nodeResolve ( options = {} ) {
const useMain = options.main !== false;
const isPreferBuiltinsSet = options.preferBuiltins === true || options.preferBuiltins === false;
const preferBuiltins = isPreferBuiltinsSet ? options.preferBuiltins : true;
const jail = options.jail || '/';

const onwarn = options.onwarn || CONSOLE_WARN;
const resolveId = options.browser ? browserResolve : _nodeResolve;
Expand Down Expand Up @@ -79,6 +80,8 @@ export default function nodeResolve ( options = {} ) {
);
}
accept( null );
} else if (resolved.indexOf(normalize(jail.trim(sep))) !== 0) {
accept( null );
} else {
accept( resolved );
}
Expand Down
3 changes: 3 additions & 0 deletions test/samples/jail/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import uppercase from 'string/uppercase.js';

export default uppercase( 'foo' );
22 changes: 22 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,4 +415,26 @@ describe( 'rollup-plugin-node-resolve', function () {
assert.equal( err.message, 'Could not resolve \'foo\' from ' + path.resolve( __dirname, entry ) );
});
});

it( 'mark as external to module outside the jail', () => {
return rollup.rollup({
entry: 'samples/jail/main.js',
plugins: [ nodeResolve({
jail: `${__dirname}/samples/`
}) ]
}).then( (bundle) => {
assert.deepEqual(bundle.imports, [ 'string/uppercase.js' ]);
});
});

it( 'bundle module defined inside the jail', () => {
return rollup.rollup({
entry: 'samples/jail/main.js',
plugins: [ nodeResolve({
jail: `${__dirname}/`
}) ]
}).then( (bundle) => {
assert.deepEqual(bundle.imports, []);
});
});
});

0 comments on commit 48127ab

Please sign in to comment.