forked from symfony/webpack-encore
/
sass.js
169 lines (130 loc) · 5.57 KB
/
sass.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* This file is part of the Symfony Webpack Encore package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
'use strict';
const expect = require('chai').expect;
const WebpackConfig = require('../../lib/WebpackConfig');
const RuntimeConfig = require('../../lib/config/RuntimeConfig');
const sassLoader = require('../../lib/loaders/sass');
const cssLoader = require('../../lib/loaders/css');
const sinon = require('sinon');
function createConfig() {
const runtimeConfig = new RuntimeConfig();
runtimeConfig.context = __dirname;
runtimeConfig.babelRcFileExists = false;
return new WebpackConfig(runtimeConfig);
}
describe('loaders/sass', () => {
it('getLoaders() basic usage', () => {
const config = createConfig();
config.enableSourceMaps(true);
// make the cssLoader return nothing
const cssLoaderStub = sinon.stub(cssLoader, 'getLoaders')
.callsFake(() => []);
const actualLoaders = sassLoader.getLoaders(config);
expect(actualLoaders).to.have.lengthOf(2);
expect(actualLoaders[0].loader).to.equal('resolve-url-loader');
expect(actualLoaders[0].options.sourceMap).to.be.true;
expect(actualLoaders[1].loader).to.equal('sass-loader');
expect(actualLoaders[1].options.sourceMap).to.be.true;
expect(cssLoaderStub.getCall(0).args[1]).to.be.false;
cssLoader.getLoaders.restore();
});
it('getLoaders() with resolve-url-loader but not sourcemaps', () => {
const config = createConfig();
config.enableSourceMaps(false);
// make the cssLoader return nothing
sinon.stub(cssLoader, 'getLoaders')
.callsFake(() => []);
const actualLoaders = sassLoader.getLoaders(config);
expect(actualLoaders).to.have.lengthOf(2);
expect(actualLoaders[0].loader).to.equal('resolve-url-loader');
expect(actualLoaders[0].options.sourceMap).to.be.false;
expect(actualLoaders[1].loader).to.equal('sass-loader');
// sourcemaps always enabled when resolve-url-loader is enabled
expect(actualLoaders[1].options.sourceMap).to.be.true;
cssLoader.getLoaders.restore();
});
it('getLoaders() with resolve-url-loader options', () => {
const config = createConfig();
config.enableSassLoader(() => {}, {
resolveUrlLoaderOptions: {
removeCR: true
}
});
// make the cssLoader return nothing
sinon.stub(cssLoader, 'getLoaders')
.callsFake(() => []);
const actualLoaders = sassLoader.getLoaders(config);
expect(actualLoaders).to.have.lengthOf(2);
expect(actualLoaders[0].loader).to.equal('resolve-url-loader');
expect(actualLoaders[0].options.removeCR).to.be.true;
cssLoader.getLoaders.restore();
});
it('getLoaders() without resolve-url-loader', () => {
const config = createConfig();
config.enableSassLoader(() => {}, {
resolveUrlLoader: false,
});
config.enableSourceMaps(false);
// make the cssLoader return nothing
sinon.stub(cssLoader, 'getLoaders')
.callsFake(() => []);
const actualLoaders = sassLoader.getLoaders(config);
expect(actualLoaders).to.have.lengthOf(1);
expect(actualLoaders[0].loader).to.equal('sass-loader');
expect(actualLoaders[0].options.sourceMap).to.be.false;
cssLoader.getLoaders.restore();
});
it('getLoaders() with options callback', () => {
const config = createConfig();
// make the cssLoader return nothing
sinon.stub(cssLoader, 'getLoaders')
.callsFake(() => []);
config.enableSassLoader(function(sassOptions) {
sassOptions.custom_optiona = 'baz';
sassOptions.other_option = true;
});
const actualLoaders = sassLoader.getLoaders(config);
expect(actualLoaders[1].options).to.deep.equals({
sourceMap: true,
custom_optiona: 'baz',
other_option: true
});
cssLoader.getLoaders.restore();
});
it('getLoaders() with a callback that returns an object', () => {
const config = createConfig();
// make the cssLoader return nothing
sinon.stub(cssLoader, 'getLoaders')
.callsFake(() => []);
config.enableSassLoader(function(sassOptions) {
sassOptions.custom_option = 'baz';
// This should override the original config
return { foo: true };
});
const actualLoaders = sassLoader.getLoaders(config);
expect(actualLoaders[1].options).to.deep.equals({ foo: true });
cssLoader.getLoaders.restore();
});
it('getLoaders() with CSS modules enabled', () => {
const config = createConfig();
config.enableSourceMaps(true);
// make the cssLoader return nothing
const cssLoaderStub = sinon.stub(cssLoader, 'getLoaders')
.callsFake(() => []);
const actualLoaders = sassLoader.getLoaders(config, true);
expect(actualLoaders).to.have.lengthOf(2);
expect(actualLoaders[0].loader).to.equal('resolve-url-loader');
expect(actualLoaders[0].options.sourceMap).to.be.true;
expect(actualLoaders[1].loader).to.equal('sass-loader');
expect(actualLoaders[1].options.sourceMap).to.be.true;
expect(cssLoaderStub.getCall(0).args[1]).to.be.true;
cssLoader.getLoaders.restore();
});
});