Skip to content

Instantly share code, notes, and snippets.

View pushkar100's full-sized avatar

Pushkar DK pushkar100

  • ThoughtSpot
  • Bangalore
  • 00:29 (UTC +05:30)
View GitHub Profile
@pushkar100
pushkar100 / tree-shaking-example.js
Last active October 18, 2020 08:51
Tree shaking example
/* ./simple-arithmetic.js */
export const add = (a, b) => a + b
export const subtract = (a, b) => a - b
/* ./complex-arithmetic.js */
export const multiply = (a, b) => a * b
export const divide = (a, b) => a / b
const power = (a, b) => Math.pow(a, b)
export default power
@pushkar100
pushkar100 / magic-comments-example.js
Created October 6, 2020 06:16
Webpack magic comments example
import(
/* webpackChunkName: "my-chunk-name" */
/* webpackMode: "lazy" */
'module'
);
@pushkar100
pushkar100 / chunk-exports-no-split.js
Last active October 18, 2020 09:22
Chunk exports without partial split
// (A) What happens when you try to load dynamic chunks:
/* ./utilities.js */
export const add = (a, b) => a + b
export const subtract = (a, b) => a - b
export const multiply = (a, b) => a * b
export const divide = (a, b) => a / b
const power = (a, b) => Math.pow(a, b)
export default power
@pushkar100
pushkar100 / chunk-exports-partial-split.js
Last active October 18, 2020 08:45
Chunk exports with partial split
// (B) What you need to manually do to import only A from the dynamic chunk:
/* ./utilities.js */
export const add = (a, b) => a + b
export const subtract = (a, b) => a - b
export const multiply = (a, b) => a * b
export const divide = (a, b) => a / b
const power = (a, b) => Math.pow(a, b)
export default power
@pushkar100
pushkar100 / webpackExports-usage-example.js
Created October 6, 2020 06:22
webpackExports usage example
// Use an array to fetch multiple names exports:
import(/* webpackExports: ["named1", "named2"] */ "module")
// Use the "default" keyword to fetch the default export as well:
import(/* webpackExports: ["named1", "named2", "default"] */ "module")
// Array is not necessary to fetch a single export:
import(/* webpackExports: "named1" */ "module")
@pushkar100
pushkar100 / webpackExports-single-export.js
Last active October 18, 2020 08:47
webpackExports single export
/* ./utilities.js */
export const add = (a, b) => a + b
export const subtract = (a, b) => a - b
export const multiply = (a, b) => a * b
export const divide = (a, b) => a / b
const power = (a, b) => Math.pow(a, b)
export default power
@pushkar100
pushkar100 / side-effects-live-bindings-example.js
Created October 6, 2020 06:30
Side effects and Live bindings example
// Side effect & live binding example:
/* ./file1.js */
console.log('Loaded module') // Illustates side effect
let i = 0 // Illustrates live binding
export const A = () => {
console.log('A', i)
i += 5
}
export const B = () => {
@pushkar100
pushkar100 / se-lb-output-example.js
Created October 6, 2020 06:31
Side effects and live bindings output example
/* What will happen if state is duplicated (i.e maintains two states which is wrong!) */
// Console output:
Loaded module
A 0
Loaded module
B 0
const getLogMetaData = () => ({
source: 'www'
})
class Greetings extends React.Component {
state = {};
componentDidMount() {
this.setState({
meta: getLogMetaData()
const getLogMetaData = () => ({
source: 'www'
})
const useLogger = () => {
const [meta, setMeta] = useState({});
const logger = (id, event) => {
console.log(`id: ${id} | event: ${event}`);
console.log(`source: ${meta.source}`);
};