<commit_before>'use strict'

const { dirname, join } = require('path')
const webpack = require('webpack')
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const TerserPlugin = require('terser-webpack-plugin')
const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin')

const isProduction = process.env.NODE_ENV === 'production'
const highlightjsStyles = dirname(require.resolve('highlight.js/styles/github.css'))
const resolve = dir => join(__dirname, dir)
const assetsPath = '_assets'
const publicPath = filePath => `${assetsPath}/${filePath}`
const assetPath = filePath => `<%= basePath %>${filePath}`
const fileNameTmpl = `[name]${isProduction ? '.[contenthash:7]' : ''}`

const { LANGUAGES } = require('./src/shared/highlight')

const plugins = [
  new VueLoaderPlugin(),
  // restrict highlight.js to what we need
  // https://bjacobel.com/2016/12/04/highlight-bundle-size/
  new webpack.ContextReplacementPlugin(
    /highlight\.js\/lib\/languages$/,
    new RegExp(`^./(${LANGUAGES.join('|')})$`)
  ),
  // extract css into its own file
  new MiniCssExtractPlugin({
    filename: publicPath(`styles/${fileNameTmpl}.css`)
  }),
  // copy custom static assets
  new CopyWebpackPlugin([
    {
      from: highlightjsStyles,
      to: publicPath('styles/hljs'),
      ignore: ['.*']
    }
  ]),
  // https://github.com/ampedandwired/html-webpack-plugin
  new HtmlWebpackPlugin({
    filename: resolve('lib/templates/index.ejs'),
    template: resolve('src/templates/index.ejs'),
    excludeChunks: ['inject'],
    inject: false,
    locales: require('./src/locales'),
    assetPath
  }),
  new HtmlWebpackPlugin({
    filename: resolve('lib/templates/sketch.ejs'),
    template: resolve('src/templates/sketch.ejs'),
    excludeChunks: ['main'],
    inject: false,
    assetPath
  }),
  new HtmlWebpackPlugin({
    filename: resolve('lib/templates/tokens.ejs'),
    template: resolve('src/templates/tokens.ejs'),
    excludeChunks: ['main'],
    inject: false,
    assetPath
  })
]

if (isProduction) {
  // const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer')
  // plugins.push(
  //   new BundleAnalyzerPlugin()
  // )
} else {
  plugins.push(
    new FriendlyErrorsPlugin({
      onErrors: () => {
        const notifier = require('node-notifier')

        return (severity, errors) => {
          if (severity !== 'error') return

          const error = errors[0]
          const filename = error.file && error.file.split('!').pop()

          notifier.notify({
            title: 'UIengine UI',
            message: `${severity}: ${error.name}`,
            subtitle: filename || '',
            timeout: 5
          })

          notifier.on('click', () => {
            const exec = require('child_process').exec
            exec(`$EDITOR ${resolve(filename)}`)<commit_msg>⏱ ui: add webpack bundle analyzer<commit_after>'use strict'

const { dirname, join } = require('path')
const webpack = require('webpack')
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const TerserPlugin = require('terser-webpack-plugin')
const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin')

const isProduction = process.env.NODE_ENV === 'production'
const highlightjsStyles = dirname(require.resolve('highlight.js/styles/github.css'))
const resolve = dir => join(__dirname, dir)
const assetsPath = '_assets'
const publicPath = filePath => `${assetsPath}/${filePath}`
const assetPath = filePath => `<%= basePath %>${filePath}`
const fileNameTmpl = `[name]${isProduction ? '.[contenthash:7]' : ''}`

const { LANGUAGES } = require('./src/shared/highlight')

const plugins = [
  new VueLoaderPlugin(),
  // restrict highlight.js to what we need
  // https://bjacobel.com/2016/12/04/highlight-bundle-size/
  new webpack.ContextReplacementPlugin(
    /highlight\.js\/lib\/languages$/,
    new RegExp(`^./(${LANGUAGES.join('|')})$`)
  ),
  // extract css into its own file
  new MiniCssExtractPlugin({
    filename: publicPath(`styles/${fileNameTmpl}.css`)
  }),
  // copy custom static assets
  new CopyWebpackPlugin([
    {
      from: highlightjsStyles,
      to: publicPath('styles/hljs'),
      ignore: ['.*']
    }
  ]),
  // https://github.com/ampedandwired/html-webpack-plugin
  new HtmlWebpackPlugin({
    filename: resolve('lib/templates/index.ejs'),
    template: resolve('src/templates/index.ejs'),
    excludeChunks: ['inject'],
    inject: false,
    locales: require('./src/locales'),
    assetPath
  }),
  new HtmlWebpackPlugin({
    filename: resolve('lib/templates/sketch.ejs'),
    template: resolve('src/templates/sketch.ejs'),
    excludeChunks: ['main'],
    inject: false,
    assetPath
  }),
  new HtmlWebpackPlugin({
    filename: resolve('lib/templates/tokens.ejs'),
    template: resolve('src/templates/tokens.ejs'),
    excludeChunks: ['main'],
    inject: false,
    assetPath
  })
]

if (isProduction) {
  const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer')
  plugins.push(
    new BundleAnalyzerPlugin({
      analyzerMode: 'static',
      openAnalyzer: false,
      reportFilename: resolve('report.html')
    })
  )
} else {
  plugins.push(
    new FriendlyErrorsPlugin({
      onErrors: () => {
        const notifier = require('node-notifier')

        return (severity, errors) => {
          if (severity !== 'error') return

          const error = errors[0]
          const filename = error.file && error.file.split('!').pop()

          notifier.notify({
            title: 'UIengine UI',
            message: `${severity}: ${error.name}`,
            subtitle: filename || '',
            timeout: 5
          })

          notifier.on('click', () => {
            const exec = require('child_process').exec
            exec(`$EDITOR ${resolve(filename)}`)
{'fraction_non_alphanumeric': 0.158004158004158, 'fraction_numerical': 0.003518311210618903, 'mean_word_length': 3.4607703281027105, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5874332', 'n_tokens_mistral': 1925, 'n_tokens_neox': 1881, 'n_words': 372}