From ac0432ed0b541346119f2fc463a709ff897a3b71 Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Fri, 5 Feb 2021 15:12:14 +0300 Subject: [PATCH] feat: support the `resolve.byDependecy.sass` option --- package-lock.json | 6 +-- package.json | 2 +- src/utils.js | 1 + test/__snapshots__/loader.test.js.snap | 54 +++++++++++++++++++++++++ test/helpers/getCodeFromSass.js | 4 ++ test/loader.test.js | 25 ++++++++++++ test/sass/by-dependency.sass | 4 ++ test/sass/custom-main-files/custom.sass | 2 + test/scss/by-dependency.scss | 5 +++ test/scss/custom-main-files/custom.scss | 3 ++ 10 files changed, 102 insertions(+), 4 deletions(-) create mode 100644 test/sass/by-dependency.sass create mode 100644 test/sass/custom-main-files/custom.sass create mode 100644 test/scss/by-dependency.scss create mode 100644 test/scss/custom-main-files/custom.scss diff --git a/package-lock.json b/package-lock.json index a3e9a085..571bd550 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14008,9 +14008,9 @@ "dev": true }, "webpack": { - "version": "5.20.0", - "resolved": "https://registry.npmjs.org/webpack/-/webpack-5.20.0.tgz", - "integrity": "sha512-k7X/+gisrvirr4bzv8JVLzy2RhWiVzd3gA9qzS5je3lwqYrMc5F549vLOk34BoNkkUp4SIpt1rQp48pTxBMXvA==", + "version": "5.20.2", + "resolved": "https://registry.npmjs.org/webpack/-/webpack-5.20.2.tgz", + "integrity": "sha512-gGPip54KK7DznaaPHVuNGqym3LAXXL+bPkZ9SlLTCdHmmk+m5x+D4UZdhWvw32CMahYlZwZYPsioFIw36/txYQ==", "dev": true, "requires": { "@types/eslint-scope": "^3.7.0", diff --git a/package.json b/package.json index f43789b7..57f46549 100644 --- a/package.json +++ b/package.json @@ -93,7 +93,7 @@ "semver": "^7.3.4", "standard-version": "^9.1.0", "style-loader": "^2.0.0", - "webpack": "^5.20.0" + "webpack": "^5.20.2" }, "keywords": [ "sass", diff --git a/src/utils.js b/src/utils.js index c2e21d0e..50ed3a6b 100644 --- a/src/utils.js +++ b/src/utils.js @@ -369,6 +369,7 @@ function getWebpackResolver( ); const webpackResolve = promiseResolve( resolverFactory({ + dependencyType: "sass", conditionNames: ["sass", "style"], mainFields: ["sass", "style", "main", "..."], mainFiles: ["_index", "index", "..."], diff --git a/test/__snapshots__/loader.test.js.snap b/test/__snapshots__/loader.test.js.snap index 07fd3b0b..5fb1b5e5 100644 --- a/test/__snapshots__/loader.test.js.snap +++ b/test/__snapshots__/loader.test.js.snap @@ -167371,3 +167371,57 @@ exports[`loader should work with the "material-components-web" package without t exports[`loader should work with the "material-components-web" package without the "includePaths" option (dart-sass) (scss): warnings 1`] = `Array []`; exports[`loader should work with the "material-components-web" package without the "includePaths" option (dart-sass) (scss): warnings 2`] = `Array []`; + +exports[`loader should work with the 'resolve.byDependecy.sass' option (dart-sass) (sass): css 1`] = ` +".a { + color: red; +} + +.b { + color: red; +}" +`; + +exports[`loader should work with the 'resolve.byDependecy.sass' option (dart-sass) (sass): errors 1`] = `Array []`; + +exports[`loader should work with the 'resolve.byDependecy.sass' option (dart-sass) (sass): warnings 1`] = `Array []`; + +exports[`loader should work with the 'resolve.byDependecy.sass' option (dart-sass) (scss): css 1`] = ` +".a { + color: red; +} + +.b { + color: red; +}" +`; + +exports[`loader should work with the 'resolve.byDependecy.sass' option (dart-sass) (scss): errors 1`] = `Array []`; + +exports[`loader should work with the 'resolve.byDependecy.sass' option (dart-sass) (scss): warnings 1`] = `Array []`; + +exports[`loader should work with the 'resolve.byDependecy.sass' option (node-sass) (sass): css 1`] = ` +".a { + color: red; } + +.b { + color: red; } +" +`; + +exports[`loader should work with the 'resolve.byDependecy.sass' option (node-sass) (sass): errors 1`] = `Array []`; + +exports[`loader should work with the 'resolve.byDependecy.sass' option (node-sass) (sass): warnings 1`] = `Array []`; + +exports[`loader should work with the 'resolve.byDependecy.sass' option (node-sass) (scss): css 1`] = ` +".a { + color: red; } + +.b { + color: red; } +" +`; + +exports[`loader should work with the 'resolve.byDependecy.sass' option (node-sass) (scss): errors 1`] = `Array []`; + +exports[`loader should work with the 'resolve.byDependecy.sass' option (node-sass) (scss): warnings 1`] = `Array []`; diff --git a/test/helpers/getCodeFromSass.js b/test/helpers/getCodeFromSass.js index bb11b72c..53f721d7 100644 --- a/test/helpers/getCodeFromSass.js +++ b/test/helpers/getCodeFromSass.js @@ -281,6 +281,9 @@ function getCodeFromSass(testId, options) { __dirname, "../../node_modules/@material" ); + const pathToCustomMainFiles = isSass + ? path.resolve(testFolder, "sass/custom-main-files/custom.sass") + : path.resolve(testFolder, "scss/custom-main-files/custom.scss"); // Pseudo importer for tests function testImporter(url) { @@ -754,6 +757,7 @@ function getCodeFromSass(testId, options) { pathToPackageWithSameImport ) .replace(/@material/, pathToMaterial) + .replace(/custom-main-files/, pathToCustomMainFiles) .replace(/^~/, testNodeModules); } diff --git a/test/loader.test.js b/test/loader.test.js index 8a5c7fa9..cb10b2c8 100644 --- a/test/loader.test.js +++ b/test/loader.test.js @@ -972,6 +972,31 @@ describe("loader", () => { expect(getErrors(stats)).toMatchSnapshot("errors"); }); + it(`should work with the 'resolve.byDependecy.sass' option (${implementationName}) (${syntax})`, async () => { + const testId = getTestId("by-dependency", syntax); + const options = { + implementation: getImplementationByName(implementationName), + }; + const compiler = getCompiler(testId, { + loader: { options }, + resolve: { + byDependency: { + sass: { + mainFiles: ["custom"], + }, + }, + }, + }); + const stats = await compile(compiler); + const codeFromBundle = getCodeFromBundle(stats, compiler); + const codeFromSass = getCodeFromSass(testId, options); + + expect(codeFromBundle.css).toBe(codeFromSass.css); + expect(codeFromBundle.css).toMatchSnapshot("css"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); + }); + if (implementation === dartSass) { it(`should output an understandable error with a problem in "@use" (${implementationName}) (${syntax})`, async () => { const testId = getTestId("error-use", syntax); diff --git a/test/sass/by-dependency.sass b/test/sass/by-dependency.sass new file mode 100644 index 00000000..82226c70 --- /dev/null +++ b/test/sass/by-dependency.sass @@ -0,0 +1,4 @@ +@import "custom-main-files" + +.b + color: red diff --git a/test/sass/custom-main-files/custom.sass b/test/sass/custom-main-files/custom.sass new file mode 100644 index 00000000..9fb803fd --- /dev/null +++ b/test/sass/custom-main-files/custom.sass @@ -0,0 +1,2 @@ +.a + color: red diff --git a/test/scss/by-dependency.scss b/test/scss/by-dependency.scss new file mode 100644 index 00000000..3c4ee844 --- /dev/null +++ b/test/scss/by-dependency.scss @@ -0,0 +1,5 @@ +@import "custom-main-files"; + +.b { + color: red; +} diff --git a/test/scss/custom-main-files/custom.scss b/test/scss/custom-main-files/custom.scss new file mode 100644 index 00000000..e88d5156 --- /dev/null +++ b/test/scss/custom-main-files/custom.scss @@ -0,0 +1,3 @@ +.a { + color: red +}