From c391d6b7d2a2a337daec77de438dc51c2bd6953d Mon Sep 17 00:00:00 2001 From: Jan Amann Date: Fri, 2 Jul 2021 02:40:49 +0200 Subject: [PATCH] Supported double quoted use imports from built-ins (#147) --- CHANGELOG.md | 3 +++ src/utils/rewritePaths.js | 2 +- test/__snapshots__/index.test.js.snap | 18 ++++++++++++++++++ test/index.test.js | 11 +++++++++++ test/scss/shared/_math-double-quotes.scss | 3 +++ 5 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 test/scss/shared/_math-double-quotes.scss diff --git a/CHANGELOG.md b/CHANGELOG.md index f52caf7..204fd4a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,7 @@ # Change Log +## [2.2.4] - Unreleased +* Support double quoted imports of built-ins. [#145](https://github.com/shakacode/sass-resources-loader/pull/147) by [Jan Amann](https://github.com/amannn). + ## [2.2.3] - 2021-06-21 * Fix regression introduced in 2.2.2 for "causes TypeError: Cannot read property 'indexOf' of undefined". [#145](https://github.com/shakacode/sass-resources-loader/pull/145) by [Juan Antonio Gómez](https://github.com/shokmaster). diff --git a/src/utils/rewritePaths.js b/src/utils/rewritePaths.js index 3a9f07a..93a4531 100644 --- a/src/utils/rewritePaths.js +++ b/src/utils/rewritePaths.js @@ -24,7 +24,7 @@ export default function rewritePaths(error, file, contents, moduleContext, callb const rewritten = contents.replace(useRegexp, (entire, importer, single, double, unquoted) => { // Don't rewrite imports from built-ins - if (single && single.indexOf('sass:') === 0) { + if ([single, double].some(match => match && match.indexOf('sass:') === 0)) { return entire; } diff --git a/test/__snapshots__/index.test.js.snap b/test/__snapshots__/index.test.js.snap index 74dcbcf..5e9b38c 100644 --- a/test/__snapshots__/index.test.js.snap +++ b/test/__snapshots__/index.test.js.snap @@ -103,6 +103,20 @@ exports[`sass-resources-loader imports should not rewrite the path for built-ins @use 'sass:math'; +$padding: #{math.div(4 / 2)}px; + +div { + padding: $padding; + margin: #{math.div(4 / 2)}px;; +} +" +`; + +exports[`sass-resources-loader imports should not rewrite the path for built-ins with @use and double quotes 1`] = ` +"// Should be de-duplicated +@use 'sass:math'; + + $padding: #{math.div(4 / 2)}px; div { @@ -140,6 +154,10 @@ exports[`sass-resources-loader resources should parse array resources 1`] = ` @forward \\"variables\\"; +@use \\"sass:math\\"; + +$padding: #{math.div(4 / 2)}px; + @use 'sass:math'; $padding: #{math.div(4 / 2)}px; diff --git a/test/index.test.js b/test/index.test.js index 3157219..d35f184 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -252,6 +252,17 @@ describe('sass-resources-loader', () => { const output = require('./output/use-builtin').default; expect(output).toMatchSnapshot(); })); + + it('should not rewrite the path for built-ins with @use and double quotes', () => execTest('use-builtin', { + hoistUseStatements: true, + resources: [ + path.resolve(__dirname, './scss/shared/_math-double-quotes.scss'), + ], + }).then(() => { + // eslint-disable-next-line global-require + const output = require('./output/use-builtin').default; + expect(output).toMatchSnapshot(); + })); }); describe('getOutput', () => { diff --git a/test/scss/shared/_math-double-quotes.scss b/test/scss/shared/_math-double-quotes.scss new file mode 100644 index 0000000..b042447 --- /dev/null +++ b/test/scss/shared/_math-double-quotes.scss @@ -0,0 +1,3 @@ +@use "sass:math"; + +$padding: #{math.div(4 / 2)}px;