redoc/webpack.config.ts

142 lines
3.5 KiB
TypeScript
Raw Normal View History

2017-11-20 00:26:41 +03:00
import * as webpack from 'webpack';
2017-11-19 23:36:36 +03:00
import * as HtmlWebpackPlugin from 'html-webpack-plugin';
2017-11-20 01:59:28 +03:00
const nodeExternals = require('webpack-node-externals')({
// bundle in moudules that need transpiling + non-js (e.g. css)
2017-12-07 14:52:32 +03:00
whitelist: ['swagger2openapi', /reftools/, /\.(?!(?:jsx?|json)$).{1,5}$/i],
2017-11-20 01:59:28 +03:00
});
2017-10-12 00:01:37 +03:00
const VERSION = JSON.stringify(require('./package.json').version);
const REVISION = JSON.stringify(
require('child_process')
.execSync('git rev-parse --short HEAD')
.toString()
.trim(),
);
2017-11-19 23:36:36 +03:00
export default env => {
2017-10-12 00:01:37 +03:00
env = env || {};
let entry;
2017-11-19 23:11:40 +03:00
2017-11-20 00:59:25 +03:00
if (env.lib) {
2017-11-19 23:11:40 +03:00
entry = env.standalone ? ['./src/polyfills.ts', './src/standalone.tsx'] : './src/index.ts';
} else {
2017-11-19 23:11:40 +03:00
// playground or performance test
entry = env.perf
2018-01-29 14:09:38 +03:00
? ['./benchmark/index.tsx'] // perf test
2017-11-19 23:11:40 +03:00
: [
// playground
2017-11-20 13:06:10 +03:00
'./src/polyfills.ts',
2017-11-19 23:11:40 +03:00
'react-dev-utils/webpackHotDevClient',
'react-hot-loader/patch',
'./demo/playground/hmr-playground.tsx',
];
}
2017-11-19 23:36:36 +03:00
const config: webpack.Configuration = {
entry: entry,
2017-10-12 00:01:37 +03:00
output: {
2017-11-19 22:27:44 +03:00
filename: env.standalone ? 'redoc.standalone.js' : 'redoc.lib.js',
2017-11-20 00:59:25 +03:00
path: __dirname + (env.lib ? '/bundles' : 'lib'),
2017-10-12 00:01:37 +03:00
},
devServer: {
contentBase: __dirname + '/demo',
2017-11-20 13:06:10 +03:00
host: '0.0.0.0',
2017-10-12 00:01:37 +03:00
watchContentBase: true,
port: 9090,
stats: 'errors-only',
},
devtool: 'source-map',
resolve: {
extensions: ['.ts', '.tsx', '.js', '.json'],
},
2017-11-15 00:40:38 +03:00
node: {
fs: 'empty',
},
2017-10-12 00:01:37 +03:00
externals: {
esprima: 'esprima',
2017-11-20 13:06:10 +03:00
'node-fetch': 'null',
2017-10-12 00:01:37 +03:00
},
module: {
rules: [
{
test: /\.tsx?$/,
2017-11-20 00:26:41 +03:00
use: [
'react-hot-loader/webpack',
{
loader: 'awesome-typescript-loader',
options: {
2018-01-29 14:09:38 +03:00
module: env.perf ? 'esnext' : 'es2015',
2017-11-20 00:26:41 +03:00
},
},
],
2017-11-15 00:40:38 +03:00
exclude: ['node_modules'],
},
{
test: /node_modules\/(swagger2openapi|reftools)\/.*\.js$/,
use: {
loader: 'awesome-typescript-loader',
options: {
transpileOnly: true,
allowJs: true,
instance: 'ts2js-transpiler-only',
},
},
2017-10-12 00:01:37 +03:00
},
{
test: /\.css$/,
use: {
loader: 'css-loader',
options: {
sourceMap: true,
minimize: true,
2017-11-20 02:00:08 +03:00
},
},
2017-10-12 00:01:37 +03:00
},
2017-11-15 17:33:02 +03:00
{ enforce: 'pre', test: /\.js$/, loader: 'source-map-loader' },
2017-10-12 00:01:37 +03:00
],
},
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV': env.prod ? '"production"' : '"development"',
__REDOC_VERSION__: VERSION,
__REDOC_REVISION__: REVISION,
2017-10-12 00:01:37 +03:00
__DEV__: env.prod ? 'false' : 'true',
}),
new webpack.NamedModulesPlugin(),
],
};
2017-11-19 22:27:44 +03:00
2017-10-12 00:01:37 +03:00
if (env.prod) {
2017-11-20 00:26:41 +03:00
config.plugins!.push(new webpack.optimize.ModuleConcatenationPlugin());
2017-10-12 00:01:37 +03:00
}
2017-11-19 22:27:44 +03:00
2017-11-19 23:11:40 +03:00
if (env.lib) {
2017-11-20 00:26:41 +03:00
config.output!.library = 'Redoc';
config.output!.libraryTarget = 'umd';
2017-11-20 00:59:25 +03:00
if (!env.standalone) {
2017-11-20 01:59:28 +03:00
config.externals = (context, request, callback) => {
// ignore node-fetch dep of swagger2openapi as it is not used
if (/node-fetch$/i.test(request)) return callback(null, 'var undefined');
2017-11-20 01:59:28 +03:00
return nodeExternals(context, request, callback);
};
2017-11-20 00:59:25 +03:00
}
2017-11-19 23:11:40 +03:00
} else {
2017-11-20 00:26:41 +03:00
config.plugins!.push(
2017-11-19 23:11:40 +03:00
new HtmlWebpackPlugin({
2018-01-29 14:09:38 +03:00
template: env.perf ? './benchmark/index.html' : './demo/playground/index.html',
2017-11-19 23:11:40 +03:00
}),
);
2017-11-19 22:27:44 +03:00
}
2017-11-19 23:36:36 +03:00
2017-10-12 00:01:37 +03:00
return config;
};