2

(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: '../' }])
  ]
});
xji
  • 7,341
  • 4
  • 40
  • 61
  • have you used providePlugin to allow popper and jquery to be in the global scope? – PlayMa256 Aug 14 '18 at 17:31
  • @PlayMa256 Should I do that? I thought it sufficed to just use `import` as indicated by the Bootstrap documentation. Actually I can invoke jquery but problems are occurring because of the order. Guess I'll try to set up `providePlugin` and see if it works. Wonder if it will have negative effects such as increasing bundle size though I guess it shouldn't. – xji Aug 14 '18 at 18:04
  • No, it does not increase, just adds that as "global" – PlayMa256 Aug 14 '18 at 18:06
  • 1
    @PlayMa256 I tried to add `new webpack.ProvidePlugin({ $: 'jquery', jQuery: 'jquery', Popper: 'popper.js' }),` to `plugins` in Webpack config, but it didn't seem to have any effect... I can't even find `jQuery` and `Popper` in the browser window for some reason, only `$` is there. Seems that no code actually explicitly required them, so they aren't loaded by Webpack anyways? – xji Aug 14 '18 at 18:21
  • probably, i hate working with jquery. its been 4 years i had used it. – PlayMa256 Aug 14 '18 at 18:36
  • @PlayMa256 Same. I just use plain JS wherever I can. Unfortunately Bootstrap is bundled with it. I'm trying to use Bootstrap modal and am getting the error as described in https://stackoverflow.com/questions/25757968/bootstrap-modal-is-not-a-function, so it seems that there's some problem with the loading order of JS files. – xji Aug 14 '18 at 20:44
  • Try using react-bootstrap, is a jquery-less alternative – PlayMa256 Aug 14 '18 at 23:40

0 Answers0