|
| 1 | +const paths = require('./paths'); |
| 2 | +const widgetConf = require('./widget.config.json'); |
| 3 | +const XMLPlugin = require('xml-webpack-plugin'); |
| 4 | +const ArchivePlugin = require('webpack-archive-plugin'); |
| 5 | +const MiniCssExtractPlugin = require('mini-css-extract-plugin'); |
| 6 | +const UglifyJsPlugin = require('uglifyjs-webpack-plugin'); |
| 7 | +const fs = require('fs-extra'); |
| 8 | + |
| 9 | +const MODES = { |
| 10 | + DEV: 'development', |
| 11 | + PROD: 'production' |
| 12 | +}; |
| 13 | +const isProd = process.env.MODE === MODES.PROD; |
| 14 | +const isDev = process.env.MODE === MODES.DEV; |
| 15 | + |
| 16 | +const widgetDir = `${widgetConf.name}/widget`; |
| 17 | +const widgetUIDir = `${widgetDir}/ui`; |
| 18 | + |
| 19 | +const widgetXMLFiles = [ |
| 20 | + { |
| 21 | + template: paths.widgetPackageXML, |
| 22 | + filename: `package.xml`, |
| 23 | + data: { |
| 24 | + NAME: widgetConf.name |
| 25 | + } |
| 26 | + }, |
| 27 | + { |
| 28 | + template: paths.widgetConfigXML, |
| 29 | + filename: `${widgetConf.name}/${widgetConf.name}.xml`, |
| 30 | + data: { |
| 31 | + NAME: widgetConf.name, |
| 32 | + FRIENDLY_NAME: widgetConf.friendlyName, |
| 33 | + WIDGET_DESC: widgetConf.description |
| 34 | + } |
| 35 | + } |
| 36 | +]; |
| 37 | + |
| 38 | +module.exports = { |
| 39 | + mode: isDev ? MODES.DEV : MODES.PROD, |
| 40 | + target: 'web', |
| 41 | + devtool: isDev ? 'eval-source-map' : false, |
| 42 | + watch: isDev, |
| 43 | + entry: paths.srcEntry, |
| 44 | + output: { |
| 45 | + path: isDev ? paths.buildDir : paths.distDir, |
| 46 | + filename: `${widgetDir}/${widgetConf.name}.js`, |
| 47 | + libraryTarget: 'amd' |
| 48 | + }, |
| 49 | + optimization: { |
| 50 | + minimizer: [ |
| 51 | + new UglifyJsPlugin({ |
| 52 | + test: /\.js(\?.*)?$/i |
| 53 | + }) |
| 54 | + ] |
| 55 | + }, |
| 56 | + module: { |
| 57 | + rules: [ |
| 58 | + { |
| 59 | + test: /\.js$/, |
| 60 | + exclude: /node_modules/, |
| 61 | + use: { |
| 62 | + loader: 'babel-loader', |
| 63 | + options: { |
| 64 | + presets: [ '@babel/preset-env' ], |
| 65 | + plugins: [ 'add-module-exports' ] |
| 66 | + } |
| 67 | + } |
| 68 | + }, |
| 69 | + { |
| 70 | + test: /\.(sa|sc|c)ss$/, |
| 71 | + use: [ |
| 72 | + MiniCssExtractPlugin.loader, |
| 73 | + 'css-loader', |
| 74 | + { loader: 'postcss-loader', options: { config: { path: paths.confDir } } }, |
| 75 | + 'sass-loader' |
| 76 | + ] |
| 77 | + }, |
| 78 | + { |
| 79 | + test: /\.(gif|png|jpe?g|svg)$/i, |
| 80 | + use: [ |
| 81 | + { |
| 82 | + loader: 'file-loader', |
| 83 | + options: { |
| 84 | + name: `[name].[ext]`, |
| 85 | + outputPath: `${widgetUIDir}/images` |
| 86 | + } |
| 87 | + } |
| 88 | + ] |
| 89 | + } |
| 90 | + ] |
| 91 | + }, |
| 92 | + externals: [ |
| 93 | + { MxWidgetBase: 'mxui/widget/_WidgetBase' }, |
| 94 | + { dojoBaseDeclare: 'dojo/_base/declare' }, |
| 95 | + /mx|mxui|mendix|dijit|dojo|require/ |
| 96 | + ], |
| 97 | + plugins: _getPlugins() |
| 98 | +}; |
| 99 | + |
| 100 | +function _getPlugins() { |
| 101 | + //ensure distDir fir Archive Plugin |
| 102 | + fs.ensureDirSync(paths.distDir); |
| 103 | + const plugins = [ |
| 104 | + new MiniCssExtractPlugin({ |
| 105 | + filename: `${widgetUIDir}/${widgetConf.name}.css` |
| 106 | + }), |
| 107 | + new XMLPlugin({ |
| 108 | + files: widgetXMLFiles |
| 109 | + }), |
| 110 | + new ArchivePlugin({ |
| 111 | + output: `${paths.distDir}/${widgetConf.name}`, |
| 112 | + format: 'zip', |
| 113 | + ext: 'mpk' |
| 114 | + }) |
| 115 | + ]; |
| 116 | + if (paths.mxProjectRootDir) { |
| 117 | + plugins.push( |
| 118 | + new ArchivePlugin({ |
| 119 | + output: `${paths.mxProjectRootDir}/widgets/${widgetConf.name}`, |
| 120 | + format: 'zip', |
| 121 | + ext: 'mpk' |
| 122 | + }) |
| 123 | + ); |
| 124 | + } |
| 125 | + return plugins; |
| 126 | +} |
0 commit comments