Skip to content

Instantly share code, notes, and snippets.

View alancasagrande's full-sized avatar

Alan Casagrande alancasagrande

  • Berlin, Germany
View GitHub Profile
const path = require("path");
module.exports = {
...
optimization: {
splitChunks: {
chunks: 'all',
cacheGroups: {
defaultVendors: {
import _ from "lodash";
import join from "lodash/join";
function HelloComponent() {
const element = document.createElement("div");
element.innerText = join(
["Hello from bundle size demo", "with lodash join"],
" "
);
return element;
}
@alancasagrande
alancasagrande / block12.js
Last active November 14, 2020 17:11
index.js
function HelloComponent() {
const button = document.createElement('button');
button.innerText = 'Hello';
button.onclick = () => {
return import(
/* webpackChunkName: "lodash" */
'lodash/join'
).then(({ default: join }) => {
const greeting = join(
['Hello from bundle size demo', 'with lodash loaded dynamically'],
@alancasagrande
alancasagrande / block11.js
Last active November 14, 2020 16:26
index.js
import { join } from "lodash";
plugins: [
new webpack.IgnorePlugin({
resourceRegExp: /^\.\/locale$/,
contextRegExp: /moment$/,
}),
new BundleAnalyzerPlugin({ openAnalyzer: false }),
];
<script src="vendors.js"></script>
import moment from 'moment';
function HelloComponent() {
const element = document.createElement('div');
element.innerText = `Now ${moment().format('L LT')}`;
return element;
}
document.body.appendChild(HelloComponent());
import React, { useState } from 'react';
import Login from './Login';
export default function App({ user }) {
// user not logged in, show login form
if (!user) return <Login />;
return (
<div>
Hello, {user.username}. <a href="/logout">Logout</a>
export async function login(username, password) {
const res = await fetch('/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username, password }),
});
if (res.status === 401) return {};
const user = await res.json();