|
| 1 | +const webpack = require('webpack'); |
| 2 | +const path = require('path'); |
| 3 | +const ExtractTextPlugin = require('extract-text-webpack-plugin'); |
| 4 | +const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin; |
| 5 | +const UglifyJsPlugin = require('uglifyjs-webpack-plugin'); |
| 6 | +const { CheckerPlugin } = require('awesome-typescript-loader'); |
| 7 | +const dir = require('node-dir'); |
| 8 | +const md5file = require('md5-file'); |
| 9 | +const crypto = require('crypto'); |
| 10 | + |
| 11 | +const env = process.env.ENV || "production"; |
| 12 | +const isProdBuild = process.env.ENV === "production"; |
| 13 | +const nodeEnv = isProdBuild ? "production" : "development"; |
| 14 | + |
| 15 | +async function getStylesheetsHash() { |
| 16 | + const styleSheetsPath = "src/stylesheets"; |
| 17 | + |
| 18 | + return await new Promise((resolve, reject) => { |
| 19 | + dir.files(styleSheetsPath, async (err, files) => { |
| 20 | + if (err) throw err; |
| 21 | + const filteredFiles = files.filter(filePath => { |
| 22 | + console.log("CSS Stylesheet:", filePath); |
| 23 | + const fileName = path.basename(filePath); |
| 24 | + if (fileName.endsWith(".scss")) { |
| 25 | + // Only hash SCSS source files |
| 26 | + return true; |
| 27 | + } |
| 28 | + }); |
| 29 | + if (filteredFiles.length === 0) { |
| 30 | + reject( |
| 31 | + `No .scss files were found in ${styleSheetsPath}, but SCSS files were expected. SCSS stylesheets in this directory are MD5 hashed and added as a build-time variable so loading the stylesheet from the global CDN always loads the correct version.` |
| 32 | + ); |
| 33 | + } |
| 34 | + let hashes = []; |
| 35 | + for (let styleSheetPath of filteredFiles) { |
| 36 | + const hash = md5file.sync(styleSheetPath); |
| 37 | + hashes.push(hash); |
| 38 | + } |
| 39 | + // Strangely enough, the order is inconsistent so we have to sort the hashes |
| 40 | + hashes = hashes.sort(); |
| 41 | + const joinedHashesStr = hashes.join("-"); |
| 42 | + const combinedHash = crypto.createHash("md5").update(joinedHashesStr).digest("hex"); |
| 43 | + console.log(`MD5 hash of SCSS source files in ${styleSheetsPath} is ${combinedHash}.`); |
| 44 | + resolve(combinedHash); |
| 45 | + }); |
| 46 | + }); |
| 47 | +} |
| 48 | + |
| 49 | +async function getWebpackPlugins() { |
| 50 | + const plugins = [ |
| 51 | + new CheckerPlugin(), |
| 52 | + new webpack.optimize.ModuleConcatenationPlugin(), |
| 53 | + new ExtractTextPlugin("OneSignalSDKStyles.css"), |
| 54 | + new webpack.DefinePlugin({ |
| 55 | + __DEV__: env === "development", |
| 56 | + __TEST__: !!process.env.TESTS, |
| 57 | + __STAGING__: env === "staging", |
| 58 | + __VERSION__: process.env.npm_package_config_sdkVersion, |
| 59 | + __LOGGING__: env === "development", |
| 60 | + __SRC_STYLESHEETS_MD5_HASH__: JSON.stringify(await getStylesheetsHash()), |
| 61 | + "process.env.NODE_ENV": JSON.stringify(nodeEnv), |
| 62 | + }) |
| 63 | + ]; |
| 64 | + if (!!process.env.ANALYZE) { |
| 65 | + const sizeAnalysisReportPath = path.resolve(path.join('build', 'size-analysis.html')); |
| 66 | + plugins.push( |
| 67 | + new BundleAnalyzerPlugin({ |
| 68 | + // Can be `server`, `static` or `disabled`. |
| 69 | + // In `server` mode analyzer will start HTTP server to show bundle report. |
| 70 | + // In `static` mode single HTML file with bundle report will be generated. |
| 71 | + // In `disabled` mode you can use this plugin to just generate Webpack Stats JSON file by setting `generateStatsFile` to `true`. |
| 72 | + analyzerMode: 'static', |
| 73 | + // Path to bundle report file that will be generated in `static` mode. |
| 74 | + // Relative to bundles output directory. |
| 75 | + reportFilename: sizeAnalysisReportPath, |
| 76 | + // Module sizes to show in report by default. |
| 77 | + // Should be one of `stat`, `parsed` or `gzip`. |
| 78 | + // See "Definitions" section for more information. |
| 79 | + defaultSizes: 'gzip', |
| 80 | + // Automatically open report in default browser |
| 81 | + openAnalyzer: false, |
| 82 | + // If `true`, Webpack Stats JSON file will be generated in bundles output directory |
| 83 | + generateStatsFile: false, |
| 84 | + // Name of Webpack Stats JSON file that will be generated if `generateStatsFile` is `true`. |
| 85 | + // Relative to bundles output directory. |
| 86 | + statsFilename: 'stats.json', |
| 87 | + // Options for `stats.toJson()` method. |
| 88 | + // For example you can exclude sources of your modules from stats file with `source: false` option. |
| 89 | + // See more options here: https://github.com/webpack/webpack/blob/webpack-1/lib/Stats.js#L21 |
| 90 | + statsOptions: null, |
| 91 | + // Log level. Can be 'info', 'warn', 'error' or 'silent'. |
| 92 | + logLevel: 'info' |
| 93 | + }) |
| 94 | + ); |
| 95 | + } |
| 96 | + return plugins; |
| 97 | +} |
| 98 | + |
| 99 | +async function generateWebpackConfig() { |
| 100 | + return { |
| 101 | + target: 'web', |
| 102 | + entry: { |
| 103 | + 'OneSignalSDK.js': path.resolve('build/ts-to-es6/src/entries/sdk.js'), |
| 104 | + 'OneSignalSDKStyles.css': path.resolve('src/entries/stylesheet.scss'), |
| 105 | + }, |
| 106 | + output: { |
| 107 | + path: path.resolve('build/bundles'), |
| 108 | + filename: '[name]' |
| 109 | + }, |
| 110 | + mode: process.env.ENV === "production" ? "production" : "development", |
| 111 | + optimization: { |
| 112 | + minimizer: [ |
| 113 | + new UglifyJsPlugin({ |
| 114 | + sourceMap: true, |
| 115 | + uglifyOptions: { |
| 116 | + sourceMap: true, |
| 117 | + compress: { |
| 118 | + drop_console: false, |
| 119 | + drop_debugger: false, |
| 120 | + warnings: false, |
| 121 | + }, |
| 122 | + mangle: process.env.ENV === 'production' ? { |
| 123 | + reserved: [ |
| 124 | + 'AlreadySubscribedError', |
| 125 | + 'InvalidArgumentError', |
| 126 | + 'InvalidStateError', |
| 127 | + 'NotSubscribedError', |
| 128 | + 'PermissionMessageDismissedError', |
| 129 | + 'PushNotSupportedError', |
| 130 | + 'PushPermissionNotGrantedError', |
| 131 | + 'SdkInitError', |
| 132 | + 'TimeoutError' |
| 133 | + ] |
| 134 | + } : false, |
| 135 | + output: { |
| 136 | + comments: false |
| 137 | + } |
| 138 | + } |
| 139 | + }) |
| 140 | + ] |
| 141 | + }, |
| 142 | + module: { |
| 143 | + rules: [ |
| 144 | + { |
| 145 | + test: /\.js$/, |
| 146 | + exclude: /node_modules/, |
| 147 | + use: [ |
| 148 | + { |
| 149 | + loader: 'awesome-typescript-loader', |
| 150 | + options: { |
| 151 | + configFileName: "build/config/tsconfig.es5.json" |
| 152 | + } |
| 153 | + }, |
| 154 | + ] |
| 155 | + }, |
| 156 | + { |
| 157 | + test: /\.scss$/, |
| 158 | + use: ExtractTextPlugin.extract({ |
| 159 | + use: [ |
| 160 | + { |
| 161 | + loader: 'css-loader', |
| 162 | + options: { |
| 163 | + sourceMap: true, |
| 164 | + minimize: true |
| 165 | + } |
| 166 | + }, |
| 167 | + { |
| 168 | + loader: 'postcss-loader', |
| 169 | + options: { |
| 170 | + plugins: function() { |
| 171 | + return [require('autoprefixer')]; |
| 172 | + } |
| 173 | + } |
| 174 | + }, |
| 175 | + 'sass-loader' |
| 176 | + ] |
| 177 | + }) |
| 178 | + } |
| 179 | + ] |
| 180 | + }, |
| 181 | + resolve: { |
| 182 | + extensions: ['.js', '.ts'], |
| 183 | + modules: [ |
| 184 | + 'build/ts-to-es6', |
| 185 | + 'build/ts-to-es6/src', |
| 186 | + 'node_modules' |
| 187 | + ] |
| 188 | + }, |
| 189 | + devtool: 'source-map', |
| 190 | + plugins: await getWebpackPlugins(), |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +module.exports = generateWebpackConfig(); |
0 commit comments