Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support setPublicPath with env SET_PUBLIC_PATH #633

Merged
merged 1 commit into from
Mar 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/dead-simple/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script>
window.__webpack_public_path__ = '/static/xxxxxxxxxx/';
</script>
</head>
<body>

Expand Down
1 change: 1 addition & 0 deletions examples/dead-simple/src/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('a');
2 changes: 2 additions & 0 deletions examples/dead-simple/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ const app = dva();
app.model(require('./model').default);
app.router(() => <App />);
app.start(document.getElementById('root'));

import('./a');
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"files": [
"bin",
"src",
"lib"
"lib",
"template"
],
"scripts": {
"build": "node scripts/build.js",
Expand Down
23 changes: 18 additions & 5 deletions src/utils/getEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,8 @@ export default function(opts = {}) {
);
}

// Add HotDevClient
if (isBuild) {
return entryObj;
} else {
return Object.keys(entryObj).reduce(
if (!isBuild) {
entryObj = Object.keys(entryObj).reduce(
(memo, key) =>
!Array.isArray(entryObj[key])
? {
Expand All @@ -52,6 +49,22 @@ export default function(opts = {}) {
{},
);
}

// add setPublicPath
const setPublicPathFile = join(__dirname, '../../template/setPublicPath.js');
if (process.env.SET_PUBLIC_PATH) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

请问,我已经添加 SET_PUBLIC_PATH 环境变量,为何 本地访问 webpack_public_path 还是 undefined
我是模板与资源2台服务器, 运行时设置__webpack_public_path__ 应该就是用这个方案吧?
这是我的代码,在index.js最上方 引入,可以帮忙看看么
image

entryObj = Object.keys(entryObj).reduce((memo, key) => {
return {
...memo,
[key]: [
setPublicPathFile,
...(Array.isArray(entryObj[key]) ? entryObj[key] : [entryObj[key]]),
],
};
}, {});
}

return entryObj;
}

function getEntry(filePath) {
Expand Down
3 changes: 3 additions & 0 deletions template/setPublicPath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
if (window['__webpack_public_path__']) {
__webpack_public_path__ = window['__webpack_public_path__'];
}