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: enable contenthash for assets #161

Merged
merged 11 commits into from
Apr 18, 2019
9 changes: 9 additions & 0 deletions examples/simple/.manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"manifest.js": "manifest_6498f2ed.js",
"index.js": "index_48ebde7c.js",
"script.js": "script_3a4833fb.js",
"script.css": "script_93bb8fc6.css",
"static.js": "static_3f3fa875.js",
"style.js": "style_6d3bbe58.js",
"style.css": "style_93bb8fc6.css"
}
6 changes: 3 additions & 3 deletions examples/simple/client/script/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ export default class View extends React.Component {
<html>
<head>
<title>Script</title>
<link rel="stylesheet" href={helper.asset('/script.css')} />
<link rel="stylesheet" href={helper.asset('script.css')} />
</head>
<body>
<div id="container" dangerouslySetInnerHTML={{ __html: html }} />
<script src={helper.asset('/manifest.js')} />
<script src={helper.asset('/script.js')} />
<script src={helper.asset('manifest.js')} />
<script src={helper.asset('script.js')} />
</body>
</html>
);
Expand Down
2 changes: 1 addition & 1 deletion examples/simple/client/style/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default class View extends React.Component {
<html>
<head>
<title>Styling</title>
<link rel="stylesheet" href={helper.asset('/style.css')} />
<link rel="stylesheet" href={helper.asset('style.css')} />
</head>
<body>
<h1 className="title">Styling Page</h1>
Expand Down
3 changes: 3 additions & 0 deletions examples/simple/config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const path = require('path');

module.exports = appInfo => ({
keys: 'secrets',
view: {
useHashAsset: true,
},
static: {
dir: [
{
Expand Down
1 change: 1 addition & 0 deletions packages/beidou-view-react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module.exports = appInfo => ({
assetPath: '/build/',
},
view: {
useHashAsset:false,
defaultViewEngine: 'react',
defaultExtension: '.jsx',
// Isomorphic directories
Expand Down
7 changes: 7 additions & 0 deletions packages/beidou-view/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

Used by internal react and rax plugins.

# Config

If `config.view.useHashAsset` is set true, beidou will use hashed assets on the page if not local env with the help of `webpack build`. You can set CDN path in `publicPath`, which will be included.
Remember that, you should run `beidou build` first before use this feature.

You can define the assets mapping file with `config.view.hashAssetPath`, the default value of which is `path.join(appInfo.baseDir, '.manifest.json')`.

# Middlewares

We introduced **View Middlewares** mechanism since `v1.0.0`. The rendering process is fully defined by a combination of middlewares, which means you can redefined them or add custom ones if needed.
Expand Down
22 changes: 22 additions & 0 deletions packages/beidou-view/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

const path = require('path');
const { getAssetManifest } = require('./lib/utils');

module.exports = (app) => {
// get asset with hash from cwd/manifes.json;
if (app.config.view.useHashAsset) {
// define hashAssetPath once here,and according to beidou-core, beidou-view will be loaded first.
app.config.view.hashAssetPath =
app.config.view.hashAssetPath || path.join(app.baseDir, '.manifest.json');
if (app.config.env === 'local' || app.config.env === 'unittest') {
app.coreLogger.warn(
`Detect view.useHashAsset in ${app.config.env} env, will ignore it.`
);
app.assetManifest = {};
return;
}

app.assetManifest = getAssetManifest(app.config.view.hashAssetPath);
}
};
4 changes: 3 additions & 1 deletion packages/beidou-view/app/extend/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ const helper = {
* @param {Object} config asset config
*/
[Symbol.for('beidou#asset')](filename, config) {
if (this.app.config.view.useHashAsset && this.app.assetManifest[filename]) {
filename = this.app.assetManifest[filename];
}
const assetHost = config.host || config.assetHost;
const { assetPath } = config;

if (!assetHost) {
return concatUrl(assetPath, filename);
}
Expand Down
8 changes: 8 additions & 0 deletions packages/beidou-view/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const path = require('path');

module.exports = appInfo => ({
view: {
useHashAsset: false,
hashAssetPath: path.join(appInfo.baseDir, '.manifest.json'),
},
});
11 changes: 11 additions & 0 deletions packages/beidou-view/lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const fs = require('fs');
const normalizeUrl = require('normalizeurl');

exports.concatUrl = function (...pathes) {
Expand All @@ -20,3 +21,13 @@ exports.concatUrl = function (...pathes) {
};

exports.normalizeUrl = normalizeUrl;
exports.getAssetManifest = function (hashAssetPath) {
if (fs.existsSync(hashAssetPath)) {
const raw = fs.readFileSync(hashAssetPath, { encoding: 'utf8' });
return JSON.parse(raw);
} else {
throw new Error(
`Cannot find ${hashAssetPath}! Please check view.hashAssetPath config.`
);
}
};

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import assert from 'assert';

const testStr = 'base view test';

/**
* Text component
*
* @export
* @class View
* @extends {React.Component}
*/
export default class View {
static async getInitialProps() {
return {
title: 'beidou',
};
}
static async getStore() {
return {
getState: () => ({ testStr }),
};
}

static async getPartial(props) {
assert(props.state === '{"testStr":"base view test"}');
return { partial: '', list: ['a', 'b', 'c'], store: 'store' };
}

render({ partial, list, store, title }) {
// See view.test.js BaseView.prototype.renderElement
const partialResult = 'fake renderElement';
assert(partial === partialResult);

assert(list.length === 3);
for (const item of list) {
assert(item === partialResult);
}

assert(store === partialResult);

assert(title === 'beidou');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';
const path = require('path');

module.exports = _ => ({
view: {
useHashAsset: true,
hashAssetPath: '/foo/bar.json'
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "beidou-test-view-use-hash-asset-app"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"index.js": "index_ec3f4aa7.js"
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import assert from 'assert';

const testStr = 'base view test';

/**
* Text component
*
* @export
* @class View
* @extends {React.Component}
*/
export default class View {
static async getInitialProps() {
return {
title: 'beidou',
};
}
static async getStore() {
return {
getState: () => ({ testStr }),
};
}

static async getPartial(props) {
assert(props.state === '{"testStr":"base view test"}');
return { partial: '', list: ['a', 'b', 'c'], store: 'store' };
}

render({ partial, list, store, title }) {
// See view.test.js BaseView.prototype.renderElement
const partialResult = 'fake renderElement';
assert(partial === partialResult);

assert(list.length === 3);
for (const item of list) {
assert(item === partialResult);
}

assert(store === partialResult);

assert(title === 'beidou');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';


module.exports = _ => ({
view: {
useHashAsset: true,
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "beidou-test-view-use-hash-asset-app"
}