Skip to content

Commit c354bce

Browse files
committed
WIP adding tests for SSR
1 parent d516198 commit c354bce

File tree

21 files changed

+15320
-7291
lines changed

21 files changed

+15320
-7291
lines changed

build/build.js

Lines changed: 92 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1-
const rollup = require('rollup')
2-
const buble = require('rollup-plugin-buble')
3-
const commonjs = require('rollup-plugin-commonjs')
4-
const nodeResolve = require('rollup-plugin-node-resolve')
5-
const { uglify } = require('rollup-plugin-uglify')
6-
const replace = require('rollup-plugin-replace')
7-
const isProd = process.env.NODE_ENV === 'production'
8-
const version = process.env.VERSION || require('../package.json').version
9-
const chokidar = require('chokidar')
10-
const path = require('path')
1+
// @ts-check
2+
3+
import rollup from 'rollup';
4+
import buble from 'rollup-plugin-buble';
5+
import commonjs from 'rollup-plugin-commonjs';
6+
import nodeResolve from 'rollup-plugin-node-resolve';
7+
import replace from 'rollup-plugin-replace';
8+
import chokidar from 'chokidar';
9+
import path from 'path';
10+
import fs from 'fs';
11+
import _uglify from 'rollup-plugin-uglify';
12+
13+
const { uglify } = _uglify;
14+
const isProd = process.env.NODE_ENV === 'production';
15+
const dirname = path.dirname(import.meta.url.replace('file://', ''));
16+
const version =
17+
process.env.VERSION ||
18+
JSON.parse(fs.readFileSync(path.resolve(dirname, '..', 'package.json')).toString())
19+
.version;
1120

