UNPKG

@thebigcrunch/sdk

Version:
242 lines (227 loc) 6.78 kB
// // Provides webpack config for The Big Crunch SDK. // require("dotenv").config(); const webpack = require("webpack"); const path = require("path"); const UglifyJsPlugin = require("uglifyjs-webpack-plugin"); const HtmlWebpackPlugin = require("html-webpack-plugin"); const ExtractTextPlugin = require("extract-text-webpack-plugin"); const MinifyPlugin = require("babel-minify-webpack-plugin"); const configure = ({ config, entryPath, libraryName, fileName, devServer, devtool = "source-map", plugins = [], publicPath = "/public", mode }) => { const result = { entry: { index: entryPath }, output: { filename: `${fileName}.js`, sourceMapFilename: `${fileName}.map`, path: path.join(__dirname, "build"), publicPath }, module: { rules: [ { test: /\.js?$/, exclude: /(node_modules)/, loader: "babel-loader" }, { test: /\.(s*)css$/, use: ExtractTextPlugin.extract({ fallback: "style-loader", use: ["css-loader", "sass-loader"] }) }, { test: /\.(png|jp(e*)g|svg)$/, use: [ { loader: "url-loader", options: { limit: 8, // Convert images < 8kb to base64 strings name: "images/[hash]-[name].[ext]" } } ] }, { test: /\.html$/, use: [ { loader: "html-loader", options: { attrs: "img:src" } } ] } ] }, plugins: [ new webpack.IgnorePlugin(/locale/, /moment$/), new webpack.LoaderOptionsPlugin({ debug: true }), new webpack.DefinePlugin({ CONFIG: JSON.stringify(config) }) ], mode }; result.plugins = result.plugins.concat(plugins); if (devServer) { result.devServer = { contentBase: path.join(__dirname, "build"), port: config.sdkPort }; } if (libraryName) { result.output.library = libraryName; result.output.libraryExport = "default"; result.output.libraryTarget = "umd"; result.output.globalObject = `typeof self !== 'undefined' ? self : this`; } if (config.nodeEnv === "production") { result.plugins.push(new UglifyJsPlugin()); result.plugins.push(new webpack.optimize.AggressiveMergingPlugin()); result.plugins.push(new MinifyPlugin()); } else { result.devtool = devtool; } return result; }; const productionConfig = { BIG_CRUNCH_DOMAIN: "blackhole.bigcrunch.io", NGINX_MAIN_PORT: 443, BIG_CDN_DOMAIN: "rur.bigcrunch.io", S3_DOMAIN_NAME: "https://dc8bsfv4udm20.cloudfront.net", ASSETS_PROVIDER: "s3", BIG_GA_CODE: "UA-106748582-1", BIG_APP_STORE_PATH: "widgets", BIG_MAIN_WEBSERVER_VIRUTAL_PATH: "TheWebServer", BIG_SDK_URL: `https://rur.bigcrunch.io/tbc_sdk.js`, BIG_WEB_VIEW_URL: "https://bigcrunch.io" }; const testConfig = { BIG_CRUNCH_DOMAIN: "blackhole.123456.bigcrunch.io", NGINX_MAIN_PORT: 443, BIG_CDN_DOMAIN: "rur.123456.bigcrunch.io", S3_DOMAIN_NAME: "https://rur.123456.bigcrunch.io", ASSETS_PROVIDER: "s3", BIG_GA_CODE: "UA-106748582-2", BIG_APP_STORE_PATH: "widgets", BIG_MAIN_WEBSERVER_VIRUTAL_PATH: "TheWebServer", BIG_SDK_URL: `https://rur.123456.bigcrunch.io/tbc_sdk.js`, BIG_WEB_VIEW_URL: "https://123456.bigcrunch.io" }; let env = process.env; if (env.NODE_ENV === "production") { env = productionConfig; } else if (env.NODE_ENV === "test") { env = testConfig; } const nodeEnv = env.NODE_ENV; const nginxMainPort = env.NGINX_MAIN_PORT; const bigCrunchDomain = env.BIG_CRUNCH_DOMAIN; const protocol = nginxMainPort === 443 ? "https" : "http"; const spacesSocketUrl = `${protocol}://${bigCrunchDomain}:${nginxMainPort}/spaces`; const assetProvider = env.ASSETS_PROVIDER; const s3Domain = env.S3_DOMAIN_NAME; const cdnDomain = env.BIG_CDN_DOMAIN; const imageUrl = "cell_images"; const videoUrl = "cell_videos"; const audioUrl = "cell_audios"; const isS3AssetEnabled = assetProvider === "s3"; const appStoreImagesUrl = isS3AssetEnabled ? "images/apps" : "app_store_images"; const sdkConnectionUrl = `${protocol}://${bigCrunchDomain}`; const appStoreEndPoint = env.BIG_APP_STORE_PATH; const webserver = env.BIG_MAIN_WEBSERVER_VIRUTAL_PATH; const vizziesUrl = `${webserver}/v`; const gaCode = env.BIG_GA_CODE; const sdkPort = env.TBC_SDK_PORT; const mode = nodeEnv === "production" ? nodeEnv : "development"; const sdkUrl = env.BIG_SDK_URL; const webViewUrl = env.BIG_WEB_VIEW_URL; const sdkConfig = { spacesSocketUrl, vizziesUrl, nodeEnv, nginxMainPort, sdkConnectionUrl, imageUrl, videoUrl, audioUrl, cdnDomain, bigCrunchDomain, s3Domain, assetProvider, isS3AssetEnabled, appStoreImagesUrl, appStoreEndPoint, sdkPort, webViewUrl }; const embedConfig = { spacesSocketUrl, sdkUrl, nodeEnv, webViewUrl }; const odoConfig = { nodeEnv, gaCode, webViewUrl }; module.exports = [ configure({ entryPath: "./src", config: sdkConfig, libraryName: "TBC", fileName: "tbc_sdk", devServer: true, mode }), configure({ entryPath: "./embed", config: embedConfig, fileName: "tbc_embed", devServer: false, devtool: "inline-source-map", mode }), configure({ entryPath: "./odo", config: odoConfig, fileName: "tbc_odo", devServer: false, devtool: "inline-source-map", mode, publicPath: "/", plugins: [ new HtmlWebpackPlugin({ filename: "odo/index.html", template: "odo/index.html", inject: "body", sdk: "/tbc_sdk.js" }) ] }), configure({ entryPath: "./src", config: sdkConfig, libraryName: "TBC", fileName: "tbc_sdk_node", devServer: true, mode, target: "node" }) ];