-
Notifications
You must be signed in to change notification settings - Fork 2
/
webpack.config.js
204 lines (191 loc) · 5.24 KB
/
webpack.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
'use strict';
const fs = require('fs');
const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const transformMdcToLmdc = require('./scripts/transform-mdc-to-lmdc');
// Used with webpack-dev-server
const PUBLIC_PATH = '/assets/';
const IS_DEV = process.env.MDC_ENV === 'development' || process.env.MDC_ENV === 'test';
const IS_DEMOS = process.env.MDC_ENV === 'demos';
const IS_TEST = process.env.MDC_ENV === 'test';
const IS_PROD = process.env.MDC_ENV === 'production' || process.env.MDC_ENV === 'demos';
const OUT_PATH = IS_DEMOS ? path.resolve('./demos/assets') : path.resolve('./dist');
const PACKAGE_NAME = 'angularjs-mdc';
const packages = fs.readdirSync('./src');
const experimentalPackages = fs.readdirSync('./src/experimental');
const CSS_LOADER_CONFIG = [
{
loader: 'css-loader',
options: {
sourceMap: IS_DEV,
},
},
{
loader: 'postcss-loader',
options: {
sourceMap: IS_DEV,
plugins: () =>[
require('autoprefixer')({grid: false}),
transformMdcToLmdc(),
],
},
},
{
loader: 'sass-loader',
options: {
sourceMap: IS_DEV,
includePaths: ['node_modules', 'node_modules/@material/*'],
},
},
];
const DEV_SERVER_CONFIG = {
disableHostCheck: true,
contentBase: [path.join(__dirname, 'demos'), path.join(__dirname, 'node_modules')],
};
module.exports = [{
name: 'js-all',
entry: path.resolve('./src/mdc.js'),
output: {
path: OUT_PATH,
publicPath: PUBLIC_PATH,
filename: PACKAGE_NAME + '.' + (IS_PROD ? 'min.' : '') + 'js',
libraryTarget: 'umd',
library: PACKAGE_NAME,
},
devServer: DEV_SERVER_CONFIG,
devtool: (IS_DEV && !IS_TEST) ? 'source-map' : false,
module: {
rules: [{
test: /\.js$/,
exclude: /node_modules\/(!@material)/,
loader: 'babel-loader',
options: {
cacheDirectory: true,
},
}, {
test: /\.html$/,
use: [{
loader: 'html-loader',
options: {
minimize: IS_PROD,
},
}],
}],
},
}];
const EXPERIMENTAL_NAME = `${PACKAGE_NAME}-experimental`;
module.exports.push(Object.assign({}, module.exports[0], {
name: 'experimental',
entry: path.resolve('./src/experimental/index.js'),
output: {
path: OUT_PATH,
publicPath: PUBLIC_PATH,
filename: EXPERIMENTAL_NAME + '.' + (IS_PROD ? 'min.' : '') + 'js',
libraryTarget: 'umd',
library: EXPERIMENTAL_NAME,
},
}));
packages.forEach((packageName) => {
const jsPath = path.resolve(`./src/${packageName}/${packageName}.js`);
const hasJs = fs.existsSync(jsPath);
if (!hasJs) {
return;
}
module.exports.push(Object.assign({}, module.exports[0], {
name: packageName,
entry: jsPath,
output: {
path: OUT_PATH,
publicPath: PUBLIC_PATH,
filename: packageName + '.' + (IS_PROD ? 'min.' : '') + 'js',
libraryTarget: 'umd',
library: packageName,
},
}));
});
experimentalPackages.forEach((packageName) => {
const outputName = `${packageName}-experimental`;
const jsPath = path.resolve(`./src/experimental/${packageName}/index.js`);
const hasJs = fs.existsSync(jsPath);
if (!hasJs) {
return;
}
module.exports.push(Object.assign({}, module.exports[0], {
name: outputName,
entry: jsPath,
output: {
path: OUT_PATH,
publicPath: PUBLIC_PATH,
filename: outputName + '.' + (IS_PROD ? 'min.' : '') + 'js',
libraryTarget: 'umd',
library: outputName,
},
}));
});
const CSS_EXPORT = {
name: 'scss',
entry: {},
output: {
path: OUT_PATH,
publicPath: PUBLIC_PATH,
filename: '[name].' + (IS_PROD ? 'min.' : '') + 'css',
},
devServer: DEV_SERVER_CONFIG,
devtool: IS_DEV ? 'source-map' : false,
module: {
rules: [{
test: /\.scss$/,
use: IS_DEV ? [{loader: 'style-loader'}].concat(CSS_LOADER_CONFIG) : ExtractTextPlugin.extract({
fallback: 'style-loader',
use: CSS_LOADER_CONFIG,
}),
}],
},
plugins: [
new ExtractTextPlugin('[name].' + (IS_PROD ? 'min.' : '') + 'css'),
],
};
CSS_EXPORT['entry'][PACKAGE_NAME] = path.resolve('./src/mdc.scss');
CSS_EXPORT['entry'][EXPERIMENTAL_NAME] = path.resolve('./src/experimental/mdc.scss');
packages.forEach((packageName) => {
const scssPath = path.resolve(`./src/${packageName}/${packageName}.scss`);
const hasScss = fs.existsSync(scssPath);
if (!hasScss) {
return;
}
CSS_EXPORT.entry[packageName] = scssPath;
});
experimentalPackages.forEach((packageName) => {
const scssPath = path.resolve(`./src/experimental/${packageName}/${packageName}.scss`);
const hasScss = fs.existsSync(scssPath);
if (!hasScss) {
return;
}
CSS_EXPORT.entry[`${packageName}-experimental`] = scssPath;
});
module.exports.push(CSS_EXPORT);
const DEMO_CSS_EXPORT = {
name: 'demo-css',
entry: {
'demo-styles': path.resolve('./demos/all.scss'),
},
output: {
path: OUT_PATH,
publicPath: PUBLIC_PATH,
filename: '[name].css.js',
},
devServer: DEV_SERVER_CONFIG,
devtool: 'source-map',
module: {
rules: [{
test: /\.scss$/,
use: [{loader: 'style-loader'}].concat(CSS_LOADER_CONFIG),
}],
},
};
if (IS_DEMOS) {
// just build the demo CSS
module.exports = [DEMO_CSS_EXPORT];
} else if (IS_DEV) {
module.exports.push(DEMO_CSS_EXPORT);
}