2020-05-26 03:30:55 +01:00
const path = require ( "path" ) ;
const MonacoWebpackPlugin = require ( "monaco-editor-webpack-plugin" ) ;
const HtmlWebpackPlugin = require ( "html-webpack-plugin" ) ;
const { EnvironmentPlugin } = require ( "webpack" ) ;
const MiniCssExtractPlugin = require ( "mini-css-extract-plugin" ) ;
const CopyWebpackPlugin = require ( "copy-webpack-plugin" ) ;
const CleanWebpackPlugin = require ( "clean-webpack-plugin" ) ;
const CaseSensitivePathsPlugin = require ( "case-sensitive-paths-webpack-plugin" ) ;
const CreateFileWebpack = require ( "create-file-webpack" ) ;
const childProcess = require ( "child_process" ) ;
const BundleAnalyzerPlugin = require ( "webpack-bundle-analyzer" ) . BundleAnalyzerPlugin ;
const TerserPlugin = require ( "terser-webpack-plugin" ) ;
2020-08-12 21:25:53 +01:00
const isCI = require ( "is-ci" ) ;
2020-05-26 03:30:55 +01:00
const gitSha = childProcess . execSync ( "git rev-parse HEAD" ) . toString ( "utf8" ) ;
const cssRule = {
test : /\.css$/ ,
2021-01-20 15:15:01 +00:00
use : [ MiniCssExtractPlugin . loader , "css-loader" ] ,
2020-05-26 03:30:55 +01:00
} ;
const lessRule = {
test : /\.less$/ ,
use : [ MiniCssExtractPlugin . loader , "css-loader" , "less-loader" ] ,
2021-01-20 15:15:01 +00:00
exclude : [ path . resolve ( _ _dirname , "less/Common/Constants.less" ) ] ,
2020-05-26 03:30:55 +01:00
} ;
const imagesRule = {
test : /\.(jpg|jpeg|png|gif|svg|pdf|ico)$/ ,
loader : "file-loader" ,
options : {
2021-01-20 15:15:01 +00:00
name : "images/[name].[ext]" ,
} ,
2020-05-26 03:30:55 +01:00
} ;
const fontRule = {
test : /\.(woff|woff2|ttf|eot)$/ ,
loader : "file-loader" ,
options : {
2021-01-20 15:15:01 +00:00
name : "[name].[ext]" ,
} ,
2020-05-26 03:30:55 +01:00
} ;
const htmlRule = {
test : /\.html$/ ,
use : [
{
loader : "html-loader" ,
options : {
minify : false ,
removeComments : false ,
collapseWhitespace : false ,
2021-01-20 15:15:01 +00:00
root : path . resolve ( _ _dirname , "images" ) ,
} ,
} ,
] ,
2020-05-26 03:30:55 +01:00
} ;
// We compile our own code with ts-loader
const typescriptRule = {
test : /\.tsx?$/ ,
use : [
{
loader : "ts-loader" ,
options : {
2021-01-20 15:15:01 +00:00
transpileOnly : true ,
} ,
} ,
2020-05-26 03:30:55 +01:00
] ,
2021-01-20 15:15:01 +00:00
exclude : /node_modules/ ,
2020-05-26 03:30:55 +01:00
} ;
// Third party modules are compiled with babel since using ts-loader that much causes webpack to run out of memory
const ModulesRule = {
test : /\.js$/ ,
use : [
{
loader : "babel-loader" ,
options : {
cacheDirectory : ".cache/babel" ,
2021-01-20 15:15:01 +00:00
presets : [ [ "@babel/preset-env" , { targets : { ie : "11" } , useBuiltIns : false } ] ] ,
} ,
} ,
2020-05-26 03:30:55 +01:00
] ,
include : /node_modules/ ,
// Exclude large modules we know don't need transpiling
2021-01-20 15:15:01 +00:00
exclude : /vega|monaco|plotly/ ,
2020-05-26 03:30:55 +01:00
} ;
2021-01-20 15:15:01 +00:00
module . exports = function ( env = { } , argv = { } ) {
2020-05-26 03:30:55 +01:00
const mode = argv . mode || "development" ;
const rules = [ fontRule , lessRule , imagesRule , cssRule , htmlRule , typescriptRule ] ;
const envVars = {
GIT _SHA : gitSha ,
2021-01-20 15:15:01 +00:00
PORT : process . env . PORT || "1234" ,
2020-05-26 03:30:55 +01:00
} ;
if ( mode === "production" ) {
rules . push ( ModulesRule ) ;
envVars . NODE _ENV = "production" ;
}
if ( mode === "development" ) {
envVars . NODE _ENV = "development" ;
2021-01-19 22:31:55 +00:00
typescriptRule . use [ 0 ] . options . compilerOptions = { target : "ES2018" } ;
2020-05-26 03:30:55 +01:00
}
const plugins = [
new CleanWebpackPlugin ( [ "dist" ] ) ,
new CreateFileWebpack ( {
path : "./dist" ,
fileName : "version.txt" ,
2021-01-20 15:15:01 +00:00
content : ` ${ gitSha . trim ( ) } ${ new Date ( ) . toUTCString ( ) } ` ,
2020-05-26 03:30:55 +01:00
} ) ,
new CaseSensitivePathsPlugin ( ) ,
2020-07-17 02:35:18 +01:00
new MiniCssExtractPlugin ( {
2021-01-20 15:15:01 +00:00
filename : "[name].[contenthash].css" ,
2020-07-17 02:35:18 +01:00
} ) ,
2020-05-26 03:30:55 +01:00
new HtmlWebpackPlugin ( {
filename : "explorer.html" ,
template : "src/explorer.html" ,
2021-01-20 15:15:01 +00:00
chunks : [ "main" ] ,
2020-05-26 03:30:55 +01:00
} ) ,
new HtmlWebpackPlugin ( {
filename : "terminal.html" ,
template : "src/Terminal/index.html" ,
2021-01-20 15:15:01 +00:00
chunks : [ "terminal" ] ,
2020-05-26 03:30:55 +01:00
} ) ,
new HtmlWebpackPlugin ( {
filename : "quickstart.html" ,
template : "src/quickstart.html" ,
2021-01-20 15:15:01 +00:00
chunks : [ "quickstart" ] ,
2020-05-26 03:30:55 +01:00
} ) ,
new HtmlWebpackPlugin ( {
filename : "index.html" ,
template : "src/index.html" ,
2021-01-20 15:15:01 +00:00
chunks : [ "index" ] ,
2020-05-26 03:30:55 +01:00
} ) ,
new HtmlWebpackPlugin ( {
filename : "hostedExplorer.html" ,
template : "src/hostedExplorer.html" ,
2021-01-20 15:15:01 +00:00
chunks : [ "hostedExplorer" ] ,
2020-05-26 03:30:55 +01:00
} ) ,
2020-11-19 17:29:38 +00:00
new HtmlWebpackPlugin ( {
filename : "testExplorer.html" ,
2021-01-20 06:42:45 +00:00
template : "test/testExplorer/testExplorer.html" ,
2021-01-20 15:15:01 +00:00
chunks : [ "testExplorer" ] ,
2020-11-19 17:29:38 +00:00
} ) ,
2020-05-26 03:30:55 +01:00
new HtmlWebpackPlugin ( {
filename : "Heatmap.html" ,
template : "src/Controls/Heatmap/Heatmap.html" ,
2021-01-20 15:15:01 +00:00
chunks : [ "heatmap" ] ,
2020-05-26 03:30:55 +01:00
} ) ,
new HtmlWebpackPlugin ( {
filename : "notebookViewer.html" ,
2020-06-05 03:04:15 +01:00
template : "src/NotebookViewer/notebookViewer.html" ,
2021-01-20 15:15:01 +00:00
chunks : [ "notebookViewer" ] ,
2020-05-26 03:30:55 +01:00
} ) ,
new HtmlWebpackPlugin ( {
2020-07-16 07:41:05 +01:00
filename : "gallery.html" ,
2020-05-26 03:30:55 +01:00
template : "src/GalleryViewer/galleryViewer.html" ,
2021-01-20 15:15:01 +00:00
chunks : [ "galleryViewer" ] ,
2020-05-26 03:30:55 +01:00
} ) ,
new HtmlWebpackPlugin ( {
filename : "connectToGitHub.html" ,
template : "src/connectToGitHub.html" ,
2021-01-20 15:15:01 +00:00
chunks : [ "connectToGitHub" ] ,
2020-05-26 03:30:55 +01:00
} ) ,
2021-03-10 21:55:05 +00:00
new HtmlWebpackPlugin ( {
filename : "selfServe.html" ,
template : "src/SelfServe/selfServe.html" ,
chunks : [ "selfServe" ] ,
} ) ,
2020-05-26 03:30:55 +01:00
new MonacoWebpackPlugin ( ) ,
2020-06-18 14:39:47 +01:00
new CopyWebpackPlugin ( {
2021-01-20 15:15:01 +00:00
patterns : [ { from : "DataExplorer.nuspec" } , { from : "web.config" } , { from : "quickstart/*.zip" } ] ,
2020-06-18 14:39:47 +01:00
} ) ,
2021-01-20 15:15:01 +00:00
new EnvironmentPlugin ( envVars ) ,
2020-05-26 03:30:55 +01:00
] ;
if ( argv . analyze ) {
plugins . push ( new BundleAnalyzerPlugin ( ) ) ;
}
return {
mode : mode ,
entry : {
2020-11-13 02:10:59 +00:00
main : "./src/Main.tsx" ,
2020-05-26 03:30:55 +01:00
index : "./src/Index.ts" ,
quickstart : "./src/quickstart.ts" ,
2021-01-19 22:31:55 +00:00
hostedExplorer : "./src/HostedExplorer.tsx" ,
2021-01-20 06:42:45 +00:00
testExplorer : "./test/testExplorer/TestExplorer.ts" ,
2020-05-26 03:30:55 +01:00
heatmap : "./src/Controls/Heatmap/Heatmap.ts" ,
terminal : "./src/Terminal/index.ts" ,
2020-06-05 03:04:15 +01:00
notebookViewer : "./src/NotebookViewer/NotebookViewer.tsx" ,
2020-05-26 03:30:55 +01:00
galleryViewer : "./src/GalleryViewer/GalleryViewer.tsx" ,
2021-03-10 21:55:05 +00:00
selfServe : "./src/SelfServe/SelfServe.tsx" ,
2021-01-20 15:15:01 +00:00
connectToGitHub : "./src/GitHub/GitHubConnector.ts" ,
2020-05-26 03:30:55 +01:00
} ,
node : {
util : true ,
tls : "empty" ,
2021-01-20 15:15:01 +00:00
net : "empty" ,
2020-05-26 03:30:55 +01:00
} ,
output : {
chunkFilename : "[name].[chunkhash:6].js" ,
filename : "[name].[chunkhash:6].js" ,
2021-01-20 15:15:01 +00:00
path : path . resolve ( _ _dirname , "dist" ) ,
2020-05-26 03:30:55 +01:00
} ,
devtool : mode === "development" ? "cheap-eval-source-map" : "source-map" ,
plugins ,
module : {
2021-01-20 15:15:01 +00:00
rules ,
2020-05-26 03:30:55 +01:00
} ,
resolve : {
2021-01-20 15:15:01 +00:00
extensions : [ ".tsx" , ".ts" , ".js" ] ,
2020-05-26 03:30:55 +01:00
} ,
optimization : {
minimize : mode === "production" ? true : false ,
minimizer : [
new TerserPlugin ( {
cache : ".cache/terser" ,
terserOptions : {
// These options increase our initial bundle size by ~5% but the builds are significantly faster and won't run out of memory
compress : false ,
2021-01-20 15:15:01 +00:00
mangle : true ,
} ,
} ) ,
] ,
2020-05-26 03:30:55 +01:00
} ,
2020-08-12 21:25:53 +01:00
watch : isCI || mode === "production" ? false : true ,
// Hack since it is hard to disable watch entirely with webpack dev server https://github.com/webpack/webpack-dev-server/issues/1251#issuecomment-654240734
watchOptions : isCI ? { poll : 24 * 60 * 60 * 1000 } : { } ,
2020-05-26 03:30:55 +01:00
devServer : {
2020-08-13 02:12:31 +01:00
hot : false ,
2020-08-12 21:25:53 +01:00
inline : ! isCI ,
liveReload : ! isCI ,
2020-05-26 03:30:55 +01:00
https : true ,
host : "0.0.0.0" ,
port : envVars . PORT ,
stats : "minimal" ,
headers : {
"Access-Control-Allow-Origin" : "*" ,
"Access-Control-Allow-Credentials" : "true" ,
"Access-Control-Max-Age" : "3600" ,
"Access-Control-Allow-Headers" : "*" ,
2021-01-20 15:15:01 +00:00
"Access-Control-Allow-Methods" : "*" ,
2020-05-26 03:30:55 +01:00
} ,
proxy : {
"/api" : {
target : "https://main.documentdb.ext.azure.com" ,
changeOrigin : true ,
logLevel : "debug" ,
2021-01-20 15:15:01 +00:00
bypass : function ( req , res , proxyOptions ) {
2020-05-26 03:30:55 +01:00
if ( req . method === "OPTIONS" ) {
res . statusCode = 200 ;
res . send ( ) ;
}
2021-01-20 15:15:01 +00:00
} ,
2020-05-26 03:30:55 +01:00
} ,
"/proxy" : {
target : "https://main.documentdb.ext.azure.com" ,
changeOrigin : true ,
secure : false ,
logLevel : "debug" ,
pathRewrite : { "^/proxy" : "" } ,
2021-01-20 15:15:01 +00:00
router : ( req ) => {
2020-05-26 03:30:55 +01:00
let newTarget = req . headers [ "x-ms-proxy-target" ] ;
return newTarget ;
2021-01-20 15:15:01 +00:00
} ,
2020-05-26 03:30:55 +01:00
} ,
"/_explorer" : {
target : process . env . EMULATOR _ENDPOINT || "https://localhost:8081/" ,
changeOrigin : true ,
secure : false ,
2021-01-20 15:15:01 +00:00
logLevel : "debug" ,
2020-05-26 03:30:55 +01:00
} ,
"/explorerProxy" : {
target : process . env . EMULATOR _ENDPOINT || "https://localhost:8081/" ,
pathRewrite : { "^/explorerProxy" : "" } ,
changeOrigin : true ,
secure : false ,
2021-01-20 15:15:01 +00:00
logLevel : "debug" ,
} ,
} ,
2020-05-26 03:30:55 +01:00
} ,
stats : "minimal" ,
node : {
2021-01-20 15:15:01 +00:00
fs : "empty" ,
} ,
2020-05-26 03:30:55 +01:00
} ;
} ;