N
N
Ninja Mate2016-04-04 10:40:21
JavaScript
Ninja Mate, 2016-04-04 10:40:21

How to setup __webpack_public_path__ function at runtime?

I'm trying to set up a dynamic path for the build, the files are part of the wp plugin and the link to the exact directory is not known
in webpack, I read that you can specify __webpack_public_path__ in runtime, but I can't set it up correctly

//что то подобное, но не работает
var __webpack_public_path__ = window.__webpack_public_path__;
 output: {
    path: path.join(__dirname, 'dist'),
    filename: 'bundle.js',
    //publicPath:  __dirname + '/',
    //publicPath:  '/',
    //publicPath: path.join(__dirname, '/'),
    publicPath: __webpack_public_path__ ,

    chunkFilename: '[name].bundle.js'
  },

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nicholas, 2016-04-04
@victorzadorozhnyy

I solve it like this (at the beginning of the entry file):

var src = document.querySelector('[src*="bundle"]').getAttribute("src");
__webpack_public_path__ = src.substr(0, src.lastIndexOf("/") + 1);

Those. we write in the global variable __webpack_public_path__ the current path to bundle.js. Everything works fine, no additional configuration settings are needed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question