67
58

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Nuxt.jsで本番環境のみconsole.logを削除する

67
Last updated at Posted at 2019-07-20

開発用にconsole.logで色々出しておきたいけど、本番では出したくない😑ってときに。

経緯

元はUglifyJsPluginでdrop_consoleを指定していたのだけれど、ある時から

Unexpected token: keyword «const» [./node_modules/vuejs-datepicker/dist/vuejs-datepicker.esm.js:600,0]

と怒られるようになってしまった。
UglifyJsがどうやらES6+に非対応らしく、利用しているプラグインのアップデートで怒られるようになったのではと。
exclude指定とか試したんだけどどうもうまくいかないので、UglifyJsを諦めた\(^o^)/

terserのオプション指定でconsole.logを消す

Nuxt 2.1.0 以降で、terserのオプションを指定できるようになってます!

nuxt.config.js
  build: {
    terser: {
      terserOptions: {
        compress: { drop_console: true }
      },
    },
  },

こんな感じで無事console.log削除。

terserもUglifyJSもWebpackで使うミニマイザ(圧縮ツール)ですが、
Nuxtデフォルトのミニマイザはterserです。
この辺りをよく理解せずに、古い記事を参考にしてUglifyJS使っちゃってたのですが、
オプション触れるのであればterserを使った方が良いですね🙌

参考

https://ja.nuxtjs.org/api/configuration-build/#terser
https://github.com/webpack-contrib/terser-webpack-plugin

67
58
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
67
58

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?