Skip to content

Loader features refactor #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions lib/loader-features.js → lib/features.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ const packageHelper = require('./package-helper');

/**
* An object that holds internal configuration about different
* "loaders" that can be enabled.
* "loaders"/"plugins" that can be enabled/used.
*/
const loaderFeatures = {
const features = {
sass: {
method: 'enableSassLoader()',
packages: ['sass-loader', 'node-sass'],
Expand Down Expand Up @@ -55,31 +55,31 @@ const loaderFeatures = {
}
};

function getLoaderFeatureConfig(loaderName) {
if (!loaderFeatures[loaderName]) {
throw new Error(`Unknown loader feature ${loaderName}`);
function getFeatureConfig(featureName) {
if (!features[featureName]) {
throw new Error(`Unknown feature ${featureName}`);
}

return loaderFeatures[loaderName];
return features[featureName];
}

module.exports = {
getLoaderFeatureConfig,
getFeatureConfig,

ensureLoaderPackagesExist: function(loaderName) {
const config = getLoaderFeatureConfig(loaderName);
ensurePackagesExist: function(featureName) {
const config = getFeatureConfig(featureName);

packageHelper.ensurePackagesExist(
config.packages,
config.method
);
},

getLoaderFeatureMethod: function(loaderName) {
return getLoaderFeatureConfig(loaderName).method;
getFeatureMethod: function(featureName) {
return getFeatureConfig(featureName).method;
},

getLoaderFeatureDescription: function(loaderName) {
return getLoaderFeatureConfig(loaderName).description;
getFeatureDescription: function(featureName) {
return getFeatureConfig(featureName).description;
}
};
8 changes: 4 additions & 4 deletions lib/friendly-errors/formatters/missing-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
'use strict';

const chalk = require('chalk');
const loaderFeatures = require('../../loader-features');
const loaderFeatures = require('../../features');
const packageHelper = require('../../package-helper');

function formatErrors(errors) {
Expand All @@ -23,10 +23,10 @@ function formatErrors(errors) {
const fixes = [];

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

const loaderFeatureConfig = loaderFeatures.getLoaderFeatureConfig(error.loaderName);
const loaderFeatureConfig = loaderFeatures.getFeatureConfig(error.loaderName);
const packageRecommendations = packageHelper.getPackageRecommendations(
loaderFeatureConfig.packages
);
Expand All @@ -44,7 +44,7 @@ function formatErrors(errors) {
]);

if (error.loaderName) {
messages.push(`${chalk.bgGreen.black('', 'FIX', '')} To ${loaderFeatures.getLoaderFeatureDescription(error.loaderName)}:`);
messages.push(`${chalk.bgGreen.black('', 'FIX', '')} To ${loaderFeatures.getFeatureDescription(error.loaderName)}:`);
} else {
messages.push(`${chalk.bgGreen.black('', 'FIX', '')} To load ${error.file}:`);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/loaders/babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

'use strict';

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

/**
* @param {WebpackConfig} webpackConfig
Expand Down Expand Up @@ -45,7 +45,7 @@ module.exports = {
});

if (webpackConfig.useReact) {
loaderFeatures.ensureLoaderPackagesExist('react');
loaderFeatures.ensurePackagesExist('react');

babelConfig.presets.push('react');
}
Expand Down
4 changes: 2 additions & 2 deletions lib/loaders/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

'use strict';

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

/**
* @param {WebpackConfig} webpackConfig
Expand All @@ -36,7 +36,7 @@ module.exports = {
];

if (usePostCssLoader) {
loaderFeatures.ensureLoaderPackagesExist('postcss');
loaderFeatures.ensurePackagesExist('postcss');

const postCssLoaderOptions = {
sourceMap: webpackConfig.useSourceMaps
Expand Down
4 changes: 2 additions & 2 deletions lib/loaders/less.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

'use strict';

const loaderFeatures = require('../loader-features');
const loaderFeatures = require('../features');
const cssLoader = require('./css');

/**
Expand All @@ -19,7 +19,7 @@ const cssLoader = require('./css');
*/
module.exports = {
getLoaders(webpackConfig, ignorePostCssLoader = false) {
loaderFeatures.ensureLoaderPackagesExist('less');
loaderFeatures.ensurePackagesExist('less');

const config = {
sourceMap: webpackConfig.useSourceMaps
Expand Down
4 changes: 2 additions & 2 deletions lib/loaders/sass.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

'use strict';

const loaderFeatures = require('../loader-features');
const loaderFeatures = require('../features');
const cssLoader = require('./css');

/**
Expand All @@ -20,7 +20,7 @@ const cssLoader = require('./css');
*/
module.exports = {
getLoaders(webpackConfig, sassOptions = {}, ignorePostCssLoader = false) {
loaderFeatures.ensureLoaderPackagesExist('sass');
loaderFeatures.ensurePackagesExist('sass');

const sassLoaders = [...cssLoader.getLoaders(webpackConfig, ignorePostCssLoader)];
if (true === webpackConfig.sassOptions.resolve_url_loader) {
Expand Down
6 changes: 3 additions & 3 deletions lib/loaders/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

'use strict';

const loaderFeatures = require('../loader-features');
const loaderFeatures = require('../features');
const babelLoader = require('./babel');

/**
Expand All @@ -18,7 +18,7 @@ const babelLoader = require('./babel');
*/
module.exports = {
getLoaders(webpackConfig) {
loaderFeatures.ensureLoaderPackagesExist('typescript');
loaderFeatures.ensurePackagesExist('typescript');

// some defaults
let config = {
Expand All @@ -34,7 +34,7 @@ module.exports = {

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

Expand Down
10 changes: 5 additions & 5 deletions lib/loaders/vue.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

'use strict';

const loaderFeatures = require('../loader-features');
const loaderFeatures = require('../features');
const cssLoaderUtil = require('./css');
const sassLoaderUtil = require('./sass');
const lessLoaderUtil = require('./less');
Expand All @@ -23,7 +23,7 @@ const extractText = require('./extract-text');
*/
module.exports = {
getLoaders(webpackConfig, vueLoaderOptionsCallback) {
loaderFeatures.ensureLoaderPackagesExist('vue');
loaderFeatures.ensurePackagesExist('vue');

/*
* The vue-loader passes the contents of <style> and <script>
Expand Down Expand Up @@ -86,7 +86,7 @@ module.exports = {
options: {
lang: 'scss',
loaderName: 'sass-loader',
featureCommand: loaderFeatures.getLoaderFeatureMethod('sass')
featureCommand: loaderFeatures.getFeatureMethod('sass')
}
};

Expand All @@ -95,7 +95,7 @@ module.exports = {
options: {
lang: 'sass',
loaderName: 'sass-loader',
featureCommand: loaderFeatures.getLoaderFeatureMethod('sass')
featureCommand: loaderFeatures.getFeatureMethod('sass')
}
};
}
Expand All @@ -112,7 +112,7 @@ module.exports = {
options: {
lang: 'less',
loaderName: 'less-loader',
featureCommand: loaderFeatures.getLoaderFeatureMethod('less')
featureCommand: loaderFeatures.getFeatureMethod('less')
}
};
}
Expand Down
24 changes: 8 additions & 16 deletions test/functional.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,14 +287,12 @@ describe('Functional tests using webpack', function() {
'h1.c84caea6dd12bba7955dee9fedd5fd03.css',
'bg.483832e48e67e6a3b7f0ae064eadca51.css',
'manifest.json'
]
);
]);

expect(path.join(config.outputPath, 'images')).to.be.a.directory()
.with.files([
'symfony_logo.ea1ca6f7.png'
]
);
]);

webpackAssert.assertOutputFileContains(
'bg.483832e48e67e6a3b7f0ae064eadca51.css',
Expand All @@ -318,20 +316,17 @@ describe('Functional tests using webpack', function() {
'bg.css',
'font.css',
'manifest.json'
]
);
]);

expect(path.join(config.outputPath, 'images')).to.be.a.directory()
.with.files([
'symfony_logo.ea1ca6f7.png'
]
);
]);

expect(path.join(config.outputPath, 'fonts')).to.be.a.directory()
.with.files([
'Roboto.9896f773.woff2'
]
);
]);

webpackAssert.assertOutputFileContains(
'bg.css',
Expand All @@ -358,22 +353,19 @@ describe('Functional tests using webpack', function() {
.with.files([
'styles.css',
'manifest.json'
]
);
]);

expect(path.join(config.outputPath, 'images')).to.be.a.directory()
.with.files([
'symfony_logo.ea1ca6f7.png',
'symfony_logo.f27119c2.png'
]
);
]);

expect(path.join(config.outputPath, 'fonts')).to.be.a.directory()
.with.files([
'Roboto.9896f773.woff2',
'Roboto.3c37aa69.woff2'
]
);
]);

webpackAssert.assertOutputFileContains(
'styles.css',
Expand Down