Skip to content

Commit 15112b7

Browse files
committed
Refactoring each loader a bit, adding a test for less.js (others are still a todo)
1 parent d7df254 commit 15112b7

File tree

8 files changed

+198
-102
lines changed

8 files changed

+198
-102
lines changed

lib/config-generator.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ const missingLoaderTransformer = require('./friendly-errors/transformers/missing
2121
const missingLoaderFormatter = require('./friendly-errors/formatters/missing-loader');
2222
const missingPostCssConfigTransformer = require('./friendly-errors/transformers/missing-postcss-config');
2323
const missingPostCssConfigFormatter = require('./friendly-errors/formatters/missing-postcss-config');
24-
const cssLoaders = require('./loaders/css');
25-
const sassLoaders = require('./loaders/sass');
26-
const lessLoaders = require('./loaders/less');
27-
const babelLoaders = require('./loaders/babel');
24+
const cssLoaderUtil = require('./loaders/css');
25+
const sassLoaderUtil = require('./loaders/sass');
26+
const lessLoaderUtil = require('./loaders/less');
27+
const babelLoaderUtil = require('./loaders/babel');
2828

2929
class ConfigGenerator {
3030
/**
@@ -106,13 +106,13 @@ class ConfigGenerator {
106106
// match .js and .jsx
107107
test: /\.jsx?$/,
108108
exclude: /(node_modules|bower_components)/,
109-
use: babelLoaders(this.webpackConfig)
109+
use: babelLoaderUtil.getLoaders(this.webpackConfig)
110110
},
111111
{
112112
test: /\.css$/,
113113
use: ExtractTextPlugin.extract({
114114
fallback: 'style-loader' + this.getSourceMapOption(),
115-
use: cssLoaders(this.webpackConfig)
115+
use: cssLoaderUtil.getLoaders(this.webpackConfig)
116116
})
117117
},
118118
{
@@ -138,7 +138,7 @@ class ConfigGenerator {
138138
test: /\.s[ac]ss$/,
139139
use: ExtractTextPlugin.extract({
140140
fallback: 'style-loader' + this.getSourceMapOption(),
141-
use: sassLoaders(this.webpackConfig)
141+
use: sassLoaderUtil.getLoaders(this.webpackConfig)
142142
})
143143
});
144144
}
@@ -148,7 +148,7 @@ class ConfigGenerator {
148148
test: /\.less/,
149149
use: ExtractTextPlugin.extract({
150150
fallback: 'style-loader' + this.getSourceMapOption(),
151-
use: lessLoaders(this.webpackConfig)
151+
use: lessLoaderUtil.getLoaders(this.webpackConfig)
152152
})
153153
});
154154
}

lib/loaders/babel.js

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,51 +15,53 @@ const loaderFeatures = require('../loader-features');
1515
* @param {WebpackConfig} webpackConfig
1616
* @return {Array} of loaders to use for Babel
1717
*/
18-
module.exports = function(webpackConfig) {
19-
let babelConfig = {
20-
// improves performance by caching babel compiles
21-
// we add this option ALWAYS
22-
// https://github.com/babel/babel-loader#options
23-
cacheDirectory: true
24-
};
18+
module.exports = {
19+
getLoaders(webpackConfig) {
20+
let babelConfig = {
21+
// improves performance by caching babel compiles
22+
// we add this option ALWAYS
23+
// https://github.com/babel/babel-loader#options
24+
cacheDirectory: true
25+
};
2526

26-
// configure babel (unless the user is specifying .babelrc)
27-
// todo - add a sanity check for their babelrc contents
28-
if (!webpackConfig.doesBabelRcFileExist()) {
29-
Object.assign(babelConfig, {
30-
presets: [
31-
['env', {
32-
// modules don't need to be transformed - webpack will parse
33-
// the modules for us. This is a performance improvement
34-
// https://babeljs.io/docs/plugins/preset-env/#optionsmodules
35-
modules: false,
36-
targets: {
37-
browsers: '> 1%',
38-
uglify: true
39-
},
40-
useBuiltIns: true
41-
}]
42-
],
43-
});
27+
// configure babel (unless the user is specifying .babelrc)
28+
// todo - add a sanity check for their babelrc contents
29+
if (!webpackConfig.doesBabelRcFileExist()) {
30+
Object.assign(babelConfig, {
31+
presets: [
32+
['env', {
33+
// modules don't need to be transformed - webpack will parse
34+
// the modules for us. This is a performance improvement
35+
// https://babeljs.io/docs/plugins/preset-env/#optionsmodules
36+
modules: false,
37+
targets: {
38+
browsers: '> 1%',
39+
uglify: true
40+
},
41+
useBuiltIns: true
42+
}]
43+
],
44+
});
4445

45-
if (webpackConfig.useReact) {
46-
loaderFeatures.ensureLoaderPackagesExist('react');
46+
if (webpackConfig.useReact) {
47+
loaderFeatures.ensureLoaderPackagesExist('react');
4748

48-
babelConfig.presets.push('react');
49+
babelConfig.presets.push('react');
50+
}
51+
52+
// allow for babel config to be controlled
53+
webpackConfig.babelConfigurationCallback.apply(
54+
// use babelConfig as the this variable
55+
babelConfig,
56+
[babelConfig]
57+
);
4958
}
5059

51-
// allow for babel config to be controlled
52-
webpackConfig.babelConfigurationCallback.apply(
53-
// use babelConfig as the this variable
54-
babelConfig,
55-
[babelConfig]
56-
);
60+
return [
61+
{
62+
loader: 'babel-loader',
63+
options: babelConfig
64+
}
65+
];
5766
}
58-
59-
return [
60-
{
61-
loader: 'babel-loader',
62-
options: babelConfig
63-
}
64-
];
6567
};

lib/loaders/css.js

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,29 @@ const loaderFeatures = require('../loader-features');
1515
* @param {WebpackConfig} webpackConfig
1616
* @return {Array} of loaders to use for CSS files
1717
*/
18-
module.exports = function(webpackConfig) {
19-
const cssLoaders = [
20-
{
21-
loader: 'css-loader',
22-
options: {
23-
minimize: webpackConfig.isProduction(),
24-
sourceMap: webpackConfig.useSourceMaps
25-
}
26-
},
27-
];
18+
module.exports = {
19+
getLoaders(webpackConfig) {
20+
const cssLoaders = [
21+
{
22+
loader: 'css-loader',
23+
options: {
24+
minimize: webpackConfig.isProduction(),
25+
sourceMap: webpackConfig.useSourceMaps
26+
}
27+
},
28+
];
2829

29-
if (webpackConfig.usePostCssLoader) {
30-
loaderFeatures.ensureLoaderPackagesExist('postcss');
30+
if (webpackConfig.usePostCssLoader) {
31+
loaderFeatures.ensureLoaderPackagesExist('postcss');
3132

32-
cssLoaders.push({
33-
loader: 'postcss-loader',
34-
options: {
35-
sourceMap: webpackConfig.useSourceMaps
36-
}
37-
});
38-
}
33+
cssLoaders.push({
34+
loader: 'postcss-loader',
35+
options: {
36+
sourceMap: webpackConfig.useSourceMaps
37+
}
38+
});
39+
}
3940

40-
return cssLoaders;
41+
return cssLoaders;
42+
}
4143
};

lib/loaders/less.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,24 @@
1010
'use strict';
1111

1212
const loaderFeatures = require('../loader-features');
13-
const cssLoaders = require('./css');
13+
const cssLoader = require('./css');
1414

1515
/**
1616
* @param {WebpackConfig} webpackConfig
1717
* @return {Array} of loaders to use for Less files
1818
*/
19-
module.exports = function(webpackConfig) {
20-
loaderFeatures.ensureLoaderPackagesExist('less');
19+
module.exports = {
20+
getLoaders(webpackConfig) {
21+
loaderFeatures.ensureLoaderPackagesExist('less');
2122

22-
return [
23-
...cssLoaders(webpackConfig),
24-
{
25-
loader: 'less-loader',
26-
options: {
27-
sourceMap: webpackConfig.useSourceMaps
28-
}
29-
},
30-
];
23+
return [
24+
...cssLoader.getLoaders(webpackConfig),
25+
{
26+
loader: 'less-loader',
27+
options: {
28+
sourceMap: webpackConfig.useSourceMaps
29+
}
30+
},
31+
];
32+
}
3133
};

lib/loaders/sass.js

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,37 @@
1010
'use strict';
1111

1212
const loaderFeatures = require('../loader-features');
13-
const cssLoaders = require('./css');
13+
const cssLoader = require('./css');
1414

1515
/**
1616
* @param {WebpackConfig} webpackConfig
1717
* @return {Array} of loaders to use for Sass files
1818
*/
19-
module.exports = function(webpackConfig) {
20-
loaderFeatures.ensureLoaderPackagesExist('sass');
19+
module.exports = {
20+
getLoaders(webpackConfig) {
21+
loaderFeatures.ensureLoaderPackagesExist('sass');
22+
23+
const sassLoaders = [...cssLoader.getLoaders(webpackConfig)];
24+
if (true === webpackConfig.sassOptions.resolve_url_loader) {
25+
// responsible for resolving SASS url() paths
26+
// without this, all url() paths must be relative to the
27+
// entry file, not the file that contains the url()
28+
sassLoaders.push({
29+
loader: 'resolve-url-loader',
30+
options: {
31+
sourceMap: webpackConfig.useSourceMaps
32+
}
33+
});
34+
}
2135

22-
const sassLoaders = [...cssLoaders(webpackConfig)];
23-
if (true === webpackConfig.sassOptions.resolve_url_loader) {
24-
// responsible for resolving SASS url() paths
25-
// without this, all url() paths must be relative to the
26-
// entry file, not the file that contains the url()
2736
sassLoaders.push({
28-
loader: 'resolve-url-loader',
37+
loader: 'sass-loader',
2938
options: {
30-
sourceMap: webpackConfig.useSourceMaps
39+
// needed by the resolve-url-loader
40+
sourceMap: (true === webpackConfig.sassOptions.resolve_url_loader) || webpackConfig.useSourceMaps
3141
}
3242
});
33-
}
3443

35-
sassLoaders.push({
36-
loader: 'sass-loader',
37-
options: {
38-
// needed by the resolve-url-loader
39-
sourceMap: (true === webpackConfig.sassOptions.resolve_url_loader) || webpackConfig.useSourceMaps
40-
}
41-
});
42-
43-
return sassLoaders;
44+
return sassLoaders;
45+
}
4446
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"nsp": "^2.6.3",
6363
"postcss-loader": "^1.3.3",
6464
"sass-loader": "^6.0.3",
65+
"sinon": "^2.3.4",
6566
"zombie": "^5.0.5"
6667
}
6768
}

test/loaders/less.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* This file is part of the Symfony package.
3+
*
4+
* (c) Fabien Potencier <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
'use strict';
11+
12+
const expect = require('chai').expect;
13+
const WebpackConfig = require('../../lib/WebpackConfig');
14+
const RuntimeConfig = require('../../lib/config/RuntimeConfig');
15+
const lessLoader = require('../../lib/loaders/less');
16+
const cssLoader = require('../../lib/loaders/css');
17+
const sinon = require('sinon');
18+
19+
function createConfig() {
20+
const runtimeConfig = new RuntimeConfig();
21+
runtimeConfig.context = __dirname;
22+
runtimeConfig.babelRcFileExists = false;
23+
24+
return new WebpackConfig(runtimeConfig);
25+
}
26+
27+
describe('loaders/less', () => {
28+
it('getLoaders() basic usage', () => {
29+
const config = createConfig();
30+
config.enableSourceMaps(true);
31+
32+
// make the cssLoader return nothing
33+
sinon.stub(cssLoader, 'getLoaders')
34+
.callsFake(() => []);
35+
36+
const actualLoaders = lessLoader.getLoaders(config);
37+
expect(actualLoaders).to.have.lengthOf(1);
38+
expect(actualLoaders[0].options.sourceMap).to.be.true;
39+
40+
cssLoader.getLoaders.restore();
41+
});
42+
});

0 commit comments

Comments
 (0)