This repository has been archived by the owner on Jul 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
webpack.config.ts
214 lines (205 loc) · 5.57 KB
/
webpack.config.ts
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
205
206
207
208
209
210
211
212
213
214
import * as webpack from 'webpack';
import { existsSync } from 'fs';
import { resolve, join, sep, isAbsolute } from 'path';
import HtmlWebpackPlugin = require('html-webpack-plugin');
// loaders
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer-sunburst');
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const postcssImport = require('postcss-import');
const postcssCssNext = require('postcss-cssnext');
// plugins
import CoreLoadPlugin from '@dojo/cli-build-webpack/plugins/CoreLoadPlugin';
const CopyWebpackPlugin = require('copy-webpack-plugin');
const { IgnorePlugin, NormalModuleReplacementPlugin, ContextReplacementPlugin, optimize: { UglifyJsPlugin } } = webpack;
function isRelative(id: string): boolean {
const first = id.charAt(0);
return first !== '/' && first !== '@' && /^\W/.test(id);
}
const basePath = __dirname;
const localIdentName = '[hash:base64:8]';
const cssLoader = ExtractTextPlugin.extract({
use: 'css-loader?sourceMap!resolve-url-loader'
});
const cssModuleLoader = ExtractTextPlugin.extract({
use: [
'@dojo/webpack-contrib/css-module-decorator-loader',
`css-loader?modules&sourceMap&importLoaders=1&localIdentName=${localIdentName}!resolve-url-loader`,
{
loader: 'postcss-loader?sourceMap',
options: {
plugins: [
postcssImport,
postcssCssNext({
features: {
autoprefixer: {
browsers: ['last 2 versions', 'ie >= 10']
}
}
})
]
}
}
]
});
const webpackConfig = (env: any = {}, args: any) => {
return {
entry: {
'main': './src/index.ts',
'support/providers/amdRequire': './src/support/providers/amdRequire.ts',
'support/worker-proxy': './src/support/worker-proxy.ts'
},
output: {
libraryTarget: 'umd',
filename: '[name].js',
path: resolve(__dirname, 'dist')
},
context: basePath,
module: {
rules: [
{
test: /\.tsx?$/,
enforce: 'pre',
loader: 'tslint-loader',
options: {
tsConfigFile: resolve(__dirname, 'tslint.json')
}
},
{
test: /@dojo\/.*\.js$/,
enforce: 'pre',
loader: 'source-map-loader-cli',
options: { includeModulePaths: true }
},
{
test: /src[\\\/].*\.ts?$/,
enforce: 'pre',
loader: '@dojo/webpack-contrib/css-module-dts-loader?type=ts&instanceName=0_dojo'
},
{
test: /src[\\\/].*\.m\.css?$/,
enforce: 'pre',
loader: '@dojo/webpack-contrib/css-module-dts-loader?type=css'
},
{
test: /src[\\\/].*\.ts(x)?$/,
use: [
'umd-compat-loader',
{
loader: 'ts-loader',
options: { instance: 'dojo' }
}
]
},
{
test: /\.js?$/,
loader: 'umd-compat-loader'
},
{
test: new RegExp(`globalize(\\${sep}|$)`),
loader: 'imports-loader?define=>false'
},
{
test: /.*\.(gif|png|jpe?g|svg|eot|ttf|woff|woff2)$/i,
loader: 'file-loader?hash=sha512&digest=hex&name=[hash:base64:8].[ext]'
},
{
test: /\.css$/, exclude: /src[\\\/].*/,
loader: cssLoader
},
{
test: /src[\\\/].*\.css?$/,
loader: cssModuleLoader
},
{
test: /\.m\.css\.js$/,
exclude: /src[\\\/].*/,
use: ['json-css-module-loader']
},
{
test: /tests[\\\/].*\.ts?$/,
use: [
'umd-compat-loader',
{
loader: 'ts-loader',
options: { instance: 'dojo' }
}
]
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: 'src/index.html',
excludeChunks: [
'support/providers/amdRequire',
'support/worker-proxy'
],
inject: 'body'
}),
new NormalModuleReplacementPlugin(/\.m\.css$/, (result: any) => {
if (isAbsolute(result.request)) {
return;
}
const requestFileName = isRelative(result.request) ?
resolve(result.context, result.request) : resolve(basePath, 'node_modules', result.request);
const jsFileName = requestFileName + '.js';
if (existsSync(jsFileName)) {
result.request = result.request.replace(/\.m\.css$/, '.m.css.js');
}
}),
...(() => {
let distPlugins: any[] = [];
if (env.dist) {
console.log('\n\n\nDIST\n\n\n');
distPlugins = [
new UglifyJsPlugin({
sourceMap: true,
compress: { warnings: false },
exclude: /tests[/]/
}),
new OptimizeCssAssetsPlugin({
cssProcessorOptions: { map: { inline: false } }
}),
new BundleAnalyzerPlugin({
analyzerMode: 'static',
openAnalyzer: false,
reportType: 'sunburst'
})
];
}
return distPlugins;
})(),
new IgnorePlugin(/request\/providers\/node/),
new ContextReplacementPlugin(/dojo-app[\\\/]lib/, { test: () => false }),
new ContextReplacementPlugin(/.*/, { test: () => false }),
new ExtractTextPlugin({ filename: 'main.css', allChunks: true }),
new CopyWebpackPlugin([
{ context: 'src', from: '**/*', ignore: '*.ts' },
{ from: resolve(__dirname, 'projects'), to: 'projects' },
{ from: resolve(__dirname, 'data'), to: 'data' },
{ from: resolve(__dirname, 'extensions'), to: 'extensions' },
{ from: resolve(__dirname, 'node_modules/monaco-editor/min/vs'), to: 'vs' }
]),
new CoreLoadPlugin({ basePath })
],
node: {
dgram: 'empty',
net: 'empty',
tls: 'empty',
fs: 'empty'
},
resolveLoader: {
modules: [
resolve(__dirname, 'node_modules/@dojo/cli-build-webpack/loaders'),
join(__dirname, 'node_modules')
],
extensions: [ '.ts', '.js' ]
},
resolve: {
extensions: ['.ts', '.js']
},
devtool: 'cheap-module-eval-source-map'
};
};
export default webpackConfig;