Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Finish initial SSR (server side rendering) and add tests. #1227

Draft
wants to merge 20 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
1e3dd13
feat: added html sanitizer for remote rendering
anikethsaha Apr 21, 2020
c21ced4
chore: syncing
anikethsaha Jun 14, 2020
1d46637
Merge branch 'develop' into fix-validating-remote-content
trusktr Jun 17, 2020
0a8deed
feat: Qualify URLs then see if the origins match.
trusktr Apr 26, 2020
ed08428
feat: also update the client isExternal check
trusktr Apr 26, 2020
bbeaafd
fix: fix lint errors
trusktr Apr 26, 2020
8dd273b
fix: automatically fix some lint warnings
trusktr Apr 27, 2020
2bc8253
use the more robust and shared isExternal function
trusktr May 21, 2020
22f1b2b
add an intial test file for testing SSR
trusktr May 21, 2020
d516198
Merge branch 'develop' into fix-validating-remote-content
trusktr Nov 2, 2020
c354bce
WIP adding tests for SSR
trusktr Nov 2, 2020
27b2e32
WIP: update use native Node ESM for server code, update config files …
trusktr Nov 3, 2020
136e666
Merge branch 'develop' into fix-validating-remote-content-2
trusktr May 4, 2021
dcd480e
remove package-lock files to fix CI error https://github.com/npm/npm/…
trusktr May 4, 2021
1bbc846
as we're not using package-lock files, tell Lerna not to use 'npm ci'
trusktr May 4, 2021
a5526c9
improve detection of Markdown files in SSR renderer so it doesn't utt…
trusktr May 4, 2021
4df8883
remove comment
trusktr May 18, 2021
60ccf09
initial SSR working independently without a seconds server
trusktr May 18, 2021
e94b629
no reason to use dom.appendTo
trusktr Sep 26, 2021
62d111f
rename some internal variables to better indicate what is happening
trusktr Sep 26, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .eslintrc.js → .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
const deasync = require('deasync'); // powerful magic

const promiseSync = deasync((promise, cb) => {
promise.then(result => cb(null, result)).catch(error => cb(error));
});

const importSync = name => promiseSync(import(name));

// Look, no await needed here!
const jestConfig = importSync('./jest.config.js').default;
const jestGlobals = {};

for (const key of Object.keys(jestConfig.globals)) {
jestGlobals[key] = 'readonly';
}

module.exports = {
root: true,
parser: 'babel-eslint',
Expand Down Expand Up @@ -39,6 +55,7 @@ module.exports = {
'no-var': ['error'],
'no-void': ['error'],
'no-with': ['error'],
'no-prototype-builtins': 'off',
radix: ['error'],
'spaced-comment': ['error', 'always'],
strict: ['error', 'global'],
Expand All @@ -60,4 +77,16 @@ module.exports = {
$docsify: 'writable',
dom: 'writable',
},

overrides: [
{
files: ['test/**/*.js', '**/*.test.js'],
extends: ['plugin:jest/recommended', 'plugin:jest/style'],
globals: jestGlobals,
},
{
files: ['test/e2e/**/*.test.js'],
extends: ['plugin:jest-playwright/recommended'],
},
],
};
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ __diff_output__
lib/
node_modules
themes/
*lock.json

# exceptions
!.gitkeep
3 changes: 3 additions & 0 deletions .prettierrc.js → .prettierrc.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
module.exports = {
singleQuote: true,
trailingComma: 'es5',
useTabs: false,
tabWidth: 2,
arrowParens: 'avoid',
};
12 changes: 12 additions & 0 deletions babel.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
// presets: [
// [
// '@babel/preset-env',
// {
// targets: {
// node: 'current',
// },
// },
// ],
// ],
};
12 changes: 0 additions & 12 deletions babel.config.js

This file was deleted.

