Skip to content

Commit 28f1884

Browse files
feat: allow customize mainFields and extensions (#710)
1 parent 2a51502 commit 28f1884

File tree

21 files changed

+186
-5
lines changed

21 files changed

+186
-5
lines changed

lib/loader.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ function sassLoader(content) {
4040
// Supported since v4.27.0
4141
if (this.getResolve) {
4242
resolve = this.getResolve({
43-
mainFields: ['sass', 'main'],
44-
extensions: ['.scss', '.sass', '.css'],
43+
mainFields: ['sass', 'style', '...'],
44+
extensions: ['.scss', '.sass', '.css', '...'],
4545
});
4646
}
4747

test/index.test.js

+27-3
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ implementations.forEach((implementation) => {
6161
.replace(CR, '');
6262
}
6363

64-
function runWebpack(baseConfig, loaderOptions, done) {
64+
function runWebpack(baseConfig, loaderOptions, resolveOptions = {}, done) {
6565
const webpackConfig = merge(
6666
{
6767
mode: 'development',
@@ -74,6 +74,7 @@ implementations.forEach((implementation) => {
7474
rules: [
7575
{
7676
test: /\.s[ac]ss$/,
77+
resolve: resolveOptions,
7778
use: [
7879
{ loader: 'raw-loader' },
7980
{
@@ -100,7 +101,7 @@ implementations.forEach((implementation) => {
100101

101102
describe(implementationName, () => {
102103
syntaxStyles.forEach((ext) => {
103-
function execTest(testId, loaderOptions, webpackOptions) {
104+
function execTest(testId, loaderOptions, webpackOptions, resolveOptions) {
104105
const bundleName = `bundle.${ext}.${implementationName}.js`;
105106

106107
return new Promise((resolve, reject) => {
@@ -114,7 +115,7 @@ implementations.forEach((implementation) => {
114115
webpackOptions
115116
);
116117

117-
runWebpack(baseConfig, loaderOptions, (err) =>
118+
runWebpack(baseConfig, loaderOptions, resolveOptions, (err) =>
118119
err ? reject(err) : resolve()
119120
);
120121
}).then(() => {
@@ -173,6 +174,17 @@ implementations.forEach((implementation) => {
173174
));
174175
it('should resolve sass field correctly', () =>
175176
execTest(`import-sass-field`));
177+
it('should resolve style field correctly', () =>
178+
execTest(`import-style-field`));
179+
it('should resolve main field correctly', () =>
180+
execTest(`import-main-field`));
181+
it('should resolve custom-sass field correctly', () =>
182+
execTest(
183+
`import-custom-sass-field`,
184+
{},
185+
{},
186+
{ mainFields: ['custom-sass', '...'] }
187+
));
176188
// Works only in dart-sass implementation
177189
if (implementation === dartSass) {
178190
it('should resolve index file in module correctly', () =>
@@ -280,6 +292,7 @@ implementations.forEach((implementation) => {
280292
},
281293
},
282294
{},
295+
{},
283296
(err) => (err ? reject(err) : resolve())
284297
);
285298
}).then(() => {
@@ -392,6 +405,7 @@ implementations.forEach((implementation) => {
392405
entry: pathToErrorFile,
393406
},
394407
{},
408+
{},
395409
(err) => {
396410
if (implementation === nodeSass) {
397411
err.message.should.match(
@@ -413,6 +427,7 @@ implementations.forEach((implementation) => {
413427
entry: pathToErrorImport,
414428
},
415429
{},
430+
{},
416431
(err) => {
417432
// check for file excerpt
418433
if (implementation === nodeSass) {
@@ -435,6 +450,7 @@ implementations.forEach((implementation) => {
435450
entry: pathToErrorFileNotFound,
436451
},
437452
{},
453+
{},
438454
(err) => {
439455
err.message.should.match(/@import "does-not-exist";/);
440456
if (implementation === nodeSass) {
@@ -457,6 +473,7 @@ implementations.forEach((implementation) => {
457473
entry: pathToErrorFileNotFound2,
458474
},
459475
{},
476+
{},
460477
(err) => {
461478
err.message.should.match(/@import "\.\/another\/_module\.scss";/);
462479
if (implementation === nodeSass) {
@@ -500,6 +517,7 @@ implementations.forEach((implementation) => {
500517
entry: `${pathToSassLoader}!${pathToErrorFile}`,
501518
},
502519
{ implementation: null },
520+
{},
503521
(err) => {
504522
// eslint-disable-next-line no-underscore-dangle
505523
module._resolveFilename = originalResolve;
@@ -518,6 +536,7 @@ implementations.forEach((implementation) => {
518536
{
519537
implementation: merge(nodeSass, { info: 'asdfj' }),
520538
},
539+
{},
521540
(err) => {
522541
err.message.should.match(/Unknown Sass implementation "asdfj"\./);
523542
done();
@@ -533,6 +552,7 @@ implementations.forEach((implementation) => {
533552
{
534553
implementation: merge(nodeSass, { info: 'node-sass\t1' }),
535554
},
555+
{},
536556
(err) => {
537557
err.message.should.match(/Invalid Sass version "1"\./);
538558
done();
@@ -548,6 +568,7 @@ implementations.forEach((implementation) => {
548568
{
549569
implementation: merge(nodeSass, { info: 'node-sass\t3.0.0' }),
550570
},
571+
{},
551572
(err) => {
552573
err.message.should.match(
553574
/Node Sass version 3\.0\.0 is incompatible with \^4\.0\.0\./
@@ -565,6 +586,7 @@ implementations.forEach((implementation) => {
565586
{
566587
implementation: merge(nodeSass, { info: 'dart-sass\t1.2.0' }),
567588
},
589+
{},
568590
(err) => {
569591
err.message.should.match(
570592
/Dart Sass version 1\.2\.0 is incompatible with \^1\.3\.0\./
@@ -582,6 +604,7 @@ implementations.forEach((implementation) => {
582604
{
583605
implementation: merge(nodeSass, { info: 'strange-sass\t1.0.0' }),
584606
},
607+
{},
585608
(err) => {
586609
err.message.should.match(
587610
/Unknown Sass implementation "strange-sass"\./
@@ -629,6 +652,7 @@ implementations.forEach((implementation) => {
629652
entry: pathToFile,
630653
},
631654
{ implementation: null },
655+
{},
632656
(err) => {
633657
// eslint-disable-next-line no-underscore-dangle
634658
module._resolveFilename = originalResolve;

test/node_modules/sass-custom-sass-field/nested/style.sass

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/node_modules/sass-custom-sass-field/package.json

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/node_modules/sass-main-field/nested/style.sass

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/node_modules/sass-main-field/package.json

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/node_modules/sass-style-field/nested/style.sass

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/node_modules/sass-style-field/package.json

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/node_modules/scss-custom-sass-field/nested/style.scss

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/node_modules/scss-custom-sass-field/package.json

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/node_modules/scss-main-field/nested/style.scss

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/node_modules/scss-main-field/package.json

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/node_modules/scss-style-field/nested/style.scss

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/node_modules/scss-style-field/package.json

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import "~sass-custom-sass-field"

test/sass/import-main-field.sass

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import "~sass-main-field"

test/sass/import-style-field.sass

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import "~sass-style-field"
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import "~scss-custom-sass-field";

test/scss/import-main-field.scss

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import "~scss-main-field";

test/scss/import-style-field.scss

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import "~scss-style-field";

test/tools/createSpec.js

+66
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,36 @@ function createSpec(ext) {
4848
'style.scss'
4949
)
5050
);
51+
const pathToScssStyleField = path.relative(
52+
basePath,
53+
path.resolve(
54+
testFolder,
55+
'node_modules',
56+
'scss-style-field',
57+
'nested',
58+
'style.scss'
59+
)
60+
);
61+
const pathToScssCustomSassField = path.relative(
62+
basePath,
63+
path.resolve(
64+
testFolder,
65+
'node_modules',
66+
'scss-custom-sass-field',
67+
'nested',
68+
'style.scss'
69+
)
70+
);
71+
const pathToScssMainField = path.relative(
72+
basePath,
73+
path.resolve(
74+
testFolder,
75+
'node_modules',
76+
'scss-main-field',
77+
'nested',
78+
'style.scss'
79+
)
80+
);
5181
const pathToSASSSassField = path.relative(
5282
basePath,
5383
path.resolve(
@@ -58,6 +88,36 @@ function createSpec(ext) {
5888
'style.sass'
5989
)
6090
);
91+
const pathToSASSStyleField = path.relative(
92+
basePath,
93+
path.resolve(
94+
testFolder,
95+
'node_modules',
96+
'sass-style-field',
97+
'nested',
98+
'style.sass'
99+
)
100+
);
101+
const pathToSASSCustomSassField = path.relative(
102+
basePath,
103+
path.resolve(
104+
testFolder,
105+
'node_modules',
106+
'sass-custom-sass-field',
107+
'nested',
108+
'style.sass'
109+
)
110+
);
111+
const pathToSASSMainField = path.relative(
112+
basePath,
113+
path.resolve(
114+
testFolder,
115+
'node_modules',
116+
'sass-main-field',
117+
'nested',
118+
'style.sass'
119+
)
120+
);
61121

62122
fs.readdirSync(path.join(testFolder, ext))
63123
.filter(
@@ -78,7 +138,13 @@ function createSpec(ext) {
78138
// eslint-disable-next-line no-param-reassign
79139
url = url
80140
.replace(/^~scss-sass-field/, pathToScssSassField)
141+
.replace(/^~scss-style-field/, pathToScssStyleField)
142+
.replace(/^~scss-custom-sass-field/, pathToScssCustomSassField)
143+
.replace(/^~scss-main-field/, pathToScssMainField)
81144
.replace(/^~sass-sass-field/, pathToSASSSassField)
145+
.replace(/^~sass-style-field/, pathToSASSStyleField)
146+
.replace(/^~sass-custom-sass-field/, pathToSASSCustomSassField)
147+
.replace(/^~sass-main-field/, pathToSASSMainField)
82148
.replace(/^~bootstrap-sass/, pathToBootstrap)
83149
.replace(/^~@org\/pkg/, pathToScopedNpmPkg)
84150
.replace(/^~module/, pathToModule)

0 commit comments

Comments
 (0)