Skip to content

Commit

Permalink
Merge branch 'improve/router'
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc committed Mar 23, 2018
2 parents 73e2b15 + aa5017d commit 4f3842b
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 28 deletions.
2 changes: 1 addition & 1 deletion examples/func-test/.webpackrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default {
theme: {
'brand-primary': '#7546c9',
},
publicPath: '/static/',
// publicPath: '/static/',
// ./static by default
// publicPath: './static/',
// publicPath: '/dist/static/',
Expand Down
4 changes: 3 additions & 1 deletion examples/with-dva/.webpackrc.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export default {};
export default {
publicPath: '/static/',
};
29 changes: 28 additions & 1 deletion packages/umi-build-dev/src/FilesGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,27 @@ if (process.env.NODE_ENV === 'production') {
}, {});
}

fixHtmlSuffix(routes) {
routes.forEach(route => {
if (route.routes) {
route.path = `${route.path}(.html)?`;
this.fixHtmlSuffix(route.routes);
}
});
}

getRoutesJSON(opts = {}) {
const { env = 'production', requested = {} } = opts;
const { routes, paths, config = {} } = this.service;
const { paths, config = {} } = this.service;
const routes = [...this.service.routes];

const rootRoute = routes.filter(route => route.path === '/')[0];
if (rootRoute) {
routes.push({
...rootRoute,
path: '/index.html',
});
}

const { loading } = config;
let loadingOpts = '';
Expand All @@ -260,6 +278,15 @@ if (process.env.NODE_ENV === 'production') {
}
});

if (
process.env.NODE_ENV === 'production' &&
config.exportStatic &&
config.exportStatic.htmlSuffix
) {
// 为 layout 组件加 (.html)? 兼容
this.fixHtmlSuffix(routes);
}

