@modern-js/plugin-ssg 1.0.1 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +38 -6
- package/dist/js/modern/index.js +96 -58
- package/dist/js/modern/libs/make.js +37 -0
- package/dist/js/modern/libs/replace.js +5 -4
- package/dist/js/modern/libs/util.js +38 -22
- package/dist/js/modern/server/index.js +2 -2
- package/dist/js/modern/server/prerender.js +2 -2
- package/dist/js/modern/server/process.js +1 -1
- package/dist/js/modern/types.js +0 -1
- package/dist/js/node/index.js +92 -55
- package/dist/js/node/libs/make.js +50 -0
- package/dist/js/node/libs/replace.js +5 -4
- package/dist/js/node/libs/util.js +38 -29
- package/dist/js/node/server/index.js +2 -2
- package/dist/js/node/server/prerender.js +2 -2
- package/dist/js/node/server/process.js +2 -2
- package/dist/types/libs/make.d.ts +5 -0
- package/dist/types/libs/replace.d.ts +1 -1
- package/dist/types/libs/util.d.ts +4 -8
- package/dist/types/types.d.ts +13 -17
- package/package.json +10 -11
- package/src/index.ts +84 -83
- package/src/libs/make.ts +45 -0
- package/src/libs/output.ts +1 -1
- package/src/libs/replace.ts +7 -4
- package/src/libs/util.ts +39 -27
- package/src/server/index.ts +1 -1
- package/src/server/process.ts +3 -2
- package/src/types.ts +26 -20
- package/tests/lib.test.ts +48 -169
- package/tests/util.test.ts +71 -32
- package/dist/js/modern/libs/createPage.js +0 -46
- package/dist/js/modern/libs/invoker.js +0 -56
- package/dist/js/modern/libs/render.js +0 -15
- package/dist/js/modern/loader/index.js +0 -105
- package/dist/js/modern/manifest-op.js +0 -101
- package/dist/js/node/libs/createPage.js +0 -57
- package/dist/js/node/libs/invoker.js +0 -67
- package/dist/js/node/libs/render.js +0 -22
- package/dist/js/node/loader/index.js +0 -115
- package/dist/js/node/manifest-op.js +0 -124
- package/dist/types/libs/createPage.d.ts +0 -2
- package/dist/types/libs/invoker.d.ts +0 -5
- package/dist/types/libs/render.d.ts +0 -3
- package/dist/types/loader/index.d.ts +0 -4
- package/dist/types/manifest-op.d.ts +0 -18
- package/src/libs/createPage.ts +0 -42
- package/src/libs/invoker.ts +0 -56
- package/src/libs/render.ts +0 -16
- package/src/loader/index.ts +0 -99
- package/src/manifest-op.ts +0 -111
- package/tests/operate.test.ts +0 -39
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
import { isDev } from '@modern-js/utils';
|
|
2
|
-
import { LoaderManifest, MODE } from "../manifest-op";
|
|
3
|
-
const FUNCTION_CREATE_CONTAINER_NAME = 'createContainer';
|
|
4
|
-
const FUNCTION_USE_LOADER_NAME = 'useLoader';
|
|
5
|
-
const FUNCTION_USE_STATIC_LOADER_NAME = 'useStaticLoader';
|
|
6
|
-
const CONTAINER_LOADER_NAME = 'loader';
|
|
7
|
-
const CONTAINER_STATIC_LOADER_NAME = 'staticLoader';
|
|
8
|
-
|
|
9
|
-
const noop = function noop() {
|
|
10
|
-
return {
|
|
11
|
-
name: 'babel-plugin-ssg-static-loader'
|
|
12
|
-
};
|
|
13
|
-
}; // develoment not need to static analysis
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const loader = isDev() ? noop : function () {
|
|
17
|
-
const loaderManifest = new LoaderManifest();
|
|
18
|
-
let useSSG = 0;
|
|
19
|
-
let createContainer = null;
|
|
20
|
-
let useStaticLoader = null;
|
|
21
|
-
let useLoader = null;
|
|
22
|
-
return {
|
|
23
|
-
name: 'babel-plugin-ssg-static-loader',
|
|
24
|
-
|
|
25
|
-
// reset settings whenever a new file passes through loader
|
|
26
|
-
pre() {
|
|
27
|
-
useSSG = 0;
|
|
28
|
-
createContainer = null;
|
|
29
|
-
useStaticLoader = null;
|
|
30
|
-
useLoader = null;
|
|
31
|
-
},
|
|
32
|
-
|
|
33
|
-
visitor: {
|
|
34
|
-
ImportSpecifier(path) {
|
|
35
|
-
const importName = path.get('imported.name').node;
|
|
36
|
-
const localName = path.get('local.name').node;
|
|
37
|
-
|
|
38
|
-
if (importName === FUNCTION_CREATE_CONTAINER_NAME) {
|
|
39
|
-
createContainer = localName;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
if (importName === FUNCTION_USE_STATIC_LOADER_NAME) {
|
|
43
|
-
useStaticLoader = localName;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
if (importName === FUNCTION_USE_LOADER_NAME) {
|
|
47
|
-
useLoader = localName;
|
|
48
|
-
}
|
|
49
|
-
},
|
|
50
|
-
|
|
51
|
-
Identifier(path) {
|
|
52
|
-
var _closestPath$node, _closestPath$node$cal;
|
|
53
|
-
|
|
54
|
-
// If the current file uses useLoader, the page can use SSG in MIXIN mode
|
|
55
|
-
// Todo: Mixin Mode is not support
|
|
56
|
-
const nodeName = path.node.name;
|
|
57
|
-
|
|
58
|
-
if (nodeName === useLoader && path.key === 'callee') {
|
|
59
|
-
useSSG = Math.max(useSSG, MODE.MIXIN);
|
|
60
|
-
return;
|
|
61
|
-
} // If the current file uses useStaticLoader, the page can use SSG in STRICT mode
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
if (nodeName === useStaticLoader && path.key === 'callee') {
|
|
65
|
-
useSSG = Math.max(useSSG, MODE.STRICT);
|
|
66
|
-
return;
|
|
67
|
-
} // after testing the Hook API, skip detection if the current nodeName is not 'container.(loader | staticLoader)'
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
if (nodeName !== CONTAINER_LOADER_NAME && nodeName !== CONTAINER_STATIC_LOADER_NAME) {
|
|
71
|
-
return;
|
|
72
|
-
} // if the current nodeName is 'container.(loader | staticLoader)', check whether the calling node is 'createContainer'
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
const closestPath = path.find(p => p.isCallExpression());
|
|
76
|
-
|
|
77
|
-
if ((closestPath === null || closestPath === void 0 ? void 0 : (_closestPath$node = closestPath.node) === null || _closestPath$node === void 0 ? void 0 : (_closestPath$node$cal = _closestPath$node.callee) === null || _closestPath$node$cal === void 0 ? void 0 : _closestPath$node$cal.name) === createContainer) {
|
|
78
|
-
if (nodeName === CONTAINER_LOADER_NAME) {
|
|
79
|
-
useSSG = Math.max(useSSG, MODE.MIXIN);
|
|
80
|
-
return;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
if (nodeName === CONTAINER_STATIC_LOADER_NAME) {
|
|
84
|
-
useSSG = Math.max(useSSG, MODE.STRICT);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
},
|
|
90
|
-
|
|
91
|
-
post(file) {
|
|
92
|
-
const {
|
|
93
|
-
filename
|
|
94
|
-
} = file.opts; // if the current usage mode is not determined, that is, no runtime API is used, the default is LOOSE
|
|
95
|
-
|
|
96
|
-
if (!useSSG) {
|
|
97
|
-
useSSG = MODE.LOOSE;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
loaderManifest.add(filename, useSSG);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
};
|
|
104
|
-
};
|
|
105
|
-
export default loader;
|
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
import { fs } from '@modern-js/utils';
|
|
3
|
-
const CACHE_DIRECTORY = './node_modules/.cache';
|
|
4
|
-
const cacheDir = path.join(process.cwd(), CACHE_DIRECTORY);
|
|
5
|
-
const manifest = path.join(cacheDir, 'loaderManifest.json');
|
|
6
|
-
fs.mkdirp(cacheDir);
|
|
7
|
-
export const MODE = {
|
|
8
|
-
STRICT: 1,
|
|
9
|
-
LOOSE: 2,
|
|
10
|
-
MIXIN: 3
|
|
11
|
-
};
|
|
12
|
-
export function toKey(level) {
|
|
13
|
-
return `file_${level}`;
|
|
14
|
-
}
|
|
15
|
-
export class LoaderManifest {
|
|
16
|
-
constructor() {
|
|
17
|
-
this.content = void 0;
|
|
18
|
-
this.content = {};
|
|
19
|
-
this.load();
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
get(root, targetLevel) {
|
|
23
|
-
return Object.values(MODE).reduce((total, level) => {
|
|
24
|
-
if (level > targetLevel) {
|
|
25
|
-
return total;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const key = toKey(level);
|
|
29
|
-
const allow = this.content[key].filter(file => file.includes(root));
|
|
30
|
-
return total.concat(allow);
|
|
31
|
-
}, []);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
add(filename, level) {
|
|
35
|
-
const key = toKey(level);
|
|
36
|
-
this.load();
|
|
37
|
-
|
|
38
|
-
if (this.includes(filename, key)) {
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
this.cleanExist(filename);
|
|
43
|
-
this.content[key].push(filename);
|
|
44
|
-
this.dump();
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
initContent() {
|
|
48
|
-
return Object.values(MODE).reduce((total, level) => {
|
|
49
|
-
const key = toKey(level);
|
|
50
|
-
total[key] = [];
|
|
51
|
-
return total;
|
|
52
|
-
}, {});
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
dump() {
|
|
56
|
-
fs.writeFileSync(manifest, JSON.stringify(this.content, null, 4));
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
load() {
|
|
60
|
-
const exist = fs.existsSync(manifest);
|
|
61
|
-
|
|
62
|
-
if (exist) {
|
|
63
|
-
try {
|
|
64
|
-
const contentStr = fs.readFileSync(manifest, 'utf-8');
|
|
65
|
-
this.content = JSON.parse(contentStr);
|
|
66
|
-
} catch (e) {
|
|
67
|
-
throw new Error(`解析 loader mainfest 失败:${manifest}`);
|
|
68
|
-
}
|
|
69
|
-
} else {
|
|
70
|
-
this.content = this.initContent();
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
includes(filename, key) {
|
|
75
|
-
return this.content[key].includes(filename);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
index(filename, key) {
|
|
79
|
-
return this.content[key].indexOf(filename);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
del(index, key) {
|
|
83
|
-
this.content[key].splice(index, 1);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
cleanExist(filename) {
|
|
87
|
-
Object.values(MODE).some(level => {
|
|
88
|
-
const key = toKey(level);
|
|
89
|
-
|
|
90
|
-
if (this.includes(filename, key)) {
|
|
91
|
-
const index = this.index(filename, key);
|
|
92
|
-
this.del(index, key);
|
|
93
|
-
return true;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
return false;
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
}
|
|
101
|
-
export { manifest };
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.createPageFactory = void 0;
|
|
7
|
-
|
|
8
|
-
var _reactRouterDom = require("react-router-dom");
|
|
9
|
-
|
|
10
|
-
var _util = require("./util");
|
|
11
|
-
|
|
12
|
-
const _excluded = ["path", "agreed"];
|
|
13
|
-
|
|
14
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
|
|
15
|
-
|
|
16
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
17
|
-
|
|
18
|
-
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
19
|
-
|
|
20
|
-
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
|
|
21
|
-
|
|
22
|
-
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
|
23
|
-
|
|
24
|
-
const createPageFactory = (route, listener) => config => {
|
|
25
|
-
if (Array.isArray(config)) {
|
|
26
|
-
config.forEach(cfg => {
|
|
27
|
-
listener(createPage(route, cfg), route.agreed);
|
|
28
|
-
});
|
|
29
|
-
} else {
|
|
30
|
-
listener(createPage(route, config), route.agreed);
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
exports.createPageFactory = createPageFactory;
|
|
35
|
-
|
|
36
|
-
function createPage(route, config = {}) {
|
|
37
|
-
const {
|
|
38
|
-
path,
|
|
39
|
-
agreed
|
|
40
|
-
} = route,
|
|
41
|
-
filterRoute = _objectWithoutProperties(route, _excluded);
|
|
42
|
-
|
|
43
|
-
const urlPath = (0, _util.formatPath)(config.url || path);
|
|
44
|
-
|
|
45
|
-
const ssgRoute = _objectSpread(_objectSpread({}, filterRoute), {}, {
|
|
46
|
-
urlPath
|
|
47
|
-
}); // using params completion dynamic routing
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
if (agreed && (0, _util.isDynamicUrl)(urlPath) && config.params) {
|
|
51
|
-
ssgRoute.urlPath = (0, _reactRouterDom.generatePath)(urlPath, config.params);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
ssgRoute.output = config.output;
|
|
55
|
-
ssgRoute.headers = config.headers;
|
|
56
|
-
return ssgRoute;
|
|
57
|
-
}
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.invoker = invoker;
|
|
7
|
-
|
|
8
|
-
var _normalizePath = _interopRequireDefault(require("normalize-path"));
|
|
9
|
-
|
|
10
|
-
var _util = require("./util");
|
|
11
|
-
|
|
12
|
-
var _createPage = require("./createPage");
|
|
13
|
-
|
|
14
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
15
|
-
|
|
16
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
|
|
17
|
-
|
|
18
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
19
|
-
|
|
20
|
-
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
21
|
-
|
|
22
|
-
function createContext(route, listener) {
|
|
23
|
-
return {
|
|
24
|
-
createPage: (0, _createPage.createPageFactory)(route, listener),
|
|
25
|
-
route
|
|
26
|
-
};
|
|
27
|
-
} // eslint-disable-next-line max-params
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
async function invoker(pageRoutes, agreedRouteMap, hook, autoAddAgreed, listener) {
|
|
31
|
-
for (const pageRoute of pageRoutes) {
|
|
32
|
-
const {
|
|
33
|
-
urlPath,
|
|
34
|
-
entryName
|
|
35
|
-
} = pageRoute;
|
|
36
|
-
const agreedRoutes = agreedRouteMap[entryName];
|
|
37
|
-
|
|
38
|
-
if (agreedRoutes) {
|
|
39
|
-
for (const agreedRoute of agreedRoutes) {
|
|
40
|
-
const fullpath = (0, _normalizePath.default)(`${urlPath}${agreedRoute.path}`) || '/';
|
|
41
|
-
|
|
42
|
-
const route = _objectSpread(_objectSpread({}, pageRoute), {}, {
|
|
43
|
-
path: fullpath,
|
|
44
|
-
agreed: true
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
const context = createContext(route, listener); // The hook function can return false to prevent the automatic addition of agreed routes
|
|
48
|
-
|
|
49
|
-
const isStaticPage = await hook(context);
|
|
50
|
-
|
|
51
|
-
if (!(0, _util.isDynamicUrl)(fullpath) && isStaticPage !== false) {
|
|
52
|
-
const autoAdd = autoAddAgreed(_objectSpread(_objectSpread({}, context), {}, {
|
|
53
|
-
component: agreedRoute._component
|
|
54
|
-
}));
|
|
55
|
-
autoAdd && context.createPage();
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
} else {
|
|
59
|
-
const route = _objectSpread(_objectSpread({}, pageRoute), {}, {
|
|
60
|
-
path: urlPath
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
const context = createContext(route, listener);
|
|
64
|
-
await hook(context);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.makeRender = makeRender;
|
|
7
|
-
|
|
8
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
|
|
9
|
-
|
|
10
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
11
|
-
|
|
12
|
-
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
13
|
-
|
|
14
|
-
function makeRender(ssgRoutes, render, port) {
|
|
15
|
-
return ssgRoutes.map(ssgRoute => render({
|
|
16
|
-
url: ssgRoute.urlPath,
|
|
17
|
-
headers: _objectSpread({
|
|
18
|
-
host: `localhost:${port}`
|
|
19
|
-
}, ssgRoute.headers),
|
|
20
|
-
connection: {}
|
|
21
|
-
}));
|
|
22
|
-
}
|
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = void 0;
|
|
7
|
-
|
|
8
|
-
var _utils = require("@modern-js/utils");
|
|
9
|
-
|
|
10
|
-
var _manifestOp = require("../manifest-op");
|
|
11
|
-
|
|
12
|
-
const FUNCTION_CREATE_CONTAINER_NAME = 'createContainer';
|
|
13
|
-
const FUNCTION_USE_LOADER_NAME = 'useLoader';
|
|
14
|
-
const FUNCTION_USE_STATIC_LOADER_NAME = 'useStaticLoader';
|
|
15
|
-
const CONTAINER_LOADER_NAME = 'loader';
|
|
16
|
-
const CONTAINER_STATIC_LOADER_NAME = 'staticLoader';
|
|
17
|
-
|
|
18
|
-
const noop = function noop() {
|
|
19
|
-
return {
|
|
20
|
-
name: 'babel-plugin-ssg-static-loader'
|
|
21
|
-
};
|
|
22
|
-
}; // develoment not need to static analysis
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
const loader = (0, _utils.isDev)() ? noop : function () {
|
|
26
|
-
const loaderManifest = new _manifestOp.LoaderManifest();
|
|
27
|
-
let useSSG = 0;
|
|
28
|
-
let createContainer = null;
|
|
29
|
-
let useStaticLoader = null;
|
|
30
|
-
let useLoader = null;
|
|
31
|
-
return {
|
|
32
|
-
name: 'babel-plugin-ssg-static-loader',
|
|
33
|
-
|
|
34
|
-
// reset settings whenever a new file passes through loader
|
|
35
|
-
pre() {
|
|
36
|
-
useSSG = 0;
|
|
37
|
-
createContainer = null;
|
|
38
|
-
useStaticLoader = null;
|
|
39
|
-
useLoader = null;
|
|
40
|
-
},
|
|
41
|
-
|
|
42
|
-
visitor: {
|
|
43
|
-
ImportSpecifier(path) {
|
|
44
|
-
const importName = path.get('imported.name').node;
|
|
45
|
-
const localName = path.get('local.name').node;
|
|
46
|
-
|
|
47
|
-
if (importName === FUNCTION_CREATE_CONTAINER_NAME) {
|
|
48
|
-
createContainer = localName;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
if (importName === FUNCTION_USE_STATIC_LOADER_NAME) {
|
|
52
|
-
useStaticLoader = localName;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
if (importName === FUNCTION_USE_LOADER_NAME) {
|
|
56
|
-
useLoader = localName;
|
|
57
|
-
}
|
|
58
|
-
},
|
|
59
|
-
|
|
60
|
-
Identifier(path) {
|
|
61
|
-
var _closestPath$node, _closestPath$node$cal;
|
|
62
|
-
|
|
63
|
-
// If the current file uses useLoader, the page can use SSG in MIXIN mode
|
|
64
|
-
// Todo: Mixin Mode is not support
|
|
65
|
-
const nodeName = path.node.name;
|
|
66
|
-
|
|
67
|
-
if (nodeName === useLoader && path.key === 'callee') {
|
|
68
|
-
useSSG = Math.max(useSSG, _manifestOp.MODE.MIXIN);
|
|
69
|
-
return;
|
|
70
|
-
} // If the current file uses useStaticLoader, the page can use SSG in STRICT mode
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
if (nodeName === useStaticLoader && path.key === 'callee') {
|
|
74
|
-
useSSG = Math.max(useSSG, _manifestOp.MODE.STRICT);
|
|
75
|
-
return;
|
|
76
|
-
} // after testing the Hook API, skip detection if the current nodeName is not 'container.(loader | staticLoader)'
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
if (nodeName !== CONTAINER_LOADER_NAME && nodeName !== CONTAINER_STATIC_LOADER_NAME) {
|
|
80
|
-
return;
|
|
81
|
-
} // if the current nodeName is 'container.(loader | staticLoader)', check whether the calling node is 'createContainer'
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
const closestPath = path.find(p => p.isCallExpression());
|
|
85
|
-
|
|
86
|
-
if ((closestPath === null || closestPath === void 0 ? void 0 : (_closestPath$node = closestPath.node) === null || _closestPath$node === void 0 ? void 0 : (_closestPath$node$cal = _closestPath$node.callee) === null || _closestPath$node$cal === void 0 ? void 0 : _closestPath$node$cal.name) === createContainer) {
|
|
87
|
-
if (nodeName === CONTAINER_LOADER_NAME) {
|
|
88
|
-
useSSG = Math.max(useSSG, _manifestOp.MODE.MIXIN);
|
|
89
|
-
return;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
if (nodeName === CONTAINER_STATIC_LOADER_NAME) {
|
|
93
|
-
useSSG = Math.max(useSSG, _manifestOp.MODE.STRICT);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
},
|
|
99
|
-
|
|
100
|
-
post(file) {
|
|
101
|
-
const {
|
|
102
|
-
filename
|
|
103
|
-
} = file.opts; // if the current usage mode is not determined, that is, no runtime API is used, the default is LOOSE
|
|
104
|
-
|
|
105
|
-
if (!useSSG) {
|
|
106
|
-
useSSG = _manifestOp.MODE.LOOSE;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
loaderManifest.add(filename, useSSG);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
};
|
|
113
|
-
};
|
|
114
|
-
var _default = loader;
|
|
115
|
-
exports.default = _default;
|
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.manifest = exports.MODE = exports.LoaderManifest = void 0;
|
|
7
|
-
exports.toKey = toKey;
|
|
8
|
-
|
|
9
|
-
var _path = _interopRequireDefault(require("path"));
|
|
10
|
-
|
|
11
|
-
var _utils = require("@modern-js/utils");
|
|
12
|
-
|
|
13
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
14
|
-
|
|
15
|
-
const CACHE_DIRECTORY = './node_modules/.cache';
|
|
16
|
-
|
|
17
|
-
const cacheDir = _path.default.join(process.cwd(), CACHE_DIRECTORY);
|
|
18
|
-
|
|
19
|
-
const manifest = _path.default.join(cacheDir, 'loaderManifest.json');
|
|
20
|
-
|
|
21
|
-
exports.manifest = manifest;
|
|
22
|
-
|
|
23
|
-
_utils.fs.mkdirp(cacheDir);
|
|
24
|
-
|
|
25
|
-
const MODE = {
|
|
26
|
-
STRICT: 1,
|
|
27
|
-
LOOSE: 2,
|
|
28
|
-
MIXIN: 3
|
|
29
|
-
};
|
|
30
|
-
exports.MODE = MODE;
|
|
31
|
-
|
|
32
|
-
function toKey(level) {
|
|
33
|
-
return `file_${level}`;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
class LoaderManifest {
|
|
37
|
-
constructor() {
|
|
38
|
-
this.content = void 0;
|
|
39
|
-
this.content = {};
|
|
40
|
-
this.load();
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
get(root, targetLevel) {
|
|
44
|
-
return Object.values(MODE).reduce((total, level) => {
|
|
45
|
-
if (level > targetLevel) {
|
|
46
|
-
return total;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const key = toKey(level);
|
|
50
|
-
const allow = this.content[key].filter(file => file.includes(root));
|
|
51
|
-
return total.concat(allow);
|
|
52
|
-
}, []);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
add(filename, level) {
|
|
56
|
-
const key = toKey(level);
|
|
57
|
-
this.load();
|
|
58
|
-
|
|
59
|
-
if (this.includes(filename, key)) {
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
this.cleanExist(filename);
|
|
64
|
-
this.content[key].push(filename);
|
|
65
|
-
this.dump();
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
initContent() {
|
|
69
|
-
return Object.values(MODE).reduce((total, level) => {
|
|
70
|
-
const key = toKey(level);
|
|
71
|
-
total[key] = [];
|
|
72
|
-
return total;
|
|
73
|
-
}, {});
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
dump() {
|
|
77
|
-
_utils.fs.writeFileSync(manifest, JSON.stringify(this.content, null, 4));
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
load() {
|
|
81
|
-
const exist = _utils.fs.existsSync(manifest);
|
|
82
|
-
|
|
83
|
-
if (exist) {
|
|
84
|
-
try {
|
|
85
|
-
const contentStr = _utils.fs.readFileSync(manifest, 'utf-8');
|
|
86
|
-
|
|
87
|
-
this.content = JSON.parse(contentStr);
|
|
88
|
-
} catch (e) {
|
|
89
|
-
throw new Error(`解析 loader mainfest 失败:${manifest}`);
|
|
90
|
-
}
|
|
91
|
-
} else {
|
|
92
|
-
this.content = this.initContent();
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
includes(filename, key) {
|
|
97
|
-
return this.content[key].includes(filename);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
index(filename, key) {
|
|
101
|
-
return this.content[key].indexOf(filename);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
del(index, key) {
|
|
105
|
-
this.content[key].splice(index, 1);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
cleanExist(filename) {
|
|
109
|
-
Object.values(MODE).some(level => {
|
|
110
|
-
const key = toKey(level);
|
|
111
|
-
|
|
112
|
-
if (this.includes(filename, key)) {
|
|
113
|
-
const index = this.index(filename, key);
|
|
114
|
-
this.del(index, key);
|
|
115
|
-
return true;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
return false;
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
exports.LoaderManifest = LoaderManifest;
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import { ModernRoute } from '@modern-js/server';
|
|
2
|
-
import { CreatePageListener, HookContext, AgreedRouteMap } from '../types';
|
|
3
|
-
export declare function invoker(pageRoutes: ModernRoute[], agreedRouteMap: AgreedRouteMap, hook: (context: HookContext) => any, autoAddAgreed: (context: HookContext & {
|
|
4
|
-
component: string;
|
|
5
|
-
}) => boolean, listener: CreatePageListener): Promise<void>;
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
declare type ManifestContent = Record<string, string[]>;
|
|
2
|
-
declare const manifest: string;
|
|
3
|
-
export declare const MODE: Record<string, number>;
|
|
4
|
-
export declare function toKey(level: number): string;
|
|
5
|
-
export declare class LoaderManifest {
|
|
6
|
-
content: ManifestContent;
|
|
7
|
-
constructor();
|
|
8
|
-
get(root: string, targetLevel: number): string[];
|
|
9
|
-
add(filename: string, level: number): void;
|
|
10
|
-
private initContent;
|
|
11
|
-
private dump;
|
|
12
|
-
private load;
|
|
13
|
-
private includes;
|
|
14
|
-
private index;
|
|
15
|
-
private del;
|
|
16
|
-
private cleanExist;
|
|
17
|
-
}
|
|
18
|
-
export { manifest };
|
package/src/libs/createPage.ts
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { generatePath } from 'react-router-dom';
|
|
2
|
-
import {
|
|
3
|
-
CreatePageListener,
|
|
4
|
-
CreatePageParam,
|
|
5
|
-
FreshPageConfig,
|
|
6
|
-
SsgRoute,
|
|
7
|
-
UserInterfaceRoute,
|
|
8
|
-
} from '../types';
|
|
9
|
-
import { formatPath, isDynamicUrl } from './util';
|
|
10
|
-
|
|
11
|
-
export const createPageFactory =
|
|
12
|
-
(route: UserInterfaceRoute, listener: CreatePageListener) =>
|
|
13
|
-
(config?: CreatePageParam) => {
|
|
14
|
-
if (Array.isArray(config)) {
|
|
15
|
-
config.forEach(cfg => {
|
|
16
|
-
listener(createPage(route, cfg), route.agreed);
|
|
17
|
-
});
|
|
18
|
-
} else {
|
|
19
|
-
listener(createPage(route, config), route.agreed);
|
|
20
|
-
}
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
function createPage(
|
|
24
|
-
route: UserInterfaceRoute,
|
|
25
|
-
config: FreshPageConfig = {},
|
|
26
|
-
): SsgRoute {
|
|
27
|
-
const { path, agreed, ...filterRoute } = route;
|
|
28
|
-
|
|
29
|
-
const urlPath = formatPath(config.url || path);
|
|
30
|
-
const ssgRoute: SsgRoute = {
|
|
31
|
-
...filterRoute,
|
|
32
|
-
urlPath,
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
// using params completion dynamic routing
|
|
36
|
-
if (agreed && isDynamicUrl(urlPath) && config.params) {
|
|
37
|
-
ssgRoute.urlPath = generatePath(urlPath, config.params);
|
|
38
|
-
}
|
|
39
|
-
ssgRoute.output = config.output;
|
|
40
|
-
ssgRoute.headers = config.headers;
|
|
41
|
-
return ssgRoute;
|
|
42
|
-
}
|