@modern-js/plugin-ssg 1.0.0 → 1.1.2-rc.0

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.
Files changed (56) hide show
  1. package/.eslintrc.js +6 -0
  2. package/CHANGELOG.md +42 -0
  3. package/README.md +1 -4
  4. package/dist/js/modern/index.js +97 -58
  5. package/dist/js/modern/libs/make.js +37 -0
  6. package/dist/js/modern/libs/output.js +2 -1
  7. package/dist/js/modern/libs/replace.js +5 -4
  8. package/dist/js/modern/libs/util.js +39 -22
  9. package/dist/js/modern/server/index.js +4 -3
  10. package/dist/js/modern/server/prerender.js +2 -2
  11. package/dist/js/modern/server/process.js +8 -7
  12. package/dist/js/node/index.js +97 -56
  13. package/dist/js/node/libs/make.js +50 -0
  14. package/dist/js/node/libs/output.js +7 -3
  15. package/dist/js/node/libs/replace.js +5 -4
  16. package/dist/js/node/libs/util.js +43 -30
  17. package/dist/js/node/server/index.js +5 -3
  18. package/dist/js/node/server/prerender.js +2 -2
  19. package/dist/js/node/server/process.js +7 -6
  20. package/dist/types/libs/make.d.ts +5 -0
  21. package/dist/types/libs/replace.d.ts +1 -1
  22. package/dist/types/libs/util.d.ts +4 -8
  23. package/dist/types/types.d.ts +13 -17
  24. package/package.json +10 -11
  25. package/src/index.ts +85 -89
  26. package/src/libs/make.ts +45 -0
  27. package/src/libs/output.ts +3 -2
  28. package/src/libs/replace.ts +7 -4
  29. package/src/libs/util.ts +40 -27
  30. package/src/server/index.ts +3 -2
  31. package/src/server/process.ts +7 -5
  32. package/src/types.ts +26 -20
  33. package/tests/.eslintrc.js +6 -0
  34. package/tests/lib.test.ts +49 -170
  35. package/tests/util.test.ts +71 -32
  36. package/dist/js/modern/libs/createPage.js +0 -46
  37. package/dist/js/modern/libs/invoker.js +0 -55
  38. package/dist/js/modern/libs/render.js +0 -15
  39. package/dist/js/modern/loader/index.js +0 -105
  40. package/dist/js/modern/manifest-op.js +0 -100
  41. package/dist/js/node/libs/createPage.js +0 -57
  42. package/dist/js/node/libs/invoker.js +0 -66
  43. package/dist/js/node/libs/render.js +0 -22
  44. package/dist/js/node/loader/index.js +0 -115
  45. package/dist/js/node/manifest-op.js +0 -120
  46. package/dist/types/libs/createPage.d.ts +0 -2
  47. package/dist/types/libs/invoker.d.ts +0 -5
  48. package/dist/types/libs/render.d.ts +0 -3
  49. package/dist/types/loader/index.d.ts +0 -4
  50. package/dist/types/manifest-op.d.ts +0 -18
  51. package/src/libs/createPage.ts +0 -42
  52. package/src/libs/invoker.ts +0 -55
  53. package/src/libs/render.ts +0 -16
  54. package/src/loader/index.ts +0 -99
  55. package/src/manifest-op.ts +0 -110
  56. package/tests/operate.test.ts +0 -39
