forked from microsoft/FluidFramework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.prod.js
More file actions
195 lines (174 loc) · 6.75 KB
/
Copy pathwebpack.prod.js
File metadata and controls
195 lines (174 loc) · 6.75 KB
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
/*!
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
* Licensed under the MIT License.
*/
const HtmlWebpackPlugin = require('html-webpack-plugin')
const common = require('./webpack.common.js');
const fs = require('fs');
const path = require('path');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const { merge } = require('webpack-merge');
const _ = require('underscore');
const baseConfig = require('./webpack.base.js');
const getCommandLineArgs = function () {
const params = {};
const validParams = [
'--name=', '--fileType=', '--clearDist=', '--dir=', '--distPath=', '--distTypesPath', '--tsConfigFile'
];
const extractParams = args => {
for (let param of args) {
for (let p of validParams) {
if (param.startsWith(p)) {
const separatorIndex = param.indexOf('=');
const paramName = param.substring(2, separatorIndex);
const paramValue = param.substring(separatorIndex + 1);
params[paramName] = paramValue;
break;
}
}
}
};
extractParams(process.argv);
return params;
};
const getParams = params => {
const cmdLineParams = getCommandLineArgs();
const result = _.extend({}, params, cmdLineParams);
if (result.name === undefined) {
throw new Error('Webpack config: "name" not given.');
}
if (!result.fileType) {
throw new Error('Webpack config: "fileType" not defined.');
}
if (!result.merge || typeof result.merge !== 'object') {
result.merge = {};
}
if (!result.clearDist && result.clearDist !== false) {
result.clearDist = true;
}
if (!path.isAbsolute(result.distPath)) {
result.distPath = path.resolve(result.distPath);
}
if (result.distTypesPath === '') {
result.generateDeclarations = false;
} else {
result.generateDeclarations = true;
}
if (result.distTypesPath && !path.isAbsolute(result.distTypesPath)) {
result.distTypesPath = path.resolve(result.distTypesPath);
}
if (!path.isAbsolute(result.dir)) {
result.dir = path.resolve(result.dir);
}
if (result.tsConfigFile && !path.isAbsolute(result.tsConfigFile)) {
result.tsConfigFile = path.resolve(result.tsConfigFile);
}
return result;
};
/**
* @typedef AppfwLibraryParameters
* @property {string} name The project's name. Used in generated bundle file names.
* Can also be set on the command line as --name=[name].
* @property {string} fileType The file type of the project. Either 'js' or 'ts'. Can also be set on the command line
* as --fileType=[fileType].
* @property {string} [dir=./] The path that contains the entry point file, either absolute or relative to the
* project root. Can also be set on the command line as --dir=[dir].
* @property {string} [distPath=dist/lib] The path where generated output files should be stored, either absolute or
* relative to the project root. Can also be set on the command line as --distPath=[distPath].
* @property {string} [distTypesPath=dist/types] The path where generated output files should be stored, either
* absolute or relative to the project root. When an empty string is provided, no declaration files will be
* generated. Can also be set on the command line as --distTypesPath=[distTypesPath].
* @property {string} [tsConfigFile] The name of the TypeScript configuration file (tsconfig). Either as an absolute
* path or relative to the project root. Can also be set on the command line as --tsConfigFile=[tsConfigFile].
* @property {object} [merge={}] An object that contains valid webpack configuration options. Will be merged with the
* default options defined in this config.
*/
/**
* The webpack base configuration for Application Framework libraries (e.g. AppComponents).
* @param {AppfwLibraryParameters} args Configuration parameters.
* @return {array<object>} An array of webpack configurations with two entries. The first one is for a non-minified
* development build, the second one is a minified production bundle.
*/
const CommonWebpackLibTSConfig = function (args) {
args = _.extend({ fileType: 'ts' }, baseConfig.commonParams, { distTypesPath: 'dist/types' }, args);
const params = getParams(args);
const commonLibConfig = {
devtool: 'source-map',
output: {
library: params.name,
path: params.distPath
},
externals: {
"@fluid-experimental/property-properties": {
amd: "@fluid-experimental/property-properties",
commonjs: "@fluid-experimental/property-properties",
commonjs2: "@fluid-experimental/property-properties",
root: ["Properties"]
}
},
module: {
rules: [
{
test: /\.[tj]sx?$/,
exclude: [/node_modules/, /\.min\.js$/],
use: {
loader: require.resolve('babel-loader'),
options: {
babelrc: false,
presets: [
[
'@babel/preset-env',
{ targets: { 'ie': 11 }, modules: 'commonjs' }
]
],
plugins: ['@babel/plugin-transform-runtime']
}
}
}
]
}
};
const tsLibConfig = baseConfig.TSConfig(params.fileType, params.generateDeclarations, params.distTypesPath,
params.tsConfigFile);
const libConfig = merge(baseConfig.BaseConfig, commonLibConfig, tsLibConfig, params.merge);
const isFile = fs.statSync(params.dir).isFile();
const entryFile = path.resolve(params.dir, isFile ? '' : './webpack.index.' + params.fileType);
let devEntry = {};
devEntry[params.name] = entryFile;
let prodEntry = {};
prodEntry[params.name + '.min'] = entryFile;
const pathsToClean = [params.distPath];
if (params.fileType === 'ts' && params.generateDeclarations) pathsToClean.push(params.distTypesPath);
return [
merge(libConfig, {
mode: 'development',
entry: devEntry,
plugins: [
new CleanWebpackPlugin({ cleanOnceBeforeBuildPatterns: pathsToClean }, process.cwd())
]
}),
merge(libConfig, {
mode: 'production',
entry: prodEntry
})
];
};
module.exports = (env) => CommonWebpackLibTSConfig({
distPath: path.resolve(__dirname, 'dist', 'lib'),
name: '@fluid-experimental/property-inspector-table',
dir: path.resolve(__dirname, 'src'),
merge: merge(common(env), {
externals: [{
'react': 'react',
"@fluid-experimental/property-binder": "@fluid-experimental/property-binder",
'@fluid-experimental/property-changeset': '@fluid-experimental/property-changeset',
'@fluid-experimental/property-dds':'@fluid-experimental/property-dds',
'@fluid-experimental/property-properties': '@fluid-experimental/property-properties',
'@fluid-experimental/property-proxy': '@fluid-experimental/property-proxy',
}
],
plugins: [
new HtmlWebpackPlugin('styles.css')
]
})
});