GoogleChromeLabs / webpack-libs-optimizations

Using a library in your webpack project? Here’s how to optimize it
Apache License 2.0
3.36k stars 112 forks source link

Templating libraries: include only the runtime #15

Closed danburzo closed 4 years ago

danburzo commented 6 years ago

There are certain JavaScript libraries that make use of their own templating languages. In a Webpack-built project, if you keep all your templates as separate files, you can use a specialized loader to get the pre-compiled version in your code. If the library supports it, you can exclude the parsing part from the bundle.

For example, we have some parts of our app that use Ractive(v0.6) , and we're using ractive-loader to load them. Previously, we'd included the whole Ractive library in the bundle, but we've shaved off a hefty part of it by doing this in our Webpack config:

resolve: {
    alias: {
      ractive: path.resolve(__dirname, 'node_modules/ractive/ractive.runtime.min.js')
    }
}

I'm sure this is applicable to more libraries, are there similar opportunities?

iamakulov commented 6 years ago

Oh wow, that’s a nice optimization opportunity!

Other examples I can think of (we need to test them though, preferably in a real application):

iamakulov commented 6 years ago

Want to make a PR? :–) (Either with ractive only – I’ll test and add other libraries myself – or with the mentioned libraries too)

danburzo commented 6 years ago

Sure, I'll make a PR with a few examples for libraries I find include their parser in their default entry point.