Skip to content

Commit c4b6e04

Browse files
committed
minor #123 Loader features refactor (davidmpaz)
This PR was squashed before being merged into the master branch (closes #123). Discussion ---------- Loader features refactor These are internal changes to match current usage of `loader-features.js`, renaming mostly. Package checker is not used uniquely for loaders, but also node modules in general, like plugins. Commits ------- b7eff23 Loader features refactor
2 parents fbc67c2 + b7eff23 commit c4b6e04

File tree

9 files changed

+41
-49
lines changed

9 files changed

+41
-49
lines changed

lib/loader-features.js renamed to lib/features.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ const packageHelper = require('./package-helper');
1313

1414
/**
1515
* An object that holds internal configuration about different
16-
* "loaders" that can be enabled.
16+
* "loaders"/"plugins" that can be enabled/used.
1717
*/
18-
const loaderFeatures = {
18+
const features = {
1919
sass: {
2020
method: 'enableSassLoader()',
2121
packages: ['sass-loader', 'node-sass'],
@@ -55,31 +55,31 @@ const loaderFeatures = {
5555
}
5656
};
5757

58-
function getLoaderFeatureConfig(loaderName) {
59-
if (!loaderFeatures[loaderName]) {
60-
throw new Error(`Unknown loader feature ${loaderName}`);
58+
function getFeatureConfig(featureName) {
59+
if (!features[featureName]) {
60+
throw new Error(`Unknown feature ${featureName}`);
6161
}
6262

63-
return loaderFeatures[loaderName];
63+
return features[featureName];
6464
}
6565

6666
module.exports = {
67-
getLoaderFeatureConfig,
67+
getFeatureConfig,
6868

69-
ensureLoaderPackagesExist: function(loaderName) {
70-
const config = getLoaderFeatureConfig(loaderName);
69+
ensurePackagesExist: function(featureName) {
70+
const config = getFeatureConfig(featureName);
7171

7272
packageHelper.ensurePackagesExist(
7373
config.packages,
7474
config.method
7575
);
7676
},
7777

78-
getLoaderFeatureMethod: function(loaderName) {
79-
return getLoaderFeatureConfig(loaderName).method;
78+
getFeatureMethod: function(featureName) {
79+
return getFeatureConfig(featureName).method;
8080
},
8181

82-
getLoaderFeatureDescription: function(loaderName) {
83-
return getLoaderFeatureConfig(loaderName).description;
82+
getFeatureDescription: function(featureName) {
83+
return getFeatureConfig(featureName).description;
8484
}
8585
};

lib/friendly-errors/formatters/missing-loader.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
'use strict';
1111

1212
const chalk = require('chalk');
13-
const loaderFeatures = require('../../loader-features');
13+
const loaderFeatures = require('../../features');
1414
const packageHelper = require('../../package-helper');
1515

1616
function formatErrors(errors) {
@@ -23,10 +23,10 @@ function formatErrors(errors) {
2323
const fixes = [];
2424

2525
if (error.loaderName) {
26-
let neededCode = `Encore.${loaderFeatures.getLoaderFeatureMethod(error.loaderName)}`;
26+
let neededCode = `Encore.${loaderFeatures.getFeatureMethod(error.loaderName)}`;
2727
fixes.push(`Add ${chalk.green(neededCode)} to your webpack.config.js file.`);
2828

29-
const loaderFeatureConfig = loaderFeatures.getLoaderFeatureConfig(error.loaderName);
29+
const loaderFeatureConfig = loaderFeatures.getFeatureConfig(error.loaderName);
3030
const packageRecommendations = packageHelper.getPackageRecommendations(
3131
loaderFeatureConfig.packages
3232
);
@@ -44,7 +44,7 @@ function formatErrors(errors) {
4444
]);
4545

4646
if (error.loaderName) {
47-
messages.push(`${chalk.bgGreen.black('', 'FIX', '')} To ${loaderFeatures.getLoaderFeatureDescription(error.loaderName)}:`);
47+
messages.push(`${chalk.bgGreen.black('', 'FIX', '')} To ${loaderFeatures.getFeatureDescription(error.loaderName)}:`);
4848
} else {
4949
messages.push(`${chalk.bgGreen.black('', 'FIX', '')} To load ${error.file}:`);
5050
}

lib/loaders/babel.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
'use strict';
1111

12-
const loaderFeatures = require('../loader-features');
12+
const loaderFeatures = require('../features');
1313

1414
/**
1515
* @param {WebpackConfig} webpackConfig
@@ -45,7 +45,7 @@ module.exports = {
4545
});
4646

4747
if (webpackConfig.useReact) {
48-
loaderFeatures.ensureLoaderPackagesExist('react');
48+
loaderFeatures.ensurePackagesExist('react');
4949

5050
babelConfig.presets.push('react');
5151
}

lib/loaders/css.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
'use strict';
1111

12-
const loaderFeatures = require('../loader-features');
12+
const loaderFeatures = require('../features');
1313

1414
/**
1515
* @param {WebpackConfig} webpackConfig
@@ -36,7 +36,7 @@ module.exports = {
3636
];
3737

3838
if (usePostCssLoader) {
39-
loaderFeatures.ensureLoaderPackagesExist('postcss');
39+
loaderFeatures.ensurePackagesExist('postcss');
4040

4141
const postCssLoaderOptions = {
4242
sourceMap: webpackConfig.useSourceMaps

lib/loaders/less.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
'use strict';
1111

12-
const loaderFeatures = require('../loader-features');
12+
const loaderFeatures = require('../features');
1313
const cssLoader = require('./css');
1414

1515
/**
@@ -19,7 +19,7 @@ const cssLoader = require('./css');
1919
*/
2020
module.exports = {
2121
getLoaders(webpackConfig, ignorePostCssLoader = false) {
22-
loaderFeatures.ensureLoaderPackagesExist('less');
22+
loaderFeatures.ensurePackagesExist('less');
2323

2424
const config = {
2525
sourceMap: webpackConfig.useSourceMaps

lib/loaders/sass.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
'use strict';
1111

12-
const loaderFeatures = require('../loader-features');
12+
const loaderFeatures = require('../features');
1313
const cssLoader = require('./css');
1414

1515
/**
@@ -20,7 +20,7 @@ const cssLoader = require('./css');
2020
*/
2121
module.exports = {
2222
getLoaders(webpackConfig, sassOptions = {}, ignorePostCssLoader = false) {
23-
loaderFeatures.ensureLoaderPackagesExist('sass');
23+
loaderFeatures.ensurePackagesExist('sass');
2424

2525
const sassLoaders = [...cssLoader.getLoaders(webpackConfig, ignorePostCssLoader)];
2626
if (true === webpackConfig.sassOptions.resolve_url_loader) {

lib/loaders/typescript.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
'use strict';
1111

12-
const loaderFeatures = require('../loader-features');
12+
const loaderFeatures = require('../features');
1313
const babelLoader = require('./babel');
1414

1515
/**
@@ -18,7 +18,7 @@ const babelLoader = require('./babel');
1818
*/
1919
module.exports = {
2020
getLoaders(webpackConfig) {
21-
loaderFeatures.ensureLoaderPackagesExist('typescript');
21+
loaderFeatures.ensurePackagesExist('typescript');
2222

2323
// some defaults
2424
let config = {
@@ -34,7 +34,7 @@ module.exports = {
3434

3535
// fork-ts-checker-webpack-plugin integration
3636
if (webpackConfig.useForkedTypeScriptTypeChecking) {
37-
loaderFeatures.ensureLoaderPackagesExist('forkedtypecheck');
37+
loaderFeatures.ensurePackagesExist('forkedtypecheck');
3838
// force transpileOnly to speed up
3939
config.transpileOnly = true;
4040

lib/loaders/vue.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
'use strict';
1111

12-
const loaderFeatures = require('../loader-features');
12+
const loaderFeatures = require('../features');
1313
const cssLoaderUtil = require('./css');
1414
const sassLoaderUtil = require('./sass');
1515
const lessLoaderUtil = require('./less');
@@ -23,7 +23,7 @@ const extractText = require('./extract-text');
2323
*/
2424
module.exports = {
2525
getLoaders(webpackConfig, vueLoaderOptionsCallback) {
26-
loaderFeatures.ensureLoaderPackagesExist('vue');
26+
loaderFeatures.ensurePackagesExist('vue');
2727

2828
/*
2929
* The vue-loader passes the contents of <style> and <script>
@@ -86,7 +86,7 @@ module.exports = {
8686
options: {
8787
lang: 'scss',
8888
loaderName: 'sass-loader',
89-
featureCommand: loaderFeatures.getLoaderFeatureMethod('sass')
89+
featureCommand: loaderFeatures.getFeatureMethod('sass')
9090
}
9191
};
9292

@@ -95,7 +95,7 @@ module.exports = {
9595
options: {
9696
lang: 'sass',
9797
loaderName: 'sass-loader',
98-
featureCommand: loaderFeatures.getLoaderFeatureMethod('sass')
98+
featureCommand: loaderFeatures.getFeatureMethod('sass')
9999
}
100100
};
101101
}
@@ -112,7 +112,7 @@ module.exports = {
112112
options: {
113113
lang: 'less',
114114
loaderName: 'less-loader',
115-
featureCommand: loaderFeatures.getLoaderFeatureMethod('less')
115+
featureCommand: loaderFeatures.getFeatureMethod('less')
116116
}
117117
};
118118
}

test/functional.js

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -287,14 +287,12 @@ describe('Functional tests using webpack', function() {
287287
'h1.c84caea6dd12bba7955dee9fedd5fd03.css',
288288
'bg.483832e48e67e6a3b7f0ae064eadca51.css',
289289
'manifest.json'
290-
]
291-
);
290+
]);
292291

293292
expect(path.join(config.outputPath, 'images')).to.be.a.directory()
294293
.with.files([
295294
'symfony_logo.ea1ca6f7.png'
296-
]
297-
);
295+
]);
298296

299297
webpackAssert.assertOutputFileContains(
300298
'bg.483832e48e67e6a3b7f0ae064eadca51.css',
@@ -318,20 +316,17 @@ describe('Functional tests using webpack', function() {
318316
'bg.css',
319317
'font.css',
320318
'manifest.json'
321-
]
322-
);
319+
]);
323320

324321
expect(path.join(config.outputPath, 'images')).to.be.a.directory()
325322
.with.files([
326323
'symfony_logo.ea1ca6f7.png'
327-
]
328-
);
324+
]);
329325

330326
expect(path.join(config.outputPath, 'fonts')).to.be.a.directory()
331327
.with.files([
332328
'Roboto.9896f773.woff2'
333-
]
334-
);
329+
]);
335330

336331
webpackAssert.assertOutputFileContains(
337332
'bg.css',
@@ -358,22 +353,19 @@ describe('Functional tests using webpack', function() {
358353
.with.files([
359354
'styles.css',
360355
'manifest.json'
361-
]
362-
);
356+
]);
363357

364358
expect(path.join(config.outputPath, 'images')).to.be.a.directory()
365359
.with.files([
366360
'symfony_logo.ea1ca6f7.png',
367361
'symfony_logo.f27119c2.png'
368-
]
369-
);
362+
]);
370363

371364
expect(path.join(config.outputPath, 'fonts')).to.be.a.directory()
372365
.with.files([
373366
'Roboto.9896f773.woff2',
374367
'Roboto.3c37aa69.woff2'
375-
]
376-
);
368+
]);
377369

378370
webpackAssert.assertOutputFileContains(
379371
'styles.css',

0 commit comments

Comments
 (0)