// 添加 layout wrapper
const layoutFile = this.getLayoutFile();
const wrappedRoutes = [
Expand Down
8 changes: 4 additions & 4 deletions packages/umi-build-dev/src/FilesGenerator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('FilesGenerator (getRoutesJSON)', () => {
expect(JSON.parse(routesJSON)).toEqual([
{
component:
"require('<%= cwd %>/packages/umi-build-dev/src/defaultLayout.js').default",
"require('<%= cwd %>/packages/umi-build-dev/src/DefaultLayout.js').default",
routes: [
{
path: '/a',
Expand Down Expand Up @@ -107,7 +107,7 @@ describe('FilesGenerator (getRoutesJSON)', () => {
expect(JSON.parse(routesJSON)).toEqual([
{
component:
"require('<%= cwd %>/packages/umi-build-dev/src/defaultLayout.js').default",
"require('<%= cwd %>/packages/umi-build-dev/src/DefaultLayout.js').default",
routes: [
{
path: '/a',
Expand Down Expand Up @@ -173,7 +173,7 @@ describe('FilesGenerator (getRoutesJSON)', () => {
expect(JSON.parse(routesJSON)).toEqual([
{
component:
"require('<%= cwd %>/packages/umi-build-dev/src/defaultLayout.js').default",
"require('<%= cwd %>/packages/umi-build-dev/src/DefaultLayout.js').default",
routes: [
{
path: '/a',
Expand Down Expand Up @@ -245,7 +245,7 @@ describe('FilesGenerator (getRoutesJSON)', () => {
expect(JSON.parse(routesJSON)).toEqual([
{
component:
"require('<%= cwd %>/packages/umi-build-dev/src/defaultLayout.js').default",
"require('<%= cwd %>/packages/umi-build-dev/src/DefaultLayout.js').default",
routes: [
{
path: '/a',
Expand Down
38 changes: 29 additions & 9 deletions packages/umi-build-dev/src/HtmlGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import normalizeEntry from './normalizeEntry';

const debug = require('debug')('umi:HtmlGenerator');

function makeSureSlashSuffix(path) {
return path.endsWith('/') ? path : `${path}/`;
}

export default class HtmlGenerator {
constructor(service, opts = {}) {
this.service = service;
Expand Down Expand Up @@ -46,13 +50,14 @@ export default class HtmlGenerator {
}
}

// 仅在 build 时调用
generate() {
const { config, routes, paths } = this.service;
generateForRoutes(routes) {
const { config, paths } = this.service;
const pagesConfig = config.pages || {};

if (config.exportStatic) {
const pagesConfig = config.pages || {};
routes.forEach(route => {
routes.forEach(route => {
if (route.routes) {
this.generateForRoutes(route.routes);
} else {
const { path } = route;
const content = this.getContent({
route,
Expand All @@ -61,7 +66,16 @@ export default class HtmlGenerator {
const outputPath = join(paths.absOutputPath, this.getHtmlPath(path));
mkdirp(dirname(outputPath));
writeFileSync(outputPath, content, 'utf-8');
});
}
});
}

// 仅在 build 时调用
generate() {
const { config, routes, paths } = this.service;

if (config.exportStatic) {
this.generateForRoutes(routes);
} else {
const content = this.getContent();
const outputPath = join(paths.absOutputPath, 'index.html');
Expand All @@ -71,14 +85,20 @@ export default class HtmlGenerator {

getContent(opts = {}) {
const { pageConfig = {}, route = {} } = opts;
const { paths, webpackConfig } = this.service;
const { paths, webpackConfig, config } = this.service;
const { document, context = {} } = pageConfig;

// e.g.
// path: /user.html
// component: ./user/page.js
// entry: ./user
const { path, component } = route;
const { component } = route;
let { path } = route;
const isExportStatic =
config.exportStatic && !config.exportStatic.htmlSuffix;
if (isExportStatic) {
path = makeSureHaveLastSlash(path);
}

if (!context.path) {
context.path = path;
Expand Down
27 changes: 27 additions & 0 deletions packages/umi-build-dev/src/HtmlGenerator.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import expect from 'expect';
import HtmlGenerator from './HtmlGenerator';

describe('HtmlGenerator.getHtmlPath', () => {
it('no htmlSuffix', () => {
const hg = new HtmlGenerator({
config: {},
});
expect(hg.getHtmlPath('/')).toEqual('index.html');
expect(hg.getHtmlPath('/list/')).toEqual('list/index.html');
expect(hg.getHtmlPath('/list/list')).toEqual('list/list/index.html');
expect(hg.getHtmlPath('/list/list/')).toEqual('list/list/index.html');
});

it('with htmlSuffix', () => {
const hg = new HtmlGenerator({
config: {
exportStatic: {
htmlSuffix: true,
},
},
});
expect(hg.getHtmlPath('/')).toEqual('index.html');
expect(hg.getHtmlPath('/list.html')).toEqual('list.html');
expect(hg.getHtmlPath('/list/list.html')).toEqual('list/list.html');
});
});
32 changes: 21 additions & 11 deletions packages/umi-build-dev/src/getRouteConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,31 @@ export default function(paths, config = {}) {
: getRoutesByPagesDir(paths);

if (config.exportStatic) {
routes.forEach(route => {
if (route.path.indexOf(':') > -1) {
throw new Error(
`Variable path ${route.path} don\'t work with exportStatic`,
);
}
patchRoutes(routes, config);
}

return routes;
}

function patchRoutes(routes, config) {
routes.forEach(route => {
if (route.path.indexOf(':') > -1) {
throw new Error(
`Variable path ${route.path} don\'t work with exportStatic`,
);
}

if (route.routes) {
patchRoutes(route.routes, config);
} else {
if (
typeof config.exportStatic === 'object' &&
config.exportStatic.htmlSuffix
) {
route.path = addHtmlSuffix(route.path);
}
});
}

return routes;
}
});
}

function routesConfigExists(root) {
Expand All @@ -41,7 +50,8 @@ function routesConfigExists(root) {
}

function addHtmlSuffix(path) {
return path.slice(-1) === '/' ? path : `${path}.html`;
if (path === '/') return path;
return path.endsWith('/') ? `${path.slice(0, -1)}.html` : `${path}.html`;
}

function getRoutesByConfig(routesConfigFile) {
Expand Down
5 changes: 5 additions & 0 deletions packages/umi-build-dev/src/getRouteConfig.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ describe('getRouteConfig', () => {
exact: true,
component: './pages/users/list.js',
},
{
path: '/index.html',
exact: true,
component: './pages/index.js',
},
]);
});

Expand Down
2 changes: 1 addition & 1 deletion packages/umi-build-dev/src/getWebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export default function(service = {}) {
publicPath: `/`,
}
: {
publicPath: webpackRCConfig.publicPath || `./${staticDirectory}/`,
publicPath: webpackRCConfig.publicPath || `/${staticDirectory}/`,
commons: webpackRCConfig.commons || [
{
async: 'common',
Expand Down

0 comments on commit 4f3842b

Please sign in to comment.