172 changes: 92 additions & 80 deletions build/build.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
const rollup = require('rollup')
const buble = require('rollup-plugin-buble')
const commonjs = require('rollup-plugin-commonjs')
const nodeResolve = require('rollup-plugin-node-resolve')
const { uglify } = require('rollup-plugin-uglify')
const replace = require('rollup-plugin-replace')
const isProd = process.env.NODE_ENV === 'production'
const version = process.env.VERSION || require('../package.json').version
const chokidar = require('chokidar')
const path = require('path')
// @ts-check

import rollup from 'rollup';
import buble from 'rollup-plugin-buble';
import commonjs from 'rollup-plugin-commonjs';
import nodeResolve from 'rollup-plugin-node-resolve';
import replace from 'rollup-plugin-replace';
import chokidar from 'chokidar';
import path from 'path';
import fs from 'fs';
import _uglify from 'rollup-plugin-uglify';

const { uglify } = _uglify;
const isProd = process.env.NODE_ENV === 'production';
const dirname = path.dirname(import.meta.url.replace('file://', ''));
const version =
process.env.VERSION ||
JSON.parse(fs.readFileSync(path.resolve(dirname, '..', 'package.json')).toString())
.version;

/**
* @param {{
Expand All @@ -24,89 +33,96 @@ async function build(opts) {
plugins: (opts.plugins || []).concat([
buble({
transforms: {
dangerousForOf: true
}}),
dangerousForOf: true,
},
}),
commonjs(),
nodeResolve(),
replace({
__VERSION__: version,
'process.env.SSR': false
})
'process.env.SSR': false,
}),
]),
onwarn: function (message) {
onwarn: function(message) {
if (message.code === 'UNRESOLVED_IMPORT') {
throw new Error(
`Could not resolve module ` +
message.source +
`. Try running 'npm install' or using rollup's 'external' option if this is an external dependency. ` +
`Module ${message.source} is imported in ${message.importer}`
)
message.source +
`. Try running 'npm install' or using rollup's 'external' option if this is an external dependency. ` +
`Module ${message.source} is imported in ${message.importer}`
);
}
}
},
})
.then(function (bundle) {
var dest = 'lib/' + (opts.output || opts.input)
.then(function(bundle) {
var dest = 'lib/' + (opts.output || opts.input);

console.log(dest)
console.log(dest);
return bundle.write({
format: 'iife',
output: opts.globalName ? {name: opts.globalName} : {},
output: opts.globalName ? { name: opts.globalName } : {},
file: dest,
strict: false
})
})
strict: false,
});
});
}

async function buildCore() {
const promises = []
const promises = [];

promises.push(build({
input: 'src/core/index.js',
output: 'docsify.js',
}))
promises.push(
build({
input: 'src/core/index.js',
output: 'docsify.js',
})
);

if (isProd) {
promises.push(build({
input: 'src/core/index.js',
output: 'docsify.min.js',
plugins: [uglify()]
}))
promises.push(
build({
input: 'src/core/index.js',
output: 'docsify.min.js',
plugins: [uglify()],
})
);
}

await Promise.all(promises)
await Promise.all(promises);
}

async function buildAllPlugin() {
var plugins = [
{name: 'search', input: 'search/index.js'},
{name: 'ga', input: 'ga.js'},
{name: 'matomo', input: 'matomo.js'},
{name: 'emoji', input: 'emoji.js'},
{name: 'external-script', input: 'external-script.js'},
{name: 'front-matter', input: 'front-matter/index.js'},
{name: 'zoom-image', input: 'zoom-image.js'},
{name: 'disqus', input: 'disqus.js'},
{name: 'gitalk', input: 'gitalk.js'}
]
{ name: 'search', input: 'search/index.js' },
{ name: 'ga', input: 'ga.js' },
{ name: 'matomo', input: 'matomo.js' },
{ name: 'emoji', input: 'emoji.js' },
{ name: 'external-script', input: 'external-script.js' },
{ name: 'front-matter', input: 'front-matter/index.js' },
{ name: 'zoom-image', input: 'zoom-image.js' },
{ name: 'disqus', input: 'disqus.js' },
{ name: 'gitalk', input: 'gitalk.js' },
];

const promises = plugins.map(item => {
return build({
input: 'src/plugins/' + item.input,
output: 'plugins/' + item.name + '.js'
})
})
output: 'plugins/' + item.name + '.js',
});
});

if (isProd) {
plugins.forEach(item => {
promises.push(build({
input: 'src/plugins/' + item.input,
output: 'plugins/' + item.name + '.min.js',
plugins: [uglify()]
}))
})
promises.push(
build({
input: 'src/plugins/' + item.input,
output: 'plugins/' + item.name + '.min.js',
plugins: [uglify()],
})
);
});
}

await Promise.all(promises)
await Promise.all(promises);
}

async function main() {
Expand All @@ -116,41 +132,37 @@ async function main() {
atomic: true,
awaitWriteFinish: {
stabilityThreshold: 1000,
pollInterval: 100
}
pollInterval: 100,
},
})
.on('change', p => {
console.log('[watch] ', p)
const dirs = p.split(path.sep)
console.log('[watch] ', p);
const dirs = p.split(path.sep);
if (dirs[1] === 'core') {
buildCore()
buildCore();
} else if (dirs[2]) {
const name = path.basename(dirs[2], '.js')
const name = path.basename(dirs[2], '.js');
const input = `src/plugins/${name}${
/\.js/.test(dirs[2]) ? '' : '/index'
}.js`
}.js`;

build({
input,
output: 'plugins/' + name + '.js'
})
output: 'plugins/' + name + '.js',
});
}
})
.on('ready', () => {
console.log('[start]')
buildCore()
buildAllPlugin()
})
console.log('[start]');
buildCore();
buildAllPlugin();
});
} else {
await Promise.all([
buildCore(),
buildAllPlugin()
])
await Promise.all([buildCore(), buildAllPlugin()]);
}
}

main().catch((e) => {
console.error(e)
process.exit(1)
})

main().catch(e => {
console.error(e);
process.exit(1);
});
20 changes: 14 additions & 6 deletions build/cover.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
var fs = require('fs')
var read = fs.readFileSync
var write = fs.writeFileSync
var version = process.env.VERSION || require('../package.json').version
// @ts-check

var file = __dirname + '/../docs/_coverpage.md'
var cover = read(file, 'utf8').toString()
import fs from 'fs'
import path from 'path'

const read = fs.readFileSync
const write = fs.writeFileSync
const dirname = path.dirname(import.meta.url.replace('file://', ''));
const version =
process.env.VERSION ||
JSON.parse(fs.readFileSync(path.resolve(dirname, '..', 'package.json')).toString())
.version;

const file = dirname + '/../docs/_coverpage.md'
let cover = read(file, 'utf8').toString()

console.log('Replace version number in cover page...')
cover = cover.replace(
Expand Down
11 changes: 7 additions & 4 deletions build/css.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const fs = require('fs')
const path = require('path')
const {spawn} = require('child_process')
import fs from 'fs'
import path from 'path'
import child_process from 'child_process'

const {spawn} = child_process
const dirname = path.dirname(import.meta.url.replace('file://', ''));

const args = process.argv.slice(2)
fs.readdir(path.join(__dirname, '../src/themes'), (err, files) => {
fs.readdir(path.join(dirname, '../src/themes'), (err, files) => {
if (err) {
console.error('err', err)
process.exit(1)
Expand Down
10 changes: 6 additions & 4 deletions build/mincss.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const cssnano = require('cssnano').process
const path = require('path')
const fs = require('fs')
import _cssnano from 'cssnano'
import path from 'path'
import fs from 'fs'

files = fs.readdirSync(path.resolve('lib/themes'))
const cssnano = _cssnano.process

const files = fs.readdirSync(path.resolve('lib/themes'))

files.forEach(file => {
file = path.resolve('lib/themes', file)
Expand Down
Loading