Permalink
Browse files

feat: manifest add absolute path and complete path support (#20)

<!--
Thank you for your pull request. Please review below requirements.
Bug fixes and new features should include tests and possibly benchmarks.
Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md

感谢您贡献代码。请确认下列 checklist 的完成情况。
Bug 修复和新功能必须包含测试,必要时请附上性能测试。
Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md
-->

##### Checklist
<!-- Remove items that do not apply. For completed items, change [ ] to [x]. -->

- [x] `npm test` passes
- [x] tests and/or benchmarks are included
- [ ] documentation is changed or added
- [x] commit message follows commit guidelines

##### Description of change
<!-- Provide a description of the change below this comment. -->
由于 webpack 设置 publicPath 后 manifest 会包含比较完整或相对的路径。
例如 umi :https://github.com/umijs/umi/blob/master/packages/af-webpack/src/getConfig/index.js#L38

所以需要在构建最后资源文件路径时候先判断 manifest 中对应的 urlpath 形式。
  • Loading branch information...
okoala authored and popomore committed Aug 15, 2018
1 parent c4ae975 commit 6d9be84495ae1e789e20276f33f184e736cf7e86
@@ -62,6 +62,15 @@ class Assets {
urlpath = this.manifest[urlpath];
assert(urlpath, `Don't find ${entry} in manifest.json`);
}

if (urlpath.startsWith('/')) {
return `${this.host}${urlpath}`;
}

if (urlpath.startsWith('http://') || urlpath.startsWith('https://')) {
return urlpath;
}

return `${this.resourceBase}${urlpath}`;
}

@@ -18,25 +18,25 @@
"await-event": "^2.1.0",
"cross-spawn": "^6.0.5",
"debug": "^3.1.0",
"detect-port": "^1.2.2",
"detect-port": "^1.2.3",
"mz": "^2.7.0",
"mz-modules": "^2.1.0",
"sdk-base": "^3.4.0",
"utility": "^1.13.1"
"sdk-base": "^3.5.0",
"utility": "^1.14.0"
},
"devDependencies": {
"autod": "^3.0.1",
"autod-egg": "^1.1.0",
"egg": "^2.6.1",
"egg-bin": "^4.6.3",
"egg": "^2.10.0",
"egg-bin": "^4.8.1",
"egg-ci": "^1.8.0",
"egg-mock": "^3.17.0",
"egg-mock": "^3.19.2",
"egg-view-ejs": "^2.0.0",
"egg-view-nunjucks": "^2.1.6",
"eslint": "^4.19.1",
"eslint": "^5.3.0",
"eslint-config-egg": "^7.0.0",
"puppeteer": "^1.3.0",
"supertest": "^3.0.0",
"puppeteer": "^1.7.0",
"supertest": "^3.1.0",
"uglify-js": "^3.3.21",
"webstorm-disable-index": "^1.2.0"
},
@@ -402,4 +402,50 @@ describe('test/assets.test.js', () => {
await app.ready();
});
});

describe('complex manifest', () => {
let app;
before(() => {
mock.env('default');
app = mock.app({
baseDir: 'apps/complex-manifest',
});
return app.ready();
});

after(() => app.close());
afterEach(mock.restore);

it('should publicPath work', () => {
const ctx = app.mockContext();
ctx.helper.assets.setEntry('index.js');
const script = ctx.helper.assets.getScript();
assert(script.includes('__webpack_public_path__ = \'/public\/\';'));
assert(script.includes('src="/public/index.js"'));
});

it('should contain host if setting assets.url', () => {
mock(app.config.assets, 'url', 'http://remotehost');
const ctx = app.mockContext();
ctx.helper.assets.setEntry('index.js');
const script = ctx.helper.assets.getScript();
assert(script.includes('__webpack_public_path__ = \'/public\/\';'));
assert(script.includes('src="http://remotehost/public/index.js"'));
const style = ctx.helper.assets.getStyle();
assert(style.includes('href="http://remotehost/index.css"'));
});

it('should assets.publicPath not work if resource path is a absolute url', () => {
const ctx = app.mockContext();
const style = ctx.helper.assets.getStyle('index.css');
assert(style.includes('href="/index.css"'));
});

it('should assets.url not work if resource path is a complete url', () => {
mock(app.config.assets, 'url', 'http://remotehost');
const ctx = app.mockContext();
const script = ctx.helper.assets.getScript('page1.js');
assert(script.includes('src="http://cdn.com/page1.js"'));
});
});
});
@@ -0,0 +1,13 @@
'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
async index() {
await this.ctx.render('index.js', {
data: 1,
});
}
}

module.exports = HomeController;
@@ -0,0 +1 @@
body { color: white; }
@@ -0,0 +1,4 @@
'use strict';

/* global window */
console.log('data:', window.context.data);
@@ -0,0 +1,7 @@
'use strict';

module.exports = app => {
const { router, controller } = app;

router.get('/', controller.home.index);
};
@@ -0,0 +1,12 @@
<!doctype html>
<html>
<head>
{{ helper.assets.getStyle() | safe }}
</head>
<body>
<div id="root"></div>
<script>window.atob = undefined;</script>
{{ helper.assets.getContext() | safe }}
{{ helper.assets.getScript() | safe }}
</body>
</html>
@@ -0,0 +1,16 @@
'use strict';

const path = require('path');

exports.keys = '123456';
exports.view = {
mapping: {
'.js': 'assets',
'.jsx': 'assets',
},
};
exports.assets = {
publicPath: '/public/',
templateViewEngine: 'nunjucks',
templatePath: path.join(__dirname, '../app/view/layout.html'),
};
@@ -0,0 +1,5 @@
{
"index.js": "index.js",
"index.css": "/index.css",
"page1.js": "http://cdn.com/page1.js"
}
@@ -0,0 +1,8 @@
'use strict';

module.exports = {
nunjucks: {
enable: true,
package: 'egg-view-nunjucks',
},
};
@@ -0,0 +1,3 @@
{
"name": "egg-view-assets"
}
@@ -0,0 +1 @@
body { color: white; }

0 comments on commit 6d9be84

Please sign in to comment.