From 58b6298c26758019f0cfc17599928de7b1ef9fbb Mon Sep 17 00:00:00 2001 From: Alexander Krasnoyarov Date: Mon, 28 Sep 2020 23:03:46 +0300 Subject: [PATCH] test: code --- src/utils.js | 31 +++++++++++--------------- test/__snapshots__/loader.test.js.snap | 14 ++++++++++++ test/loader.test.js | 2 +- 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/utils.js b/src/utils.js index dc54373..ae938fb 100644 --- a/src/utils.js +++ b/src/utils.js @@ -238,11 +238,11 @@ async function getDependencies( const isArray = Array.isArray(resolved); - // `stylus` can return files with glob characters, we should escape them to avid re globbing + // `stylus` returns forward slashes on windows // eslint-disable-next-line no-param-reassign result.resolved = isArray - ? resolved.map((item) => fastGlob.escapePath(path.normalize(item))) - : fastGlob.escapePath(path.normalize(resolved)); + ? resolved.map((item) => path.normalize(item)) + : path.normalize(resolved); const dependenciesOfDependencies = []; @@ -373,37 +373,32 @@ async function createEvaluator(loaderContext, code, options) { const { resolved } = dependency; if (!Array.isArray(resolved)) { - node.string = resolved; + // Avoid re globbing when resolved import contains glob characters + node.string = fastGlob.escapePath(resolved); } else if (resolved.length > 0) { + let hasError = false; + const blocks = resolved.map((item) => { const clonedImported = imported.clone(); const clonedNode = this.visit(clonedImported.path).first; - clonedNode.string = item; + // Avoid re globbing when resolved import contains glob characters + clonedNode.string = fastGlob.escapePath(item); let result; try { result = super.visitImport(clonedImported); } catch (error) { - loaderContext.emitError( - new Error( - `Stylus resolver error: ${error.message}${ - webpackResolveError - ? `\n\nWebpack resolver error details:\n${webpackResolveError.details}\n\n` + - `Webpack resolver error missing:\n${webpackResolveError.missing}\n\n` - : '' - }` - ) - ); - - return imported; + hasError = true; } return result; }); - return mergeBlocks(blocks); + if (!hasError) { + return mergeBlocks(blocks); + } } } } diff --git a/test/__snapshots__/loader.test.js.snap b/test/__snapshots__/loader.test.js.snap index 8f6a63b..e8bf28b 100644 --- a/test/__snapshots__/loader.test.js.snap +++ b/test/__snapshots__/loader.test.js.snap @@ -348,6 +348,20 @@ exports[`loader imports files listed in option as glob: errors 1`] = `Array []`; exports[`loader imports files listed in option as glob: warnings 1`] = `Array []`; +exports[`loader imports files with special characters listed in glob: css 1`] = ` +".directoryfile { + color: #f00; +} +.nestedfile { + color: #f00; +} +" +`; + +exports[`loader imports files with special characters listed in glob: errors 1`] = `Array []`; + +exports[`loader imports files with special characters listed in glob: warnings 1`] = `Array []`; + exports[`loader imports the right file based on context: css 1`] = ` ".a-color { color: #aaa; diff --git a/test/loader.test.js b/test/loader.test.js index 40931b7..7530e8e 100644 --- a/test/loader.test.js +++ b/test/loader.test.js @@ -844,7 +844,7 @@ describe('loader', () => { expect(getErrors(stats)).toMatchSnapshot('errors'); }); - it.skip('imports files with special characters listed in glob', async () => { + it('imports files with special characters listed in glob', async () => { const testId = './import-glob-special.styl'; const compiler = getCompiler(testId); const stats = await compile(compiler);