(There is a similar question. However, my Webpack configuration is significantly different, and now the official documentation doesn't seem to recommend using script-loader
, so I thought there might be a different solution to this question.)
Phoenix framework 1.4.0 uses Webpack by default. I was able to successfully configure it up to now.
Bootstrap.js requires jquery
and popper.js
to be loaded before it to work. So I specified:
import css from '../css/app.scss';
// webpack automatically bundles all modules in your
// entry points. Those entry points can be configured
// in "webpack.config.js".
//
// Import dependencies
//
import 'phoenix_html';
import 'jquery';
import 'popper.js';
import 'bootstrap';
// Import local files
//
// Local files can be imported directly using relative
// paths "./socket" or full ones "web/static/js/socket".
// import socket from "./socket"
import { startLesson } from './lesson/show.ts';
in my app.js
However, the generated app.js
looks like this:
/***/ "../deps/phoenix_html/priv/static/phoenix_html.js":
/*!********************************************************!*\
!*** ../deps/phoenix_html/priv/static/phoenix_html.js ***!
\********************************************************/
...
/***/ "./css/app.scss":
/*!**********************!*\
!*** ./css/app.scss ***!
\**********************/
...
/***/ "./js/app.js":
/*!*******************!*\
!*** ./js/app.js ***!
\*******************/
...
/***/ "./js/lesson/show.ts":
/*!***************************!*\
!*** ./js/lesson/show.ts ***!
\***************************/
...
/***/ "./node_modules/bootstrap/dist/js/bootstrap.js":
/*!*****************************************************!*\
!*** ./node_modules/bootstrap/dist/js/bootstrap.js ***!
\*****************************************************/
...
/***/ "./node_modules/jquery/dist/jquery.js":
/*!********************************************!*\
!*** ./node_modules/jquery/dist/jquery.js ***!
\********************************************/
...
/***/ "./node_modules/popper.js/dist/esm/popper.js":
/*!***************************************************!*\
!*** ./node_modules/popper.js/dist/esm/popper.js ***!
\***************************************************/
...
/***/ "./node_modules/webpack/buildin/global.js":
/*!***********************************!*\
!*** (webpack)/buildin/global.js ***!
\***********************************/
...
which apparently put the vendor JS files in alphabetical order and thus made bootstrap.js
unusable.
Actually, the official Bootstrap documentation suggests that only writing import 'bootstrap';
should be enough. However, that also resulted in jquery
and popper.js
being bundled after Bootstrap.js.
Also, I think the custom JS files, i.e. ./js/app.js
and ./js/lesson/show.ts
should be loaded after the dependencies are loaded?
How should I change the Webpack configuration so that the JS files are bundled in the correct order?
The current webpack.config.js
:
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
module.exports = (env, options) => ({
optimization: {
minimizer: [new UglifyJsPlugin({ cache: true, parallel: true, sourceMap: false }), new OptimizeCSSAssetsPlugin({})]
},
entry: './js/app.js',
// entry: './js/app.ts',
output: {
filename: 'app.js',
path: path.resolve(__dirname, '../priv/static/js')
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
},
{
test: /\.tsx?$/,
exclude: /node_modules/,
use: 'ts-loader'
},
{
test: /\.scss$/,
include: [path.resolve(__dirname, 'css')],
use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader']
}
]
},
plugins: [
new MiniCssExtractPlugin({ filename: '../css/app.css' }),
new CopyWebpackPlugin([{ from: 'static/', to: '../' }])
]
});