diff --git a/lib/loader.js b/lib/loader.js index cb90888b..86ea39e8 100644 --- a/lib/loader.js +++ b/lib/loader.js @@ -40,8 +40,8 @@ function sassLoader(content) { // Supported since v4.27.0 if (this.getResolve) { resolve = this.getResolve({ - mainFields: ['sass', 'main'], - extensions: ['.scss', '.sass', '.css'], + mainFields: ['sass', 'style', '...'], + extensions: ['.scss', '.sass', '.css', '...'], }); } diff --git a/test/index.test.js b/test/index.test.js index 2d3ea956..0ba8a273 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -61,7 +61,7 @@ implementations.forEach((implementation) => { .replace(CR, ''); } - function runWebpack(baseConfig, loaderOptions, done) { + function runWebpack(baseConfig, loaderOptions, resolveOptions = {}, done) { const webpackConfig = merge( { mode: 'development', @@ -74,6 +74,7 @@ implementations.forEach((implementation) => { rules: [ { test: /\.s[ac]ss$/, + resolve: resolveOptions, use: [ { loader: 'raw-loader' }, { @@ -100,7 +101,7 @@ implementations.forEach((implementation) => { describe(implementationName, () => { syntaxStyles.forEach((ext) => { - function execTest(testId, loaderOptions, webpackOptions) { + function execTest(testId, loaderOptions, webpackOptions, resolveOptions) { const bundleName = `bundle.${ext}.${implementationName}.js`; return new Promise((resolve, reject) => { @@ -114,7 +115,7 @@ implementations.forEach((implementation) => { webpackOptions ); - runWebpack(baseConfig, loaderOptions, (err) => + runWebpack(baseConfig, loaderOptions, resolveOptions, (err) => err ? reject(err) : resolve() ); }).then(() => { @@ -173,6 +174,17 @@ implementations.forEach((implementation) => { )); it('should resolve sass field correctly', () => execTest(`import-sass-field`)); + it('should resolve style field correctly', () => + execTest(`import-style-field`)); + it('should resolve main field correctly', () => + execTest(`import-main-field`)); + it('should resolve custom-sass field correctly', () => + execTest( + `import-custom-sass-field`, + {}, + {}, + { mainFields: ['custom-sass', '...'] } + )); // Works only in dart-sass implementation if (implementation === dartSass) { it('should resolve index file in module correctly', () => @@ -280,6 +292,7 @@ implementations.forEach((implementation) => { }, }, {}, + {}, (err) => (err ? reject(err) : resolve()) ); }).then(() => { @@ -392,6 +405,7 @@ implementations.forEach((implementation) => { entry: pathToErrorFile, }, {}, + {}, (err) => { if (implementation === nodeSass) { err.message.should.match( @@ -413,6 +427,7 @@ implementations.forEach((implementation) => { entry: pathToErrorImport, }, {}, + {}, (err) => { // check for file excerpt if (implementation === nodeSass) { @@ -435,6 +450,7 @@ implementations.forEach((implementation) => { entry: pathToErrorFileNotFound, }, {}, + {}, (err) => { err.message.should.match(/@import "does-not-exist";/); if (implementation === nodeSass) { @@ -457,6 +473,7 @@ implementations.forEach((implementation) => { entry: pathToErrorFileNotFound2, }, {}, + {}, (err) => { err.message.should.match(/@import "\.\/another\/_module\.scss";/); if (implementation === nodeSass) { @@ -500,6 +517,7 @@ implementations.forEach((implementation) => { entry: `${pathToSassLoader}!${pathToErrorFile}`, }, { implementation: null }, + {}, (err) => { // eslint-disable-next-line no-underscore-dangle module._resolveFilename = originalResolve; @@ -518,6 +536,7 @@ implementations.forEach((implementation) => { { implementation: merge(nodeSass, { info: 'asdfj' }), }, + {}, (err) => { err.message.should.match(/Unknown Sass implementation "asdfj"\./); done(); @@ -533,6 +552,7 @@ implementations.forEach((implementation) => { { implementation: merge(nodeSass, { info: 'node-sass\t1' }), }, + {}, (err) => { err.message.should.match(/Invalid Sass version "1"\./); done(); @@ -548,6 +568,7 @@ implementations.forEach((implementation) => { { implementation: merge(nodeSass, { info: 'node-sass\t3.0.0' }), }, + {}, (err) => { err.message.should.match( /Node Sass version 3\.0\.0 is incompatible with \^4\.0\.0\./ @@ -565,6 +586,7 @@ implementations.forEach((implementation) => { { implementation: merge(nodeSass, { info: 'dart-sass\t1.2.0' }), }, + {}, (err) => { err.message.should.match( /Dart Sass version 1\.2\.0 is incompatible with \^1\.3\.0\./ @@ -582,6 +604,7 @@ implementations.forEach((implementation) => { { implementation: merge(nodeSass, { info: 'strange-sass\t1.0.0' }), }, + {}, (err) => { err.message.should.match( /Unknown Sass implementation "strange-sass"\./ @@ -629,6 +652,7 @@ implementations.forEach((implementation) => { entry: pathToFile, }, { implementation: null }, + {}, (err) => { // eslint-disable-next-line no-underscore-dangle module._resolveFilename = originalResolve; diff --git a/test/node_modules/sass-custom-sass-field/nested/style.sass b/test/node_modules/sass-custom-sass-field/nested/style.sass new file mode 100644 index 00000000..d3e4216c --- /dev/null +++ b/test/node_modules/sass-custom-sass-field/nested/style.sass @@ -0,0 +1,2 @@ +a + display: block diff --git a/test/node_modules/sass-custom-sass-field/package.json b/test/node_modules/sass-custom-sass-field/package.json new file mode 100644 index 00000000..da84791f --- /dev/null +++ b/test/node_modules/sass-custom-sass-field/package.json @@ -0,0 +1,12 @@ +{ + "name": "sass-sass-field", + "version": "1.0.0", + "description": "test", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "MIT", + "custom-sass": "nested/style.sass" +} diff --git a/test/node_modules/sass-main-field/nested/style.sass b/test/node_modules/sass-main-field/nested/style.sass new file mode 100644 index 00000000..d3e4216c --- /dev/null +++ b/test/node_modules/sass-main-field/nested/style.sass @@ -0,0 +1,2 @@ +a + display: block diff --git a/test/node_modules/sass-main-field/package.json b/test/node_modules/sass-main-field/package.json new file mode 100644 index 00000000..04485814 --- /dev/null +++ b/test/node_modules/sass-main-field/package.json @@ -0,0 +1,11 @@ +{ + "name": "sass-main-field", + "version": "1.0.0", + "description": "test", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "MIT", + "main": "nested/style.sass" +} diff --git a/test/node_modules/sass-style-field/nested/style.sass b/test/node_modules/sass-style-field/nested/style.sass new file mode 100644 index 00000000..d3e4216c --- /dev/null +++ b/test/node_modules/sass-style-field/nested/style.sass @@ -0,0 +1,2 @@ +a + display: block diff --git a/test/node_modules/sass-style-field/package.json b/test/node_modules/sass-style-field/package.json new file mode 100644 index 00000000..b4a01519 --- /dev/null +++ b/test/node_modules/sass-style-field/package.json @@ -0,0 +1,12 @@ +{ + "name": "sass-sass-field", + "version": "1.0.0", + "description": "test", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "MIT", + "style": "nested/style.sass" +} diff --git a/test/node_modules/scss-custom-sass-field/nested/style.scss b/test/node_modules/scss-custom-sass-field/nested/style.scss new file mode 100644 index 00000000..d1308905 --- /dev/null +++ b/test/node_modules/scss-custom-sass-field/nested/style.scss @@ -0,0 +1,3 @@ +a { + display: block; +} diff --git a/test/node_modules/scss-custom-sass-field/package.json b/test/node_modules/scss-custom-sass-field/package.json new file mode 100644 index 00000000..75b2c165 --- /dev/null +++ b/test/node_modules/scss-custom-sass-field/package.json @@ -0,0 +1,12 @@ +{ + "name": "scss-sass-field", + "version": "1.0.0", + "description": "test", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "MIT", + "custom-sass": "nested/style.scss" +} diff --git a/test/node_modules/scss-main-field/nested/style.scss b/test/node_modules/scss-main-field/nested/style.scss new file mode 100644 index 00000000..d1308905 --- /dev/null +++ b/test/node_modules/scss-main-field/nested/style.scss @@ -0,0 +1,3 @@ +a { + display: block; +} diff --git a/test/node_modules/scss-main-field/package.json b/test/node_modules/scss-main-field/package.json new file mode 100644 index 00000000..2e003ba9 --- /dev/null +++ b/test/node_modules/scss-main-field/package.json @@ -0,0 +1,11 @@ +{ + "name": "scss-main-field", + "version": "1.0.0", + "description": "test", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "MIT", + "main": "nested/style.scss" +} diff --git a/test/node_modules/scss-style-field/nested/style.scss b/test/node_modules/scss-style-field/nested/style.scss new file mode 100644 index 00000000..d1308905 --- /dev/null +++ b/test/node_modules/scss-style-field/nested/style.scss @@ -0,0 +1,3 @@ +a { + display: block; +} diff --git a/test/node_modules/scss-style-field/package.json b/test/node_modules/scss-style-field/package.json new file mode 100644 index 00000000..85e29563 --- /dev/null +++ b/test/node_modules/scss-style-field/package.json @@ -0,0 +1,12 @@ +{ + "name": "scss-sass-field", + "version": "1.0.0", + "description": "test", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "MIT", + "style": "nested/style.scss" +} diff --git a/test/sass/import-custom-sass-field.sass b/test/sass/import-custom-sass-field.sass new file mode 100644 index 00000000..217aba54 --- /dev/null +++ b/test/sass/import-custom-sass-field.sass @@ -0,0 +1 @@ +@import "~sass-custom-sass-field" diff --git a/test/sass/import-main-field.sass b/test/sass/import-main-field.sass new file mode 100644 index 00000000..b4cb7178 --- /dev/null +++ b/test/sass/import-main-field.sass @@ -0,0 +1 @@ +@import "~sass-main-field" diff --git a/test/sass/import-style-field.sass b/test/sass/import-style-field.sass new file mode 100644 index 00000000..dfdfeb0a --- /dev/null +++ b/test/sass/import-style-field.sass @@ -0,0 +1 @@ +@import "~sass-style-field" diff --git a/test/scss/import-custom-sass-field.scss b/test/scss/import-custom-sass-field.scss new file mode 100644 index 00000000..864e480a --- /dev/null +++ b/test/scss/import-custom-sass-field.scss @@ -0,0 +1 @@ +@import "~scss-custom-sass-field"; diff --git a/test/scss/import-main-field.scss b/test/scss/import-main-field.scss new file mode 100644 index 00000000..677ee792 --- /dev/null +++ b/test/scss/import-main-field.scss @@ -0,0 +1 @@ +@import "~scss-main-field"; diff --git a/test/scss/import-style-field.scss b/test/scss/import-style-field.scss new file mode 100644 index 00000000..69231895 --- /dev/null +++ b/test/scss/import-style-field.scss @@ -0,0 +1 @@ +@import "~scss-style-field"; diff --git a/test/tools/createSpec.js b/test/tools/createSpec.js index b23fd8ef..35c1e526 100644 --- a/test/tools/createSpec.js +++ b/test/tools/createSpec.js @@ -48,6 +48,36 @@ function createSpec(ext) { 'style.scss' ) ); + const pathToScssStyleField = path.relative( + basePath, + path.resolve( + testFolder, + 'node_modules', + 'scss-style-field', + 'nested', + 'style.scss' + ) + ); + const pathToScssCustomSassField = path.relative( + basePath, + path.resolve( + testFolder, + 'node_modules', + 'scss-custom-sass-field', + 'nested', + 'style.scss' + ) + ); + const pathToScssMainField = path.relative( + basePath, + path.resolve( + testFolder, + 'node_modules', + 'scss-main-field', + 'nested', + 'style.scss' + ) + ); const pathToSASSSassField = path.relative( basePath, path.resolve( @@ -58,6 +88,36 @@ function createSpec(ext) { 'style.sass' ) ); + const pathToSASSStyleField = path.relative( + basePath, + path.resolve( + testFolder, + 'node_modules', + 'sass-style-field', + 'nested', + 'style.sass' + ) + ); + const pathToSASSCustomSassField = path.relative( + basePath, + path.resolve( + testFolder, + 'node_modules', + 'sass-custom-sass-field', + 'nested', + 'style.sass' + ) + ); + const pathToSASSMainField = path.relative( + basePath, + path.resolve( + testFolder, + 'node_modules', + 'sass-main-field', + 'nested', + 'style.sass' + ) + ); fs.readdirSync(path.join(testFolder, ext)) .filter( @@ -78,7 +138,13 @@ function createSpec(ext) { // eslint-disable-next-line no-param-reassign url = url .replace(/^~scss-sass-field/, pathToScssSassField) + .replace(/^~scss-style-field/, pathToScssStyleField) + .replace(/^~scss-custom-sass-field/, pathToScssCustomSassField) + .replace(/^~scss-main-field/, pathToScssMainField) .replace(/^~sass-sass-field/, pathToSASSSassField) + .replace(/^~sass-style-field/, pathToSASSStyleField) + .replace(/^~sass-custom-sass-field/, pathToSASSCustomSassField) + .replace(/^~sass-main-field/, pathToSASSMainField) .replace(/^~bootstrap-sass/, pathToBootstrap) .replace(/^~@org\/pkg/, pathToScopedNpmPkg) .replace(/^~module/, pathToModule)