redoc/webpack.config.ts

137 lines
3.7 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';
2018-03-17 16:11:52 +03:00
import * as ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';
import * as path from 'path';
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(),
);
const BANNER = `ReDoc - OpenAPI/Swagger-generated API Reference Documentation
2018-03-21 21:08:46 +03:00
-------------------------------------------------------------
Version: ${VERSION}
Repo: https://github.com/Rebilly/ReDoc`;
export default (env: { standalone?: boolean } = {}, { mode }) => ({
2018-03-17 18:58:28 +03:00
entry: env.standalone ? ['./src/polyfills.ts', './src/standalone.tsx'] : './src/index.ts',
output: {
filename: env.standalone ? 'redoc.standalone.js' : 'redoc.lib.js',
path: path.join(__dirname, '/bundles'),
library: 'Redoc',
libraryTarget: 'umd',
globalObject: 'this',
2018-03-17 18:58:28 +03:00
},
devtool: 'source-map',
resolve: {
extensions: ['.ts', '.tsx', '.js', '.json'],
},
node: {
fs: 'empty',
},
performance: false,
optimization: {
minimize: !!env.standalone,
},
externals: env.standalone
? {
esprima: 'esprima',
'node-fetch': 'null',
}
2018-03-17 18:58:28 +03:00
: (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');
if (/esprima$/i.test(request)) return callback(null, 'var undefined');
return nodeExternals(context, request, callback);
},
2017-10-12 00:01:37 +03:00
2018-03-17 18:58:28 +03:00
module: {
rules: [
{
test: /\.tsx?$/,
use: [
{
loader: 'ts-loader',
options: {
compilerOptions: {
module: 'es2015',
},
},
},
{
loader: 'babel-loader',
options: {
plugins: [
'@babel/plugin-syntax-typescript',
['@babel/plugin-syntax-decorators', { legacy: true }],
'@babel/plugin-syntax-jsx',
[
'babel-plugin-styled-components',
{
minify: true,
displayName: mode !== 'production',
},
],
],
},
},
],
2018-03-17 18:58:28 +03:00
exclude: ['node_modules'],
},
{
test: /node_modules\/(swagger2openapi|reftools)\/.*\.js$/,
use: {
loader: 'ts-loader',
options: {
instance: 'ts2js-transpiler-only',
transpileOnly: true,
compilerOptions: {
allowJs: true,
2017-11-15 00:40:38 +03:00
},
},
2017-10-12 00:01:37 +03:00
},
2018-03-17 18:58:28 +03:00
},
{
test: /\.css$/,
use: {
loader: 'css-loader',
options: {
sourceMap: false,
2018-03-17 18:58:28 +03:00
minimize: true,
},
2017-10-12 00:01:37 +03:00
},
2018-03-17 18:58:28 +03:00
},
{ enforce: 'pre', test: /\.js$/, loader: 'source-map-loader' },
2017-10-12 00:01:37 +03:00
],
2018-03-17 18:58:28 +03:00
},
plugins: [
new webpack.DefinePlugin({
__REDOC_VERSION__: VERSION,
__REDOC_REVISION__: REVISION,
}),
2018-03-17 19:50:55 +03:00
new ForkTsCheckerWebpackPlugin({ silent: true }),
new webpack.BannerPlugin(BANNER),
ignore(/js-yaml\/dumper\.js$/),
ignore(/json-schema-ref-parser\/lib\/dereference\.js/),
ignore(/^\.\/SearchWorker\.worker$/),
2018-03-17 18:58:28 +03:00
],
});
function ignore(regexp) {
return new webpack.NormalModuleReplacementPlugin(regexp, require.resolve('lodash/noop.js'));
}