> For the complete documentation index, see [llms.txt](https://notes.lirenxn.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://notes.lirenxn.com/2020/wk-notes-01-17-bundling-browser-module-treeshake-2.md).

# Wk-notes-01-17-bundling-browser-module-treeshake-2

### How to use browser js module

**A great cdn:** <https://unpkg.com/>

**No bundling and shit !!**

`htm` <https://github.com/developit/htm>

```javascript
<!DOCTYPE html>
<html lang="en">
  <title>htm Demo</title>
  <script type="module">
    import { html, Component, render } from 'https://unpkg.com/htm/preact/standalone.module.js';
​
    class App extends Component {
      addTodo() {
        const { todos = [] } = this.state;
        this.setState({ todos: todos.concat(`Item ${todos.length}`) });
      }
      render({ page }, { todos = [] }) {
        return html`
          <div class="app">
            <${Header} name="ToDo's (${page})" />
            <ul>
              ${todos.map(todo => html`
                <li>${todo}</li>
              `)}
            </ul>
            <button onClick=${() => this.addTodo()}>Add Todo</button>
            <${Footer}>footer content here<//>
          </div>
        `;
      }
    }
​
    const Header = ({ name }) => html`<h1>${name} List</h1>`
​
    const Footer = props => html`<footer ...${props} />`
​
    render(html`<${App} page="All" />`, document.body);
  </script>
</html>
```

**Separate Bundling builds**

**Purpose :** To support various consumers including modern browser

Example: <https://unpkg.com/browse/preact@10.2.1/dist/>

[**Enable Modern Javascript from npm**](https://jasonformat.com/enabling-modern-js-on-npm/)

**Question:** how does webpack(npm) consume the pkg

### Treeshake-2

Webpack-v4 enables treeshake out of box. If you are using the correct syntax, and the referenced pkg is correctly bundled. it would be treeshaked.

<https://www.azavea.com/blog/2019/03/07/lessons-on-tree-shaking-lodash/>

> To enable tree-shaking in lodash, you have to import functions using syntax like `import foo from 'lodash/foo'`. `import { foo } from 'lodash'` will not tree-shake, nor will, obviously, `import _ from 'lodash'`. Support for this syntax was implemented in Lodash v4.

<https://developers.google.com/web/fundamentals/performance/optimizing-javascript/tree-shaking>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://notes.lirenxn.com/2020/wk-notes-01-17-bundling-browser-module-treeshake-2.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
