Skip to content

Commit f8e0c39

Browse files
committed
chore(webpack): refactor extensions
1 parent 19fefad commit f8e0c39

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

webpack.config.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ module.exports = {
4141

4242
resolve: {
4343
// ensure loader extensions match
44-
extensions: ['.ts','.js','.json','.css','.html'].reduce(function(memo, val) {
45-
return memo.concat('.async' + val, val); // ensure .async also works
46-
}, [''])
44+
extensions: prepend(['.ts','.js','.json','.css','.html'], '.async') // ensure .async.ts etc also works
4745
},
4846

4947
module: {
@@ -113,6 +111,15 @@ function root(args) {
113111
return path.join.apply(path, [__dirname].concat(args));
114112
}
115113

114+
function prepend(extensions, args) {
115+
args = args || [];
116+
if (!Array.isArray(args)) { args = [args] }
117+
return extensions.reduce(function(memo, val) {
118+
return memo.concat(val, args.map(function(prefix) {
119+
return prefix + val
120+
}));
121+
}, ['']);
122+
}
116123
function rootNode(args) {
117124
args = Array.prototype.slice.call(arguments, 0);
118125
return root.apply(path, ['node_modules'].concat(args));

webpack.prod.config.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ module.exports = {
5555
resolve: {
5656
cache: false,
5757
// ensure loader extensions match
58-
extensions: ['.ts','.js','.json','.css','.html'].reduce(function(memo, val) {
59-
return memo.concat('.async' + val, val); // ensure .async also works
60-
}, [''])
58+
extensions: prepend(['.ts','.js','.json','.css','.html'], '.async') // ensure .async.ts etc also works
6159
},
6260

6361
module: {
@@ -190,6 +188,9 @@ module.exports = {
190188
};
191189

192190
// Helper functions
191+
function gzipMaxLevel(buffer, callback) {
192+
return zlib['gzip'](buffer, {level: 9}, callback)
193+
}
193194

194195
function root(args) {
195196
args = Array.prototype.slice.call(arguments, 0);
@@ -201,6 +202,12 @@ function rootNode(args) {
201202
return root.apply(path, ['node_modules'].concat(args));
202203
}
203204

204-
function gzipMaxLevel(buffer, callback) {
205-
return zlib['gzip'](buffer, {level: 9}, callback)
205+
function prepend(extensions, args) {
206+
args = args || [];
207+
if (!Array.isArray(args)) { args = [args] }
208+
return extensions.reduce(function(memo, val) {
209+
return memo.concat(val, args.map(function(prefix) {
210+
return prefix + val
211+
}));
212+
}, ['']);
206213
}

webpack.test.config.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ var ENV = process.env.ENV = process.env.NODE_ENV = 'test';
1515
module.exports = {
1616
resolve: {
1717
cache: false,
18-
extensions: ['.ts','.js','.json','.css','.html'].reduce(function(memo, val) {
19-
return memo.concat('.async' + val, val); // ensure .async also works
20-
}, [''])
18+
extensions: prepend(['.ts','.js','.json','.css','.html'], '.async') // ensure .async.ts etc also works
2119
},
2220
devtool: 'inline-source-map',
2321
module: {
@@ -117,3 +115,13 @@ function rootNode(args) {
117115
args = Array.prototype.slice.call(arguments, 0);
118116
return root.apply(path, ['node_modules'].concat(args));
119117
}
118+
119+
function prepend(extensions, args) {
120+
args = args || [];
121+
if (!Array.isArray(args)) { args = [args] }
122+
return extensions.reduce(function(memo, val) {
123+
return memo.concat(val, args.map(function(prefix) {
124+
return prefix + val
125+
}));
126+
}, ['']);
127+
}

0 commit comments

Comments
 (0)