• Stars
    star
    269
  • Rank 152,662 (Top 4 %)
  • Language
    TypeScript
  • Created almost 4 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Make your Nuxt.js webpack builds faster ⚡

NPM Downloads

Webpack Build Time Optimisations for Nuxt.js! ⚡️
Instantly speed up your Nuxt.js webpack build time.


Status: Stable v2 , bridge , v3 ⚠️
Made possible by my Sponsor Program 💖
Follow me @harlan_zw 🐦

Previously: "nuxt-build-optimisations", see migration notes.

Can't use Vite with Nuxt yet?

i.e Nuxt Vite or Nuxt 3

Truly sad... But I do have some good news. While you won't be able to achieve instant app starts anytime soon, nuxt-webpack-optimisations can get things snappy again.

Webpack Optimisations

nuxt-webpack-optimisations is a collection of webpack config changes that will let you speed up your build times and audit them.

By making smarter and riskier assumptions on how you want to run your environment in development, this module has been benchmarked to reduce your build time by ~50% when cold , ~95%+ when hot 🔥 (using hardsource).

How risky are we talking

The riskier optimisations are enabled only on development and relate to over caching, which is always easy to fix with a good old rm -rf node_modules/.cache 💩.

✔️ This module has been tested to cause no issues in production environments.

Features

Features are enabled by their risk profile. The risk profile is the likelihood of issues coming up.

Tools

Always

Dev

Production

Compatibility

  • ✔️ Nuxt v2
  • ✔️ Nuxt bridge
  • Nuxt v3 Note: Vite needs to be disabled. You probably don't need this module.

Setup

Install the module.

yarn add -D nuxt-webpack-optimisations
# npm i -D nuxt-webpack-optimisations

Within your nuxt.config.ts or nuxt.config.js

buildModules: [
  'nuxt-webpack-optimisations',
],

Typescript

For Nuxt config typescript support, add the module within your tsconfig.json.

{
  "compilerOptions": {
    "types": [
      "nuxt-webpack-optimisations"
    ]
  }
}

Usage

All non-risky features are enabled by default, only hardsource and parallel are disabled.

If you'd like to get more performance than the default you can try

// nuxt.config.ts
export default {
  // ...
  webpackOptimisations: {
    features: {
      // enable risky optimisations in dev only
      hardSourcePlugin: process.env.NODE_ENV === 'development',
      parallelPlugin: process.env.NODE_ENV === 'development',
    }
  }
}

Note: It's recommended to avoid running risky in non-development environments. Caching in CI environments can lead to issues.

Modifying Esbuild Behaviour

Out of the box the esbuild optimisations will use the default options, only changing the target for earlier browsers to support legacy code.

In most cases you won't need to change this, if you do then you'll need to configure the client, server and modern (if using) build separately.

Example:

// nuxt.config.ts
export default {
  // ...
  webpackOptimisations: {
    // https://github.com/privatenumber/esbuild-loader#%EF%B8%8F-options
    esbuildLoaderOptions: {
      client: {
        // any esbuild options can be set here
        minifyIdentifiers: false,
        // as well as any esbuild-loader options
        target: 'es2015',
      },
      server: {
        minifyIdentifiers: false,
        target: 'node14',
      },
      // only needed if you're using modern: true
      modern: {
        target: 'es2017',
      },
    }
  }
}

Something isn't working

A lot of the speed improvements are from heavy caching, if you have any issues the first thing you should do is clear your cache.

# Linux / Mac
rm -rf node_modules/.cache

# windows
rd /s  "node_modules/.cache"

If you'd like to see what features are running you can enable the debug mode.

// nuxt.config.ts
export default {
  webpackOptimisations: {
    debug: true
  }
}

Configuration

Features

Type: object

Default: Non-risky features enabled.

You can disable features if you'd like to skip optimisations.

// nuxt.config.ts
export default {
  webpackOptimisations: {
    features: {
      // Note: just an example of keys, these are all keys and their default
      postcssNoPolyfills: true,
      esbuildLoader: true,
      esbuildMinifier: true,
      imageFileLoader: true,
      webpackOptimisations: true,
      cacheLoader: true,
      hardSourcePlugin: false,
      parallelPlugin: false,
    }
  }
}

esbuildLoaderOptions

Type: object

Default:

// nuxt.config.ts
export default {
  // ...
  webpackOptimisations: {
    // https://github.com/privatenumber/esbuild-loader#%EF%B8%8F-options
    esbuildLoaderOptions: {
      client: {
        target: 'es2015',
      },
      server: {
        target: 'node14',
      },
      modern: {
        target: 'es2015',
      },
    }
  }
}

See esbuild-loader for full options.

esbuildMinifyOptions

Type: object

Default:

// nuxt.config.ts
export default {
  // ...
  webpackOptimisations: {
    // https://github.com/privatenumber/esbuild-loader#minifyplugin
    esbuildMinifyOptions: {
      client: {
        target: 'es2015',
      },
      server: {
        target: 'node14',
      },
      modern: {
        target: 'es2015',
      },
    }
  },
}

See esbuild-loader for full options.

Measure

Type: boolean or object

Default: false

