Skip to content

Commit

Permalink
bundle optimization in webpack (#2350)
Browse files Browse the repository at this point in the history
* chore: optimize webpack bundles to use common node_modules
  • Loading branch information
arslanashraf7 authored Feb 21, 2022
1 parent a9b52ff commit 19031f3
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 28 deletions.
3 changes: 3 additions & 0 deletions mitxpro/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
{% endif %}
</script>
{% render_bundle 'style' %}
{% render_bundle 'common' %}
{% render_bundle 'header-root' %}

<title>{% block title %}{% endblock %}</title>
<meta name="description" content="{% block description %}{% endblock %}">
<meta name="keywords" content="{% block keywords %}{% endblock %}">
Expand Down
17 changes: 0 additions & 17 deletions static/js/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
} from "ramda"
import _truncate from "lodash/truncate"
import qs from "query-string"
import { assert } from "chai"
import * as R from "ramda"
import moment from "moment"

Expand Down Expand Up @@ -127,22 +126,6 @@ export const objectToFormData = (object: Object) => {
return formData
}

export const assertRaises = async (
asyncFunc: Function,
expectedMessage: string
) => {
let exception
try {
await asyncFunc()
} catch (ex) {
exception = ex
}
if (!exception) {
throw new Error("No exception caught")
}
assert.equal(exception.message, expectedMessage)
}

// Example return values: "January 1, 2019", "December 31, 2019"
export const formatPrettyDate = (momentDate: Moment) =>
momentDate.format("MMMM D, YYYY")
Expand Down
17 changes: 16 additions & 1 deletion static/js/lib/util_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { assert } from "chai"
import moment from "moment"

import {
assertRaises,
wait,
enumerate,
findItemWithTextId,
Expand Down Expand Up @@ -35,6 +34,22 @@ import {
makeCourseRunProduct
} from "../factories/ecommerce"

const assertRaises = async (
asyncFunc: Function,
expectedMessage: string
) => {
let exception
try {
await asyncFunc()
} catch (ex) {
exception = ex
}
if (!exception) {
throw new Error("No exception caught")
}
assert.equal(exception.message, expectedMessage)
}

import { PRODUCT_TYPE_COURSERUN, PRODUCT_TYPE_PROGRAM } from "../constants"

describe("utility functions", () => {
Expand Down
22 changes: 15 additions & 7 deletions webpack.config.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const insertHotReload = (host, port, entries) =>
const devConfig = Object.assign({}, config, {
context: __dirname,
mode: "development",
devtool: "inline-source-map",
output: {
path: path.resolve("./static/bundles/"),
filename: "[name].js"
Expand All @@ -29,15 +30,22 @@ const devConfig = Object.assign({}, config, {
new webpack.HotModuleReplacementPlugin(),
new BundleTracker({ filename: "./webpack-stats.json" })
],
devtool: "source-map",
optimization: {
namedModules: true,
splitChunks: {
name: "common",
minChunks: 2
chunkIds: 'named',
moduleIds: 'named',
splitChunks: {
chunks: "all",
minChunks: 2,
automaticNameDelimiter: '-',
cacheGroups: {
common: {
test: /[\\/]node_modules[\\/]/,
name: 'common',
chunks: 'all',
},
},
},
noEmitOnErrors: true
}
},
})

devConfig.module.rules = [
Expand Down
15 changes: 12 additions & 3 deletions webpack.config.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,20 @@ module.exports = Object.assign(prodConfig, {
})
],
optimization: {
minimize: true,
chunkIds: 'named',
splitChunks: {
name: "common",
minChunks: 2
chunks: "all",
minChunks: 2,
automaticNameDelimiter: '-',
cacheGroups: {
common: {
test: /[\\/]node_modules[\\/]/,
name: 'common',
chunks: 'all',
}
},
},
minimize: true
},
devtool: "source-map"
})

0 comments on commit 19031f3

Please sign in to comment.