@modern-js/plugin-docsite 0.0.0-bundle-deps-202110123508
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 +342 -0
- package/LICENSE +21 -0
- package/README.md +30 -0
- package/dist/js/modern/build-task.js +29 -0
- package/dist/js/modern/features/build.js +17 -0
- package/dist/js/modern/features/constant.js +7 -0
- package/dist/js/modern/features/dev.js +24 -0
- package/dist/js/modern/features/index.js +47 -0
- package/dist/js/modern/features/utils/chokidar.js +37 -0
- package/dist/js/modern/features/utils/generate-files.js +197 -0
- package/dist/js/modern/features/utils/valid.js +17 -0
- package/dist/js/modern/features/utils/webpack.js +63 -0
- package/dist/js/modern/index.js +49 -0
- package/dist/js/node/build-task.js +33 -0
- package/dist/js/node/features/build.js +31 -0
- package/dist/js/node/features/constant.js +21 -0
- package/dist/js/node/features/dev.js +39 -0
- package/dist/js/node/features/index.js +68 -0
- package/dist/js/node/features/utils/chokidar.js +55 -0
- package/dist/js/node/features/utils/generate-files.js +223 -0
- package/dist/js/node/features/utils/valid.js +36 -0
- package/dist/js/node/features/utils/webpack.js +80 -0
- package/dist/js/node/index.js +61 -0
- package/dist/js/static/DocsNav.jsx.tpl +42 -0
- package/dist/js/static/DocsRoutes.jsx.tpl +19 -0
- package/dist/js/static/DocsToc.jsx.tpl +20 -0
- package/dist/js/static/docs-entry.jsx.tpl +41 -0
- package/dist/js/static/docs-page.jsx.tpl +107 -0
- package/dist/js/static/docs.css +1008 -0
- package/dist/js/static/index.html.ejs +15 -0
- package/dist/js/static/stories-entry.ts.tpl +6 -0
- package/dist/js/static/storybook-config.js.tpl +32 -0
- package/dist/js/static/tsconfig.json +25 -0
- package/dist/types/build-task.d.ts +1 -0
- package/dist/types/features/build.d.ts +5 -0
- package/dist/types/features/constant.d.ts +3 -0
- package/dist/types/features/dev.d.ts +2 -0
- package/dist/types/features/index.d.ts +13 -0
- package/dist/types/features/utils/chokidar.d.ts +1 -0
- package/dist/types/features/utils/generate-files.d.ts +4 -0
- package/dist/types/features/utils/valid.d.ts +9 -0
- package/dist/types/features/utils/webpack.d.ts +3 -0
- package/dist/types/index.d.ts +15 -0
- package/modern.config.js +2 -0
- package/package.json +98 -0
- package/src/build-task.ts +31 -0
- package/src/features/build.ts +31 -0
- package/src/features/constant.ts +8 -0
- package/src/features/dev.ts +37 -0
- package/src/features/index.ts +68 -0
- package/src/features/utils/chokidar.ts +44 -0
- package/src/features/utils/generate-files.ts +238 -0
- package/src/features/utils/valid.ts +23 -0
- package/src/features/utils/webpack.ts +107 -0
- package/src/index.ts +42 -0
- package/src/type.d.ts +4 -0
- package/static/DocsNav.jsx.tpl +42 -0
- package/static/DocsRoutes.jsx.tpl +19 -0
- package/static/DocsToc.jsx.tpl +20 -0
- package/static/docs-entry.jsx.tpl +41 -0
- package/static/docs-page.jsx.tpl +107 -0
- package/static/docs.css +1008 -0
- package/static/index.html.ejs +15 -0
- package/static/stories-entry.ts.tpl +6 -0
- package/static/storybook-config.js.tpl +32 -0
- package/static/tsconfig.json +25 -0
- package/tsconfig.json +13 -0
@@ -0,0 +1,223 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.generateFiles = generateFiles;
|
7
|
+
|
8
|
+
var _difference2 = _interopRequireDefault(require("lodash/difference"));
|
9
|
+
|
10
|
+
var _template2 = _interopRequireDefault(require("lodash/template"));
|
11
|
+
|
12
|
+
var _union2 = _interopRequireDefault(require("lodash/union"));
|
13
|
+
|
14
|
+
var _camelCase2 = _interopRequireDefault(require("lodash/camelCase"));
|
15
|
+
|
16
|
+
var _startCase2 = _interopRequireDefault(require("lodash/startCase"));
|
17
|
+
|
18
|
+
var _path = _interopRequireDefault(require("path"));
|
19
|
+
|
20
|
+
var _utils = require("@modern-js/utils");
|
21
|
+
|
22
|
+
var _grayMatter = _interopRequireDefault(require("gray-matter"));
|
23
|
+
|
24
|
+
var _githubSlugger = _interopRequireDefault(require("github-slugger"));
|
25
|
+
|
26
|
+
var _sortPaths = _interopRequireDefault(require("sort-paths"));
|
27
|
+
|
28
|
+
var _unistUtilVisit = _interopRequireDefault(require("unist-util-visit"));
|
29
|
+
|
30
|
+
var _mdastUtilToString = _interopRequireDefault(require("mdast-util-to-string"));
|
31
|
+
|
32
|
+
var _unistBuilder = _interopRequireDefault(require("unist-builder"));
|
33
|
+
|
34
|
+
var _mdx = _interopRequireDefault(require("@mdx-js/mdx"));
|
35
|
+
|
36
|
+
var _parser = _interopRequireDefault(require("@modern-js/deps/compiled/babel/parser"));
|
37
|
+
|
38
|
+
var _constant = require("../constant");
|
39
|
+
|
40
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
41
|
+
|
42
|
+
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; }
|
43
|
+
|
44
|
+
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; }
|
45
|
+
|
46
|
+
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; }
|
47
|
+
|
48
|
+
const loadTemplate = async (file, customPath = false) => {
|
49
|
+
let result = '';
|
50
|
+
|
51
|
+
if (customPath) {
|
52
|
+
result = await _utils.fs.readFile(file, 'utf-8');
|
53
|
+
} else {
|
54
|
+
result = await _utils.fs.readFile(_path.default.join(_constant.UTILS_STATIC, file), 'utf-8');
|
55
|
+
}
|
56
|
+
|
57
|
+
return (0, _template2.default)(result, {
|
58
|
+
interpolate: /<%=([\s\S]+?)%>/g
|
59
|
+
});
|
60
|
+
};
|
61
|
+
|
62
|
+
// eslint-disable-next-line max-statements
|
63
|
+
async function handleFile(appDirectory, tmpDir, file, images) {
|
64
|
+
const moduleName = file.replace(/\.(md|mdx)$/, '');
|
65
|
+
const content = await _utils.fs.readFile(_path.default.resolve(appDirectory, 'docs', file));
|
66
|
+
const parsed = (0, _grayMatter.default)(content);
|
67
|
+
|
68
|
+
if (!parsed.data.title) {
|
69
|
+
parsed.data.title = (0, _startCase2.default)((0, _camelCase2.default)(_path.default.basename(moduleName)));
|
70
|
+
}
|
71
|
+
|
72
|
+
const slugger = new _githubSlugger.default();
|
73
|
+
slugger.reset();
|
74
|
+
const babelOptions = {
|
75
|
+
allowUndeclaredExports: true,
|
76
|
+
sourceType: 'module'
|
77
|
+
};
|
78
|
+
const imported = [];
|
79
|
+
const exported = [];
|
80
|
+
const toc = [];
|
81
|
+
|
82
|
+
const modifier = () => tree => {
|
83
|
+
(0, _unistUtilVisit.default)(tree, 'import', node => {
|
84
|
+
const parsedNode = _parser.default.parse(node.value, babelOptions);
|
85
|
+
|
86
|
+
const nodes = parsedNode.program.body;
|
87
|
+
nodes.forEach(n => {
|
88
|
+
if (n.type === 'ImportDeclaration') {
|
89
|
+
n.specifiers.forEach(sp => {
|
90
|
+
imported.push({
|
91
|
+
id: sp.local.name,
|
92
|
+
node
|
93
|
+
});
|
94
|
+
});
|
95
|
+
}
|
96
|
+
});
|
97
|
+
});
|
98
|
+
(0, _unistUtilVisit.default)(tree, 'export', node => {
|
99
|
+
const parsedNode = _parser.default.parse(node.value, babelOptions);
|
100
|
+
|
101
|
+
const nodes = parsedNode.program.body;
|
102
|
+
nodes.forEach(n => {
|
103
|
+
if (n.type === 'ExportNamedDeclaration') {
|
104
|
+
n.specifiers.forEach(sp => {
|
105
|
+
exported.push({
|
106
|
+
id: sp.exported.name,
|
107
|
+
node
|
108
|
+
});
|
109
|
+
});
|
110
|
+
}
|
111
|
+
});
|
112
|
+
});
|
113
|
+
const extraImported = (0, _difference2.default)(imported.map(n => n.id), exported.map(n => n.id)); // we have to put the export statement with the import,
|
114
|
+
// otherwise the mdx's babel transformer cannot found
|
115
|
+
// the corresponding imports
|
116
|
+
|
117
|
+
extraImported.forEach(id => {
|
118
|
+
const {
|
119
|
+
node
|
120
|
+
} = imported.find(n => n.id === id);
|
121
|
+
node.value += `\nexport { ${id} };`;
|
122
|
+
});
|
123
|
+
(0, _unistUtilVisit.default)(tree, 'heading', node => {
|
124
|
+
if (node.depth === 2) {
|
125
|
+
const slug = slugger.slug((0, _mdastUtilToString.default)(node));
|
126
|
+
node.data = {
|
127
|
+
hProperties: {
|
128
|
+
id: slug
|
129
|
+
}
|
130
|
+
};
|
131
|
+
toc.push({
|
132
|
+
slug,
|
133
|
+
text: (0, _mdastUtilToString.default)(node)
|
134
|
+
});
|
135
|
+
}
|
136
|
+
});
|
137
|
+
(0, _unistUtilVisit.default)(tree, 'root', node => {
|
138
|
+
(node.children || []).unshift((0, _unistBuilder.default)('heading', {
|
139
|
+
depth: 1,
|
140
|
+
children: [(0, _unistBuilder.default)('text', {
|
141
|
+
value: parsed.data.title
|
142
|
+
})]
|
143
|
+
}));
|
144
|
+
});
|
145
|
+
(0, _unistUtilVisit.default)(tree, 'image', node => {
|
146
|
+
const {
|
147
|
+
url
|
148
|
+
} = node;
|
149
|
+
|
150
|
+
if (!url.startsWith('http')) {
|
151
|
+
const fullPath = _path.default.resolve(_path.default.dirname(_path.default.resolve(appDirectory, 'docs', file)), url);
|
152
|
+
|
153
|
+
const relativePath = _path.default.relative(_path.default.resolve(appDirectory, 'assets'), fullPath);
|
154
|
+
|
155
|
+
if (relativePath.startsWith('..')) {
|
156
|
+
_utils.logger.warn(`${url} referenced in ${file} is not under the "assets" folder`);
|
157
|
+
} else {
|
158
|
+
images.push(relativePath);
|
159
|
+
node.url = `/${_path.default.relative(_path.default.dirname(_path.default.resolve(appDirectory, 'docs', file)), _path.default.resolve(appDirectory, 'docs/assets', relativePath))}`;
|
160
|
+
}
|
161
|
+
}
|
162
|
+
});
|
163
|
+
};
|
164
|
+
|
165
|
+
const transpiled = await (0, _mdx.default)(parsed.content, {
|
166
|
+
remarkPlugins: [modifier]
|
167
|
+
});
|
168
|
+
const totalExported = (0, _union2.default)(imported.map(n => n.id), exported.map(n => n.id));
|
169
|
+
|
170
|
+
const outputFile = _path.default.resolve(tmpDir, `${moduleName}/mdx.jsx`);
|
171
|
+
|
172
|
+
await _utils.fs.outputFile(outputFile, _constant.MDX_DEFAULT_RENDERER + transpiled, {
|
173
|
+
encoding: 'utf8'
|
174
|
+
});
|
175
|
+
|
176
|
+
const pageFile = _path.default.resolve(tmpDir, `${moduleName}/index.jsx`);
|
177
|
+
|
178
|
+
const pageTemplate = await loadTemplate('docs-page.jsx.tpl');
|
179
|
+
const pageContent = pageTemplate({
|
180
|
+
moduleName,
|
181
|
+
toc: JSON.stringify(toc),
|
182
|
+
relRoot: _path.default.relative(_path.default.dirname(outputFile), tmpDir),
|
183
|
+
imports: totalExported.length ? `import { ${totalExported.join(', ')} } from './mdx';` : '',
|
184
|
+
imported: totalExported.join(',')
|
185
|
+
});
|
186
|
+
await _utils.fs.outputFile(pageFile, pageContent, {
|
187
|
+
encoding: 'utf8'
|
188
|
+
});
|
189
|
+
return _objectSpread(_objectSpread({}, parsed.data), {}, {
|
190
|
+
moduleName,
|
191
|
+
pageFile
|
192
|
+
});
|
193
|
+
}
|
194
|
+
|
195
|
+
async function generateFiles(appDirectory, tmpDir, files, isDev) {
|
196
|
+
await _utils.fs.remove(tmpDir);
|
197
|
+
await _utils.fs.ensureDir(tmpDir);
|
198
|
+
const images = [];
|
199
|
+
const meta = await Promise.all(files.map(file => handleFile(appDirectory, tmpDir, file, images)));
|
200
|
+
const sorted = (0, _sortPaths.default)(meta, e => e.moduleName.endsWith('index') ? e.moduleName.replace(/index$/, '') : `${e.moduleName}/`, '/');
|
201
|
+
const routesTemplate = await loadTemplate('DocsRoutes.jsx.tpl');
|
202
|
+
await _utils.fs.outputFile(_path.default.resolve(tmpDir, 'DocsRoutes.jsx'), routesTemplate({
|
203
|
+
meta: sorted
|
204
|
+
}));
|
205
|
+
const pkgInfo = await _utils.fs.readJson(_path.default.resolve(appDirectory, 'package.json'));
|
206
|
+
const entryTemplate = await loadTemplate('docs-entry.jsx.tpl');
|
207
|
+
await _utils.fs.outputFile(_path.default.resolve(tmpDir, 'docs-entry.jsx'), entryTemplate({
|
208
|
+
basename: isDev ? '/' : `${_constant.DOCS_RENDER_PATH}/${pkgInfo.name}/${pkgInfo.version}`
|
209
|
+
}));
|
210
|
+
await Promise.all(['DocsNav.jsx.tpl', 'DocsToc.jsx.tpl', 'docs.css'].map(async file => _utils.fs.copyFile(_path.default.resolve(_constant.UTILS_STATIC, file), _path.default.resolve(tmpDir, file.replace('.tpl', '')))));
|
211
|
+
await _utils.fs.outputJson(_path.default.resolve(tmpDir, 'meta.json'), sorted.map(({
|
212
|
+
title,
|
213
|
+
moduleName
|
214
|
+
}) => ({
|
215
|
+
title,
|
216
|
+
moduleName
|
217
|
+
})), {
|
218
|
+
spaces: 2
|
219
|
+
});
|
220
|
+
await _utils.fs.ensureDir(_path.default.resolve(appDirectory, 'dist/docs/assets'));
|
221
|
+
await Promise.all(Array.from(new Set(images)).map(file => _utils.fs.copyFile(_path.default.resolve(appDirectory, 'assets', file), _path.default.resolve(appDirectory, 'dist/docs/assets', file))));
|
222
|
+
return sorted;
|
223
|
+
}
|
@@ -0,0 +1,36 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.valid = void 0;
|
7
|
+
|
8
|
+
var _path = _interopRequireDefault(require("path"));
|
9
|
+
|
10
|
+
var glob = _interopRequireWildcard(require("glob"));
|
11
|
+
|
12
|
+
var _utils = require("@modern-js/utils");
|
13
|
+
|
14
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
15
|
+
|
16
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
17
|
+
|
18
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
19
|
+
|
20
|
+
const valid = ({
|
21
|
+
appDirectory,
|
22
|
+
docsDir
|
23
|
+
}) => {
|
24
|
+
const docsAbsPath = _path.default.join(appDirectory, docsDir);
|
25
|
+
|
26
|
+
const files = glob.sync(`${docsAbsPath}/**/*.{md,mdx}`);
|
27
|
+
|
28
|
+
if (!_utils.fs.existsSync(docsAbsPath) || files.length <= 0) {
|
29
|
+
console.info(_utils.chalk.yellow('No docs found, create directory "./docs" and add md(x) files'));
|
30
|
+
return false;
|
31
|
+
}
|
32
|
+
|
33
|
+
return true;
|
34
|
+
};
|
35
|
+
|
36
|
+
exports.valid = valid;
|
@@ -0,0 +1,80 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.generatorWebpackConfig = generatorWebpackConfig;
|
7
|
+
exports.runWebpack = runWebpack;
|
8
|
+
|
9
|
+
var _path = _interopRequireDefault(require("path"));
|
10
|
+
|
11
|
+
var _utils = require("@modern-js/utils");
|
12
|
+
|
13
|
+
var _htmlWebpackPlugin = _interopRequireDefault(require("@modern-js/deps/compiled/html-webpack-plugin"));
|
14
|
+
|
15
|
+
var _webpack = _interopRequireDefault(require("@modern-js/deps/compiled/webpack"));
|
16
|
+
|
17
|
+
var _webpack2 = require("@modern-js/webpack");
|
18
|
+
|
19
|
+
var _constant = require("../constant");
|
20
|
+
|
21
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
22
|
+
|
23
|
+
function generatorWebpackConfig(appDirectory, tmpDir, isDev) {
|
24
|
+
const originConfig = (0, _webpack2.getWebpackConfig)(_webpack2.WebpackConfigTarget.CLIENT);
|
25
|
+
const plugins = (originConfig.plugins || []).filter(p => p.constructor !== _webpack.default.HotModuleReplacementPlugin);
|
26
|
+
const config = {
|
27
|
+
mode: isDev ? 'development' : 'production',
|
28
|
+
context: tmpDir,
|
29
|
+
entry: {
|
30
|
+
index: _path.default.resolve(tmpDir, 'docs-entry.jsx')
|
31
|
+
},
|
32
|
+
output: {
|
33
|
+
path: _path.default.resolve(appDirectory, 'dist/docs')
|
34
|
+
},
|
35
|
+
resolve: originConfig.resolve || {
|
36
|
+
alias: {}
|
37
|
+
},
|
38
|
+
module: originConfig.module,
|
39
|
+
plugins: [...plugins, new _htmlWebpackPlugin.default({
|
40
|
+
templateContent: _utils.fs.readFileSync(_path.default.resolve(_constant.UTILS_STATIC, 'index.html.ejs'), 'utf8')
|
41
|
+
})]
|
42
|
+
};
|
43
|
+
config.resolve.modules = [...(config.resolve.modules || []), _path.default.join(__dirname, '../../../../../', 'node_modules')];
|
44
|
+
config.resolve.alias['@assets'] = _path.default.resolve(appDirectory, 'assets');
|
45
|
+
config.resolve.alias['@styles'] = _path.default.resolve(appDirectory, 'styles'); // fix this since react-live relies on core-js@2
|
46
|
+
|
47
|
+
config.resolve.alias[`${_path.default.dirname(require.resolve('core-js'))}/fn`] = 'core-js/es';
|
48
|
+
const pkgJSON = JSON.parse(_utils.fs.readFileSync(_path.default.join(appDirectory, 'package.json'), 'utf-8'));
|
49
|
+
|
50
|
+
if (pkgJSON.dependencies.react || pkgJSON.devDependencies.react) {
|
51
|
+
config.resolve.alias.react = _path.default.resolve('node_modules/react');
|
52
|
+
} else {
|
53
|
+
config.resolve.alias.react = _path.default.resolve(__dirname, '../../../../../', 'node_modules', 'react');
|
54
|
+
}
|
55
|
+
|
56
|
+
if (pkgJSON.dependencies['react-dom'] || pkgJSON.devDependencies['react-dom']) {
|
57
|
+
config.resolve.alias['react-dom'] = _path.default.resolve('node_modules/react-dom');
|
58
|
+
} else {
|
59
|
+
config.resolve.alias['react-dom'] = _path.default.resolve(__dirname, '../../../../../', 'node_modules', 'react-dom');
|
60
|
+
}
|
61
|
+
|
62
|
+
config.resolve.fallback = {
|
63
|
+
path: require.resolve('path-browserify')
|
64
|
+
};
|
65
|
+
return config;
|
66
|
+
}
|
67
|
+
|
68
|
+
async function runWebpack(config) {
|
69
|
+
await new Promise((resolve, reject) => {
|
70
|
+
(0, _webpack.default)(config).run((err, stats) => {
|
71
|
+
if (err) {
|
72
|
+
reject(err);
|
73
|
+
} else if (stats !== null && stats !== void 0 && stats.hasErrors()) {
|
74
|
+
reject(stats === null || stats === void 0 ? void 0 : stats.compilation.errors);
|
75
|
+
} else {
|
76
|
+
resolve(stats);
|
77
|
+
}
|
78
|
+
});
|
79
|
+
});
|
80
|
+
}
|
@@ -0,0 +1,61 @@
|
|
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
|
+
const core = _utils.Import.lazy('@modern-js/core', require);
|
11
|
+
|
12
|
+
const features = _utils.Import.lazy('./features', require);
|
13
|
+
|
14
|
+
var _default = core.createPlugin(() => ({
|
15
|
+
commands({
|
16
|
+
program
|
17
|
+
}) {
|
18
|
+
const {
|
19
|
+
appDirectory
|
20
|
+
} = core.useAppContext();
|
21
|
+
const devCommand = program.commandsMap.get('dev');
|
22
|
+
|
23
|
+
if (devCommand) {
|
24
|
+
devCommand.command('docs').action(async () => {
|
25
|
+
await features.buildDocs({
|
26
|
+
appDirectory,
|
27
|
+
isDev: true
|
28
|
+
});
|
29
|
+
});
|
30
|
+
}
|
31
|
+
},
|
32
|
+
|
33
|
+
// module-tools menu mode
|
34
|
+
moduleToolsMenu() {
|
35
|
+
const {
|
36
|
+
appDirectory
|
37
|
+
} = core.useAppContext();
|
38
|
+
return {
|
39
|
+
name: 'Docsite 调试',
|
40
|
+
value: 'docsite',
|
41
|
+
runTask: async () => features.buildDocs({
|
42
|
+
appDirectory,
|
43
|
+
isDev: true
|
44
|
+
})
|
45
|
+
};
|
46
|
+
},
|
47
|
+
|
48
|
+
platformBuild() {
|
49
|
+
return {
|
50
|
+
name: 'docsite',
|
51
|
+
title: 'Run Docsite log',
|
52
|
+
taskPath: require.resolve("./build-task"),
|
53
|
+
params: []
|
54
|
+
};
|
55
|
+
}
|
56
|
+
|
57
|
+
}), {
|
58
|
+
name: '@modern-js/plugin-docsite'
|
59
|
+
});
|
60
|
+
|
61
|
+
exports.default = _default;
|
@@ -0,0 +1,42 @@
|
|
1
|
+
import React from 'react';
|
2
|
+
import styled from 'styled-components';
|
3
|
+
import { Link } from 'react-router-dom';
|
4
|
+
import Menu from 'antd/es/menu';
|
5
|
+
|
6
|
+
const MenuItem = Menu.Item;
|
7
|
+
const { SubMenu } = Menu;
|
8
|
+
|
9
|
+
const Container = styled.div`
|
10
|
+
position: relative;
|
11
|
+
box-sizing: border-box;
|
12
|
+
height: 100%;
|
13
|
+
padding: 2rem 0;
|
14
|
+
border-right: 1px solid var(--theme-border);
|
15
|
+
overflow: auto;
|
16
|
+
`;
|
17
|
+
|
18
|
+
const DocsNav = ({ meta }) => {
|
19
|
+
return (
|
20
|
+
<Container>
|
21
|
+
<Menu>
|
22
|
+
{meta.map(page => {
|
23
|
+
const parts = page.moduleName.split(/\//g) || [];
|
24
|
+
if (parts[parts.length - 1] === 'index') {
|
25
|
+
parts.pop();
|
26
|
+
}
|
27
|
+
const level = Math.max(parts.length, 1);
|
28
|
+
|
29
|
+
return (
|
30
|
+
<MenuItem
|
31
|
+
key={page.moduleName}
|
32
|
+
style={{ paddingLeft: `${level + 0.5}rem` }}>
|
33
|
+
<Link to={`/${page.moduleName}`}>{page.title}</Link>
|
34
|
+
</MenuItem>
|
35
|
+
);
|
36
|
+
})}
|
37
|
+
</Menu>
|
38
|
+
</Container>
|
39
|
+
);
|
40
|
+
};
|
41
|
+
|
42
|
+
export default DocsNav;
|
@@ -0,0 +1,19 @@
|
|
1
|
+
import React from 'react';
|
2
|
+
import {
|
3
|
+
Switch,
|
4
|
+
Route,
|
5
|
+
Redirect,
|
6
|
+
} from "react-router-dom";
|
7
|
+
|
8
|
+
const DocsRoutes = ({ meta }) => {
|
9
|
+
return (
|
10
|
+
<Switch>
|
11
|
+
<Redirect exact from='/' to="/<%= meta[0].moduleName %>" />
|
12
|
+
<% meta.forEach(function(page) { %>
|
13
|
+
<Route path={'/<%= page.moduleName %>'} component={require('./<%= page.moduleName %>/index').default}/>
|
14
|
+
<% }); %>
|
15
|
+
</Switch>
|
16
|
+
);
|
17
|
+
};
|
18
|
+
|
19
|
+
export default DocsRoutes;
|
@@ -0,0 +1,20 @@
|
|
1
|
+
import React from 'react';
|
2
|
+
import styled from 'styled-components';
|
3
|
+
import Anchor from 'antd/es/anchor';
|
4
|
+
|
5
|
+
const AnchorLink = Anchor.Link;
|
6
|
+
|
7
|
+
const DocsToc = ({ entries }) => {
|
8
|
+
return (
|
9
|
+
<Anchor affix={false} scrollContainer=".markdown-body">
|
10
|
+
{entries.map(entry => (
|
11
|
+
<AnchorLink
|
12
|
+
key={entry.slug}
|
13
|
+
href={`#${entry.slug}`}
|
14
|
+
title={entry.text}
|
15
|
+
/>
|
16
|
+
))}
|
17
|
+
</Anchor>
|
18
|
+
);
|
19
|
+
};
|
20
|
+
export default DocsToc;
|
@@ -0,0 +1,41 @@
|
|
1
|
+
import React from 'react';
|
2
|
+
import { render } from 'react-dom';
|
3
|
+
import styled from 'styled-components';
|
4
|
+
import { BrowserRouter as Router } from 'react-router-dom';
|
5
|
+
import DocsNav from './DocsNav';
|
6
|
+
import DocsRoutes from './DocsRoutes';
|
7
|
+
import meta from './meta.json';
|
8
|
+
import 'antd/dist/antd.css';
|
9
|
+
import './docs.css';
|
10
|
+
|
11
|
+
const NavWrapper = styled.div`
|
12
|
+
position: absolute;
|
13
|
+
top: 0;
|
14
|
+
bottom: 0;
|
15
|
+
left: 0;
|
16
|
+
width: 12rem;
|
17
|
+
overflow-y: auto;
|
18
|
+
`;
|
19
|
+
const MainWrapper = styled.div`
|
20
|
+
position: absolute;
|
21
|
+
top: 0;
|
22
|
+
bottom: 0;
|
23
|
+
left: 12rem;
|
24
|
+
right: 0;
|
25
|
+
`;
|
26
|
+
|
27
|
+
const App = () => (
|
28
|
+
<Router basename="<%= basename || '/' %>">
|
29
|
+
<NavWrapper>
|
30
|
+
<DocsNav meta={meta} />
|
31
|
+
</NavWrapper>
|
32
|
+
<MainWrapper>
|
33
|
+
<DocsRoutes />
|
34
|
+
</MainWrapper>
|
35
|
+
</Router>
|
36
|
+
);
|
37
|
+
|
38
|
+
const root = document.createElement('div');
|
39
|
+
root.setAttribute('id', 'root');
|
40
|
+
document.body.append(root);
|
41
|
+
render(<App />, document.querySelector('#root'));
|
@@ -0,0 +1,107 @@
|
|
1
|
+
import React from 'react';
|
2
|
+
import { MDXProvider, mdx } from '@mdx-js/react';
|
3
|
+
import Highlight, { defaultProps } from 'prism-react-renderer';
|
4
|
+
import { LiveProvider, LiveEditor, LiveError, LivePreview } from 'react-live';
|
5
|
+
import styled from 'styled-components';
|
6
|
+
import DocsNav from './<%= relRoot %>/DocsNav';
|
7
|
+
import DocsToc from './<%= relRoot %>/DocsToc';
|
8
|
+
import Main from './mdx';
|
9
|
+
<%= imports %>
|
10
|
+
|
11
|
+
const CodeBlock = ({ children, className, live, render }) => {
|
12
|
+
const language = className.replace(/language-/, '');
|
13
|
+
if (live) {
|
14
|
+
return (
|
15
|
+
<div style={{ marginTop: '40px' }}>
|
16
|
+
<LiveProvider
|
17
|
+
code={children.trim()}
|
18
|
+
transformCode={code => `/** @jsx mdx */${code}`}
|
19
|
+
scope={{
|
20
|
+
mdx,
|
21
|
+
<%= imported %>
|
22
|
+
}}>
|
23
|
+
<LivePreview style={{
|
24
|
+
boxShadow: '0 0 10px 5px gray',
|
25
|
+
margin: '1rem 0',
|
26
|
+
padding: '1rem',
|
27
|
+
}}/>
|
28
|
+
<LiveError />
|
29
|
+
<LiveEditor style={{ backgroundColor: 'black', color: 'white' }} />
|
30
|
+
</LiveProvider>
|
31
|
+
</div>
|
32
|
+
);
|
33
|
+
}
|
34
|
+
if (render) {
|
35
|
+
return (
|
36
|
+
<div style={{ marginTop: '40px' }}>
|
37
|
+
<LiveProvider code={children.trim()}
|
38
|
+
transformCode={code => `/** @jsx mdx */${code}`}
|
39
|
+
scope={{
|
40
|
+
mdx,
|
41
|
+
<%= imported %>
|
42
|
+
}}>
|
43
|
+
<LivePreview style={{
|
44
|
+
boxShadow: '0 0 10px 5px gray',
|
45
|
+
margin: '1rem 0',
|
46
|
+
padding: '1rem',
|
47
|
+
}}/>
|
48
|
+
</LiveProvider>
|
49
|
+
</div>
|
50
|
+
);
|
51
|
+
}
|
52
|
+
|
53
|
+
return (
|
54
|
+
<Highlight {...defaultProps} code={children.trim()} language={language}>
|
55
|
+
{({ className, style, tokens, getLineProps, getTokenProps }) => (
|
56
|
+
<pre className={className} style={{ ...style, padding: '20px' }}>
|
57
|
+
{tokens.map((line, i) => (
|
58
|
+
<div key={i} {...getLineProps({ line, key: i })}>
|
59
|
+
{line.map((token, key) => (
|
60
|
+
<span key={key} {...getTokenProps({ token, key })} />
|
61
|
+
))}
|
62
|
+
</div>
|
63
|
+
))}
|
64
|
+
</pre>
|
65
|
+
)}
|
66
|
+
</Highlight>
|
67
|
+
);
|
68
|
+
};
|
69
|
+
|
70
|
+
const components = {
|
71
|
+
pre: props => <div {...props} />,
|
72
|
+
code: CodeBlock,
|
73
|
+
<%= imported %>
|
74
|
+
};
|
75
|
+
|
76
|
+
const Wrapper = styled.div`
|
77
|
+
height: 100%;
|
78
|
+
display: flex;
|
79
|
+
flex-direction: row;
|
80
|
+
`;
|
81
|
+
const MainWrapper = styled.div`
|
82
|
+
flex: 1;
|
83
|
+
overflow-y: scroll;
|
84
|
+
padding-right: 230px !important;
|
85
|
+
`;
|
86
|
+
const TocWrapper = styled.div`
|
87
|
+
position: fixed;
|
88
|
+
top: 2rem;
|
89
|
+
right: 1.5rem;
|
90
|
+
`;
|
91
|
+
|
92
|
+
const Page = () => {
|
93
|
+
return (
|
94
|
+
<Wrapper>
|
95
|
+
<MainWrapper className="markdown-body">
|
96
|
+
<MDXProvider components={components}>
|
97
|
+
<Main />
|
98
|
+
</MDXProvider>
|
99
|
+
</MainWrapper>
|
100
|
+
<TocWrapper>
|
101
|
+
<DocsToc entries={JSON.parse('<%= toc %>')}/>
|
102
|
+
</TocWrapper>
|
103
|
+
</Wrapper>
|
104
|
+
);
|
105
|
+
};
|
106
|
+
|
107
|
+
export default Page;
|