@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,197 @@
|
|
1
|
+
import _difference from "lodash/difference";
|
2
|
+
import _template from "lodash/template";
|
3
|
+
import _union from "lodash/union";
|
4
|
+
import _camelCase from "lodash/camelCase";
|
5
|
+
import _startCase from "lodash/startCase";
|
6
|
+
|
7
|
+
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; }
|
8
|
+
|
9
|
+
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; }
|
10
|
+
|
11
|
+
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; }
|
12
|
+
|
13
|
+
import path from 'path';
|
14
|
+
import { logger, fs } from '@modern-js/utils';
|
15
|
+
import matter from 'gray-matter';
|
16
|
+
import GithubSlugger from 'github-slugger';
|
17
|
+
import sortPaths from 'sort-paths';
|
18
|
+
import visit from 'unist-util-visit';
|
19
|
+
import toString from 'mdast-util-to-string';
|
20
|
+
import u from 'unist-builder';
|
21
|
+
import mdx from '@mdx-js/mdx';
|
22
|
+
import babelParser from '@modern-js/deps/compiled/babel/parser';
|
23
|
+
import { DOCS_RENDER_PATH, MDX_DEFAULT_RENDERER, UTILS_STATIC } from "../constant";
|
24
|
+
|
25
|
+
const loadTemplate = async (file, customPath = false) => {
|
26
|
+
let result = '';
|
27
|
+
|
28
|
+
if (customPath) {
|
29
|
+
result = await fs.readFile(file, 'utf-8');
|
30
|
+
} else {
|
31
|
+
result = await fs.readFile(path.join(UTILS_STATIC, file), 'utf-8');
|
32
|
+
}
|
33
|
+
|
34
|
+
return _template(result, {
|
35
|
+
interpolate: /<%=([\s\S]+?)%>/g
|
36
|
+
});
|
37
|
+
};
|
38
|
+
|
39
|
+
// eslint-disable-next-line max-statements
|
40
|
+
async function handleFile(appDirectory, tmpDir, file, images) {
|
41
|
+
const moduleName = file.replace(/\.(md|mdx)$/, '');
|
42
|
+
const content = await fs.readFile(path.resolve(appDirectory, 'docs', file));
|
43
|
+
const parsed = matter(content);
|
44
|
+
|
45
|
+
if (!parsed.data.title) {
|
46
|
+
parsed.data.title = _startCase(_camelCase(path.basename(moduleName)));
|
47
|
+
}
|
48
|
+
|
49
|
+
const slugger = new GithubSlugger();
|
50
|
+
slugger.reset();
|
51
|
+
const babelOptions = {
|
52
|
+
allowUndeclaredExports: true,
|
53
|
+
sourceType: 'module'
|
54
|
+
};
|
55
|
+
const imported = [];
|
56
|
+
const exported = [];
|
57
|
+
const toc = [];
|
58
|
+
|
59
|
+
const modifier = () => tree => {
|
60
|
+
visit(tree, 'import', node => {
|
61
|
+
const parsedNode = babelParser.parse(node.value, babelOptions);
|
62
|
+
const nodes = parsedNode.program.body;
|
63
|
+
nodes.forEach(n => {
|
64
|
+
if (n.type === 'ImportDeclaration') {
|
65
|
+
n.specifiers.forEach(sp => {
|
66
|
+
imported.push({
|
67
|
+
id: sp.local.name,
|
68
|
+
node
|
69
|
+
});
|
70
|
+
});
|
71
|
+
}
|
72
|
+
});
|
73
|
+
});
|
74
|
+
visit(tree, 'export', node => {
|
75
|
+
const parsedNode = babelParser.parse(node.value, babelOptions);
|
76
|
+
const nodes = parsedNode.program.body;
|
77
|
+
nodes.forEach(n => {
|
78
|
+
if (n.type === 'ExportNamedDeclaration') {
|
79
|
+
n.specifiers.forEach(sp => {
|
80
|
+
exported.push({
|
81
|
+
id: sp.exported.name,
|
82
|
+
node
|
83
|
+
});
|
84
|
+
});
|
85
|
+
}
|
86
|
+
});
|
87
|
+
});
|
88
|
+
|
89
|
+
const extraImported = _difference(imported.map(n => n.id), exported.map(n => n.id)); // we have to put the export statement with the import,
|
90
|
+
// otherwise the mdx's babel transformer cannot found
|
91
|
+
// the corresponding imports
|
92
|
+
|
93
|
+
|
94
|
+
extraImported.forEach(id => {
|
95
|
+
const {
|
96
|
+
node
|
97
|
+
} = imported.find(n => n.id === id);
|
98
|
+
node.value += `\nexport { ${id} };`;
|
99
|
+
});
|
100
|
+
visit(tree, 'heading', node => {
|
101
|
+
if (node.depth === 2) {
|
102
|
+
const slug = slugger.slug(toString(node));
|
103
|
+
node.data = {
|
104
|
+
hProperties: {
|
105
|
+
id: slug
|
106
|
+
}
|
107
|
+
};
|
108
|
+
toc.push({
|
109
|
+
slug,
|
110
|
+
text: toString(node)
|
111
|
+
});
|
112
|
+
}
|
113
|
+
});
|
114
|
+
visit(tree, 'root', node => {
|
115
|
+
(node.children || []).unshift(u('heading', {
|
116
|
+
depth: 1,
|
117
|
+
children: [u('text', {
|
118
|
+
value: parsed.data.title
|
119
|
+
})]
|
120
|
+
}));
|
121
|
+
});
|
122
|
+
visit(tree, 'image', node => {
|
123
|
+
const {
|
124
|
+
url
|
125
|
+
} = node;
|
126
|
+
|
127
|
+
if (!url.startsWith('http')) {
|
128
|
+
const fullPath = path.resolve(path.dirname(path.resolve(appDirectory, 'docs', file)), url);
|
129
|
+
const relativePath = path.relative(path.resolve(appDirectory, 'assets'), fullPath);
|
130
|
+
|
131
|
+
if (relativePath.startsWith('..')) {
|
132
|
+
logger.warn(`${url} referenced in ${file} is not under the "assets" folder`);
|
133
|
+
} else {
|
134
|
+
images.push(relativePath);
|
135
|
+
node.url = `/${path.relative(path.dirname(path.resolve(appDirectory, 'docs', file)), path.resolve(appDirectory, 'docs/assets', relativePath))}`;
|
136
|
+
}
|
137
|
+
}
|
138
|
+
});
|
139
|
+
};
|
140
|
+
|
141
|
+
const transpiled = await mdx(parsed.content, {
|
142
|
+
remarkPlugins: [modifier]
|
143
|
+
});
|
144
|
+
|
145
|
+
const totalExported = _union(imported.map(n => n.id), exported.map(n => n.id));
|
146
|
+
|
147
|
+
const outputFile = path.resolve(tmpDir, `${moduleName}/mdx.jsx`);
|
148
|
+
await fs.outputFile(outputFile, MDX_DEFAULT_RENDERER + transpiled, {
|
149
|
+
encoding: 'utf8'
|
150
|
+
});
|
151
|
+
const pageFile = path.resolve(tmpDir, `${moduleName}/index.jsx`);
|
152
|
+
const pageTemplate = await loadTemplate('docs-page.jsx.tpl');
|
153
|
+
const pageContent = pageTemplate({
|
154
|
+
moduleName,
|
155
|
+
toc: JSON.stringify(toc),
|
156
|
+
relRoot: path.relative(path.dirname(outputFile), tmpDir),
|
157
|
+
imports: totalExported.length ? `import { ${totalExported.join(', ')} } from './mdx';` : '',
|
158
|
+
imported: totalExported.join(',')
|
159
|
+
});
|
160
|
+
await fs.outputFile(pageFile, pageContent, {
|
161
|
+
encoding: 'utf8'
|
162
|
+
});
|
163
|
+
return _objectSpread(_objectSpread({}, parsed.data), {}, {
|
164
|
+
moduleName,
|
165
|
+
pageFile
|
166
|
+
});
|
167
|
+
}
|
168
|
+
|
169
|
+
export async function generateFiles(appDirectory, tmpDir, files, isDev) {
|
170
|
+
await fs.remove(tmpDir);
|
171
|
+
await fs.ensureDir(tmpDir);
|
172
|
+
const images = [];
|
173
|
+
const meta = await Promise.all(files.map(file => handleFile(appDirectory, tmpDir, file, images)));
|
174
|
+
const sorted = sortPaths(meta, e => e.moduleName.endsWith('index') ? e.moduleName.replace(/index$/, '') : `${e.moduleName}/`, '/');
|
175
|
+
const routesTemplate = await loadTemplate('DocsRoutes.jsx.tpl');
|
176
|
+
await fs.outputFile(path.resolve(tmpDir, 'DocsRoutes.jsx'), routesTemplate({
|
177
|
+
meta: sorted
|
178
|
+
}));
|
179
|
+
const pkgInfo = await fs.readJson(path.resolve(appDirectory, 'package.json'));
|
180
|
+
const entryTemplate = await loadTemplate('docs-entry.jsx.tpl');
|
181
|
+
await fs.outputFile(path.resolve(tmpDir, 'docs-entry.jsx'), entryTemplate({
|
182
|
+
basename: isDev ? '/' : `${DOCS_RENDER_PATH}/${pkgInfo.name}/${pkgInfo.version}`
|
183
|
+
}));
|
184
|
+
await Promise.all(['DocsNav.jsx.tpl', 'DocsToc.jsx.tpl', 'docs.css'].map(async file => fs.copyFile(path.resolve(UTILS_STATIC, file), path.resolve(tmpDir, file.replace('.tpl', '')))));
|
185
|
+
await fs.outputJson(path.resolve(tmpDir, 'meta.json'), sorted.map(({
|
186
|
+
title,
|
187
|
+
moduleName
|
188
|
+
}) => ({
|
189
|
+
title,
|
190
|
+
moduleName
|
191
|
+
})), {
|
192
|
+
spaces: 2
|
193
|
+
});
|
194
|
+
await fs.ensureDir(path.resolve(appDirectory, 'dist/docs/assets'));
|
195
|
+
await Promise.all(Array.from(new Set(images)).map(file => fs.copyFile(path.resolve(appDirectory, 'assets', file), path.resolve(appDirectory, 'dist/docs/assets', file))));
|
196
|
+
return sorted;
|
197
|
+
}
|
@@ -0,0 +1,17 @@
|
|
1
|
+
import path from 'path';
|
2
|
+
import * as glob from 'glob';
|
3
|
+
import { chalk, fs } from '@modern-js/utils';
|
4
|
+
export const valid = ({
|
5
|
+
appDirectory,
|
6
|
+
docsDir
|
7
|
+
}) => {
|
8
|
+
const docsAbsPath = path.join(appDirectory, docsDir);
|
9
|
+
const files = glob.sync(`${docsAbsPath}/**/*.{md,mdx}`);
|
10
|
+
|
11
|
+
if (!fs.existsSync(docsAbsPath) || files.length <= 0) {
|
12
|
+
console.info(chalk.yellow('No docs found, create directory "./docs" and add md(x) files'));
|
13
|
+
return false;
|
14
|
+
}
|
15
|
+
|
16
|
+
return true;
|
17
|
+
};
|
@@ -0,0 +1,63 @@
|
|
1
|
+
import path from 'path';
|
2
|
+
import { fs } from '@modern-js/utils';
|
3
|
+
import HtmlWebpackPlugin from '@modern-js/deps/compiled/html-webpack-plugin';
|
4
|
+
import webpack from '@modern-js/deps/compiled/webpack';
|
5
|
+
import { getWebpackConfig, WebpackConfigTarget } from '@modern-js/webpack';
|
6
|
+
import { UTILS_STATIC } from "../constant";
|
7
|
+
export function generatorWebpackConfig(appDirectory, tmpDir, isDev) {
|
8
|
+
const originConfig = getWebpackConfig(WebpackConfigTarget.CLIENT);
|
9
|
+
const plugins = (originConfig.plugins || []).filter(p => p.constructor !== webpack.HotModuleReplacementPlugin);
|
10
|
+
const config = {
|
11
|
+
mode: isDev ? 'development' : 'production',
|
12
|
+
context: tmpDir,
|
13
|
+
entry: {
|
14
|
+
index: path.resolve(tmpDir, 'docs-entry.jsx')
|
15
|
+
},
|
16
|
+
output: {
|
17
|
+
path: path.resolve(appDirectory, 'dist/docs')
|
18
|
+
},
|
19
|
+
resolve: originConfig.resolve || {
|
20
|
+
alias: {}
|
21
|
+
},
|
22
|
+
module: originConfig.module,
|
23
|
+
plugins: [...plugins, new HtmlWebpackPlugin({
|
24
|
+
templateContent: fs.readFileSync(path.resolve(UTILS_STATIC, 'index.html.ejs'), 'utf8')
|
25
|
+
})]
|
26
|
+
};
|
27
|
+
config.resolve.modules = [...(config.resolve.modules || []), path.join(__dirname, '../../../../../', 'node_modules')];
|
28
|
+
config.resolve.alias['@assets'] = path.resolve(appDirectory, 'assets');
|
29
|
+
config.resolve.alias['@styles'] = path.resolve(appDirectory, 'styles'); // fix this since react-live relies on core-js@2
|
30
|
+
|
31
|
+
config.resolve.alias[`${path.dirname(require.resolve('core-js'))}/fn`] = 'core-js/es';
|
32
|
+
const pkgJSON = JSON.parse(fs.readFileSync(path.join(appDirectory, 'package.json'), 'utf-8'));
|
33
|
+
|
34
|
+
if (pkgJSON.dependencies.react || pkgJSON.devDependencies.react) {
|
35
|
+
config.resolve.alias.react = path.resolve('node_modules/react');
|
36
|
+
} else {
|
37
|
+
config.resolve.alias.react = path.resolve(__dirname, '../../../../../', 'node_modules', 'react');
|
38
|
+
}
|
39
|
+
|
40
|
+
if (pkgJSON.dependencies['react-dom'] || pkgJSON.devDependencies['react-dom']) {
|
41
|
+
config.resolve.alias['react-dom'] = path.resolve('node_modules/react-dom');
|
42
|
+
} else {
|
43
|
+
config.resolve.alias['react-dom'] = path.resolve(__dirname, '../../../../../', 'node_modules', 'react-dom');
|
44
|
+
}
|
45
|
+
|
46
|
+
config.resolve.fallback = {
|
47
|
+
path: require.resolve('path-browserify')
|
48
|
+
};
|
49
|
+
return config;
|
50
|
+
}
|
51
|
+
export async function runWebpack(config) {
|
52
|
+
await new Promise((resolve, reject) => {
|
53
|
+
webpack(config).run((err, stats) => {
|
54
|
+
if (err) {
|
55
|
+
reject(err);
|
56
|
+
} else if (stats !== null && stats !== void 0 && stats.hasErrors()) {
|
57
|
+
reject(stats === null || stats === void 0 ? void 0 : stats.compilation.errors);
|
58
|
+
} else {
|
59
|
+
resolve(stats);
|
60
|
+
}
|
61
|
+
});
|
62
|
+
});
|
63
|
+
}
|
@@ -0,0 +1,49 @@
|
|
1
|
+
import { Import } from '@modern-js/utils';
|
2
|
+
const core = Import.lazy('@modern-js/core', require);
|
3
|
+
const features = Import.lazy('./features', require);
|
4
|
+
export default core.createPlugin(() => ({
|
5
|
+
commands({
|
6
|
+
program
|
7
|
+
}) {
|
8
|
+
const {
|
9
|
+
appDirectory
|
10
|
+
} = core.useAppContext();
|
11
|
+
const devCommand = program.commandsMap.get('dev');
|
12
|
+
|
13
|
+
if (devCommand) {
|
14
|
+
devCommand.command('docs').action(async () => {
|
15
|
+
await features.buildDocs({
|
16
|
+
appDirectory,
|
17
|
+
isDev: true
|
18
|
+
});
|
19
|
+
});
|
20
|
+
}
|
21
|
+
},
|
22
|
+
|
23
|
+
// module-tools menu mode
|
24
|
+
moduleToolsMenu() {
|
25
|
+
const {
|
26
|
+
appDirectory
|
27
|
+
} = core.useAppContext();
|
28
|
+
return {
|
29
|
+
name: 'Docsite 调试',
|
30
|
+
value: 'docsite',
|
31
|
+
runTask: async () => features.buildDocs({
|
32
|
+
appDirectory,
|
33
|
+
isDev: true
|
34
|
+
})
|
35
|
+
};
|
36
|
+
},
|
37
|
+
|
38
|
+
platformBuild() {
|
39
|
+
return {
|
40
|
+
name: 'docsite',
|
41
|
+
title: 'Run Docsite log',
|
42
|
+
taskPath: require.resolve("./build-task"),
|
43
|
+
params: []
|
44
|
+
};
|
45
|
+
}
|
46
|
+
|
47
|
+
}), {
|
48
|
+
name: '@modern-js/plugin-docsite'
|
49
|
+
});
|
@@ -0,0 +1,33 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
var _utils = require("@modern-js/utils");
|
4
|
+
|
5
|
+
const core = _utils.Import.lazy('@modern-js/core', require);
|
6
|
+
|
7
|
+
const features = _utils.Import.lazy('./features', require);
|
8
|
+
|
9
|
+
const taskMain = async ({
|
10
|
+
appContext
|
11
|
+
}) => {
|
12
|
+
const {
|
13
|
+
appDirectory
|
14
|
+
} = appContext;
|
15
|
+
await features.buildDocs({
|
16
|
+
appDirectory
|
17
|
+
});
|
18
|
+
};
|
19
|
+
|
20
|
+
(async () => {
|
21
|
+
const {
|
22
|
+
appContext
|
23
|
+
} = await core.cli.init();
|
24
|
+
await core.manager.run(async () => {
|
25
|
+
try {
|
26
|
+
await taskMain({
|
27
|
+
appContext
|
28
|
+
});
|
29
|
+
} catch (e) {
|
30
|
+
console.error(e.message);
|
31
|
+
}
|
32
|
+
});
|
33
|
+
})();
|
@@ -0,0 +1,31 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.build = build;
|
7
|
+
|
8
|
+
var _utils = require("@modern-js/utils");
|
9
|
+
|
10
|
+
const wp = _utils.Import.lazy('./utils/webpack', require);
|
11
|
+
|
12
|
+
const gen = _utils.Import.lazy('./utils/generate-files', require); // eslint-disable-next-line max-params
|
13
|
+
|
14
|
+
|
15
|
+
async function build(appDirectory, tmpDir, files, webpackConfig, isDev) {
|
16
|
+
const meta = await gen.generateFiles(appDirectory, tmpDir, files, isDev);
|
17
|
+
|
18
|
+
try {
|
19
|
+
await wp.runWebpack(webpackConfig);
|
20
|
+
|
21
|
+
_utils.logger.log(_utils.chalk.green('build docs successful'));
|
22
|
+
|
23
|
+
return meta;
|
24
|
+
} catch (err) {
|
25
|
+
_utils.logger.error('failed to build docs');
|
26
|
+
|
27
|
+
_utils.logger.error(err);
|
28
|
+
|
29
|
+
return null;
|
30
|
+
}
|
31
|
+
}
|
@@ -0,0 +1,21 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.UTILS_STATIC = exports.MDX_DEFAULT_RENDERER = exports.DOCS_RENDER_PATH = void 0;
|
7
|
+
|
8
|
+
var _path = _interopRequireDefault(require("path"));
|
9
|
+
|
10
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
11
|
+
|
12
|
+
const UTILS_STATIC = _path.default.join(__dirname, '../../static');
|
13
|
+
|
14
|
+
exports.UTILS_STATIC = UTILS_STATIC;
|
15
|
+
const DOCS_RENDER_PATH = '/api/v1/docs/render';
|
16
|
+
exports.DOCS_RENDER_PATH = DOCS_RENDER_PATH;
|
17
|
+
const MDX_DEFAULT_RENDERER = `
|
18
|
+
import React from 'react'
|
19
|
+
import { mdx } from '@mdx-js/react'
|
20
|
+
`;
|
21
|
+
exports.MDX_DEFAULT_RENDERER = MDX_DEFAULT_RENDERER;
|
@@ -0,0 +1,39 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.dev = dev;
|
7
|
+
|
8
|
+
var _path = _interopRequireDefault(require("path"));
|
9
|
+
|
10
|
+
var _utils = require("@modern-js/utils");
|
11
|
+
|
12
|
+
var _webpack = _interopRequireDefault(require("@modern-js/deps/compiled/webpack"));
|
13
|
+
|
14
|
+
var _webpackDevServer = _interopRequireDefault(require("webpack-dev-server"));
|
15
|
+
|
16
|
+
var _chokidar = require("./utils/chokidar");
|
17
|
+
|
18
|
+
var _generateFiles = require("./utils/generate-files");
|
19
|
+
|
20
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
21
|
+
|
22
|
+
// eslint-disable-next-line max-params
|
23
|
+
async function dev(appDirectory, tmpDir, files, webpackConfig, isDev, port) {
|
24
|
+
await (0, _generateFiles.generateFiles)(appDirectory, tmpDir, files, isDev);
|
25
|
+
const compiler = (0, _webpack.default)(webpackConfig);
|
26
|
+
const server = new _webpackDevServer.default({
|
27
|
+
host: '0.0.0.0',
|
28
|
+
port,
|
29
|
+
historyApiFallback: true,
|
30
|
+
static: {
|
31
|
+
directory: _path.default.resolve(appDirectory, 'assets'),
|
32
|
+
publicPath: '/assets'
|
33
|
+
}
|
34
|
+
}, compiler);
|
35
|
+
server.startCallback(() => {
|
36
|
+
_utils.logger.info(`Starting server on http://localhost:${port}`);
|
37
|
+
});
|
38
|
+
(0, _chokidar.chokidarFile)(appDirectory, tmpDir, isDev);
|
39
|
+
}
|
@@ -0,0 +1,68 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.buildDocs = buildDocs;
|
7
|
+
|
8
|
+
var _path = _interopRequireDefault(require("path"));
|
9
|
+
|
10
|
+
var _utils = require("@modern-js/utils");
|
11
|
+
|
12
|
+
var _valid = require("./utils/valid");
|
13
|
+
|
14
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
15
|
+
|
16
|
+
const buildFeat = _utils.Import.lazy('./build', require);
|
17
|
+
|
18
|
+
const devFeat = _utils.Import.lazy('./dev', require);
|
19
|
+
|
20
|
+
const wp = _utils.Import.lazy('./utils/webpack', require);
|
21
|
+
|
22
|
+
const glob = _utils.Import.lazy('glob', require);
|
23
|
+
|
24
|
+
const DEFAULT_PORT = 5000;
|
25
|
+
|
26
|
+
async function buildDocs({
|
27
|
+
appDirectory,
|
28
|
+
isDev = false,
|
29
|
+
port = DEFAULT_PORT
|
30
|
+
}) {
|
31
|
+
if (!(0, _valid.valid)({
|
32
|
+
appDirectory,
|
33
|
+
docsDir: 'docs'
|
34
|
+
})) {
|
35
|
+
return;
|
36
|
+
}
|
37
|
+
|
38
|
+
const docsDir = _path.default.resolve(appDirectory, 'docs');
|
39
|
+
|
40
|
+
if (!_utils.fs.pathExistsSync(docsDir)) {
|
41
|
+
return;
|
42
|
+
}
|
43
|
+
|
44
|
+
const files = glob.sync('**/*.{md,mdx}', {
|
45
|
+
cwd: docsDir,
|
46
|
+
ignore: '**/_*'
|
47
|
+
});
|
48
|
+
|
49
|
+
if (files.length === 0) {
|
50
|
+
_utils.logger.warn('not find md(x) files');
|
51
|
+
|
52
|
+
return;
|
53
|
+
}
|
54
|
+
|
55
|
+
const tmpDir = _path.default.join(appDirectory, './node_modules/.modern-js/docs');
|
56
|
+
|
57
|
+
_utils.fs.ensureDirSync(tmpDir);
|
58
|
+
|
59
|
+
const finalWebpackConfig = wp.generatorWebpackConfig(appDirectory, tmpDir, isDev);
|
60
|
+
|
61
|
+
if (!isDev) {
|
62
|
+
_utils.logger.info('build docs');
|
63
|
+
|
64
|
+
await buildFeat.build(appDirectory, tmpDir, files, finalWebpackConfig, false);
|
65
|
+
} else {
|
66
|
+
await devFeat.dev(appDirectory, tmpDir, files, finalWebpackConfig, true, port);
|
67
|
+
}
|
68
|
+
}
|
@@ -0,0 +1,55 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.chokidarFile = chokidarFile;
|
7
|
+
|
8
|
+
var _path = _interopRequireDefault(require("path"));
|
9
|
+
|
10
|
+
var _utils = require("@modern-js/utils");
|
11
|
+
|
12
|
+
var _chokidar = _interopRequireDefault(require("chokidar"));
|
13
|
+
|
14
|
+
var _glob = _interopRequireDefault(require("glob"));
|
15
|
+
|
16
|
+
var _generateFiles = require("./generate-files");
|
17
|
+
|
18
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
19
|
+
|
20
|
+
function chokidarFile(appDirectory, tmpDir, isDev) {
|
21
|
+
let dirty = false;
|
22
|
+
let building = false;
|
23
|
+
|
24
|
+
_chokidar.default.watch('docs/**/*.{md,mdx}', {
|
25
|
+
cwd: appDirectory,
|
26
|
+
ignoreInitial: true
|
27
|
+
}).on('all', async () => {
|
28
|
+
if (building) {
|
29
|
+
dirty = true;
|
30
|
+
} else {
|
31
|
+
building = true;
|
32
|
+
dirty = false;
|
33
|
+
|
34
|
+
_utils.logger.info('changed, collect and rebuild docs');
|
35
|
+
|
36
|
+
const files = _glob.default.sync('**/*.{md,mdx}', {
|
37
|
+
cwd: _path.default.resolve(appDirectory, 'docs'),
|
38
|
+
ignore: '**/_*'
|
39
|
+
});
|
40
|
+
|
41
|
+
if (files.length) {
|
42
|
+
await (0, _generateFiles.generateFiles)(appDirectory, tmpDir, files, isDev);
|
43
|
+
|
44
|
+
_utils.logger.info('built');
|
45
|
+
} // eslint-disable-next-line require-atomic-updates
|
46
|
+
|
47
|
+
|
48
|
+
building = false;
|
49
|
+
|
50
|
+
if (dirty) {
|
51
|
+
await (0, _generateFiles.generateFiles)(appDirectory, tmpDir, files, isDev);
|
52
|
+
}
|
53
|
+
}
|
54
|
+
});
|
55
|
+
}
|