Skip to content

Commit 6e52cb0

Browse files
npm: add 'voyager.standalone.js' as fully standalone bundle (#299)
1 parent d91d5a8 commit 6e52cb0

File tree

2 files changed

+107
-77
lines changed

2 files changed

+107
-77
lines changed

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
"serve": "ts-node ./scripts/serve-directory.ts -p 9090 demo-dist",
1818
"clean:release": "rm -rf middleware",
1919
"clean:worker": "rm -rf worker-dist",
20-
"bundle:standalone": "webpack --config=webpack.config.ts --mode=production",
20+
"bundle:min": "webpack --config=webpack.config.ts --mode=production --env min",
21+
"bundle:standalone": "webpack --config=webpack.config.ts --mode=production --env standalone",
2122
"bundle:lib": "webpack --config=webpack.config.ts --mode=production --env lib",
22-
"bundle": "rm -rf dist && npm run bundle:lib && npm run bundle:standalone",
23+
"bundle": "rm -rf dist && npm run bundle:lib && npm run bundle:standalone && npm run bundle:min",
2324
"compile:middleware": "tsc -d src/middleware/index.ts --outDir middleware --lib ES6,DOM,esnext.asynciterable",
2425
"build:worker": "npm run clean:worker && docker-compose up --abort-on-container-exit --build build-worker",
2526
"build:release": "npm run build:worker && npm run bundle && npm run compile:middleware && npm run declarations",

webpack.config.ts

+104-75
Original file line numberDiff line numberDiff line change
@@ -11,89 +11,118 @@ const BANNER = `GraphQL Voyager - Represent any GraphQL API as an interactive gr
1111
Repo: ${packageJSON.repository.url}`;
1212

1313
interface Env {
14+
min?: boolean;
1415
lib?: boolean;
16+
standalone?: boolean;
1517
}
1618

1719
export default function buildWebpackConfig(env: Env): webpack.Configuration {
18-
return {
19-
performance: {
20-
hints: false,
21-
},
20+
if (env.lib === true) {
21+
return {
22+
...baseConfig,
23+
externals: NodeExternals(),
24+
output: {
25+
...baseConfig.output,
26+
filename: 'voyager.lib.js',
27+
},
28+
};
29+
}
2230

23-
optimization: {
24-
minimize: !env.lib,
25-
},
31+
if (env.standalone === true) {
32+
return {
33+
...baseConfig,
34+
optimization: { minimize: true },
35+
externals: undefined,
36+
output: {
37+
...baseConfig.output,
38+
filename: 'voyager.standalone.js',
39+
},
40+
};
41+
}
2642

27-
resolve: {
28-
extensions: ['.ts', '.tsx', '.mjs', '.js', '.json', '.css', '.svg'],
29-
alias: { '../../worker': '../../worker-dist' },
30-
},
31-
32-
externals: env.lib
33-
? NodeExternals()
34-
: {
35-
react: {
36-
root: 'React',
37-
commonjs2: 'react',
38-
commonjs: 'react',
39-
amd: 'react',
40-
},
41-
'react-dom': {
42-
root: 'ReactDOM',
43-
commonjs2: 'react-dom',
44-
commonjs: 'react-dom',
45-
amd: 'react-dom',
46-
},
47-
},
48-
entry: './src/index.tsx',
49-
output: {
50-
path: path.resolve(__dirname, 'dist'),
51-
filename: env.lib ? 'voyager.lib.js' : 'voyager.min.js',
52-
sourceMapFilename: '[file].map',
53-
library: 'GraphQLVoyager',
54-
libraryTarget: 'umd',
55-
umdNamedDefine: true,
56-
},
57-
module: {
58-
rules: [
59-
{
60-
test: /\.tsx?$/,
61-
use: {
62-
loader: 'ts-loader',
63-
options: { compilerOptions: { noEmit: false } },
64-
},
65-
exclude: [/\.(spec|e2e)\.ts$/],
66-
},
67-
{
68-
test: /\.css$/,
69-
exclude: /variables\.css$/,
70-
use: [
71-
MiniCssExtractPlugin.loader,
72-
{
73-
loader: 'css-loader',
74-
options: { sourceMap: true },
75-
},
76-
{ loader: 'postcss-loader', options: { sourceMap: true } },
77-
],
43+
// TODO: delete in next major version
44+
if (env.min === true) {
45+
return {
46+
...baseConfig,
47+
optimization: { minimize: true },
48+
externals: {
49+
react: {
50+
root: 'React',
51+
commonjs2: 'react',
52+
commonjs: 'react',
53+
amd: 'react',
7854
},
79-
{
80-
test: /variables\.css$/,
81-
use: [{ loader: 'postcss-variables-loader?es5=1' }],
55+
'react-dom': {
56+
root: 'ReactDOM',
57+
commonjs2: 'react-dom',
58+
commonjs: 'react-dom',
59+
amd: 'react-dom',
8260
},
83-
{
84-
test: /\.svg$/,
85-
issuer: /\.tsx?$/,
86-
use: [{ loader: '@svgr/webpack' }],
61+
},
62+
output: {
63+
...baseConfig.output,
64+
filename: 'voyager.min.js',
65+
},
66+
};
67+
}
68+
throw new Error('Please specify correct env');
69+
}
70+
71+
const baseConfig: webpack.Configuration = {
72+
performance: {
73+
hints: false,
74+
},
75+
resolve: {
76+
extensions: ['.ts', '.tsx', '.mjs', '.js', '.json', '.css', '.svg'],
77+
alias: { '../../worker': '../../worker-dist' },
78+
},
79+
entry: './src/index.tsx',
80+
output: {
81+
path: path.resolve(__dirname, 'dist'),
82+
sourceMapFilename: '[file].map',
83+
library: 'GraphQLVoyager',
84+
libraryTarget: 'umd',
85+
umdNamedDefine: true,
86+
},
87+
module: {
88+
rules: [
89+
{
90+
test: /\.tsx?$/,
91+
use: {
92+
loader: 'ts-loader',
93+
options: { compilerOptions: { noEmit: false } },
8794
},
88-
],
89-
},
95+
exclude: [/\.(spec|e2e)\.ts$/],
96+
},
97+
{
98+
test: /\.css$/,
99+
exclude: /variables\.css$/,
100+
use: [
101+
MiniCssExtractPlugin.loader,
102+
{
103+
loader: 'css-loader',
104+
options: { sourceMap: true },
105+
},
106+
{ loader: 'postcss-loader', options: { sourceMap: true } },
107+
],
108+
},
109+
{
110+
test: /variables\.css$/,
111+
use: [{ loader: 'postcss-variables-loader?es5=1' }],
112+
},
113+
{
114+
test: /\.svg$/,
115+
issuer: /\.tsx?$/,
116+
use: [{ loader: '@svgr/webpack' }],
117+
},
118+
],
119+
},
90120

91-
plugins: [
92-
new MiniCssExtractPlugin({
93-
filename: 'voyager.css',
94-
}),
121+
plugins: [
122+
new MiniCssExtractPlugin({
123+
filename: 'voyager.css',
124+
}),
95125

96-
new webpack.BannerPlugin(BANNER),
97-
],
98-
};
99-
}
126+
new webpack.BannerPlugin(BANNER),
127+
],
128+
};

0 commit comments

Comments
 (0)