Skip to content

Commit

Permalink
WIP adding tests for SSR
Browse files Browse the repository at this point in the history
  • Loading branch information
trusktr committed Nov 2, 2020
1 parent d516198 commit c354bce
Show file tree
Hide file tree
Showing 21 changed files with 15,320 additions and 7,291 deletions.
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
35 changes: 0 additions & 35 deletions build/ssr.js

This file was deleted.

5 changes: 4 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ module.exports = {
...sharedConfig,
displayName: 'unit',
setupFilesAfterEnv: ['<rootDir>/test/config/jest.setup-tests.js'],
testMatch: ['<rootDir>/test/unit/*.test.js'],
testMatch: [
'<rootDir>/test/unit/**/*.test.js',
'<rootDir>/packages/docsify-server-renderer/src/**/*.test.js',
],
testURL: serverGlobals.BLANK_URL,
},
// Integration Tests (Jest)
Expand Down
Loading

0 comments on commit c354bce

Please sign in to comment.