redoc/webpack.config.ts

144 lines
3.8 KiB
TypeScript
Raw Normal View History

2018-05-17 11:40:47 +03:00
/* tslint:disable:no-implicit-dependencies */
2018-03-17 16:11:52 +03:00
import * as ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';
2018-05-17 11:40:47 +03:00
import * as HtmlWebpackPlugin from 'html-webpack-plugin';
import * as webpack from 'webpack';
2018-03-17 16:11:52 +03:00
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
2018-05-17 11:40:47 +03:00
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',
2018-05-17 11:40:47 +03:00
declaration: false,
},
},
},
{
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/),
env.standalone ? ignore(/^\.\/SearchWorker\.worker$/) : ignore(/$non-existing^/),
2018-03-17 18:58:28 +03:00
],
});
function ignore(regexp) {
return new webpack.NormalModuleReplacementPlugin(regexp, require.resolve('lodash/noop.js'));
}