Skip to content

Commit

Permalink
Merge pull request #161 from umijs/improve-umi
Browse files Browse the repository at this point in the history
Improve umi
  • Loading branch information
sorrycc authored Mar 6, 2018
2 parents c26d0d3 + 8336672 commit 2ba245b
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 17 deletions.
1 change: 1 addition & 0 deletions examples/func-test/global.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('global');
10 changes: 9 additions & 1 deletion examples/func-test/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Button } from 'antd-mobile';
import Link from 'umi/link';
import router from 'umi/router';
import styles from './index.less';

export default function() {
Expand All @@ -20,7 +21,14 @@ export default function() {
<Link to="/users/detail">go to /users/detail</Link>
</li>
</ul>
<Button type="primary">一个没啥用的 Button</Button>
<Button
type="primary"
onClick={() => {
router.push('/list');
}}
>
go to /list with router.push()
</Button>
</div>
);
}
Binary file added examples/func-test/public/dva.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion examples/routes-via-config/_routes.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
},
{
"path": "/list",
"exact": true,
"component": "./pages/b"
},
{
Expand Down
1 change: 1 addition & 0 deletions packages/af-webpack/src/getConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ export default function getConfig(opts = {}) {
/\.json$/,
/\.(js|jsx|ts|tsx)$/,
/\.(css|less|scss|sass)$/,
...(opts.urlLoaderExcludes || []),
],
loader: require.resolve('url-loader'),
options: {
Expand Down
13 changes: 13 additions & 0 deletions packages/af-webpack/src/getUserConfig/configs/urlLoaderExcludes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import assert from 'assert';

export default function() {
return {
name: 'urlLoaderExcludes',
validate(val) {
assert(
Array.isArray(val),
`The urlLoaderExcludes config must be Array, but got ${val}`,
);
},
};
}
28 changes: 18 additions & 10 deletions packages/umi-build-dev/src/FilesGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ export default class FilesGenerator {
},
});

// rebuild 时只生成 router.js
this.generateRouterJS();
this.generateEntry();

if (this.onChange) this.onChange();
if (this.hasRebuildError) {
// 从出错中恢复时,刷新浏览器
Expand All @@ -101,11 +102,8 @@ export default class FilesGenerator {
}
}

