Skip to content

Commit 4dbe443

Browse files
committed
chore(cr): add real aliases support
1 parent 729a696 commit 4dbe443

File tree

4 files changed

+55
-30
lines changed

4 files changed

+55
-30
lines changed

lib/WebpackConfig.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,15 @@ class WebpackConfig {
102102
this.handlebarsConfigurationCallback = () => {};
103103
this.loaderConfigurationCallbacks = {
104104
javascript: () => {},
105-
js: () => {},
106105
css: () => {},
107106
images: () => {},
108107
fonts: () => {},
109108
sass: () => {},
110-
scss: () => {},
111109
less: () => {},
112110
stylus: () => {},
113111
vue: () => {},
114112
eslint: () => {},
115113
typescript: () => {},
116-
ts: () => {},
117114
handlebars: () => {},
118115
};
119116

@@ -735,8 +732,19 @@ class WebpackConfig {
735732
configureLoaderRule(name, callback) {
736733
logger.warning('Be careful when using Encore.configureLoaderRule(), this is a low-level method that can potentially breaks Encore and Webpack when not used carefully.');
737734

735+
// Key: alias, Value: existing loader in `this.loaderConfigurationCallbacks`
736+
const aliases = {
737+
js: 'javascript',
738+
ts: 'typescript',
739+
scss: 'sass',
740+
};
741+
742+
if (name in aliases) {
743+
name = aliases[name];
744+
}
745+
738746
if (!(name in this.loaderConfigurationCallbacks)) {
739-
throw new Error(`Loader "${name}" is not configurable. Valid loaders are "${Object.keys(this.loaderConfigurationCallbacks).join('", "')}".`);
747+
throw new Error(`Loader "${name}" is not configurable. Valid loaders are "${Object.keys(this.loaderConfigurationCallbacks).join('", "')}" and the aliases "${Object.keys(aliases).join('", "')}".`);
740748
}
741749

742750
if (typeof callback !== 'function') {

lib/config-generator.js

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -222,25 +222,11 @@ class ConfigGenerator {
222222

223223
buildRulesConfig() {
224224
const applyRuleConfigurationCallback = (name, defaultRules) => {
225-
if (!(name in this.webpackConfig.loaderConfigurationCallbacks)) {
226-
throw new Error(`Loader "${name}" is not configurable. Valid loaders are "${Object.keys(this.webpackConfig.loaderConfigurationCallbacks).join('", "')}".`);
227-
}
228-
229225
return applyOptionsCallback(this.webpackConfig.loaderConfigurationCallbacks[name], defaultRules);
230226
};
231227

232-
const applyRuleConfigurationCallbacks = (names, defaultRules) => {
233-
let rules = defaultRules;
234-
235-
names.forEach(name => {
236-
rules = applyRuleConfigurationCallback(name, rules);
237-
});
238-
239-
return rules;
240-
};
241-
242228
let rules = [
243-
applyRuleConfigurationCallbacks(['javascript', 'js'], {
229+
applyRuleConfigurationCallback('javascript', {
244230
// match .js and .jsx
245231
test: /\.jsx?$/,
246232
exclude: this.webpackConfig.babelOptions.exclude,
@@ -323,7 +309,7 @@ class ConfigGenerator {
323309
}
324310

325311
if (this.webpackConfig.useSassLoader) {
326-
rules.push(applyRuleConfigurationCallbacks(['sass', 'scss'], {
312+
rules.push(applyRuleConfigurationCallback('sass', {
327313
test: /\.s[ac]ss$/,
328314
oneOf: [
329315
{
@@ -385,7 +371,7 @@ class ConfigGenerator {
385371
}
386372

387373
if (this.webpackConfig.useTypeScriptLoader) {
388-
rules.push(applyRuleConfigurationCallbacks(['typescript', 'ts'], {
374+
rules.push(applyRuleConfigurationCallback('typescript', {
389375
test: /\.tsx?$/,
390376
exclude: /node_modules/,
391377
use: tsLoaderUtil.getLoaders(this.webpackConfig)

test/WebpackConfig.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1161,7 +1161,7 @@ describe('WebpackConfig object', () => {
11611161

11621162
expect(() => {
11631163
config.configureLoaderRule('reason');
1164-
}).to.throw('Loader "reason" is not configurable. Valid loaders are "javascript", "js", "css", "images", "fonts", "sass", "scss", "less", "stylus", "vue", "eslint", "typescript", "ts", "handlebars".');
1164+
}).to.throw('Loader "reason" is not configurable. Valid loaders are "javascript", "css", "images", "fonts", "sass", "less", "stylus", "vue", "eslint", "typescript", "handlebars" and the aliases "js", "ts", "scss".');
11651165
});
11661166

11671167
it('Call method with not a valid callback', () => {

test/config-generator.js

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,11 +1025,23 @@ describe('The config-generator function', () => {
10251025
config.enableSingleRuntimeChunk();
10261026
});
10271027

1028-
it('configure rule for "javascript and "js"', () => {
1028+
it('configure rule for "javascript"', () => {
10291029
config.configureLoaderRule('javascript', (loaderRule) => {
10301030
loaderRule.test = /\.m?js$/;
1031+
loaderRule.use[0].options.fooBar = 'fooBar';
10311032
});
1033+
1034+
const webpackConfig = configGenerator(config);
1035+
const rule = findRule(/\.m?js$/, webpackConfig.module.rules);
1036+
1037+
expect('file.js').to.match(rule.test);
1038+
expect('file.mjs').to.match(rule.test);
1039+
expect(rule.use[0].options.fooBar).to.equal('fooBar');
1040+
});
1041+
1042+
it('configure rule for the alias "js"', () => {
10321043
config.configureLoaderRule('js', (loaderRule) => {
1044+
loaderRule.test = /\.m?js$/;
10331045
loaderRule.use[0].options.fooBar = 'fooBar';
10341046
});
10351047

@@ -1074,20 +1086,28 @@ describe('The config-generator function', () => {
10741086
expect(rule.options.name).to.equal('dirname-fonts/[hash:42].[ext]');
10751087
});
10761088

1077-
it('configure rule for "sass" and "scss"', () => {
1089+
it('configure rule for "sass"', () => {
10781090
config.enableSassLoader();
10791091
config.configureLoaderRule('sass', (loaderRule) => {
1080-
loaderRule.use.push('Option pushed when configuring Sass.');
1092+
loaderRule.use[2].options.fooBar = 'fooBar';
10811093
});
1094+
1095+
const webpackConfig = configGenerator(config);
1096+
const rule = findRule(/\.s[ac]ss$/, webpackConfig.module.rules);
1097+
1098+
expect(rule.use[2].options.fooBar).to.equal('fooBar');
1099+
});
1100+
1101+
it('configure rule for the alias "scss"', () => {
1102+
config.enableSassLoader();
10821103
config.configureLoaderRule('scss', (loaderRule) => {
1083-
loaderRule.use.push('Option pushed when configuring SCSS.');
1104+
loaderRule.use[2].options.fooBar = 'fooBar';
10841105
});
10851106

10861107
const webpackConfig = configGenerator(config);
10871108
const rule = findRule(/\.s[ac]ss$/, webpackConfig.module.rules);
10881109

1089-
expect(rule.use.pop()).to.equal('Option pushed when configuring SCSS.');
1090-
expect(rule.use.pop()).to.equal('Option pushed when configuring Sass.');
1110+
expect(rule.use[2].options.fooBar).to.equal('fooBar');
10911111
});
10921112

10931113
it('configure rule for "less"', () => {
@@ -1159,16 +1179,27 @@ describe('The config-generator function', () => {
11591179
config.configureLoaderRule('typescript', (loaderRule) => {
11601180
loaderRule.use[1].options.happyPackMode = true;
11611181
});
1182+
1183+
const webpackConfig = configGenerator(config);
1184+
const rule = findRule(/\.tsx?$/, webpackConfig.module.rules);
1185+
1186+
expect(rule.use[1].options.silent).to.be.true;
1187+
expect(rule.use[1].options.happyPackMode).to.be.true;
1188+
});
1189+
1190+
it('configure rule for the alias "ts"', () => {
1191+
config.enableTypeScriptLoader((options) => {
1192+
options.silent = true;
1193+
});
11621194
config.configureLoaderRule('ts', (loaderRule) => {
1163-
loaderRule.use[1].options.logInfoToStdOut = true;
1195+
loaderRule.use[1].options.happyPackMode = true;
11641196
});
11651197

11661198
const webpackConfig = configGenerator(config);
11671199
const rule = findRule(/\.tsx?$/, webpackConfig.module.rules);
11681200

11691201
expect(rule.use[1].options.silent).to.be.true;
11701202
expect(rule.use[1].options.happyPackMode).to.be.true;
1171-
expect(rule.use[1].options.logInfoToStdOut).to.be.true;
11721203
});
11731204

11741205
it('configure rule for "handlebars"', () => {

0 commit comments

Comments
 (0)