docsify/test/config/server.js
John Hildenbiddle fa6df6d584
fix: Legacy bugs (styles, site plugin error, and dev server error) (#1743)
* Add try/catch w/ error message to plugin calls

* Update lifecycle.js

* Update lifecycle.js

* Fix docsify-plugin-carbon error

* Fix ESLint errors

* Simplify conditional JS loading

* Fix styles in legacy browser w/o CSS var support

* Fix gitignore paths

* Fix BrowserSync IE error

* Fix search field presentation in IE11

- Removed fixed height and allow element to size naturally via font-size and padding
- Remove default "x" rendered on IE input fields

* Revert "Update lifecycle.js"

This reverts commit 2a58be69eb75a87b74d0408765e1d222282ac95b.

* Revert "Update lifecycle.js"

This reverts commit 67c5410b049237887811350f38e3f9bc8a2c78dc.

* Revert "Add try/catch w/ error message to plugin calls"

This reverts commit 631e9248786f87a5b2f7892fc73a53543744d288.

* Fix docsify-plugin-carbon error & ESLint errors

Co-authored-by: 沈唁 <52o@qq52o.cn>
2022-02-05 03:21:50 -06:00

124 lines
3.2 KiB
JavaScript

const browserSync = require('browser-sync').create();
const path = require('path');
const hasStartArg = process.argv.includes('--start');
const serverConfig = {
hostname: '127.0.0.1',
port: hasStartArg ? 3002 : 3001,
};
function startServer(options = {}, cb = Function.prototype) {
const defaults = {
...serverConfig,
middleware: [
{
route: '/_blank.html',
handle: function (req, res, next) {
res.setHeader('Content-Type', 'text/html');
res.end('');
next();
},
},
],
notify: false,
open: false,
rewriteRules: [
// Replace docsify-related CDN URLs with local paths
{
match:
/(https?:)?\/\/cdn\.jsdelivr\.net\/npm\/docsify(@\d?\.?\d?\.?\d)?\/lib\//g,
replace: '/lib/',
},
],
server: {
baseDir: path.resolve(__dirname, '../'),
routes: {
'/docs': path.resolve(__dirname, '../../docs'),
'/docs/changelog.md': './CHANGELOG.md',
'/lib': path.resolve(__dirname, '../../lib'),
'/node_modules': path.resolve(__dirname, '../../node_modules'),
},
},
snippetOptions: {
rule: {
match: /<\/body>/i,
fn: function (snippet, match) {
// Override changelog alias to load local changelog (see routes)
const newSnippet = `
${snippet.replace(/<script[^>]*/, '$& type="text/plain"')}
<script>
(function() {
var aliasConfig = (window && window.$docsify && window.$docsify.alias) || {};
var isIE = /*@cc_on!@*/false || !!document.documentMode;
// Fix /docs site configuration during tests
aliasConfig['.*?/changelog'] = '/changelog.md';
// Enable BrowserSync snippet for non-IE browsers
if (!isIE) {
document.querySelector('#__bs_script__').removeAttribute('type');
}
})();
</script>
${match}
`;
return newSnippet;
},
},
},
ui: false,
};
console.log('\n');
// Set TEST_HOST environment variable
process.env.TEST_HOST = `http://${serverConfig.hostname}:${serverConfig.port}`;
// Start server
browserSync.init(
// Config
{
...defaults,
...options,
},
// Callback
cb
);
}
async function startServerAsync() {
await new Promise((resolve, reject) => {
startServer({}, () => {
console.log('\n');
resolve();
});
});
}
function stopServer() {
browserSync.exit();
}
// Allow starting the test server from the CLI. Useful for viewing test content
// like fixtures (/index.html)) and local docs site (/docs) used for testing.
if (hasStartArg) {
startServer({
open: true,
port: serverConfig.port,
directory: true,
startPath: '/docs',
});
}
// Display friendly message about manually starting a server instance
else if (require.main === module) {
console.info('Use --start argument to manually start server instance');
}
module.exports = {
start: startServer,
startAsync: startServerAsync,
stop: stopServer,
TEST_HOST: `http://${serverConfig.hostname}:${serverConfig.port}`,
};