Justin Wood
Justin Wood

Reputation: 10041

webpack attempting to load file from incorrect path

I am using wasm-pack-plugin in order to compile some rust code into my webpack project.

I am serving my javascript from localhost:4000/js/app.js. I have the following in my app.js file.

import("../../pkg").then(module => {
  module.run_app();
})

Unfortunately, when I attempt to run my application, it tries to load localhost:4000/0.app.js. This is returning a 404. If I manually go to localhost:4000/js/0.app.js I get the file.

How can I tell webpack that it should be attempting to hit /js/0.app.js instead of 0.app.js?

Edit

webpack.config.js

const path = require('path')
const glob = require('glob')
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')
const webpack = require('webpack')
const WasmPackPlugin = require("@wasm-tool/wasm-pack-plugin");

module.exports = (env, options) => ({
  optimization: {
    minimizer: [
      new UglifyJsPlugin({ cache: true, parallel: true, sourceMap: false }),
      new OptimizeCSSAssetsPlugin({}),
    ],
  },
  entry: {
    './js/app.js': glob.sync('./vendor/**/*.js').concat(['./js/app.js']),
  },
  output: {
    filename: 'app.js',
    path: path.resolve(__dirname, '../priv/static/js'),
    webassemblyModuleFilename: "app.wasm"
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
        },
      },
      {
        test: /\.s[ca]ss$/,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader', // translates CSS into CommonJS
          {
            loader: 'postcss-loader', // Run post css actions
            options: {
              plugins: function () { // post css plugins, can be exported to postcss.config.js
                return [
                  require('precss'),
                  require('autoprefixer')
                ];
              }
            }
          },
          'sass-loader', // compiles Sass to CSS, using Node Sass by default
        ],
      },
      {
        test: /\.css$/,
        use: [MiniCssExtractPlugin.loader, 'css-loader'],
      },
      {
        test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
        use: [
          {
            loader: 'file-loader',
            options: {
              name: '[name].[ext]',
              outputPath: 'fonts/'
            }
          }
        ]
      },
      {
        test: /\.(png|jpe?g|gif|ico)$/,
        loader: 'file-loader?name=assets/[name].[hash].[ext]'
      },
      {
        test: /\.elm$/,
        exclude: [/elm-stuff/, /node_modules/],
        use: {
          loader: 'elm-webpack-loader',
          options: {
            cwd: './elm'
          }
        }
      }
    ],
  },
  plugins: [
    new MiniCssExtractPlugin({ filename: '../css/app.css' }),
    new CopyWebpackPlugin([{ from: 'static/', to: '../' }]),
    new webpack.ProvidePlugin({
      $: 'jquery',
      jQuery: 'jquery',
      bootstrap: 'bootstrap-sass',
      moment: 'moment'
    }),
    new WasmPackPlugin({
      crateDirectory: path.resolve(__dirname, "dashboard_employee"),
    })
  ],
})

I am using Phoenix Framework, which is why I am putting the output in priv/static/js.

Upvotes: 1

Views: 550

Answers (1)

skovy
skovy

Reputation: 5650

You can try setting the output.publicPath:

output: {
  publicPath: "/js/"
}

Upvotes: 3

Related Questions