Skip to content

Instantly share code, notes, and snippets.

@Risto-Stevcev
Last active October 14, 2019 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Risto-Stevcev/1b24baf0b6b5a8c0965ee4f32a10ed7a to your computer and use it in GitHub Desktop.
Save Risto-Stevcev/1b24baf0b6b5a8c0965ee4f32a10ed7a to your computer and use it in GitHub Desktop.
Attempt at tree shaking and code splitting combo for bs-declaredom
let _ = Js.log "hello world!"
let x =
let open RistostevcevBsDeclaredom.Html in
div [|text "foobar"|]
let _ =
if Js.Option.isSome HotModuleReload.module_hot then
let _ = HotModuleReload.on_accept @@ fun () -> Js.log "accept: reloading..."
and _ = HotModuleReload.on_dispose @@ fun () -> Js.log "dispose: reloading..."
in ()
const path = require('path');
const fs = require('fs')
const dir_js = path.resolve(__dirname, 'lib/es6_global/src/');
const dir_build = path.resolve(__dirname, 'dist');
const declaredom = 'node_modules/@ristostevcev/bs-declaredom/lib/es6_global/src/'
/**
NOTE: this currently doesn't work very well. The resulting code split ends up significantly larger than the
tree shaking alone, which shouldn't be the case. It should just have the declaredom stuff in a separate file so
that it can be versioned and cached by the browser, resulting in fast load times
**/
/*
```sh
Built at: 10/12/2019 12:58:05 PM
Asset Size Chunks Chunk Names
declaredom.bundle.js 1.67 KiB 1 [emitted] declaredom
declaredom.bundle.js.map 8.17 KiB 1 [emitted] [dev] declaredom
index.bundle.js 2.58 KiB 2 [emitted] index
index.bundle.js.map 11.3 KiB 2 [emitted] [dev] index
vendors~declaredom.bundle.js 193 KiB 3 [emitted] vendors~declaredom
vendors~declaredom.bundle.js.map 799 KiB 3 [emitted] [dev] vendors~declaredom
vendors~declaredom~index.bundle.js 195 KiB 0 [emitted] vendors~declaredom~index
vendors~declaredom~index.bundle.js.map 1.22 MiB 0 [emitted] [dev] vendors~declaredom~index
```
*/
module.exports = {
entry: {
index: path.resolve(dir_js, 'Frontend.bs.js'),
declaredom: [].concat(
fs.readdirSync(declaredom)
.filter(e => e.endsWith('.bs.js'))
.map(filename => path.resolve(__dirname, declaredom, filename)),
fs.readdirSync(declaredom + 'css/')
.map(filename => path.resolve(__dirname, declaredom, 'css', filename)),
fs.readdirSync(declaredom + 'html/')
.map(filename => path.resolve(__dirname, declaredom, 'html', filename))
)
},
output: {
filename: '[name].bundle.js',
chunkFilename: '[name].bundle.js',
path: path.resolve(__dirname, 'dist'),
},
devServer: {
contentBase: dir_build,
},
module: {
rules: [
{
sideEffects: false,
test: /\.m?js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'],
plugins: ['@babel/plugin-proposal-object-rest-spread']
}
}
}
]
},
stats: {
// Nice colored output
colors: true
},
optimization: {
splitChunks: {
chunks: 'all',
},
},
mode: 'production',
// Create source maps for the bundle
devtool: 'source-map',
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment