@shuvi/platform-shared 1.0.0-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.
- package/esm/shared/application.d.ts +82 -0
- package/esm/shared/application.js +87 -0
- package/esm/shared/applicationTypes.d.ts +45 -0
- package/esm/shared/applicationTypes.js +1 -0
- package/esm/shared/helper/getAppData.d.ts +20 -0
- package/esm/shared/helper/getAppData.js +26 -0
- package/esm/shared/helper/getFilesOfRoute.d.ts +10 -0
- package/esm/shared/helper/getFilesOfRoute.js +30 -0
- package/esm/shared/helper/getPageData.d.ts +1 -0
- package/esm/shared/helper/getPageData.js +13 -0
- package/esm/shared/helper/getPublicPath.d.ts +1 -0
- package/esm/shared/helper/getPublicPath.js +6 -0
- package/esm/shared/helper/index.d.ts +5 -0
- package/esm/shared/helper/index.js +5 -0
- package/esm/shared/helper/router.d.ts +4 -0
- package/esm/shared/helper/router.js +60 -0
- package/esm/shared/index.d.ts +9 -0
- package/esm/shared/index.js +10 -0
- package/esm/shared/lifecycle.d.ts +46 -0
- package/esm/shared/lifecycle.js +40 -0
- package/esm/shared/loader/index.d.ts +3 -0
- package/esm/shared/loader/index.js +3 -0
- package/esm/shared/loader/loader.d.ts +9 -0
- package/esm/shared/loader/loader.js +114 -0
- package/esm/shared/loader/loaderManager.d.ts +16 -0
- package/esm/shared/loader/loaderManager.js +49 -0
- package/esm/shared/loader/types.d.ts +73 -0
- package/esm/shared/loader/types.js +1 -0
- package/esm/shared/models/error.d.ts +13 -0
- package/esm/shared/models/error.js +32 -0
- package/esm/shared/response.d.ts +51 -0
- package/esm/shared/response.js +126 -0
- package/esm/shared/router.d.ts +1 -0
- package/esm/shared/router.js +1 -0
- package/esm/shared/routerTypes.d.ts +28 -0
- package/esm/shared/routerTypes.js +1 -0
- package/esm/shared/shuvi-singleton-runtimeConfig.d.ts +12 -0
- package/esm/shared/shuvi-singleton-runtimeConfig.js +19 -0
- package/esm/shuvi-app/application.d.ts +4 -0
- package/esm/shuvi-app/application.js +34 -0
- package/esm/shuvi-app/shared.d.ts +1 -0
- package/esm/shuvi-app/shared.js +1 -0
- package/esm/shuvi-app/shuvi-runtime-app.d.ts +14 -0
- package/esm/shuvi-app/shuvi-runtime-app.js +1 -0
- package/esm/shuvi-app/shuvi-runtime-index.d.ts +10 -0
- package/esm/shuvi-app/shuvi-runtime-index.js +9 -0
- package/lib/node/index.d.ts +2 -0
- package/lib/node/index.js +18 -0
- package/lib/node/paths.d.ts +3 -0
- package/lib/node/paths.js +12 -0
- package/lib/node/platform/index.d.ts +16 -0
- package/lib/node/platform/index.js +23 -0
- package/lib/node/platform/plugins/main/hooks.d.ts +9 -0
- package/lib/node/platform/plugins/main/hooks.js +12 -0
- package/lib/node/platform/plugins/main/index.d.ts +16 -0
- package/lib/node/platform/plugins/main/index.js +32 -0
- package/lib/node/platform/plugins/main/shuvi-app.d.ts +8 -0
- package/lib/node/platform/plugins/main/shuvi-app.js +2 -0
- package/lib/node/platform/runtimeFiles.d.ts +11 -0
- package/lib/node/platform/runtimeFiles.js +47 -0
- package/lib/node/project/file-presets/files/app/core/entry.js.d.ts +3 -0
- package/lib/node/project/file-presets/files/app/core/entry.js.js +52 -0
- package/lib/node/project/file-presets/files/app/core/error.js.d.ts +2 -0
- package/lib/node/project/file-presets/files/app/core/error.js.js +9 -0
- package/lib/node/project/file-presets/files/app/core/platform.js.d.ts +3 -0
- package/lib/node/project/file-presets/files/app/core/platform.js.js +9 -0
- package/lib/node/project/file-presets/files/app/core/polyfill.js.d.ts +3 -0
- package/lib/node/project/file-presets/files/app/core/polyfill.js.js +6 -0
- package/lib/node/project/file-presets/files/app/core/runtimeConfig.js.d.ts +3 -0
- package/lib/node/project/file-presets/files/app/core/runtimeConfig.js.js +12 -0
- package/lib/node/project/file-presets/files/app/core/setPublicRuntimeConfig.js.d.ts +2 -0
- package/lib/node/project/file-presets/files/app/core/setPublicRuntimeConfig.js.js +7 -0
- package/lib/node/project/file-presets/files/app/core/setRuntimeConfig.js.d.ts +2 -0
- package/lib/node/project/file-presets/files/app/core/setRuntimeConfig.js.js +7 -0
- package/lib/node/project/file-presets/files/app/user/app.js.d.ts +3 -0
- package/lib/node/project/file-presets/files/app/user/app.js.js +15 -0
- package/lib/node/project/file-presets/files/app/user/error.js.d.ts +3 -0
- package/lib/node/project/file-presets/files/app/user/error.js.js +15 -0
- package/lib/node/project/file-presets/index.d.ts +4 -0
- package/lib/node/project/file-presets/index.js +58 -0
- package/lib/node/project/index.d.ts +2 -0
- package/lib/node/project/index.js +7 -0
- package/lib/node/project/projectContext.d.ts +13 -0
- package/lib/node/project/projectContext.js +16 -0
- package/lib/node/route/helpers.d.ts +20 -0
- package/lib/node/route/helpers.js +199 -0
- package/lib/node/route/index.d.ts +1 -0
- package/lib/node/route/index.js +7 -0
- package/lib/node/route/route.d.ts +37 -0
- package/lib/node/route/route.js +212 -0
- package/lib/shared/application.d.ts +82 -0
- package/lib/shared/application.js +91 -0
- package/lib/shared/applicationTypes.d.ts +45 -0
- package/lib/shared/applicationTypes.js +2 -0
- package/lib/shared/helper/getAppData.d.ts +20 -0
- package/lib/shared/helper/getAppData.js +30 -0
- package/lib/shared/helper/getFilesOfRoute.d.ts +10 -0
- package/lib/shared/helper/getFilesOfRoute.js +34 -0
- package/lib/shared/helper/getPageData.d.ts +1 -0
- package/lib/shared/helper/getPageData.js +17 -0
- package/lib/shared/helper/getPublicPath.d.ts +1 -0
- package/lib/shared/helper/getPublicPath.js +10 -0
- package/lib/shared/helper/index.d.ts +5 -0
- package/lib/shared/helper/index.js +24 -0
- package/lib/shared/helper/router.d.ts +4 -0
- package/lib/shared/helper/router.js +68 -0
- package/lib/shared/index.d.ts +9 -0
- package/lib/shared/index.js +28 -0
- package/lib/shared/lifecycle.d.ts +46 -0
- package/lib/shared/lifecycle.js +45 -0
- package/lib/shared/loader/index.d.ts +3 -0
- package/lib/shared/loader/index.js +19 -0
- package/lib/shared/loader/loader.d.ts +9 -0
- package/lib/shared/loader/loader.js +122 -0
- package/lib/shared/loader/loaderManager.d.ts +16 -0
- package/lib/shared/loader/loaderManager.js +53 -0
- package/lib/shared/loader/types.d.ts +73 -0
- package/lib/shared/loader/types.js +2 -0
- package/lib/shared/models/error.d.ts +13 -0
- package/lib/shared/models/error.js +35 -0
- package/lib/shared/response.d.ts +51 -0
- package/lib/shared/response.js +134 -0
- package/lib/shared/router.d.ts +1 -0
- package/lib/shared/router.js +11 -0
- package/lib/shared/routerTypes.d.ts +28 -0
- package/lib/shared/routerTypes.js +2 -0
- package/lib/shared/shuvi-singleton-runtimeConfig.d.ts +12 -0
- package/lib/shared/shuvi-singleton-runtimeConfig.js +26 -0
- package/package.json +90 -0
- package/shuvi-app-extensions.d.ts +58 -0
- package/template/document.ejs +10 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const service_1 = require("@shuvi/service");
|
|
4
|
+
const paths_1 = require("../../../paths");
|
|
5
|
+
const hooks_1 = require("./hooks");
|
|
6
|
+
const core = (0, service_1.createPlugin)({
|
|
7
|
+
setup: ({ addHooks }) => {
|
|
8
|
+
addHooks(hooks_1.extendedHooks);
|
|
9
|
+
},
|
|
10
|
+
addRuntimeService: () => [
|
|
11
|
+
{
|
|
12
|
+
// must be export separately, we need the module path to always be the
|
|
13
|
+
// same as what we've defined in
|
|
14
|
+
// "packages/toolpack/src/webpack/config/parts/external.ts"
|
|
15
|
+
source: (0, paths_1.resolvePkgFile)('lib/shared/shuvi-singleton-runtimeConfig'),
|
|
16
|
+
exported: '{ getRuntimeConfig }'
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
source: (0, paths_1.resolvePkgFile)('esm/shuvi-app/shuvi-runtime-index'),
|
|
20
|
+
exported: '*'
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
source: (0, paths_1.resolvePkgFile)('esm/shuvi-app/shuvi-runtime-app'),
|
|
24
|
+
filepath: 'app.ts',
|
|
25
|
+
exported: '*'
|
|
26
|
+
}
|
|
27
|
+
]
|
|
28
|
+
});
|
|
29
|
+
exports.default = {
|
|
30
|
+
core,
|
|
31
|
+
types: (0, paths_1.resolvePkgFile)('lib/node/platform/plugins/main/shuvi-app.d.ts')
|
|
32
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { extendedHooks } from './hooks';
|
|
2
|
+
declare module '@shuvi/runtime' {
|
|
3
|
+
interface CustomCorePluginHooks {
|
|
4
|
+
addEntryCode: typeof extendedHooks.addEntryCode;
|
|
5
|
+
addPolyfill: typeof extendedHooks.addPolyfill;
|
|
6
|
+
modifyRuntimeConfig: typeof extendedHooks.modifyRuntimeConfig;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { IPluginContext } from '@shuvi/service';
|
|
2
|
+
export declare const getRuntimeConfigFromConfig: (pluginContext: IPluginContext) => Promise<{
|
|
3
|
+
public: import("@shuvi/service/lib/core/apiTypes").IRuntimeConfig;
|
|
4
|
+
server: import("@shuvi/service/lib/core/apiTypes").IRuntimeConfig;
|
|
5
|
+
}>;
|
|
6
|
+
/**
|
|
7
|
+
* A creator for `getPresetRuntimeFiles` which helps platforms to build a bunch of runtime files including
|
|
8
|
+
* `core/error`, `core/platform`, `core/plugins`, `core/polyfill`, `core/runtimeConfig`, `core/setRuntimeConfig`,
|
|
9
|
+
* `user/error`, `user/runtime` and `entry`
|
|
10
|
+
*/
|
|
11
|
+
export declare const getPresetRuntimeFilesCreator: (platformModule: string, polyfills: string[]) => (pluginContext: IPluginContext) => Promise<import("@shuvi/service/lib/project").FileOptions<any, any>[]>;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.getPresetRuntimeFilesCreator = exports.getRuntimeConfigFromConfig = void 0;
|
|
13
|
+
const project_1 = require("@shuvi/service/lib/project");
|
|
14
|
+
const projectContext_1 = require("../project/projectContext");
|
|
15
|
+
const file_presets_1 = require("../project/file-presets");
|
|
16
|
+
const getRuntimeConfigFromConfig = (pluginContext) => __awaiter(void 0, void 0, void 0, function* () {
|
|
17
|
+
const { pluginRunner, config } = pluginContext;
|
|
18
|
+
return yield pluginRunner.modifyRuntimeConfig({
|
|
19
|
+
public: config.publicRuntimeConfig || {},
|
|
20
|
+
server: config.runtimeConfig || {}
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
exports.getRuntimeConfigFromConfig = getRuntimeConfigFromConfig;
|
|
24
|
+
/**
|
|
25
|
+
* A creator for `getPresetRuntimeFiles` which helps platforms to build a bunch of runtime files including
|
|
26
|
+
* `core/error`, `core/platform`, `core/plugins`, `core/polyfill`, `core/runtimeConfig`, `core/setRuntimeConfig`,
|
|
27
|
+
* `user/error`, `user/runtime` and `entry`
|
|
28
|
+
*/
|
|
29
|
+
const getPresetRuntimeFilesCreator = (platformModule, polyfills) => (pluginContext) => __awaiter(void 0, void 0, void 0, function* () {
|
|
30
|
+
const getCandidates = (fileName, fallbackType) => project_1.fileUtils.getUserCustomFileCandidates(pluginContext.paths.rootDir, fileName, fallbackType);
|
|
31
|
+
const context = (0, projectContext_1.createProjectContext)();
|
|
32
|
+
context.userModule = {
|
|
33
|
+
error: getCandidates('error', 'nullish'),
|
|
34
|
+
app: getCandidates('app', 'noop')
|
|
35
|
+
};
|
|
36
|
+
context.platformModule = platformModule;
|
|
37
|
+
const { public: publicRuntimeConfig, server: serverRuntimeConfig } = yield (0, exports.getRuntimeConfigFromConfig)(pluginContext);
|
|
38
|
+
context.runtimeConfig = Object.assign(Object.assign({}, serverRuntimeConfig), publicRuntimeConfig);
|
|
39
|
+
const runner = pluginContext.pluginRunner;
|
|
40
|
+
const appPolyfills = (yield runner.addPolyfill()).flat();
|
|
41
|
+
const appEntryCodes = (yield runner.addEntryCode()).flat();
|
|
42
|
+
context.entryCodes.push(...appEntryCodes);
|
|
43
|
+
context.polyfills.push(...polyfills, ...appPolyfills);
|
|
44
|
+
const files = (0, file_presets_1.getFilePresets)(context);
|
|
45
|
+
return files;
|
|
46
|
+
});
|
|
47
|
+
exports.getPresetRuntimeFilesCreator = getPresetRuntimeFilesCreator;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
const project_1 = require("@shuvi/service/lib/project");
|
|
27
|
+
const __1 = require("../../..");
|
|
28
|
+
const os = __importStar(require("os"));
|
|
29
|
+
exports.default = (context) => (0, __1.defineFile)({
|
|
30
|
+
content: () => {
|
|
31
|
+
const codes = context.entryCodes;
|
|
32
|
+
let imports = '';
|
|
33
|
+
let body = '';
|
|
34
|
+
for (let index = 0; index < codes.length; index++) {
|
|
35
|
+
const code = codes[index];
|
|
36
|
+
const snippet = project_1.fileUtils.getCodeSnippet(code);
|
|
37
|
+
if (snippet.imports) {
|
|
38
|
+
imports += `${snippet.imports}${os.EOL}`;
|
|
39
|
+
}
|
|
40
|
+
if (snippet.body) {
|
|
41
|
+
body += `${snippet.body}${os.EOL}`;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const content = `${imports}${os.EOL}${body}`;
|
|
45
|
+
if (content.trim() === '') {
|
|
46
|
+
return '// empty entry';
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
return content;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const __1 = require("../../..");
|
|
4
|
+
const content = `import userError from '@shuvi/app/user/error'
|
|
5
|
+
export default userError
|
|
6
|
+
`;
|
|
7
|
+
exports.default = () => (0, __1.defineFile)({
|
|
8
|
+
content: () => content
|
|
9
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const __1 = require("../../..");
|
|
4
|
+
const project_1 = require("@shuvi/service/lib/project");
|
|
5
|
+
exports.default = (context) => (0, __1.defineFile)({
|
|
6
|
+
content: () => {
|
|
7
|
+
return project_1.fileUtils.getModuleExport(context.platformModule);
|
|
8
|
+
}
|
|
9
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const __1 = require("../../..");
|
|
4
|
+
const shuvi_singleton_runtimeConfig_1 = require("../../../../../../shared/shuvi-singleton-runtimeConfig");
|
|
5
|
+
exports.default = (context) => (0, __1.defineFile)({
|
|
6
|
+
content: () => {
|
|
7
|
+
const runtimeConfigContent = (0, shuvi_singleton_runtimeConfig_1.getPublicRuntimeConfig)()
|
|
8
|
+
? JSON.stringify((0, shuvi_singleton_runtimeConfig_1.getPublicRuntimeConfig)())
|
|
9
|
+
: null;
|
|
10
|
+
return `export default ${runtimeConfigContent}`;
|
|
11
|
+
}
|
|
12
|
+
});
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const __1 = require("../../..");
|
|
4
|
+
const paths_1 = require("../../../../../paths");
|
|
5
|
+
exports.default = () => (0, __1.defineFile)({
|
|
6
|
+
content: () => `export { setPublicRuntimeConfig as default } from '${(0, paths_1.resolvePkgFile)('lib/shared/shuvi-singleton-runtimeConfig')}'`
|
|
7
|
+
});
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const __1 = require("../../..");
|
|
4
|
+
const paths_1 = require("../../../../../paths");
|
|
5
|
+
exports.default = () => (0, __1.defineFile)({
|
|
6
|
+
content: () => `export { setRuntimeConfig as default } from '${(0, paths_1.resolvePkgFile)('lib/shared/shuvi-singleton-runtimeConfig')}'`
|
|
7
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const project_1 = require("@shuvi/service/lib/project");
|
|
4
|
+
const __1 = require("../../..");
|
|
5
|
+
const { getAllFiles, getFirstModuleExport } = project_1.fileUtils;
|
|
6
|
+
exports.default = (context) => {
|
|
7
|
+
const { app } = context.userModule;
|
|
8
|
+
const candidates = Array.isArray(app) ? app : [app];
|
|
9
|
+
return (0, __1.defineFile)({
|
|
10
|
+
dependencies: candidates,
|
|
11
|
+
content: () => {
|
|
12
|
+
return getFirstModuleExport(getAllFiles(candidates), candidates);
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const project_1 = require("@shuvi/service/lib/project");
|
|
4
|
+
const __1 = require("../../..");
|
|
5
|
+
const { getAllFiles, getFirstModuleExport } = project_1.fileUtils;
|
|
6
|
+
exports.default = (context) => {
|
|
7
|
+
const { error } = context.userModule;
|
|
8
|
+
const candidates = Array.isArray(error) ? error : [error];
|
|
9
|
+
return (0, __1.defineFile)({
|
|
10
|
+
dependencies: candidates,
|
|
11
|
+
content: () => {
|
|
12
|
+
return getFirstModuleExport(getAllFiles(candidates), candidates, true);
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { FileOptions, DefineFileOption } from '@shuvi/service/lib/project';
|
|
2
|
+
import { ProjectContext } from '../projectContext';
|
|
3
|
+
export declare function getFilePresets(context: ProjectContext): FileOptions[];
|
|
4
|
+
export declare const defineFile: (options: Omit<DefineFileOption, 'name'>) => Omit<DefineFileOption, "name">;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.defineFile = exports.getFilePresets = void 0;
|
|
27
|
+
const fs = __importStar(require("fs"));
|
|
28
|
+
const path = __importStar(require("path"));
|
|
29
|
+
const project_1 = require("@shuvi/service/lib/project");
|
|
30
|
+
const EXT_REGEXP = /\.[a-zA-Z]+$/;
|
|
31
|
+
/**
|
|
32
|
+
* All preset files are listed in `files` folder as real files arrange.
|
|
33
|
+
* These presets export objects as FileOptions with which fileManager will generate files.
|
|
34
|
+
*/
|
|
35
|
+
const getAllFiles = (context, dirPath, parent = '', fileList = []) => {
|
|
36
|
+
const files = fs.readdirSync(dirPath);
|
|
37
|
+
let currentFileList = fileList;
|
|
38
|
+
files.forEach((file) => {
|
|
39
|
+
const filepath = path.join(dirPath, file);
|
|
40
|
+
const name = path.join(parent, file.replace(EXT_REGEXP, ''));
|
|
41
|
+
if (fs.statSync(filepath).isDirectory()) {
|
|
42
|
+
currentFileList = getAllFiles(context, filepath, name, currentFileList);
|
|
43
|
+
// Match *.ts (source) or *.js (compiled) file, but ignore *.d.ts file
|
|
44
|
+
}
|
|
45
|
+
else if (/\.(js|ts)$/.test(file) && !/\.d\.ts$/.test(file)) {
|
|
46
|
+
const fileOptionsCreater = require(filepath).default;
|
|
47
|
+
const options = fileOptionsCreater(context);
|
|
48
|
+
currentFileList.push((0, project_1.defineFile)(Object.assign(Object.assign({}, options), { name })));
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
return currentFileList;
|
|
52
|
+
};
|
|
53
|
+
function getFilePresets(context) {
|
|
54
|
+
return getAllFiles(context, path.join(__dirname, 'files'));
|
|
55
|
+
}
|
|
56
|
+
exports.getFilePresets = getFilePresets;
|
|
57
|
+
const defineFile = (options) => options;
|
|
58
|
+
exports.defineFile = defineFile;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createProjectContext = exports.getFilePresets = void 0;
|
|
4
|
+
var file_presets_1 = require("./file-presets");
|
|
5
|
+
Object.defineProperty(exports, "getFilePresets", { enumerable: true, get: function () { return file_presets_1.getFilePresets; } });
|
|
6
|
+
var projectContext_1 = require("./projectContext");
|
|
7
|
+
Object.defineProperty(exports, "createProjectContext", { enumerable: true, get: function () { return projectContext_1.createProjectContext; } });
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { IRuntimeConfig } from '@shuvi/service/lib/core';
|
|
2
|
+
export interface UserModule {
|
|
3
|
+
app: string | string[];
|
|
4
|
+
error: string | string[];
|
|
5
|
+
}
|
|
6
|
+
export interface ProjectContext {
|
|
7
|
+
entryCodes: string[];
|
|
8
|
+
polyfills: string[];
|
|
9
|
+
runtimeConfig: IRuntimeConfig;
|
|
10
|
+
platformModule: string;
|
|
11
|
+
userModule: UserModule;
|
|
12
|
+
}
|
|
13
|
+
export declare const createProjectContext: () => ProjectContext;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createProjectContext = void 0;
|
|
4
|
+
const createProjectContext = () => {
|
|
5
|
+
return {
|
|
6
|
+
entryCodes: [],
|
|
7
|
+
polyfills: [],
|
|
8
|
+
runtimeConfig: {},
|
|
9
|
+
platformModule: '',
|
|
10
|
+
userModule: {
|
|
11
|
+
app: '',
|
|
12
|
+
error: ''
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
exports.createProjectContext = createProjectContext;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { IApiRouteConfig, IMiddlewareRouteConfig, IPageRouteConfig } from './route';
|
|
2
|
+
declare const supportFileTypes: readonly ["page", "layout", "middleware", "api"];
|
|
3
|
+
declare type GetArrayElementType<T extends readonly any[]> = T extends readonly any[] ? T[number] : never;
|
|
4
|
+
export declare type SupportFileType = GetArrayElementType<typeof supportFileTypes>;
|
|
5
|
+
declare type CapName = Capitalize<SupportFileType>;
|
|
6
|
+
declare type FileTypeChecker = Record<`is${CapName}`, (filename: string) => boolean>;
|
|
7
|
+
export declare function normalizeFilePath(filepath: string): string;
|
|
8
|
+
export declare function parseDynamicPath(normalizedRoute: string): string;
|
|
9
|
+
export declare function normalizeRoutePath(rawPath: string): string;
|
|
10
|
+
export declare const normalize: (path: string) => string;
|
|
11
|
+
export declare const isRouteFile: (file: string) => boolean;
|
|
12
|
+
export declare const getAllowFilesAndDirs: (dirname: string) => Promise<string[]>;
|
|
13
|
+
export declare const hasAllowFiles: (files: string[]) => boolean;
|
|
14
|
+
export declare const readDir: (fullPath: string) => Promise<string[]>;
|
|
15
|
+
export declare const hasRouteChildren: (files: string[], parentPath: string) => Promise<boolean>;
|
|
16
|
+
declare const fileTypeChecker: FileTypeChecker;
|
|
17
|
+
declare function sortRoutes(routes: IApiRouteConfig[]): IApiRouteConfig[];
|
|
18
|
+
declare function sortRoutes(routes: IMiddlewareRouteConfig[]): IMiddlewareRouteConfig[];
|
|
19
|
+
declare function sortRoutes(routes: IPageRouteConfig[]): IPageRouteConfig[];
|
|
20
|
+
export { fileTypeChecker, sortRoutes };
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.sortRoutes = exports.fileTypeChecker = exports.hasRouteChildren = exports.readDir = exports.hasAllowFiles = exports.getAllowFilesAndDirs = exports.isRouteFile = exports.normalize = exports.normalizeRoutePath = exports.parseDynamicPath = exports.normalizeFilePath = void 0;
|
|
16
|
+
const fs_1 = __importDefault(require("fs"));
|
|
17
|
+
const path_1 = require("path");
|
|
18
|
+
const file_1 = require("@shuvi/utils/lib/file");
|
|
19
|
+
const invariant_1 = __importDefault(require("@shuvi/utils/lib/invariant"));
|
|
20
|
+
const router_1 = require("../../shared/router");
|
|
21
|
+
const supportFileTypes = ['page', 'layout', 'middleware', 'api'];
|
|
22
|
+
const allowReadFilExtList = ['ts', 'js', 'tsx', 'jsx'];
|
|
23
|
+
const dynamicMatchAllRegex = /\[\[(.+?)\]\]/g;
|
|
24
|
+
const dynamicMatchPartRegex = /\[(.+?)\]/g;
|
|
25
|
+
function normalizeFilePath(filepath) {
|
|
26
|
+
const res = filepath
|
|
27
|
+
// Remove the file extension from the end
|
|
28
|
+
.replace(/\.\w+$/, '')
|
|
29
|
+
// Convert to unix path
|
|
30
|
+
.replace(/\\/g, '/');
|
|
31
|
+
return res.charAt(0) !== '/' ? '/' + res : res;
|
|
32
|
+
}
|
|
33
|
+
exports.normalizeFilePath = normalizeFilePath;
|
|
34
|
+
function parseDynamicPath(normalizedRoute) {
|
|
35
|
+
(0, invariant_1.default)(normalizedRoute.length >= 1, 'parseDynamicPath param normalizedRoute length should not >= 1');
|
|
36
|
+
(0, invariant_1.default)(!checkSpecialRegexChars(normalizedRoute), 'filePath should not be special regex chars: |\\{}()^$+*?');
|
|
37
|
+
return normalizedRoute
|
|
38
|
+
.slice(1)
|
|
39
|
+
.split('/')
|
|
40
|
+
.map(segment => {
|
|
41
|
+
let result = '';
|
|
42
|
+
result = segment.replace(dynamicMatchAllRegex, function (matchString, ...matchArr) {
|
|
43
|
+
return parseMatchRepeat(matchArr[0], true);
|
|
44
|
+
});
|
|
45
|
+
result = result.replace(dynamicMatchPartRegex, function (matchString, ...matchArr) {
|
|
46
|
+
return parseMatchRepeat(matchArr[0], false);
|
|
47
|
+
});
|
|
48
|
+
return `/${result}`;
|
|
49
|
+
})
|
|
50
|
+
.join('');
|
|
51
|
+
}
|
|
52
|
+
exports.parseDynamicPath = parseDynamicPath;
|
|
53
|
+
function parseMatchRepeat(param, optional) {
|
|
54
|
+
const repeat = param.startsWith('...');
|
|
55
|
+
if (repeat) {
|
|
56
|
+
param = param.slice(3);
|
|
57
|
+
}
|
|
58
|
+
return repeat
|
|
59
|
+
? optional
|
|
60
|
+
? `:${param}*`
|
|
61
|
+
: `:${param}+`
|
|
62
|
+
: `:${param}${optional ? '?' : ''}`;
|
|
63
|
+
}
|
|
64
|
+
function checkSpecialRegexChars(string) {
|
|
65
|
+
return /[|\\{}()^$+*?]/g.test(string);
|
|
66
|
+
}
|
|
67
|
+
function normalizeRoutePath(rawPath) {
|
|
68
|
+
// /xxxx/index -> /xxxx/
|
|
69
|
+
let routePath = rawPath.replace(/\/index$/, '/');
|
|
70
|
+
// remove the last slash
|
|
71
|
+
// e.g. /abc/ -> /abc
|
|
72
|
+
if (routePath !== '/' && routePath.slice(-1) === '/') {
|
|
73
|
+
routePath = routePath.slice(0, -1);
|
|
74
|
+
}
|
|
75
|
+
routePath = parseDynamicPath(routePath);
|
|
76
|
+
return routePath;
|
|
77
|
+
}
|
|
78
|
+
exports.normalizeRoutePath = normalizeRoutePath;
|
|
79
|
+
const normalize = (path) => {
|
|
80
|
+
const result = normalizeRoutePath(normalizeFilePath(path));
|
|
81
|
+
if (result === '/') {
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
84
|
+
return result.replace(/^\//, '');
|
|
85
|
+
};
|
|
86
|
+
exports.normalize = normalize;
|
|
87
|
+
const isRouteFile = (file) => {
|
|
88
|
+
return Object.keys(fileTypeChecker).some(key => {
|
|
89
|
+
return fileTypeChecker[key](file);
|
|
90
|
+
});
|
|
91
|
+
};
|
|
92
|
+
exports.isRouteFile = isRouteFile;
|
|
93
|
+
const getAllowFilesAndDirs = (dirname) => __awaiter(void 0, void 0, void 0, function* () {
|
|
94
|
+
const result = [];
|
|
95
|
+
const resultHistory = {};
|
|
96
|
+
const files = yield (0, exports.readDir)(dirname);
|
|
97
|
+
for (const file of files) {
|
|
98
|
+
const isDir = yield (0, file_1.isDirectory)((0, path_1.join)(dirname, file));
|
|
99
|
+
if (isDir) {
|
|
100
|
+
result.push(file);
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
if ((0, exports.isRouteFile)(file)) {
|
|
104
|
+
const ext = (0, path_1.extname)(file);
|
|
105
|
+
const extWithoutDot = ext.slice(1);
|
|
106
|
+
const pureFilename = (0, path_1.basename)(file, ext);
|
|
107
|
+
const fileHistory = (resultHistory[pureFilename] =
|
|
108
|
+
resultHistory[pureFilename] || {});
|
|
109
|
+
const { type } = fileHistory;
|
|
110
|
+
switch (extWithoutDot) {
|
|
111
|
+
case 'js':
|
|
112
|
+
if (type) {
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
fileHistory.type = 'js';
|
|
116
|
+
break;
|
|
117
|
+
case 'jsx':
|
|
118
|
+
if (type) {
|
|
119
|
+
if (['ts', 'tsx'].includes(type)) {
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
fileHistory.type = 'jsx';
|
|
123
|
+
result[fileHistory.index] = file;
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
fileHistory.type = 'jsx';
|
|
127
|
+
break;
|
|
128
|
+
case 'ts':
|
|
129
|
+
if (type) {
|
|
130
|
+
if (type === 'tsx') {
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
fileHistory.type = 'ts';
|
|
134
|
+
result[fileHistory.index] = file;
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
fileHistory.type = 'ts';
|
|
138
|
+
break;
|
|
139
|
+
case 'tsx':
|
|
140
|
+
fileHistory.type = 'tsx';
|
|
141
|
+
if (type) {
|
|
142
|
+
result[fileHistory.index] = file;
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
fileHistory.index = result.length;
|
|
148
|
+
result.push(file);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return result;
|
|
152
|
+
});
|
|
153
|
+
exports.getAllowFilesAndDirs = getAllowFilesAndDirs;
|
|
154
|
+
const hasAllowFiles = (files) => files.some(file => (0, exports.isRouteFile)(file));
|
|
155
|
+
exports.hasAllowFiles = hasAllowFiles;
|
|
156
|
+
const readDir = (fullPath) => {
|
|
157
|
+
return fs_1.default.promises.readdir(fullPath, { encoding: 'utf-8' });
|
|
158
|
+
};
|
|
159
|
+
exports.readDir = readDir;
|
|
160
|
+
const hasRouteChildren = (files, parentPath) => __awaiter(void 0, void 0, void 0, function* () {
|
|
161
|
+
for (const file of files) {
|
|
162
|
+
if ((0, exports.isRouteFile)(file)) {
|
|
163
|
+
return true;
|
|
164
|
+
}
|
|
165
|
+
const fullPath = (0, path_1.join)(parentPath, file);
|
|
166
|
+
if (yield (0, file_1.isDirectory)(fullPath)) {
|
|
167
|
+
let result = yield (0, exports.hasRouteChildren)(yield (0, exports.readDir)(fullPath), fullPath);
|
|
168
|
+
if (result) {
|
|
169
|
+
return true;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return false;
|
|
174
|
+
});
|
|
175
|
+
exports.hasRouteChildren = hasRouteChildren;
|
|
176
|
+
const fileTypeChecker = {};
|
|
177
|
+
exports.fileTypeChecker = fileTypeChecker;
|
|
178
|
+
supportFileTypes.forEach(fileType => {
|
|
179
|
+
const allowNames = allowReadFilExtList.map(ext => `${fileType}.${ext}`);
|
|
180
|
+
const capName = (fileType.charAt(0).toUpperCase() +
|
|
181
|
+
fileType.slice(1));
|
|
182
|
+
fileTypeChecker[`is${capName}`] = (filename) => {
|
|
183
|
+
return allowNames.includes(filename);
|
|
184
|
+
};
|
|
185
|
+
});
|
|
186
|
+
function sortRoutes(routes) {
|
|
187
|
+
let rankRoutes = routes.map(route => {
|
|
188
|
+
return [route.path, route];
|
|
189
|
+
});
|
|
190
|
+
rankRoutes = (0, router_1.rankRouteBranches)(rankRoutes);
|
|
191
|
+
return rankRoutes.map(rankRoute => {
|
|
192
|
+
const rankRawRoute = rankRoute[1];
|
|
193
|
+
if (rankRawRoute.children) {
|
|
194
|
+
rankRawRoute.children = sortRoutes(rankRawRoute.children);
|
|
195
|
+
}
|
|
196
|
+
return rankRoute[1];
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
exports.sortRoutes = sortRoutes;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { IPageRouteConfig, IApiRouteConfig, IMiddlewareRouteConfig, getPageRoutes, getApiRoutes, getMiddlewareRoutes } from './route';
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getMiddlewareRoutes = exports.getApiRoutes = exports.getPageRoutes = void 0;
|
|
4
|
+
var route_1 = require("./route");
|
|
5
|
+
Object.defineProperty(exports, "getPageRoutes", { enumerable: true, get: function () { return route_1.getPageRoutes; } });
|
|
6
|
+
Object.defineProperty(exports, "getApiRoutes", { enumerable: true, get: function () { return route_1.getApiRoutes; } });
|
|
7
|
+
Object.defineProperty(exports, "getMiddlewareRoutes", { enumerable: true, get: function () { return route_1.getMiddlewareRoutes; } });
|