@@ -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,66 +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
- }
28
-
29
- async function invoker(pageRoutes, agreedRouteMap, hook, autoAddAgreed, listener) {
30
- for (const pageRoute of pageRoutes) {
31
- const {
32
- urlPath,
33
- entryName
34
- } = pageRoute;
35
- const agreedRoutes = agreedRouteMap[entryName];
36
-
37
- if (agreedRoutes) {
38
- for (const agreedRoute of agreedRoutes) {
39
- const fullpath = (0, _normalizePath.default)(`${urlPath}${agreedRoute.path}`) || '/';
40
-
41
- const route = _objectSpread(_objectSpread({}, pageRoute), {}, {
42
- path: fullpath,
43
- agreed: true
44
- });
45
-
46
- const context = createContext(route, listener); // The hook function can return false to prevent the automatic addition of agreed routes
47
-
48
- const isStaticPage = await hook(context);
49
-
50
- if (!(0, _util.isDynamicUrl)(fullpath) && isStaticPage !== false) {
51
- const autoAdd = autoAddAgreed(_objectSpread(_objectSpread({}, context), {}, {
52
- component: agreedRoute._component
53
- }));
54
- autoAdd && context.createPage();
55
- }
56
- }
57
- } else {
58
- const route = _objectSpread(_objectSpread({}, pageRoute), {}, {
59
- path: urlPath
60
- });
61
-
62
- const context = createContext(route, listener);
63
- await hook(context);
64
- }
65
- }
66
- }
@@ -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,120 +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 _utils = require("@modern-js/utils");
10
-
11
- const CACHE_DIRECTORY = './node_modules/.cache';
12
-
13
- const cacheDir = _utils.path.join(process.cwd(), CACHE_DIRECTORY);
14
-
15
- const manifest = _utils.path.join(cacheDir, 'loaderManifest.json');
16
-
17
- exports.manifest = manifest;
18
-
19
- _utils.fs.mkdirp(cacheDir);
20
-
21
- const MODE = {
22
- STRICT: 1,
23
- LOOSE: 2,
24
- MIXIN: 3
25
- };
26
- exports.MODE = MODE;
27
-
28
- function toKey(level) {
29
- return `file_${level}`;
30
- }
31
-
32
- class LoaderManifest {
33
- constructor() {
34
- this.content = void 0;
35
- this.content = {};
36
- this.load();
37
- }
38
-
39
- get(root, targetLevel) {
40
- return Object.values(MODE).reduce((total, level) => {
41
- if (level > targetLevel) {
42
- return total;
43
- }
44
-
45
- const key = toKey(level);
46
- const allow = this.content[key].filter(file => file.includes(root));
47
- return total.concat(allow);
48
- }, []);
49
- }
50
-
51
- add(filename, level) {
52
- const key = toKey(level);
53
- this.load();
54
-
55
- if (this.includes(filename, key)) {
56
- return;
57
- }
58
-
59
- this.cleanExist(filename);
60
- this.content[key].push(filename);
61
- this.dump();
62
- }
63
-
64
- initContent() {
65
- return Object.values(MODE).reduce((total, level) => {
66
- const key = toKey(level);
67
- total[key] = [];
68
- return total;
69
- }, {});
70
- }
71
-
72
- dump() {
73
- _utils.fs.writeFileSync(manifest, JSON.stringify(this.content, null, 4));
74
- }
75
-
76
- load() {
77
- const exist = _utils.fs.existsSync(manifest);
78
-
79
- if (exist) {
80
- try {
81
- const contentStr = _utils.fs.readFileSync(manifest, 'utf-8');
82
-
83
- this.content = JSON.parse(contentStr);
84
- } catch (e) {
85
- throw new Error(`解析 loader mainfest 失败:${manifest}`);
86
- }
87
- } else {
88
- this.content = this.initContent();
89
- }
90
- }
91
-
92
- includes(filename, key) {
93
- return this.content[key].includes(filename);
94
- }
95
-
96
- index(filename, key) {
97
- return this.content[key].indexOf(filename);
98
- }
99
-
100
- del(index, key) {
101
- this.content[key].splice(index, 1);
102
- }
103
-
104
- cleanExist(filename) {
105
- Object.values(MODE).some(level => {
106
- const key = toKey(level);
107
-
108
- if (this.includes(filename, key)) {
109
- const index = this.index(filename, key);
110
- this.del(index, key);
111
- return true;
112
- }
113
-
114
- return false;
115
- });
116
- }
117
-
118
- }
119
-
120
- exports.LoaderManifest = LoaderManifest;
@@ -1,2 +0,0 @@
1
- import { CreatePageListener, CreatePageParam, UserInterfaceRoute } from '../types';
2
- export declare const createPageFactory: (route: UserInterfaceRoute, listener: CreatePageListener) => (config?: CreatePageParam | undefined) => void;
@@ -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,3 +0,0 @@
1
- import { compile } from '../server/prerender';
2
- import { SsgRoute } from '../types';
3
- export declare function makeRender(ssgRoutes: SsgRoute[], render: ReturnType<typeof compile>, port: number): Promise<string>[];
@@ -1,4 +0,0 @@
1
- declare const loader: () => {
2
- name: string;
3
- };
4
- export default loader;
@@ -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 };
@@ -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
- }
@@ -1,55 +0,0 @@
1
- import normalize from 'normalize-path';
2
- import { ModernRoute } from '@modern-js/server';
3
- import {
4
- CreatePageListener,
5
- HookContext,
6
- UserInterfaceRoute,
7
- AgreedRouteMap,
8
- } from '../types';
9
- import { isDynamicUrl } from './util';
10
- import { createPageFactory } from './createPage';
11
-
12
- function createContext(
13
- route: UserInterfaceRoute,
14
- listener: CreatePageListener,
15
- ) {
16
- return {
17
- createPage: createPageFactory(route, listener),
18
- route,
19
- };
20
- }
21
-
22
- export async function invoker(
23
- pageRoutes: ModernRoute[],
24
- agreedRouteMap: AgreedRouteMap,
25
- hook: (context: HookContext) => any,
26
- autoAddAgreed: (context: HookContext & { component: string }) => boolean,
27
- listener: CreatePageListener,
28
- ) {
29
- for (const pageRoute of pageRoutes) {
30
- const { urlPath, entryName } = pageRoute;
31
-
32
- const agreedRoutes = agreedRouteMap[entryName];
33
- if (agreedRoutes) {
34
- for (const agreedRoute of agreedRoutes) {
35
- const fullpath = normalize(`${urlPath}${agreedRoute.path}`) || '/';
36
- const route = { ...pageRoute, path: fullpath, agreed: true };
37
- const context = createContext(route, listener);
38
- // The hook function can return false to prevent the automatic addition of agreed routes
39
- const isStaticPage = await hook(context);
40
-
41
- if (!isDynamicUrl(fullpath) && isStaticPage !== false) {
42
- const autoAdd = autoAddAgreed({
43
- ...context,
44
- component: agreedRoute._component,
45
- });
46
- autoAdd && context.createPage();
47
- }
48
- }
49
- } else {
50
- const route = { ...pageRoute, path: urlPath };
51
- const context = createContext(route, listener);
52
- await hook(context);
53
- }
54
- }
55
- }
@@ -1,16 +0,0 @@
1
- import { compile } from '../server/prerender';
2
- import { SsgRoute } from '../types';
3
-
4
- export function makeRender(
5
- ssgRoutes: SsgRoute[],
6
- render: ReturnType<typeof compile>,
7
- port: number,
8
- ): Promise<string>[] {
9
- return ssgRoutes.map((ssgRoute: SsgRoute) =>
10
- render({
11
- url: ssgRoute.urlPath,
12
- headers: { host: `localhost:${port}`, ...ssgRoute.headers },
13
- connection: {},
14
- }),
15
- );
16
- }
@@ -1,99 +0,0 @@
1
- import { isDev } from '@modern-js/utils';
2
- import { LoaderManifest, MODE } from '../manifest-op';
3
-
4
- const FUNCTION_CREATE_CONTAINER_NAME = 'createContainer';
5
- const FUNCTION_USE_LOADER_NAME = 'useLoader';
6
- const FUNCTION_USE_STATIC_LOADER_NAME = 'useStaticLoader';
7
- const CONTAINER_LOADER_NAME = 'loader';
8
- const CONTAINER_STATIC_LOADER_NAME = 'staticLoader';
9
-
10
- const noop = function () {
11
- return { name: 'babel-plugin-ssg-static-loader' };
12
- };
13
-
14
- // develoment not need to static analysis
15
- const loader = isDev()
16
- ? noop
17
- : function () {
18
- const loaderManifest = new LoaderManifest();
19
- let useSSG = 0;
20
- let createContainer: string | null = null;
21
- let useStaticLoader: string | null = null;
22
- let useLoader: string | null = null;
23
-
24
- return {
25
- name: 'babel-plugin-ssg-static-loader',
26
- // reset settings whenever a new file passes through loader
27
- pre() {
28
- useSSG = 0;
29
- createContainer = null;
30
- useStaticLoader = null;
31
- useLoader = null;
32
- },
33
- visitor: {
34
- ImportSpecifier(path: any) {
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
- Identifier(path: any) {
51
- // If the current file uses useLoader, the page can use SSG in MIXIN mode
52
- // Todo: Mixin Mode is not support
53
- const nodeName = path.node.name;
54
- if (nodeName === useLoader && path.key === 'callee') {
55
- useSSG = Math.max(useSSG, MODE.MIXIN);
56
- return;
57
- }
58
-
59
- // If the current file uses useStaticLoader, the page can use SSG in STRICT mode
60
- if (nodeName === useStaticLoader && path.key === 'callee') {
61
- useSSG = Math.max(useSSG, MODE.STRICT);
62
- return;
63
- }
64
-
65
- // after testing the Hook API, skip detection if the current nodeName is not 'container.(loader | staticLoader)'
66
- if (
67
- nodeName !== CONTAINER_LOADER_NAME &&
68
- nodeName !== CONTAINER_STATIC_LOADER_NAME
69
- ) {
70
- return;
71
- }
72
-
73
- // if the current nodeName is 'container.(loader | staticLoader)', check whether the calling node is 'createContainer'
74
- const closestPath = path.find((p: any) => p.isCallExpression());
75
- if (closestPath?.node?.callee?.name === createContainer) {
76
- if (nodeName === CONTAINER_LOADER_NAME) {
77
- useSSG = Math.max(useSSG, MODE.MIXIN);
78
- return;
79
- }
80
-
81
- if (nodeName === CONTAINER_STATIC_LOADER_NAME) {
82
- useSSG = Math.max(useSSG, MODE.STRICT);
83
- }
84
- }
85
- },
86
- },
87
- post(file: any) {
88
- const { filename } = file.opts;
89
- // if the current usage mode is not determined, that is, no runtime API is used, the default is LOOSE
90
- if (!useSSG) {
91
- useSSG = MODE.LOOSE;
92
- }
93
-
94
- loaderManifest.add(filename, useSSG);
95
- },
96
- };
97
- };
98
-
99
- export default loader;