1+ 'use strict' ;
2+
3+ var path = require ( 'path' ) ;
4+
5+ var webpack = require ( 'webpack' ) ,
6+ ExtractTextPlugin = require ( 'extract-text-webpack-plugin' ) ,
7+ BowerWebpackPlugin = require ( 'bower-webpack-plugin' ) ,
8+ EntryGeneratorPlugin = require ( 'entry-generator-webpack-plugin' ) ,
9+ OmitTildePlugin = require ( 'omit-tilde-webpack-plugin' ) ;
10+
11+ /**
12+ * Add common features.
13+ * @this {Config} A webpack-configurator instance
14+ * @param {string } loaderRoot The base path in which to locate loaders
15+ * @param {object } globals A hash of globals
16+ * @returns {Config } The given webpack-configurator instance
17+ */
18+ function common ( loaderRoot , globals ) {
19+ /* jshint validthis:true */
20+ return this
21+ . merge ( {
22+ context : process . cwd ( ) ,
23+ cache : true ,
24+ devtool : 'source-map' ,
25+ entry : {
26+ vendor : './app/vendor.js'
27+ } ,
28+ output : {
29+ filename : '[name].[chunkhash].js' ,
30+ chunkFilename : '[name].[chunkhash].js' ,
31+ devtoolModuleFilenameTemplate : '[resource-path]' ,
32+ devtoolFallbackModuleFilenameTemplate : '[resource-path]?[hash]'
33+ } ,
34+ resolve : {
35+ alias : {
36+ npm : path . resolve ( 'node_modules' )
37+ } ,
38+ root : path . resolve ( 'bower_components' ) ,
39+ fallback : path . resolve ( 'node_modules' )
40+ } ,
41+ resolveLoader : {
42+ root : loaderRoot ,
43+ fallback : path . resolve ( 'node_modules' )
44+ } ,
45+ node : {
46+ fs : 'empty'
47+ }
48+ } )
49+
50+ // before compile
51+ . preLoader ( 'linting' , {
52+ test : / \. j s $ / i,
53+ exclude : / [ \\ \/ ] ( n o d e _ m o d u l e s | b o w e r _ c o m p o n e n t s ) [ \\ \/ ] / i,
54+ loader : 'jshint'
55+ } )
56+
57+ // some obscure modules like to 'require()' angular, but bower angular does not export anything
58+ . loader ( 'export-angular' , {
59+ test : / [ \\ \/ ] a n g u l a r \. j s $ / i,
60+ include : / [ \\ \/ ] b o w e r _ c o m p o n e n t s [ \\ \/ ] / i,
61+ loader : 'exports?angular'
62+ } )
63+
64+ // supported file types
65+ . loader ( 'css' , {
66+ test : / \. c s s $ / i,
67+ loader : ExtractTextPlugin . extract ( 'css?minimize&sourceMap!resolve-url?sourceMap' )
68+ } )
69+ . loader ( 'sass' , {
70+ test : / \. s c s s $ / i,
71+ loader : ExtractTextPlugin . extract ( 'css?minimize&sourceMap!resolve-url?sourceMap!sass?sourceMap' )
72+ } )
73+ . loader ( 'image' , {
74+ test : / \. ( j p e ? g | p n g | g i f | s v g ) ( [ # ? ] .* ) ? $ / i,
75+ loaders : [
76+ 'file?hash=sha512&digest=hex&name=[hash].[ext]' ,
77+ 'image-webpack?optimizationLevel=7&interlaced=false'
78+ ]
79+ } )
80+ . loader ( 'woff' , {
81+ test : / \. w o f f 2 ? ( [ # ? ] .* ) ? $ / i,
82+ loader : 'url?limit=10000&mimetype=application/font-woff&name=[hash].[ext]'
83+ } )
84+ . loader ( 'font' , {
85+ test : / \. ( e o t | t t f | i c o | o t f ) ( [ # ? ] .* ) ? $ / i,
86+ loader : 'file?name=[hash].[ext]'
87+ } )
88+ . loader ( 'js-bower' , {
89+ test : / \. j s $ / i,
90+ include : / [ \\ \/ ] b o w e r _ c o m p o n e n t s [ \\ \/ ] / i,
91+ loaders : [
92+ 'sourcemap-sources?format=outputRelative' ,
93+ 'ng-annotate?sourceMap'
94+ ]
95+ } )
96+ . loader ( 'js' , {
97+ test : / \. j s $ / i,
98+ exclude : / [ \\ \/ ] ( b o w e r _ c o m p o n e n t s | w e b p a c k | c s s - l o a d e r ) [ \\ \/ ] / i,
99+ loaders : [
100+ 'sourcemap-sources?format=projectRelative' ,
101+ 'ng-annotate?sourceMap' ,
102+ 'sourcemap-sources?format=absolute' , // fix ng-annotate source maps in Windows but tweaking incoming map
103+ 'nginject?sourceMap&deprecate&singleQuote' ,
104+ 'babel?sourceMap&ignore=buffer&compact=false'
105+ // https://github.com/feross/buffer/issues/79
106+ // http://stackoverflow.com/a/29857361/5535360
107+ ]
108+ } )
109+ . loader ( 'html' , {
110+ test : / \. h t m l ? $ / i,
111+ loader : 'html?removeComments=false&attrs=img:src link:href'
112+ } )
113+ . loader ( 'json' , {
114+ test : / \. j s o n $ / i,
115+ loader : 'json'
116+ } )
117+
118+ // bower
119+ . plugin ( 'generate-vendor' , EntryGeneratorPlugin , [
120+ './app/vendor.js' ,
121+ EntryGeneratorPlugin . bowerDependenciesSource ( )
122+ ] )
123+ . plugin ( 'omit-tilde' , OmitTildePlugin , [ {
124+ include : [ 'package.json' , 'bower.json' ] ,
125+ deprecate : true
126+ } ] )
127+ . plugin ( 'bower' , BowerWebpackPlugin , [ {
128+ includes : / \. ( ( j s | c s s ) | ( w o f f 2 ? | e o t | t t f | o t f ) ( [ # ? ] .* ) ? ) $ / i,
129+ searchResolveModulesDirectories : false
130+ } ] )
131+
132+ // globals
133+ . plugin ( 'provide' , webpack . ProvidePlugin , [ globals ] )
134+
135+ // output, chunking, optimisation
136+ . plugin ( 'extract-text' , ExtractTextPlugin , [
137+ undefined ,
138+ '[name].[contenthash].css' ,
139+ { allChunks : true }
140+ ] )
141+ . plugin ( 'commons' , webpack . optimize . CommonsChunkPlugin , [ {
142+ name : 'vendor' ,
143+ minChunks : Infinity
144+ } ] )
145+ . plugin ( 'dedupe' , webpack . optimize . DedupePlugin )
146+ . plugin ( 'occurence-order' , webpack . optimize . OccurenceOrderPlugin ) ;
147+ }
148+
149+ module . exports = common ;
0 commit comments