Skip to content

Commit 2d30831

Browse files
committed
chore(cr): add real aliases support
1 parent ebb08d8 commit 2d30831

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
@@ -101,18 +101,15 @@ class WebpackConfig {
101101
this.handlebarsConfigurationCallback = () => {};
102102
this.loaderConfigurationCallbacks = {
103103
javascript: () => {},
104-
js: () => {},
105104
css: () => {},
106105
images: () => {},
107106
fonts: () => {},
108107
sass: () => {},
109-
scss: () => {},
110108
less: () => {},
111109
stylus: () => {},
112110
vue: () => {},
113111
eslint: () => {},
114112
typescript: () => {},
115-
ts: () => {},
116113
handlebars: () => {},
117114
};
118115

@@ -701,8 +698,19 @@ class WebpackConfig {
701698
configureLoaderRule(name, callback) {
702699
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.');
703700

701+
// Key: alias, Value: existing loader in `this.loaderConfigurationCallbacks`
702+
const aliases = {
703+
js: 'javascript',
704+
ts: 'typescript',
705+
scss: 'sass',
706+
};
707+
708+
if (name in aliases) {
709+
name = aliases[name];
710+
}
711+
704712
if (!(name in this.loaderConfigurationCallbacks)) {
705-
throw new Error(`Loader "${name}" is not configurable. Valid loaders are "${Object.keys(this.loaderConfigurationCallbacks).join('", "')}".`);
713+
throw new Error(`Loader "${name}" is not configurable. Valid loaders are "${Object.keys(this.loaderConfigurationCallbacks).join('", "')}" and the aliases "${Object.keys(aliases).join('", "')}".`);
706714
}
707715

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

lib/config-generator.js

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

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

231-
const applyRuleConfigurationCallbacks = (names, defaultRules) => {
232-
let rules = defaultRules;
233-
234-
names.forEach(name => {
235-
rules = applyRuleConfigurationCallback(name, rules);
236-
});
237-
238-
return rules;
239-
};
240-
241227
let rules = [
242-
applyRuleConfigurationCallbacks(['javascript', 'js'], {
228+
applyRuleConfigurationCallback('javascript', {
243229
// match .js and .jsx
244230
test: /\.jsx?$/,
245231
exclude: this.webpackConfig.babelOptions.exclude,
@@ -322,7 +308,7 @@ class ConfigGenerator {
322308
}
323309

324310
if (this.webpackConfig.useSassLoader) {
325-
rules.push(applyRuleConfigurationCallbacks(['sass', 'scss'], {
311+
rules.push(applyRuleConfigurationCallback('sass', {
326312
test: /\.s[ac]ss$/,
327313
use: cssExtractLoaderUtil.prependLoaders(this.webpackConfig, sassLoaderUtil.getLoaders(this.webpackConfig))
328314
}));
@@ -360,7 +346,7 @@ class ConfigGenerator {
360346
}
361347

362348
if (this.webpackConfig.useTypeScriptLoader) {
363-
rules.push(applyRuleConfigurationCallbacks(['typescript', 'ts'], {
349+
rules.push(applyRuleConfigurationCallback('typescript', {
364350
test: /\.tsx?$/,
365351
exclude: /node_modules/,
366352
use: tsLoaderUtil.getLoaders(this.webpackConfig)

test/WebpackConfig.js

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

11011101
expect(() => {
11021102
config.configureLoaderRule('reason');
1103-
}).to.throw('Loader "reason" is not configurable. Valid loaders are "javascript", "js", "css", "images", "fonts", "sass", "scss", "less", "stylus", "vue", "eslint", "typescript", "ts", "handlebars".');
1103+
}).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".');
11041104
});
11051105

11061106
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
@@ -986,11 +986,23 @@ describe('The config-generator function', () => {
986986
config.enableSingleRuntimeChunk();
987987
});
988988

989-
it('configure rule for "javascript and "js"', () => {
989+
it('configure rule for "javascript"', () => {
990990
config.configureLoaderRule('javascript', (loaderRule) => {
991991
loaderRule.test = /\.m?js$/;
992+
loaderRule.use[0].options.fooBar = 'fooBar';
992993
});
994+
995+
const webpackConfig = configGenerator(config);
996+
const rule = findRule(/\.m?js$/, webpackConfig.module.rules);
997+
998+
expect('file.js').to.match(rule.test);
999+
expect('file.mjs').to.match(rule.test);
1000+
expect(rule.use[0].options.fooBar).to.equal('fooBar');
1001+
});
1002+
1003+
it('configure rule for the alias "js"', () => {
9931004
config.configureLoaderRule('js', (loaderRule) => {
1005+
loaderRule.test = /\.m?js$/;
9941006
loaderRule.use[0].options.fooBar = 'fooBar';
9951007
});
9961008

@@ -1035,20 +1047,28 @@ describe('The config-generator function', () => {
10351047
expect(rule.options.name).to.equal('dirname-fonts/[hash:42].[ext]');
10361048
});
10371049

1038-
it('configure rule for "sass" and "scss"', () => {
1050+
it('configure rule for "sass"', () => {
10391051
config.enableSassLoader();
10401052
config.configureLoaderRule('sass', (loaderRule) => {
1041-
loaderRule.use.push('Option pushed when configuring Sass.');
1053+
loaderRule.use[2].options.fooBar = 'fooBar';
10421054
});
1055+
1056+
const webpackConfig = configGenerator(config);
1057+
const rule = findRule(/\.s[ac]ss$/, webpackConfig.module.rules);
1058+
1059+
expect(rule.use[2].options.fooBar).to.equal('fooBar');
1060+
});
1061+
1062+
it('configure rule for the alias "scss"', () => {
1063+
config.enableSassLoader();
10431064
config.configureLoaderRule('scss', (loaderRule) => {
1044-
loaderRule.use.push('Option pushed when configuring SCSS.');
1065+
loaderRule.use[2].options.fooBar = 'fooBar';
10451066
});
10461067

10471068
const webpackConfig = configGenerator(config);
10481069
const rule = findRule(/\.s[ac]ss$/, webpackConfig.module.rules);
10491070

1050-
expect(rule.use.pop()).to.equal('Option pushed when configuring SCSS.');
1051-
expect(rule.use.pop()).to.equal('Option pushed when configuring Sass.');
1071+
expect(rule.use[2].options.fooBar).to.equal('fooBar');
10521072
});
10531073

10541074
it('configure rule for "less"', () => {
@@ -1120,16 +1140,27 @@ describe('The config-generator function', () => {
11201140
config.configureLoaderRule('typescript', (loaderRule) => {
11211141
loaderRule.use[1].options.happyPackMode = true;
11221142
});
1143+
1144+
const webpackConfig = configGenerator(config);
1145+
const rule = findRule(/\.tsx?$/, webpackConfig.module.rules);
1146+
1147+
expect(rule.use[1].options.silent).to.be.true;
1148+
expect(rule.use[1].options.happyPackMode).to.be.true;
1149+
});
1150+
1151+
it('configure rule for the alias "ts"', () => {
1152+
config.enableTypeScriptLoader((options) => {
1153+
options.silent = true;
1154+
});
11231155
config.configureLoaderRule('ts', (loaderRule) => {
1124-
loaderRule.use[1].options.logInfoToStdOut = true;
1156+
loaderRule.use[1].options.happyPackMode = true;
11251157
});
11261158

11271159
const webpackConfig = configGenerator(config);
11281160
const rule = findRule(/\.tsx?$/, webpackConfig.module.rules);
11291161

11301162
expect(rule.use[1].options.silent).to.be.true;
11311163
expect(rule.use[1].options.happyPackMode).to.be.true;
1132-
expect(rule.use[1].options.logInfoToStdOut).to.be.true;
11331164
});
11341165

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

0 commit comments

Comments
 (0)