From 6d9be84495ae1e789e20276f33f184e736cf7e86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BB=99=E6=A3=AE?= Date: Wed, 15 Aug 2018 19:15:24 +0800 Subject: [PATCH] feat: manifest add absolute path and complete path support (#20) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ##### Checklist - [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 由于 webpack 设置 publicPath 后 manifest 会包含比较完整或相对的路径。 例如 umi :https://github.com/umijs/umi/blob/master/packages/af-webpack/src/getConfig/index.js#L38 所以需要在构建最后资源文件路径时候先判断 manifest 中对应的 urlpath 形式。 --- lib/assets_context.js | 9 ++++ package.json | 18 ++++---- test/assets.test.js | 46 +++++++++++++++++++ .../complex-manifest/app/controller/home.js | 13 ++++++ .../complex-manifest/app/public/index.css | 1 + .../apps/complex-manifest/app/public/index.js | 4 ++ .../apps/complex-manifest/app/router.js | 7 +++ .../apps/complex-manifest/app/view/index.js | 0 .../complex-manifest/app/view/layout.html | 12 +++++ .../complex-manifest/config/config.default.js | 16 +++++++ .../complex-manifest/config/manifest.json | 5 ++ .../apps/complex-manifest/config/plugin.js | 8 ++++ .../apps/complex-manifest/package.json | 3 ++ test/fixtures/apps/ui/app/public/index.css | 1 + 14 files changed, 134 insertions(+), 9 deletions(-) create mode 100644 test/fixtures/apps/complex-manifest/app/controller/home.js create mode 100644 test/fixtures/apps/complex-manifest/app/public/index.css create mode 100644 test/fixtures/apps/complex-manifest/app/public/index.js create mode 100644 test/fixtures/apps/complex-manifest/app/router.js create mode 100644 test/fixtures/apps/complex-manifest/app/view/index.js create mode 100644 test/fixtures/apps/complex-manifest/app/view/layout.html create mode 100644 test/fixtures/apps/complex-manifest/config/config.default.js create mode 100644 test/fixtures/apps/complex-manifest/config/manifest.json create mode 100644 test/fixtures/apps/complex-manifest/config/plugin.js create mode 100644 test/fixtures/apps/complex-manifest/package.json create mode 100644 test/fixtures/apps/ui/app/public/index.css diff --git a/lib/assets_context.js b/lib/assets_context.js index 17ac330..404b7c5 100644 --- a/lib/assets_context.js +++ b/lib/assets_context.js @@ -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}`; } diff --git a/package.json b/package.json index b1e09c0..a981a3f 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/test/assets.test.js b/test/assets.test.js index a867cff..63fc9dd 100644 --- a/test/assets.test.js +++ b/test/assets.test.js @@ -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"')); + }); + }); }); diff --git a/test/fixtures/apps/complex-manifest/app/controller/home.js b/test/fixtures/apps/complex-manifest/app/controller/home.js new file mode 100644 index 0000000..cf8f58b --- /dev/null +++ b/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; diff --git a/test/fixtures/apps/complex-manifest/app/public/index.css b/test/fixtures/apps/complex-manifest/app/public/index.css new file mode 100644 index 0000000..e336c0e --- /dev/null +++ b/test/fixtures/apps/complex-manifest/app/public/index.css @@ -0,0 +1 @@ +body { color: white; } \ No newline at end of file diff --git a/test/fixtures/apps/complex-manifest/app/public/index.js b/test/fixtures/apps/complex-manifest/app/public/index.js new file mode 100644 index 0000000..0324ab3 --- /dev/null +++ b/test/fixtures/apps/complex-manifest/app/public/index.js @@ -0,0 +1,4 @@ +'use strict'; + +/* global window */ +console.log('data:', window.context.data); diff --git a/test/fixtures/apps/complex-manifest/app/router.js b/test/fixtures/apps/complex-manifest/app/router.js new file mode 100644 index 0000000..4547a88 --- /dev/null +++ b/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); +}; diff --git a/test/fixtures/apps/complex-manifest/app/view/index.js b/test/fixtures/apps/complex-manifest/app/view/index.js new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/apps/complex-manifest/app/view/layout.html b/test/fixtures/apps/complex-manifest/app/view/layout.html new file mode 100644 index 0000000..0557e06 --- /dev/null +++ b/test/fixtures/apps/complex-manifest/app/view/layout.html @@ -0,0 +1,12 @@ + + + + {{ helper.assets.getStyle() | safe }} + + +
+ + {{ helper.assets.getContext() | safe }} + {{ helper.assets.getScript() | safe }} + + diff --git a/test/fixtures/apps/complex-manifest/config/config.default.js b/test/fixtures/apps/complex-manifest/config/config.default.js new file mode 100644 index 0000000..e15d3d9 --- /dev/null +++ b/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'), +}; diff --git a/test/fixtures/apps/complex-manifest/config/manifest.json b/test/fixtures/apps/complex-manifest/config/manifest.json new file mode 100644 index 0000000..6dfd228 --- /dev/null +++ b/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" +} diff --git a/test/fixtures/apps/complex-manifest/config/plugin.js b/test/fixtures/apps/complex-manifest/config/plugin.js new file mode 100644 index 0000000..2b51586 --- /dev/null +++ b/test/fixtures/apps/complex-manifest/config/plugin.js @@ -0,0 +1,8 @@ +'use strict'; + +module.exports = { + nunjucks: { + enable: true, + package: 'egg-view-nunjucks', + }, +}; diff --git a/test/fixtures/apps/complex-manifest/package.json b/test/fixtures/apps/complex-manifest/package.json new file mode 100644 index 0000000..13148e0 --- /dev/null +++ b/test/fixtures/apps/complex-manifest/package.json @@ -0,0 +1,3 @@ +{ + "name": "egg-view-assets" +} diff --git a/test/fixtures/apps/ui/app/public/index.css b/test/fixtures/apps/ui/app/public/index.css new file mode 100644 index 0000000..e336c0e --- /dev/null +++ b/test/fixtures/apps/ui/app/public/index.css @@ -0,0 +1 @@ +body { color: white; } \ No newline at end of file