generateFiles() {
generateEntry() {
const { paths, entryJSTpl, config, libraryName } = this.service;
this.service.applyPlugins('generateFiles');

this.generateRouterJS();

// Generate umi.js
let entryContent = readFileSync(
Expand Down Expand Up @@ -134,6 +132,14 @@ if (process.env.NODE_ENV === 'production') {
`;
}
writeFileSync(paths.absLibraryJSPath, entryContent, 'utf-8');
}

generateFiles() {
const { paths } = this.service;
this.service.applyPlugins('generateFiles');

this.generateRouterJS();
this.generateEntry();

// Generate registerServiceWorker.js
writeFileSync(
Expand Down Expand Up @@ -184,8 +190,8 @@ if (process.env.NODE_ENV === 'production') {
getRouterContent() {
const { routes, config, paths } = this.service;

const routesByPath = routes.reduce((memo, { path, component }) => {
memo[path] = component;
const routesByPath = routes.reduce((memo, route) => {
memo[route.path] = route;
return memo;
}, {});

Expand All @@ -202,7 +208,8 @@ if (process.env.NODE_ENV === 'production') {
)}').default,`;
}
let routesContent = Object.keys(routesByPath).map(key => {
const pageJSFile = winPath(relative(paths.tmpDirPath, routesByPath[key]));
const route = routesByPath[key];
const pageJSFile = winPath(relative(paths.tmpDirPath, route.component));
debug(`requested: ${JSON.stringify(getRequest())}`);
const isDev = process.env.NODE_ENV === 'development';

Expand All @@ -218,7 +225,7 @@ if (process.env.NODE_ENV === 'production') {
isCompiling = true;
}
} else {
webpackChunkName = normalizeEntry(routesByPath[key]);
webpackChunkName = normalizeEntry(route.component);
component = `dynamic(() => import(/* webpackChunkName: '${webpackChunkName}' */'${pageJSFile}'), { ${loadingOpts} })`;
}
component = this.service.applyPlugins('modifyRouteComponent', {
Expand All @@ -231,7 +238,8 @@ if (process.env.NODE_ENV === 'production') {
},
});

return ` <Route exact path="${key}" component={${component}} />`;
const exact = route.exact ? 'exact ' : '';
return ` <Route ${exact}path="${key}" component={${component}} />`;
});

routesContent = this.service.applyPlugins('modifyRoutesContent', {
Expand Down
8 changes: 6 additions & 2 deletions packages/umi-build-dev/src/HtmlGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,18 @@ export default class HtmlGenerator {
getContent(opts = {}) {
const { pageConfig = {}, route = {} } = opts;
const { paths, webpackConfig } = this.service;
const { document, context } = pageConfig;
const { document, context = {} } = pageConfig;

// e.g.
// path: /user.html
// component: ./user/page.js
// entry: ./user
const { path, component } = route;

if (!context.path) {
context.path = path;
}

const customDocPath = document
? join(paths.cwd, document)
: paths.absPageDocumentPath;
Expand Down Expand Up @@ -176,7 +180,7 @@ ${jsContent}
process.env.COMPRESS !== 'none'
) {
html = minify(html, {
removeAttributeQuotes: true,
removeAttributeQuotes: false, // site don't support no quote attributes
collapseWhitespace: true,
});
}
Expand Down
1 change: 1 addition & 0 deletions packages/umi-build-dev/src/getPlugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export default function(opts = {}) {

// 内置插件
const builtInPlugins = [
'./plugins/global-js',
'./plugins/global-css',
'./plugins/layout',
'./plugins/fastclick',
Expand Down
33 changes: 33 additions & 0 deletions packages/umi-build-dev/src/plugins/global-js.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { join } from 'path';
import { existsSync } from 'fs';

export default function(api) {
const { paths } = api.service;

const globalFiles = [
join(paths.absSrcPath, 'global.tsx'),
join(paths.absSrcPath, 'global.ts'),
join(paths.absSrcPath, 'global.jsx'),
join(paths.absSrcPath, 'global.js'),
];

function getGlobalJS() {
for (const file of globalFiles) {
if (existsSync(file)) {
return file;
}
}
}

api.register('modifyEntryFile', ({ memo }) => {
const globalJS = getGlobalJS();
if (globalJS) {
memo = `import '${globalJS}';\r\n${memo}`;
}
return memo;
});

api.register('modifyPageWatchers', ({ memo }) => {
return [...memo, ...globalFiles];
});
}
33 changes: 30 additions & 3 deletions packages/umi-build-dev/src/plugins/hd/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
import px2rem from 'postcss-plugin-px2rem';
import { join } from 'path';
import { existsSync } from 'fs';

export default function(api) {
const { config, libraryName } = api.service;
const { config, paths } = api.service;

const hdFiles = [
join(paths.absSrcPath, 'hd.tsx'),
join(paths.absSrcPath, 'hd.ts'),
join(paths.absSrcPath, 'hd.jsx'),
join(paths.absSrcPath, 'hd.js'),
];

function getHdJS() {
for (const file of hdFiles) {
if (existsSync(file)) {
return file;
}
}
return join(__dirname, './template/index.js');
}

api.register('modifyConfigPlugins', ({ memo }) => {
memo.push(api => {
Expand All @@ -29,9 +46,19 @@ export default function(api) {
minPixelValue: 2,
}),
];
const hdFile = join(__dirname, './template/index.js');
memo.entry[libraryName].unshift(hdFile);
return memo;
});

api.register('modifyEntryFile', ({ memo }) => {
const hdJS = getHdJS();
if (hdJS) {
memo = `import '${hdJS}';\r\n${memo}`;
}
return memo;
});

api.register('modifyPageWatchers', ({ memo }) => {
return [...memo, ...hdFiles];
});
}
}

0 comments on commit 2ba245b

Please sign in to comment.