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

支持多级目录生成对应html #11

Merged
merged 1 commit into from
Mar 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ MPA(multiple-page application) plugin for umi.
- ✔︎ 禁用 umi 内置的路由功能
- ✔︎ 禁用 umi 默认生成的 entry 配置
- ✔︎ 支持通过 targets 配置的补丁方案,配 `BABEL_POLYFILL=none` 则不打补丁
- ✔︎ 支持自动查找 `src/pages` 下的 js 文件为 entry
- ✔︎ 支持多级目录自动查找 `src/pages` 下的 js 文件为 entry
- ✔︎ import 的 html 文件会被生成到 dist 目录下
- ✔︎ Hot Module Replacement
- ✔︎ 通过 `splitChunks` 配置提取公共部分
Expand Down Expand Up @@ -102,6 +102,15 @@ entry 的额外配置项目前支持:

可以用 `[name]`、`[path]`、`[hash]` 和 `[ext]`,详见 https://github.com/webpack-contrib/file-loader 。

### deepPageEntry

在自动查找 `src/pages` 下的 js 或 ts 文件为 entry 时,是否进入子目录查找

- Type: `Boolean`
- Default: `false`

注:会跳过以 `__` 或 `.` 开头的目录

### splitChunks

配置 webpack 的 splitChunks,用于提取 common 或 vendors 等。
Expand Down
27 changes: 22 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IApi } from 'umi-plugin-types';
import { existsSync, readdirSync } from 'fs';
import { existsSync, readdirSync, lstatSync } from 'fs';
import { join, extname, basename, dirname } from 'path';
import { cloneDeep, isPlainObject } from 'lodash';
import { cloneDeep, isPlainObject, flattenDeep } from 'lodash';
import AJV from 'ajv';
import schema from './schema';

Expand All @@ -14,6 +14,7 @@ const semver = require('semver');
interface IOption {
entry?: object,
htmlName?: string,
deepPageEntry?: boolean,
splitChunks?: object | boolean,
html?: {
template?: string,
Expand All @@ -29,6 +30,18 @@ interface IEntryConfig {
context?: object,
}

function getFiles(absPath: string, path: string, files: string[]) {
return files.map(f => {
const lstat = lstatSync(join(absPath, path, f));
if(f.charAt(0) !== '.' && !f.startsWith('__') && lstat.isDirectory()) {
const subDirFiles = readdirSync(join(absPath, path, f));
return getFiles(absPath, join(path, f), subDirFiles);
} else {
return join(path, f);
}
})
}

export default function(api: IApi, options = {} as IOption) {
const { log, paths } = api;

Expand Down Expand Up @@ -86,10 +99,14 @@ ${errors.join('\n')}
log.info(
`[umi-plugin-mpa] options.entry is null, find files in pages for entry`,
);
webpackConfig.entry = readdirSync(paths.absPagesPath)
.filter(f => f.charAt(0) !== '.' && /\.(j|t)sx?$/.test(extname(f)))
// 是否进入子目录生成路由
const allFiles = options.deepPageEntry
? flattenDeep(getFiles(paths.absPagesPath, '', readdirSync(paths.absPagesPath)))
: readdirSync(paths.absPagesPath);
webpackConfig.entry = (allFiles as string[])
.filter(f => basename(f).charAt(0) !== '.' && /\.(j|t)sx?$/.test(extname(f)))
.reduce((memo, f) => {
const name = basename(f, extname(f));
const name = f.replace(/\.(j|t)sx?$/, '');
memo[name] = [join(paths.absPagesPath, f)];
return memo;
}, {});
Expand Down
6 changes: 6 additions & 0 deletions src/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ describe('schema', () => {
})).toEqual(true);
});

it('deepPageEntry', () => {
expect(ajv.validate(schema, {
deepPageEntry: true,
})).toEqual(true);
});

it('splitChunks', () => {
// boolean
expect(ajv.validate(schema, {
Expand Down
3 changes: 3 additions & 0 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export default {
type: 'string',
minLength: 1,
},
deepPageEntry: {
type: 'boolean',
},
splitChunks: {
anyOf: [
{ type: 'boolean' },
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/deep-entry/.umirc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

export default {
plugins: [
['../../../dist/index', {
deepPageEntry: true,
}],
],
};
101 changes: 101 additions & 0 deletions test/fixtures/deep-entry/expected/deep/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
/******/ }
/******/ };
/******/
/******/ // define __esModule on exports
/******/ __webpack_require__.r = function(exports) {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/
/******/ // create a fake namespace object
/******/ // mode & 1: value is a module id, require it
/******/ // mode & 2: merge all properties of value into the ns
/******/ // mode & 4: return value when already ns object
/******/ // mode & 8|1: behave like require
/******/ __webpack_require__.t = function(value, mode) {
/******/ if(mode & 1) value = __webpack_require__(value);
/******/ if(mode & 8) return value;
/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
/******/ var ns = Object.create(null);
/******/ __webpack_require__.r(ns);
/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
/******/ return ns;
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "/";
/******/
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {

module.exports = __webpack_require__(1);


/***/ }),
/* 1 */
/***/ (function(module, exports) {

alert('deep');

/***/ })
/******/ ]);
103 changes: 103 additions & 0 deletions test/fixtures/deep-entry/expected/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
/******/ }
/******/ };
/******/
/******/ // define __esModule on exports
/******/ __webpack_require__.r = function(exports) {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/
/******/ // create a fake namespace object
/******/ // mode & 1: value is a module id, require it
/******/ // mode & 2: merge all properties of value into the ns
/******/ // mode & 4: return value when already ns object
/******/ // mode & 8|1: behave like require
/******/ __webpack_require__.t = function(value, mode) {
/******/ if(mode & 1) value = __webpack_require__(value);
/******/ if(mode & 8) return value;
/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
/******/ var ns = Object.create(null);
/******/ __webpack_require__.r(ns);
/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
/******/ return ns;
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "/";
/******/
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 2);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */,
/* 1 */,
/* 2 */
/***/ (function(module, exports, __webpack_require__) {

module.exports = __webpack_require__(3);


/***/ }),
/* 3 */
/***/ (function(module, exports) {

alert('foooo');

/***/ })
/******/ ]);
1 change: 1 addition & 0 deletions test/fixtures/deep-entry/pages/__test__/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alert('err');
1 change: 1 addition & 0 deletions test/fixtures/deep-entry/pages/deep/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alert('deep');
1 change: 1 addition & 0 deletions test/fixtures/deep-entry/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alert('foooo');