最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Example of setting webpack public path at runtime - Stack Overflow

programmeradmin3浏览0评论

I am struggling to find an example of how to set the public path of an output file of a webpack bundle.

The documentation says:

If you don't know the publicPath while piling, you can omit it and set __webpack_public_path__ on your entry point.

Like so:

__webpack_public_path__ = myRuntimePublicPath

Would anyone be kind enough to create a JSFiddle example how this can be done?

I am struggling to find an example of how to set the public path of an output file of a webpack bundle.

The documentation says:

If you don't know the publicPath while piling, you can omit it and set __webpack_public_path__ on your entry point.

Like so:

__webpack_public_path__ = myRuntimePublicPath

Would anyone be kind enough to create a JSFiddle example how this can be done?

Share Improve this question edited Oct 5, 2016 at 17:26 CLJ asked Oct 5, 2016 at 16:51 CLJCLJ 1,9276 gold badges22 silver badges38 bronze badges 2
  • This is exactly how you do it. Make sure this is one of the first statements to run in your application. – Mario Tacke Commented Oct 5, 2016 at 17:16
  • For example in the index.html? – vlio20 Commented Jan 15, 2017 at 21:34
Add a ment  | 

1 Answer 1

Reset to default 14

Nothing has changed after almost two years. It's still surprisingly difficult to find an example of setting public path for webpack at runtime.

Prerequisites

  • webpack 4.5.0
  • an app big enough to leverage code splitting

For simplicity let's say our html lives in index.html and app entry point is app.js

An example that works

index.html

<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
    <div id="app"></div>
    <script type="application/javascript">
        window.resourceBasePath = '/path/to/scripts/on/server/';
    </script>
    <script type="application/javascript" src="/path/to/scripts/on/server/app.js"></script>
</body>
</html>

app.js

// it is important to set global var before any imports
__webpack_public_path__ = window.resourceBasePath;

import React from 'react';
import ReactDOM from 'react-dom';
import {store, history} from './store';

const render = () => {
    import('./root').then((module) => {
        const Root = module.default;

        ReactDOM.render(
            <Root
                store={store}
                history={history}
            />,
            document.getElementById('app'),
        );
    });
};

render();

if (module.hot) {
    module.hot.accept('./root', render);
}

An example that doesn't work

Webpack publicPath documentation says it's enough just to set a global variable with the right name. I did that:

index.html

<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
    <div id="app"></div>
    <script type="application/javascript">
        __webpack_public_path__ = '/path/to/scripts/on/server/';
    </script>
    <script type="application/javascript" src="/path/to/scripts/on/server/app.js"></script>
</body>
</html>

app.js

import React from 'react';
import ReactDOM from 'react-dom';
import {store, history} from './store';

const render = () => {
    import('./root').then((module) => {
        const Root = module.default;

        ReactDOM.render(
            <Root
                store={store}
                history={history}
            />,
            document.getElementById('app'),
        );
    });
};

render();

if (module.hot) {
    module.hot.accept('./root', render);
}

In this case my app fails plaining in console it couldn't load 0.js from current path to index.html. Which means that setting public path didn't have any impact.

发布评论

评论列表(0)

  1. 暂无评论