forked from surveyjs/survey-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
245 lines (231 loc) · 8.22 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
'use strict';
var webpack = require('webpack');
var path = require('path');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var dts = require('dts-bundle');
var rimraf = require('rimraf');
var GenerateJsonPlugin = require('generate-json-webpack-plugin');
var packageJson = require('./package.json');
var fs = require('fs');
var banner = [
"surveyjs - Survey JavaScript library v" + packageJson.version,
"Copyright (c) 2015-2017 Devsoft Baltic OÜ - http://surveyjs.org/",
"License: MIT (http://www.opensource.org/licenses/mit-license.php)",
].join("\n");
// TODO add to dts_bundler
var dts_banner = ["Type definitions for Survey JavaScript library v" + packageJson.version,
"Project: http://surveyjs.org/",
"Definitions by: Devsoft Baltic OÜ <https://github.com/surveyjs/>",
""].join("\n");
var platformOptions = {
'react': {
externals: {
'react': {
root: 'React',
commonjs2: 'react',
commonjs: 'react',
amd: 'react'
},
'react-dom': {
root: 'ReactDOM',
commonjs2: 'react-dom',
commonjs: 'react-dom',
amd: 'react-dom'
}
},
keywords: ['react', 'react-component'],
dependencies: { 'react': '^15.0.1', 'react-dom': '^15.0.1' }
},
'knockout': {
externals: {
'knockout': {
root: 'ko',
commonjs2: 'knockout',
commonjs: 'knockout',
amd: 'knockout'
}
},
keywords: ['knockout'],
dependencies: {'knockout': '^3.4.0'}
},
'jquery': {
externals: {
'jquery': {
root: 'jQuery',
commonjs2: 'jquery',
commonjs: 'jquery',
amd: 'jquery'
}
},
keywords: ['jquery', 'jquery-plugin'],
dependencies: { 'jquery': '>=1.12.4', '@types/react': '15.0.21' }
},
'angular': {
externals: {},
keywords: ['angular', 'angular-component'],
dependencies: { '@types/react': '15.0.21' }
},
'vue': {
externals: {
'vue': {
root: 'Vue',
commonjs2: 'vue',
commonjs: 'vue',
amd: 'vue'
}
},
keywords: ['vue'],
dependencies: { 'vue': "^2.1.10" }
}
};
module.exports = function (options) {
//TODO
options.platformPrefix = options.platform == 'knockout' ? 'ko' : options.platform;
var packagePath = './packages/survey-' + options.platform + '/';
var extractCSS = new ExtractTextPlugin({ filename: packagePath + 'survey.css' });
var percentage_handler = function handler(percentage, msg) {
if ( 0 === percentage ) {
console.log('Build started... good luck!');
} else if ( 1 === percentage ) {
if (options.buildType === "prod") {
dts.bundle({
name: '../../survey.' + options.platformPrefix,
main: packagePath + 'typings/entries/' + options.platform + '.d.ts',
outputAsModuleFolder: true,
headerText: dts_banner
});
rimraf.sync(packagePath + 'typings');
fs.createReadStream('./npmREADME.md').pipe(fs.createWriteStream(packagePath + 'README.md'));
}
//TODO someday need to remove
if (options.platform === "knockout") {
if (options.buildType === "prod") {
fs.rename('./packages/survey-knockout/survey.knockout.min.js', './packages/survey-knockout/survey.ko.min.js');
} else {
fs.rename('./packages/survey-knockout/survey.knockout.js', './packages/survey-knockout/survey.ko.js');
}
}
}
};
var packagePlatformJson = {
'name': 'survey-' + options.platform,
'version': packageJson.version,
'description': 'survey.js is a JavaScript Survey Library. It is a modern way to add a survey to your website. It uses JSON for survey metadata and results.',
'keywords': [
'Survey',
'JavaScript',
'Bootstrap',
'Library'
].concat(platformOptions[options.platform].keywords),
'homepage': 'https://surveyjs.org/',
'license': 'MIT',
'files': [
'survey.css',
'survey.' + options.platformPrefix + '.d.ts',
'survey.' + options.platformPrefix + '.js',
'survey.' + options.platformPrefix + '.min.js'
],
'main': 'survey.' + options.platformPrefix + '.min.js',
'repository': {
'type': 'git',
'url': 'https://github.com/surveyjs/surveyjs.git'
},
'typings': 'survey.' + options.platformPrefix + '.d.ts',
'dependencies': platformOptions[options.platform].dependencies
};
var config = {
entry: {},
resolve: {
extensions: ['.ts', '.js', '.tsx', '.scss'],
alias: {
tslib: path.join(__dirname, './src/entries/chunks/helpers.ts'),
}
},
module: {
rules: [
{
test: /\.(ts|tsx)$/,
loader: 'ts-loader',
options: {
compilerOptions: {
'declaration': options.buildType === 'prod',
'outDir': packagePath + 'typings/'
},
appendTsSuffixTo: [/\.vue$/]
}
},
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
esModule: true
}
},
{
test: /\.scss$/,
loader: extractCSS.extract({
fallbackLoader: 'style-loader',
loader: 'css-loader!sass-loader'
})
},
{
test: /\.html$/,
loader: 'html-loader'
}
]
},
output: {
filename: packagePath + '[name]' + (options.buildType === 'prod' ? '.min': '') + '.js',
library: 'Survey',
libraryTarget: 'umd',
umdNamedDefine: true
},
externals: platformOptions[options.platform].externals,
plugins: [
new webpack.ProgressPlugin(percentage_handler),
new webpack.DefinePlugin({
"process.env.ENVIRONMENT": JSON.stringify(options.buildType),
"process.env.VERSION": JSON.stringify(packageJson.version)
}),
extractCSS
],
devtool: 'inline-source-map'
};
if (options.platform === 'angular' || options.platform === 'jquery') {
config.resolve.alias['react'] = 'preact-compat';
config.resolve.alias['react-dom'] = 'preact-compat';
// TODO because of preact-compat https://github.com/developit/preact-compat/issues/192 need to better decision
config.module.rules.push({
loader: 'babel-loader',
include: [
path.join(__dirname, './node_modules/preact-compat/src'),
],
options: {
presets: [
['latest', { modules: false }],
],
},
});
// EO TODO
}
if (options.buildType === 'prod') {
config.devtool = false;
config.plugins = config.plugins.concat([
new webpack.optimize.UglifyJsPlugin(),
new webpack.BannerPlugin(banner),
new GenerateJsonPlugin(
packagePath + 'package.json',
packagePlatformJson,
undefined,
2
)
]);
}
if (options.buildType === 'dev') {
config.plugins = config.plugins.concat([
new webpack.LoaderOptionsPlugin({ debug: true})
]);
}
config.entry['survey.' + options.platform] = path.resolve(__dirname, './src/entries/' + options.platform);
return config;
};