diff --git a/generator/index.js b/generator/index.js index 29acb86..7fd5ba0 100644 --- a/generator/index.js +++ b/generator/index.js @@ -7,7 +7,7 @@ const chalk = require('chalk') module.exports = (api, options, rootOptions) => { if (!api.hasPlugin('router')) { - throw new Error(`Please install router plugin with 'vue add router'.`) + throw new Error('Please install router plugin with \'vue add router\'.') } api.extendPackage({ @@ -43,16 +43,16 @@ module.exports = (api, options, rootOptions) => { const file = getFile(api, './src/main.js') if (file) { let contents = fs.readFileSync(file, { encoding: 'utf8' }) - contents = contents.replace(/import router from ('|")\.\/router(\.\w+)?('|")/, `import { createRouter } from $1./router$3`) - contents = contents.replace(/import store from ('|")\.\/store(\.\w+)?('|")/, `import { createStore } from $1./store$3`) - contents = contents.replace(/import ('|")\.\/registerServiceWorker('|")\n/, ``) - contents = contents.replace(/const apolloProvider = createProvider\(({(.|\s)*?})?\)\n/, ``) + contents = contents.replace(/import router from ('|")\.\/router(\.\w+)?('|")/, 'import { createRouter } from $1./router$3') + contents = contents.replace(/import store from ('|")\.\/store(\.\w+)?('|")/, 'import { createStore } from $1./store$3') + contents = contents.replace(/import ('|")\.\/registerServiceWorker('|")\n/, '') + contents = contents.replace(/const apolloProvider = createProvider\(({(.|\s)*?})?\)\n/, '') contents = contents.replace(/new Vue\({((.|\s)*)}\)\.\$mount\(.*?\)/, `export async function createApp ({ beforeApp = () => {}, afterApp = () => {} } = {}) { const router = createRouter() - ${templateOptions.vuex ? `const store = createStore()` : ''} + ${templateOptions.vuex ? 'const store = createStore()' : ''} ${templateOptions.apollo ? `const apolloProvider = createProvider({ ssr: process.server, })` : ''} @@ -88,7 +88,7 @@ module.exports = (api, options, rootOptions) => { let contents = fs.readFileSync(file, { encoding: 'utf8' }) contents = contents.replace(/export default new Router\({((.|\s)+)}\)/, `export function createRouter () { return new Router({ - ${contents.includes('mode:') ? '' : `mode: 'history',`}$1}) + ${contents.includes('mode:') ? '' : 'mode: \'history\','}$1}) }`) contents = contents.replace(/mode:\s*("|')(hash|abstract)("|'),/, '') fs.writeFileSync(file, contents, { encoding: 'utf8' }) @@ -127,7 +127,7 @@ module.exports = (api, options, rootOptions) => { contents = contents.replace(/export default app => {((.|\s)*)}/, `export default app => {$1 ssrMiddleware(app, { prodOnly: true }) }`) - contents = `import ssrMiddleware from '@akryum/vue-cli-plugin-ssr/lib/app'\n` + contents + contents = 'import ssrMiddleware from \'@akryum/vue-cli-plugin-ssr/lib/app\'\n' + contents fs.writeFileSync(file, contents, { encoding: 'utf8' }) } diff --git a/index.js b/index.js index 0a6d295..e36400e 100644 --- a/index.js +++ b/index.js @@ -88,7 +88,7 @@ module.exports = (api, options) => { config.port = port config.host = host - return await createServer({ + return createServer({ port, host, }) diff --git a/lib/app.js b/lib/app.js index 939bfde..d19e816 100644 --- a/lib/app.js +++ b/lib/app.js @@ -19,6 +19,7 @@ module.exports = (app, options) => { if (options.prodOnly && !isProd) return const templatePath = config.templatePath + const { publicPath } = config.service.projectOptions try { // Vue bundle renderer @@ -81,7 +82,7 @@ module.exports = (app, options) => { }) } - if(config.applyDefaultServer){ + if (config.applyDefaultServer) { // Serve static files const serve = (filePath, cache) => express.static(filePath, { maxAge: cache && isProd ? config.staticCacheTtl : 0, @@ -95,7 +96,6 @@ module.exports = (app, options) => { app.use('/service-worker.js', serve(config.serviceWorkerPath)) } const serveStaticFiles = serve(config.distPath, true) - const { publicPath } = config.service.projectOptions app.use(publicPath, (req, res, next) => { if (/index\.html/g.test(req.path)) { next() @@ -153,7 +153,7 @@ module.exports = (app, options) => { res.status(context.httpCode) } - if(config.onRender){ + if (config.onRender) { config.onRender(res, context) } diff --git a/lib/dev-server.js b/lib/dev-server.js index 6afc0ba..80dfe0e 100644 --- a/lib/dev-server.js +++ b/lib/dev-server.js @@ -72,7 +72,7 @@ module.exports.setupDevServer = ({ server, templatePath, onUpdate }) => new Prom if (stats.hasErrors()) return // Fix dev server render error when a custom path is used for the server-renderer bundles - const ssrPlugin = (clientConfig.plugins.find((p) => (p.__pluginName || p.constructor.name) === 'ssr') || {}).options || {} + const ssrPlugin = (clientConfig.plugins.find((p) => (p.__pluginName || p.constructor.name) === 'ssr') || {}).options || {} clientManifest = JSON.parse(readFile( devMiddleware.fileSystem, @@ -116,7 +116,7 @@ module.exports.setupDevServer = ({ server, templatePath, onUpdate }) => new Prom // Try to find ssr plugin config options // to fix dev server render error when a custom path is used for the server-renderer bundles - const ssrPlugin = (serverConfig.plugins.find((p) => (p.__pluginName || p.constructor.name) === 'ssr') || {}).options || {} + const ssrPlugin = (serverConfig.plugins.find((p) => (p.__pluginName || p.constructor.name) === 'ssr') || {}).options || {} // read bundle generated by vue-ssr-webpack-plugin serverBundle = JSON.parse(readFile( @@ -131,7 +131,7 @@ module.exports.setupDevServer = ({ server, templatePath, onUpdate }) => new Prom function onCompilationCompleted () { if (firstRun) { firstRun = false - if(config.copyUrlOnStart) { + if (config.copyUrlOnStart) { require('clipboardy').write(url) copied = chalk.dim('(copied to clipboard)') } @@ -139,7 +139,7 @@ module.exports.setupDevServer = ({ server, templatePath, onUpdate }) => new Prom setTimeout(() => { console.log() - console.log(` App running at:`) + console.log(' App running at:') console.log(` - Local: ${chalk.cyan(url)} ${copied}`) }) } diff --git a/lib/loaders/css-context.js b/lib/loaders/css-context.js index 6abaed6..d48eae2 100644 --- a/lib/loaders/css-context.js +++ b/lib/loaders/css-context.js @@ -27,23 +27,23 @@ module.exports.pitch = function (remainingRequest) { if (isServer && isProduction) { const shared = [ - `// load the styles`, + '// load the styles', `var content = require(${request});`, // content list format is [id, css, media, sourceMap] - `if(typeof content === 'string') content = [[module.id, content, '']];`, - `module.exports = content;`, - `if(content.locals) module.exports = content.locals;`, + 'if(typeof content === \'string\') content = [[module.id, content, \'\']];', + 'module.exports = content;', + 'if(content.locals) module.exports = content.locals;', ] // on the server: attach to Vue SSR context if (isVue) { // inside *.vue file: expose a function so it can be called in // component's lifecycle hooks return shared.concat([ - `// add CSS to SSR context`, + '// add CSS to SSR context', `var add = require(${addStylesServerPath}).default`, - `module.exports.__inject__ = function (context) {`, + 'module.exports.__inject__ = function (context) {', ` add(${id}, content, ${isProduction}, context)`, - `};`, + '};', ]).join('\n') } else { // normal import diff --git a/lib/server.js b/lib/server.js index 6a6310c..59e3d23 100644 --- a/lib/server.js +++ b/lib/server.js @@ -7,12 +7,13 @@ exports.createServer = ({ port, host, }) => { + // eslint-disable-next-line no-async-promise-executor return new Promise(async (resolve, reject) => { const clustered = process.env.NODE_ENV === 'production' && config.clustered // If the clustering mode is enabled and we are in the master process, create one worker per CPU core if (cluster.isMaster && clustered) { - let cpus = require('os').cpus().length + const cpus = require('os').cpus().length console.log(`[${new Date().toTimeString().split(' ')[0]}] Setting up clusters for ${cpus} cores`) for (let i = 0; i < cpus; i += 1) { cluster.fork() diff --git a/lib/webpack.js b/lib/webpack.js index 4aa4266..bd268a5 100644 --- a/lib/webpack.js +++ b/lib/webpack.js @@ -53,7 +53,7 @@ exports.chainWebpack = (webpackConfig) => { webpackConfig.entry('app').clear().add(config.entry(target)) webpackConfig.plugin('define').tap(args => { - return [Object.assign(args[0], {'process.client': target === 'client', 'process.server': target === 'server'})] + return [Object.assign(args[0], { 'process.client': target === 'client', 'process.server': target === 'server' })] }) webpackConfig.stats(isProd ? 'normal' : 'none') @@ -61,7 +61,7 @@ exports.chainWebpack = (webpackConfig) => { if (isClient) { webpackConfig.plugin('ssr').use(VueSSRClientPlugin) - webpackConfig.plugin('loader').use(WebpackBar, [{name: 'Client', color: 'green'}]) + webpackConfig.plugin('loader').use(WebpackBar, [{ name: 'Client', color: 'green' }]) webpackConfig.devtool(!isProd ? '#cheap-module-source-map' : undefined) @@ -71,10 +71,10 @@ exports.chainWebpack = (webpackConfig) => { }) } else { webpackConfig.plugin('ssr').use(VueSSRServerPlugin) - webpackConfig.plugin('loader').use(WebpackBar, [{name: 'Server', color: 'orange'}]) + webpackConfig.plugin('loader').use(WebpackBar, [{ name: 'Server', color: 'orange' }]) webpackConfig.devtool('source-map') - webpackConfig.externals(nodeExternals({whitelist: config.nodeExternalsWhitelist})) + webpackConfig.externals(nodeExternals({ whitelist: config.nodeExternalsWhitelist })) webpackConfig.output.libraryTarget('commonjs2'); webpackConfig.target('node') webpackConfig.optimization.splitChunks(false).minimize(false) diff --git a/ui.js b/ui.js index 7846154..10dc886 100644 --- a/ui.js +++ b/ui.js @@ -15,21 +15,21 @@ module.exports = api => { api.describeTask({ match: /vue-cli-service ssr:serve --mode production/, - description: `Starts the included HTTP server for SSR in production`, + description: 'Starts the included HTTP server for SSR in production', link: 'https://github.com/Akryum/vue-cli-plugin-ssr#usage', ...taskCommon, }) api.describeTask({ match: /vue-cli-service ssr:serve/, - description: `Compiles and hot-reloads for development with SSR`, + description: 'Compiles and hot-reloads for development with SSR', link: 'https://github.com/Akryum/vue-cli-plugin-ssr#usage', ...taskCommon, }) api.describeTask({ match: /vue-cli-service ssr:build/, - description: `Compiles and minifies for production with SSR`, + description: 'Compiles and minifies for production with SSR', link: 'https://github.com/Akryum/vue-cli-plugin-ssr#usage', }) }