|
| 1 | +const path = require('path'); |
| 2 | +const fs = require('fs-extra'); |
| 3 | +const webpack = require('webpack'); |
| 4 | +const {CleanWebpackPlugin} = require('clean-webpack-plugin'); |
| 5 | +const MiniCssExtractPlugin = require('mini-css-extract-plugin'); |
| 6 | +const TerserPlugin = require('terser-webpack-plugin'); |
| 7 | + |
| 8 | +const BuildPaths = require('./lib/build-paths'); |
| 9 | +const BuildExtension = require('./lib/build-extension-webpack-plugin'); |
| 10 | + |
| 11 | +const manifest = fs.readJSONSync(path.join(BuildPaths.SRC_ROOT, 'manifest.json')); |
| 12 | +const version = manifest.version; |
| 13 | + |
| 14 | +const entries = { |
| 15 | + viewer: ['./extension/src/viewer.js'], |
| 16 | + 'viewer-alert': ['./extension/styles/viewer-alert.scss'], |
| 17 | + options: ['./extension/src/options.js'], |
| 18 | + background: ['./extension/src/background.js'], |
| 19 | + 'omnibox-page': ['./extension/src/omnibox-page.js'] |
| 20 | +}; |
| 21 | + |
| 22 | +function findThemes(darkness) { |
| 23 | + return fs.readdirSync(path.join('extension', 'themes', darkness)) |
| 24 | + .filter(filename => /\.js$/.test(filename)) |
| 25 | + .map(theme => theme.replace(/\.js$/, '')); |
| 26 | +} |
| 27 | + |
| 28 | +function includeThemes(darkness, list) { |
| 29 | + list.forEach(filename => { |
| 30 | + entries[filename] = [`./extension/themes/${darkness}/${filename}.js`]; |
| 31 | + }); |
| 32 | +} |
| 33 | + |
| 34 | +const lightThemes = findThemes('light'); |
| 35 | +const darkThemes = findThemes('dark'); |
| 36 | +const themes = {light: lightThemes, dark: darkThemes}; |
| 37 | + |
| 38 | +includeThemes('light', lightThemes); |
| 39 | +includeThemes('dark', darkThemes); |
| 40 | + |
| 41 | +console.log('Entries list:'); |
| 42 | +console.log(entries); |
| 43 | +console.log('\n'); |
| 44 | + |
| 45 | +module.exports = { |
| 46 | + context: __dirname, |
| 47 | + entry: entries, |
| 48 | + output: { |
| 49 | + path: path.join(__dirname, 'build/json_viewer/assets'), |
| 50 | + filename: '[name].js' |
| 51 | + }, |
| 52 | + module: { |
| 53 | + rules: [ |
| 54 | + { |
| 55 | + test: /\.(css|scss)$/, |
| 56 | + use: [ |
| 57 | + MiniCssExtractPlugin.loader, |
| 58 | + 'css-loader', |
| 59 | + 'sass-loader' |
| 60 | + ] |
| 61 | + } |
| 62 | + ] |
| 63 | + }, |
| 64 | + resolve: { |
| 65 | + extensions: ['.js', '.css', '.scss'], |
| 66 | + alias: { |
| 67 | + '@': path.resolve(__dirname, 'extension') |
| 68 | + } |
| 69 | + }, |
| 70 | + externals: { |
| 71 | + 'chrome-framework': 'chrome' |
| 72 | + }, |
| 73 | + plugins: [ |
| 74 | + new CleanWebpackPlugin(), |
| 75 | + new MiniCssExtractPlugin({ |
| 76 | + filename: '[name].css', |
| 77 | + chunkFilename: '[id].css' |
| 78 | + }), |
| 79 | + new webpack.DefinePlugin({ |
| 80 | + 'process.env': { |
| 81 | + NODE_ENV: JSON.stringify(process.env.NODE_ENV || 'development'), |
| 82 | + VERSION: JSON.stringify(version), |
| 83 | + THEMES: JSON.stringify(themes) |
| 84 | + } |
| 85 | + }), |
| 86 | + new BuildExtension({themes}) |
| 87 | + ], |
| 88 | + optimization: { |
| 89 | + minimize: process.env.NODE_ENV === 'production', |
| 90 | + minimizer: [ |
| 91 | + new TerserPlugin({ |
| 92 | + terserOptions: { |
| 93 | + compress: { |
| 94 | + drop_console: true |
| 95 | + } |
| 96 | + }, |
| 97 | + extractComments: false |
| 98 | + }) |
| 99 | + ] |
| 100 | + } |
| 101 | +}; |
| 102 | + |
| 103 | +if (process.env.NODE_ENV === 'production') { |
| 104 | + module.exports.plugins.push(new webpack.NoEmitOnErrorsPlugin()); |
| 105 | +} |
0 commit comments