Navigation Menu

Skip to content

Commit

Permalink
feat: manifest add absolute path and complete path support (#20)
Browse files Browse the repository at this point in the history
<!--
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 6d9be84
Show file tree
Hide file tree
Showing 14 changed files with 134 additions and 9 deletions.
9 changes: 9 additions & 0 deletions lib/assets_context.js
Expand Up @@ -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}`;
}

Expand Down
18 changes: 9 additions & 9 deletions package.json
Expand Up @@ -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"
},
Expand Down
46 changes: 46 additions & 0 deletions test/assets.test.js
Expand Up @@ -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"'));
});
});
});
13 changes: 13 additions & 0 deletions test/fixtures/apps/complex-manifest/app/controller/home.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;
1 change: 1 addition & 0 deletions test/fixtures/apps/complex-manifest/app/public/index.css
@@ -0,0 +1 @@
body { color: white; }
4 changes: 4 additions & 0 deletions test/fixtures/apps/complex-manifest/app/public/index.js
@@ -0,0 +1,4 @@
'use strict';

/* global window */
console.log('data:', window.context.data);
7 changes: 7 additions & 0 deletions test/fixtures/apps/complex-manifest/app/router.js
@@ -0,0 +1,7 @@
'use strict';

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

router.get('/', controller.home.index);
};
Empty file.
12 changes: 12 additions & 0 deletions test/fixtures/apps/complex-manifest/app/view/layout.html
@@ -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>
16 changes: 16 additions & 0 deletions test/fixtures/apps/complex-manifest/config/config.default.js
@@ -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'),
};
5 changes: 5 additions & 0 deletions test/fixtures/apps/complex-manifest/config/manifest.json
@@ -0,0 +1,5 @@
{
"index.js": "index.js",
"index.css": "/index.css",
"page1.js": "http://cdn.com/page1.js"
}
8 changes: 8 additions & 0 deletions test/fixtures/apps/complex-manifest/config/plugin.js
@@ -0,0 +1,8 @@
'use strict';

module.exports = {
nunjucks: {
enable: true,
package: 'egg-view-nunjucks',
},
};
3 changes: 3 additions & 0 deletions test/fixtures/apps/complex-manifest/package.json
@@ -0,0 +1,3 @@
{
"name": "egg-view-assets"
}
1 change: 1 addition & 0 deletions test/fixtures/apps/ui/app/public/index.css
@@ -0,0 +1 @@
body { color: white; }

0 comments on commit 6d9be84

Please sign in to comment.