Skip to content

Commit

Permalink
feat(maleo-core/server): options to disable or enable gzip (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
alvinkl authored and AndariasSilvanus committed Oct 3, 2019
1 parent 6067c57 commit 468a427
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/Maleo.js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,11 @@ Here are the API's for the configuration:
}</code>
</td>
</tr>
<tr>
<td><code>gzip</code></td>
<td><code>boolean?</code> [<code>true</code>]</td>
<td>Enable GZIP Compression</td>
</tr>
</table>

#### Customize Server
Expand Down
1 change: 1 addition & 0 deletions packages/Maleo.js/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ declare const __IS_SERVER__: boolean;
declare const __ESM__: boolean;
declare const __FAVICON__: string;
declare const __CSP__: boolean | IHelmetContentSecurityPolicyConfiguration;
declare const __ENABLE_GZIP__: boolean;
4 changes: 4 additions & 0 deletions packages/Maleo.js/src/build/webpack/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const defaultUserConfig: CustomConfig = {
buildDir: BUILD_DIR,
assetDir: path.resolve('.', BUILD_DIR, CLIENT_BUILD_DIR),
distDir: path.resolve('.', 'dist'),
gzip: true,
};

export const createWebpackConfig = (context: Context, customConfig: CustomConfig) => {
Expand All @@ -77,6 +78,7 @@ export const createWebpackConfig = (context: Context, customConfig: CustomConfig
whitelist: customWhitelist = [],
favicon: _favicon,
csp = false,
gzip,
} = customConfig;

const buildDirectory = buildDir || BUILD_DIR;
Expand Down Expand Up @@ -422,6 +424,7 @@ export const getDefaultPlugins = (
csp,
esModules,
} = context;
const { gzip } = customConfig;

const commonPlugins: Configuration['plugins'] =
([
Expand Down Expand Up @@ -497,6 +500,7 @@ export const getDefaultPlugins = (
new DefinePlugin({
__FAVICON__: JSON.stringify(favicon),
__CSP__: JSON.stringify(csp),
__ENABLE_GZIP__: gzip,
}),

isDev &&
Expand Down
1 change: 1 addition & 0 deletions packages/Maleo.js/src/interfaces/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export interface CustomConfig {
csp?: boolean | IHelmetContentSecurityPolicyConfiguration;

esModules?: boolean;
gzip?: boolean;
}

export interface StaticPages {
Expand Down
1 change: 1 addition & 0 deletions packages/Maleo.js/src/interfaces/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface IOptions {
assetDir?: string;
runHandler?: () => any;
csp?: IHelmetContentSecurityPolicyConfiguration;
gzip?: boolean;
}

// Stats
Expand Down
5 changes: 3 additions & 2 deletions packages/Maleo.js/src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export class Server {
port: options.port || 3000,
runHandler: options.runHandler || this.defaultHandler,
csp: options.csp || typeof __CSP__ === 'boolean' ? defaultCSP : __CSP__,
gzip: options.gzip || __ENABLE_GZIP__,
} as IOptions;

this.options = defaultOptions;
Expand Down Expand Up @@ -74,7 +75,7 @@ export class Server {

private setupExpress = async () => {
// Set Compression
process.env.NODE_ENV !== 'development' && this.setupCompression(this.app);
this.setupCompression(this.app);

// Set secure server
this.setupSecureServer(this.app);
Expand Down Expand Up @@ -134,7 +135,7 @@ export class Server {
level: zlib.Z_DEFAULT_COMPRESSION,
};

app.use(compression(option));
this.options.gzip && app.use(compression(option));
};

protected defaultHandler = () => {
Expand Down

0 comments on commit 468a427

Please sign in to comment.