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

Commit e0bfb53

Browse files
committed
Allow for relative imports to be skipped, even if file doesn't exist.
See rollup/rollup#532 for the explanation of the problem and the use case for skipping relative imports for files that don't exist. This pull request, in conjunction with rollup/rollup#572, resolves the issue. The revving of the rollup version in the package.json is because the new test requires rollup@^0.25.7, and npm's caret semver behavior [does not install 0.25.x when a dependency is listed at ^0.22.0](https://docs.npmjs.com/misc/semver#caret-ranges-123-025-004)
1 parent 11fd59c commit e0bfb53

File tree

5 files changed

+26
-3
lines changed

5 files changed

+26
-3
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ rollup({
3030
main: true, // Default: true
3131

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

3638
// some package.json files have a `browser` field which

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"es5-ext": "^0.10.11",
1010
"eslint": "^1.7.3",
1111
"mocha": "^2.3.3",
12-
"rollup": "^0.22.0",
12+
"rollup": "^0.25.7",
1313
"rollup-plugin-babel": "^2.2.0",
1414
"rollup-plugin-commonjs": "^2.0.0",
1515
"string-capitalize": "^1.0.1",

src/index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@ export default function nodeResolve ( options ) {
2323
let parts = importee.split( /[\/\\]/ );
2424
let id = parts.shift();
2525

26-
// scoped packages
2726
if ( id[0] === '@' && parts.length ) {
27+
// scoped packages
2828
id += `/${parts.shift()}`;
29+
} else if ( id[0] === '.' ) {
30+
// an import relative to the parent dir of the importer
31+
id = resolve( importer, '..', importee );
2932
}
3033

3134
if ( skip !== true && ~skip.indexOf( id ) ) return null;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './nonexistent-relative-dependency.js';

test/test.js

+17
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,23 @@ describe( 'rollup-plugin-node-resolve', function () {
226226
});
227227
});
228228

229+
it( 'skip: allows for a relative file to be skipped, even if the file doesn\'t exist', () => {
230+
const externalFile = path.resolve( __dirname, 'samples', 'skip-nonexistent-relative', './nonexistent-relative-dependency.js' );
231+
return rollup.rollup({
232+
entry: 'samples/skip-nonexistent-relative/main.js',
233+
external: [ externalFile ],
234+
plugins: [
235+
nodeResolve({
236+
jsnext: true,
237+
main: false,
238+
skip: [ externalFile ]
239+
})
240+
]
241+
}).then( bundle => {
242+
assert.deepEqual( bundle.imports.sort(), [ './nonexistent-relative-dependency.js' ]);
243+
});
244+
});
245+
229246
it( 'skip: true allows all unfound dependencies to be skipped without error', () => {
230247
return rollup.rollup({
231248
entry: 'samples/skip-true/main.js',

0 commit comments

Comments
 (0)