diff --git a/src/helpers.js b/src/helpers.js index 5db1765..3013587 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -43,6 +43,27 @@ function normalize (path) { return resolvedParts.join('/'); } +function join () { + if (arguments.length === 0) + return '.'; + + let joined; + for (let i = 0; i < arguments.length; ++i) { + let arg = arguments[i]; + if (arg.length > 0) { + if (joined === undefined) + joined = arg; + else + joined += '/' + arg; + } + } + + if (joined === undefined) + return '.'; + + return joined; +} + export function commonjsRequire (path, originalModuleDir) { const isRelative = path[0] === '.' || path[0] === '/'; path = normalize(path); @@ -53,8 +74,7 @@ export function commonjsRequire (path, originalModuleDir) { } else if (originalModuleDir) { relPath = normalize(originalModuleDir + '/node_modules/' + path); } else { - throw new Error('Missing test case'); - // relPath = pathModule.normalize(pathModule.join('node_modules', path)); + relPath = normalize(join('node_modules', path)); } for (let extensionIndex = 0; extensionIndex < CHECKED_EXTENSIONS.length; extensionIndex++) { const resolvedPath = relPath + CHECKED_EXTENSIONS[extensionIndex]; diff --git a/test/function/dynamic-require-package-sub/_config.js b/test/function/dynamic-require-package-sub/_config.js new file mode 100644 index 0000000..790feee --- /dev/null +++ b/test/function/dynamic-require-package-sub/_config.js @@ -0,0 +1,14 @@ +const nodeResolve = require('rollup-plugin-node-resolve'); + +module.exports = { + input: 'sub/entry.js', + description: 'resolves imports of node_modules from subdirectories', + options: { + plugins: [nodeResolve()] + }, + pluginOptions: { + dynamicRequireTargets: [ + 'function/dynamic-require-package-sub/node_modules/custom-module/**' + ] + } +}; diff --git a/test/function/dynamic-require-package-sub/node_modules/custom-module/entry.js b/test/function/dynamic-require-package-sub/node_modules/custom-module/entry.js new file mode 100644 index 0000000..f67b073 --- /dev/null +++ b/test/function/dynamic-require-package-sub/node_modules/custom-module/entry.js @@ -0,0 +1 @@ +module.exports = 'custom-module'; diff --git a/test/function/dynamic-require-package-sub/node_modules/custom-module/package.json b/test/function/dynamic-require-package-sub/node_modules/custom-module/package.json new file mode 100644 index 0000000..382adf7 --- /dev/null +++ b/test/function/dynamic-require-package-sub/node_modules/custom-module/package.json @@ -0,0 +1 @@ +{"main": "./entry.js"} diff --git a/test/function/dynamic-require-package-sub/package.json b/test/function/dynamic-require-package-sub/package.json new file mode 100644 index 0000000..e5efa20 --- /dev/null +++ b/test/function/dynamic-require-package-sub/package.json @@ -0,0 +1 @@ +{"main": "./sub/entry.js"} diff --git a/test/function/dynamic-require-package-sub/sub/entry.js b/test/function/dynamic-require-package-sub/sub/entry.js new file mode 100644 index 0000000..e071586 --- /dev/null +++ b/test/function/dynamic-require-package-sub/sub/entry.js @@ -0,0 +1,2 @@ + +assert.equal(require('custom-module'), 'custom-module'); diff --git a/test/test.js b/test/test.js index 6a50707..a724677 100644 --- a/test/test.js +++ b/test/test.js @@ -120,7 +120,7 @@ describe('rollup-plugin-commonjs', () => { (config.solo ? it.only : it)(dir, async () => { const options = Object.assign( { - input: `function/${dir}/main.js` + input: `function/${dir}/${config.input || 'main.js'}` }, config.options || {}, {