When measure is enabled with true (options or environment variable), it will use the speed-measure-webpack-plugin.

If the measure option is an object it is assumed to be speed-measure-webpack-plugin options.

// nuxt.config.ts
export default {
  // ...
  webpackOptimisations: {
    measure: {
      outputFormat: 'humanVerbose',
      granularLoaderData: true,
      loaderTopFiles: 10
    }
  }
}

You can use an environment variable to enable the measure as well.

package.json

{
  "scripts": {
    "measure": "export NUXT_MEASURE=true; nuxt dev"
  }
}

Note: Some features are disabled with measure on, such as caching.

Measure Mode

Type: client | server | modern | all

Default: client

Configure which build will be measured. Note that non-client builds may be buggy and mess with HMR.

// nuxt.config.ts
export default {
  // ...
  webpackOptimisations: {
    measureMode: 'all'
  }
}

Gotchas

Vue Property Decorator / Vue Class Component

Your babel-loader will be replaced with esbuild, which doesn't support class decorators in js.

You can either migrate your scripts to typescript or disabled the esbuild loader.

Disable Loader

// nuxt.config.ts
export default {
  // ...
  webpackOptimisations: {
    features: {
      esbuildLoader: false
    }
  }
}

Migrate to TypeScript

tsconfig.json

{
  "experimentalDecorators": true
}
<script lang="ts">
import Vue from 'vue'
import Component from 'vue-class-component'

@Component
export default class HelloWorld extends Vue {
  data () {
    return {
      hello: 'test'
    }
  }
}
</script>

Credits

Sponsors

License

MIT License © 2022 Harlan Wilton

More Repositories

1

unlighthouse

Scan your entire site with Google Lighthouse in 2 minutes (on average). Open source, fully configurable with minimal setup.
JavaScript
3,840
star
2

nuxt-seo

The complete SEO solution for Nuxt.
TypeScript
1,053
star
3

nuxt-delay-hydration

Delayed hydration for progressively enhanced apps. Reduced blocking time and improved Google Lighthouse scores. ⚡️
TypeScript
369
star
4

request-indexing

Find your missing on pages Google and request them to be indexed using the Web Search Indexing API.
TypeScript
313
star
5

nuxt-og-image

Enlightened runtime images generated with Vue templates.
TypeScript
268
star
6

harlanzw.com

My personal website built with Nuxt 3 and Nuxt Content 2.
Vue
195
star
7

nuxt-simple-sitemap

Powerfully flexible XML Sitemaps that integrate seamlessly, for Nuxt.
TypeScript
166
star
8

nuxt-schema-org

The quickest and easiest way to build Schema.org graphs for Nuxt.
TypeScript
145
star
9

zhead

All of the TypeScript definitions for <head>.
TypeScript
122
star
10

nuxt-simple-robots

(Migrated to @nuxtjs/robots) Tame the robots crawling and indexing your Nuxt site.
TypeScript
114
star
11

nuxt-link-checker

Find and magically fix links that may be negatively effecting your Nuxt sites SEO.
TypeScript
87
star
12

nuxt-seo-utils

SEO utilities to improve your Nuxt sites discoverability and shareability.
TypeScript
75
star
13

unrouted

Unrouted is a minimal, composable router built for speed, portability and DX
TypeScript
67
star
14

nuxt-site-config

Unifying site config with powerful and flexible APIs, for module authors and users.
TypeScript
65
star
15

harlanzw.com-vitepress

My personal blog built using VitePress and TailwindCSS
Vue
44
star
16

vue-cli-plugin-import-components

🔌 Automatically import components in your Vue CLI apps.
TypeScript
39
star
17

packrup

Simple utils to pack arrays, objects and strings to a flat object (and back again).
TypeScript
17
star
18

schema-org-graph

Build Schema.org graphs for JavaScript Runtimes (Browser, Node, etc). Improve your sites SEO with quick and easy Rich Results.
TypeScript
16
star
19

laradock-cli

[Unmaintained] Your new best friend for Laradock.
Dockerfile
16
star
20

changelogd

🪵 Aggressively find a packages changelog (or releases) between versions.
TypeScript
9
star
21

wp-keystone

A Wordpress Boilerplate combining the best libraries and practices
PHP
8
star
22

laravel-swiftype

Swiftype Integration for Laravel
PHP
7
star
23

nuxt-mycelium

🍄 TBA 🤫
TypeScript
7
star
24

to-vite-and-beyond-a-history-and-future-of-bundling

Vue
6
star
25

WP-Development-Environment

WorldPress Plugin - Development Environment
PHP
5
star
26

nuxt-seo-ui

Deprecated - Nuxt components with Schema.org and SEO integrations
TypeScript
5
star
27

talk-supercharged-head-management

NuxtNation 2022 Talk - VueUse Head v1
Vue
3
star
28

laravel-modern-mail

TBA
PHP
2
star
29

static

TypeScript
1
star
30

blockfolio-php

Blockfolio SDK for PHP - Unofficial
PHP
1
star
31

penguin-pass

Laravel / PHP Package for kid friendly passwords
PHP
1
star
32

nuxt-og-image-playground

Testing out nitro deployments with nuxt-og-imge.
Vue
1
star