1221
/**
1322
* @param {{
@@ -24,89 +33,96 @@ async function build(opts) {
2433
plugins: (opts.plugins || []).concat([
2534
buble({
2635
transforms: {
27-
dangerousForOf: true
28-
}}),
36+
dangerousForOf: true,
37+
},
38+
}),
2939
commonjs(),
3040
nodeResolve(),
3141
replace({
3242
__VERSION__: version,
33-
'process.env.SSR': false
34-
})
43+
'process.env.SSR': false,
44+
}),
3545
]),
36-
onwarn: function (message) {
46+
onwarn: function(message) {
3747
if (message.code === 'UNRESOLVED_IMPORT') {
3848
throw new Error(
3949
`Could not resolve module ` +
40-
message.source +
41-
`. Try running 'npm install' or using rollup's 'external' option if this is an external dependency. ` +
42-
`Module ${message.source} is imported in ${message.importer}`
43-
)
50+
message.source +
51+
`. Try running 'npm install' or using rollup's 'external' option if this is an external dependency. ` +
52+
`Module ${message.source} is imported in ${message.importer}`
53+
);
4454
}
45-
}
55+
},
4656
})
47-
.then(function (bundle) {
48-
var dest = 'lib/' + (opts.output || opts.input)
57+
.then(function(bundle) {
58+
var dest = 'lib/' + (opts.output || opts.input);
4959

50-
console.log(dest)
60+
console.log(dest);
5161
return bundle.write({
5262
format: 'iife',
53-
output: opts.globalName ? {name: opts.globalName} : {},
63+
output: opts.globalName ? { name: opts.globalName } : {},
5464
file: dest,
55-
strict: false
56-
})
57-
})
65+
strict: false,
66+
});
67+
});
5868
}
5969

6070
async function buildCore() {
61-
const promises = []
71+
const promises = [];
6272

63-
promises.push(build({
64-
input: 'src/core/index.js',
65-
output: 'docsify.js',
66-
}))
73+
promises.push(
74+
build({
75+
input: 'src/core/index.js',
76+
output: 'docsify.js',
77+
})
78+
);
6779

6880
if (isProd) {
69-
promises.push(build({
70-
input: 'src/core/index.js',
71-
output: 'docsify.min.js',
72-
plugins: [uglify()]
73-
}))
81+
promises.push(
82+
build({
83+
input: 'src/core/index.js',
84+
output: 'docsify.min.js',
85+
plugins: [uglify()],
86+
})
87+
);
7488
}
7589

76-
await Promise.all(promises)
90+
await Promise.all(promises);
7791
}
7892

7993
async function buildAllPlugin() {
8094
var plugins = [
81-
{name: 'search', input: 'search/index.js'},
82-
{name: 'ga', input: 'ga.js'},
83-
{name: 'matomo', input: 'matomo.js'},
84-
{name: 'emoji', input: 'emoji.js'},
85-
{name: 'external-script', input: 'external-script.js'},
86-
{name: 'front-matter', input: 'front-matter/index.js'},
87-
{name: 'zoom-image', input: 'zoom-image.js'},
88-
{name: 'disqus', input: 'disqus.js'},
89-
{name: 'gitalk', input: 'gitalk.js'}
90-
]
95+
{ name: 'search', input: 'search/index.js' },
96+
{ name: 'ga', input: 'ga.js' },
97+
{ name: 'matomo', input: 'matomo.js' },
98+
{ name: 'emoji', input: 'emoji.js' },
99+
{ name: 'external-script', input: 'external-script.js' },
100+
{ name: 'front-matter', input: 'front-matter/index.js' },
101+
{ name: 'zoom-image', input: 'zoom-image.js' },
102+
{ name: 'disqus', input: 'disqus.js' },
103+
{ name: 'gitalk', input: 'gitalk.js' },
104+
];
91105

92106
const promises = plugins.map(item => {
93107
return build({
94108
input: 'src/plugins/' + item.input,
95-
output: 'plugins/' + item.name + '.js'
96-
})
97-
})
109+
output: 'plugins/' + item.name + '.js',
110+
});
111+
});
98112

99113
if (isProd) {
100114
plugins.forEach(item => {
101-
promises.push(build({
102-
input: 'src/plugins/' + item.input,
103-
output: 'plugins/' + item.name + '.min.js',
104-
plugins: [uglify()]
105-
}))
106-
})
115+
promises.push(
116+
build({
117+
input: 'src/plugins/' + item.input,
118+
output: 'plugins/' + item.name + '.min.js',
119+
plugins: [uglify()],
120+
})
121+
);
122+
});
107123
}
108124

109-
await Promise.all(promises)
125+
await Promise.all(promises);
110126
}
111127

112128
async function main() {
@@ -116,41 +132,37 @@ async function main() {
116132
atomic: true,
117133
awaitWriteFinish: {
118134
stabilityThreshold: 1000,
119-
pollInterval: 100
120-
}
135+
pollInterval: 100,
136+
},
121137
})
122138
.on('change', p => {
123-
console.log('[watch] ', p)
124-
const dirs = p.split(path.sep)
139+
console.log('[watch] ', p);
140+
const dirs = p.split(path.sep);
125141
if (dirs[1] === 'core') {
126-
buildCore()
142+
buildCore();
127143
} else if (dirs[2]) {
128-
const name = path.basename(dirs[2], '.js')
144+
const name = path.basename(dirs[2], '.js');
129145
const input = `src/plugins/${name}${
130146
/\.js/.test(dirs[2]) ? '' : '/index'
131-
}.js`
147+
}.js`;
132148

133149
build({
134150
input,
135-
output: 'plugins/' + name + '.js'
136-
})
151+
output: 'plugins/' + name + '.js',
152+
});
137153
}
138154
})
139155
.on('ready', () => {
140-
console.log('[start]')
141-
buildCore()
142-
buildAllPlugin()
143-
})
156+
console.log('[start]');
157+
buildCore();
158+
buildAllPlugin();
159+
});
144160
} else {
145-
await Promise.all([
146-
buildCore(),
147-
buildAllPlugin()
148-
])
161+
await Promise.all([buildCore(), buildAllPlugin()]);
149162
}
150163
}
151164

152-
main().catch((e) => {
153-
console.error(e)
154-
process.exit(1)
155-
})
156-
165+
main().catch(e => {
166+
console.error(e);
167+
process.exit(1);
168+
});

build/cover.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1-
var fs = require('fs')
2-
var read = fs.readFileSync
3-
var write = fs.writeFileSync
4-
var version = process.env.VERSION || require('../package.json').version
1+
// @ts-check
52

6-
var file = __dirname + '/../docs/_coverpage.md'
7-
var cover = read(file, 'utf8').toString()
3+
import fs from 'fs'
4+
import path from 'path'
5+
6+
const read = fs.readFileSync
7+
const write = fs.writeFileSync
8+
const dirname = path.dirname(import.meta.url.replace('file://', ''));
9+
const version =
10+
process.env.VERSION ||
11+
JSON.parse(fs.readFileSync(path.resolve(dirname, '..', 'package.json')).toString())
12+
.version;
13+
14+
const file = dirname + '/../docs/_coverpage.md'
15+
let cover = read(file, 'utf8').toString()
816

917
console.log('Replace version number in cover page...')
1018
cover = cover.replace(

build/css.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
const fs = require('fs')
2-
const path = require('path')
3-
const {spawn} = require('child_process')
1+
import fs from 'fs'
2+
import path from 'path'
3+
import child_process from 'child_process'
4+
5+
const {spawn} = child_process
6+
const dirname = path.dirname(import.meta.url.replace('file://', ''));
47

58
const args = process.argv.slice(2)
6-
fs.readdir(path.join(__dirname, '../src/themes'), (err, files) => {
9+
fs.readdir(path.join(dirname, '../src/themes'), (err, files) => {
710
if (err) {
811
console.error('err', err)
912
process.exit(1)

build/mincss.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
const cssnano = require('cssnano').process
2-
const path = require('path')
3-
const fs = require('fs')
1+
import _cssnano from 'cssnano'
2+
import path from 'path'
3+
import fs from 'fs'
44

5-
files = fs.readdirSync(path.resolve('lib/themes'))
5+
const cssnano = _cssnano.process
6+
7+
const files = fs.readdirSync(path.resolve('lib/themes'))
68

79
files.forEach(file => {
810
file = path.resolve('lib/themes', file)

build/ssr.js

Lines changed: 0 additions & 35 deletions
This file was deleted.

jest.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ module.exports = {
2626
...sharedConfig,
2727
displayName: 'unit',
2828
setupFilesAfterEnv: ['<rootDir>/test/config/jest.setup-tests.js'],
29-
testMatch: ['<rootDir>/test/unit/*.test.js'],
29+
testMatch: [
30+
'<rootDir>/test/unit/**/*.test.js',
31+
'<rootDir>/packages/docsify-server-renderer/src/**/*.test.js',
32+
],
3033
testURL: serverGlobals.BLANK_URL,
3134
},
3235
// Integration Tests (Jest)

0 commit comments

Comments
 (0)