├── .babelrc ├── .gitignore ├── .npmignore ├── .travis.yaml ├── LICENSE ├── _config.yml ├── cli ├── cli.js ├── cliFileMethods.js └── cliQuestions.js ├── lib ├── index.js ├── performanceTest │ ├── createCompareHTML.js │ ├── createConfig.js │ └── createPrimeServer.js ├── returnHTML.js └── server.js ├── package-lock.json ├── package.json ├── readme.md └── test └── lib.test.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["@babel/preset-env", "@babel/preset-react"], 3 | "env": { 4 | "production": { 5 | "plugins": ["transform-react-remove-prop-types"] 6 | } 7 | } 8 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .DS_Store 3 | test/testFiles/* -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | src 3 | .babelrc 4 | .gitignore 5 | test/testFiles/* -------------------------------------------------------------------------------- /.travis.yaml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "10" 4 | - "8" 5 | install: npm install 6 | script: 7 | - npm test -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 SS-React 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-cayman -------------------------------------------------------------------------------- /cli/cli.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | /** 4 | * This file is used to execute the CLI for react-prime. It creates a /primessr directory 5 | * in the user's project that contains five files: 6 | * 1. index.js 7 | * 2. server.js 8 | * 3. returnHTML.js 9 | * 4. primeCompare.html, and 10 | * 5. primeServer.js 11 | */ 12 | 13 | const inquirer = require('inquirer'); 14 | const figlet = require('figlet'); 15 | const chalk = require('chalk'); 16 | const fs = require('fs'); 17 | 18 | const cliQuestions = require('./cliQuestions.js'); 19 | const createHTMLScript = require('../lib/returnHTML.js'); 20 | const createIndexScript = require('../lib/index.js'); 21 | const createServerScript = require('../lib/server.js'); 22 | const createCompareScript = require('../lib/performanceTest/createCompareHTML.js'); 23 | const createPrimeServer = require('../lib/performanceTest/createPrimeServer.js'); 24 | const createConfigScript = require('../lib/performanceTest/createConfig.js'); 25 | 26 | // chalk adds color and weight ton cli fonts 27 | console.log((chalk.rgb(46, 255, 0).bgBlack.bold(figlet.textSync('React Prime', { 28 | // chalk is setting the color etc. of large text and background, figlet is making the large text 29 | font: 'Big', 30 | horizontalLayout: 'default', 31 | verticalLayout: 'default', 32 | })))); 33 | 34 | inquirer.prompt(cliQuestions).then((answers) => { 35 | // check is ssr folder exists if not create one 36 | if (!fs.existsSync('./primessr')) { 37 | fs.mkdirSync('./primessr'); 38 | fs.mkdirSync('./primessr/performanceTest'); 39 | fs.mkdirSync('./primessr/performanceTest/reports'); 40 | } 41 | 42 | fs.readFile('package.json', 'utf8', (error, result) => { 43 | if (error) throw error; 44 | 45 | const tempObj = Object.assign({}, JSON.parse(result)); 46 | tempObj.scripts['prime:compare'] = `npm run ${answers.startScript} & npm run prime:server & node ./primessr/performanceTest/primeServer.js`; 47 | tempObj.scripts['prime:server'] = 'NODE_ENV=production node ./primessr/index.js'; 48 | tempObj.scripts['prime:CSRreport'] = `lighthouse --config-path=./primessr/performanceTest/custom-config.js --output html --output-path ./primessr/performanceTest/reports/csr-report.html http://localhost:${answers.port}`; 49 | tempObj.scripts['prime:SSRreport'] = 'lighthouse --config-path=./primessr/performanceTest/custom-config.js --output html --output-path ./primessr/performanceTest/reports/ssr-report.html http://localhost:8080'; 50 | fs.writeFileSync('package.json', JSON.stringify(tempObj, null, 2)); 51 | }); 52 | 53 | fs.writeFileSync('./primessr/performanceTest/primeCompare.html', createCompareScript(answers)); 54 | fs.writeFileSync('./primessr/performanceTest/primeServer.js', createPrimeServer()); 55 | fs.writeFileSync('./primessr/performanceTest/custom-config.js', createConfigScript()); 56 | fs.writeFileSync('./primessr/index.js', createIndexScript()); 57 | fs.writeFileSync('./primessr/server.js', createServerScript(answers)); 58 | fs.writeFileSync('./primessr/returnHTML.js', createHTMLScript(answers)); 59 | 60 | console.log(chalk.blue('\n---------------Completed---------------\n')); 61 | console.log(chalk.blue('Run "npm run prime:compare" to see a comparison of your website')); 62 | console.log(chalk.blue('Or, run "npm run prime:server" to see an SSR version of your app\n\n')); 63 | }); 64 | -------------------------------------------------------------------------------- /cli/cliFileMethods.js: -------------------------------------------------------------------------------- 1 | const fuzzy = require('fuzzy'); 2 | const glob = require('glob'); 3 | 4 | const cliFileMethods = {}; 5 | 6 | const folderDir = glob.sync('**/*/', { ignore: '**/node_modules/**' }).map((ele) => { 7 | const newDir = `${ele.slice(0, -1)}`; 8 | return newDir; 9 | }); 10 | const fileList = glob.sync('**/*.{js,jsx}', { ignore: '**/node_modules/**', nodir: true }); 11 | const htmlList = glob.sync('**/*.html', { ignore: '**/node_modules/**' }); 12 | 13 | cliFileMethods.searchFolders = (answers, input) => { 14 | const userInput = input || ''; 15 | 16 | return new Promise(((resolve) => { 17 | const fuzzyResult = fuzzy.filter(userInput, folderDir); 18 | resolve( 19 | fuzzyResult.map(ele => ele.original), 20 | ); 21 | })); 22 | }; 23 | 24 | cliFileMethods.searchFiles = (answers, input) => { 25 | const userInput = input || ''; 26 | 27 | return new Promise(((resolve) => { 28 | const fuzzyResult = fuzzy.filter(userInput, fileList); 29 | resolve( 30 | fuzzyResult.map(ele => ele.original), 31 | ); 32 | })); 33 | }; 34 | 35 | cliFileMethods.searchHtml = (answers, input) => { 36 | const userInput = input || ''; 37 | 38 | return new Promise(((resolve) => { 39 | const fuzzyResult = fuzzy.filter(userInput, htmlList); 40 | resolve( 41 | fuzzyResult.map(ele => ele.original), 42 | ); 43 | })); 44 | }; 45 | 46 | module.exports = { 47 | cliFileMethods, 48 | folderDir, 49 | fileList, 50 | htmlList, 51 | }; 52 | -------------------------------------------------------------------------------- /cli/cliQuestions.js: -------------------------------------------------------------------------------- 1 | const inquirer = require('inquirer'); 2 | const chalk = require('chalk'); 3 | const chalkAnimation = require('chalk-animation') 4 | 5 | const { 6 | cliFileMethods, folderDir, fileList, htmlList, 7 | } = require('./cliFileMethods'); 8 | 9 | inquirer.registerPrompt('autocomplete', require('inquirer-autocomplete-prompt')); 10 | 11 | const cliQuestions = [ 12 | { 13 | type: 'autocomplete', 14 | name: 'static', 15 | suggestOnly: true, 16 | message: chalk.rgb(46, 255, 0)('Type the directory containing your bundle:'), 17 | source: cliFileMethods.searchFolders, 18 | validate(answer) { 19 | if (!folderDir.includes(answer)) { 20 | return chalk.rgb(255, 0, 0).bold('Invalid entry point'); 21 | } 22 | return true; 23 | }, 24 | }, 25 | { 26 | type: 'input', 27 | name: 'startScript', 28 | message: chalk.rgb(46, 255, 0)('Type in the name of the npm script that starts your server:'), 29 | validate(answer) { 30 | if (answer.length === 0) { 31 | return chalk.rgb(255, 0, 0).bold('Enter a valid name'); 32 | } 33 | return true; 34 | }, 35 | }, 36 | { 37 | type: 'input', 38 | name: 'port', 39 | message: chalk.rgb(46, 255, 0)('Type in the port number of your client side server:'), 40 | validate(answer) { 41 | if (answer.length === 0 || typeof JSON.parse(answer) !== 'number') { 42 | return chalk.rgb(255, 0, 0).bold('Enter a valid number'); 43 | } 44 | return true; 45 | }, 46 | }, 47 | { 48 | type: 'autocomplete', 49 | name: 'component', 50 | suggestOnly: true, 51 | message: chalk.rgb(46, 255, 0)('Type the path of your root component:'), 52 | source: cliFileMethods.searchFiles, 53 | validate(answer) { 54 | if (!fileList.includes(answer)) { 55 | return chalk.rgb(255, 0, 0).bold('Invalid entry point'); 56 | } 57 | return true; 58 | }, 59 | }, 60 | { 61 | type: 'autocomplete', 62 | name: 'rootHtml', 63 | suggestOnly: true, 64 | message: chalk.rgb(46, 255, 0)('Type the path of the /build HTML file containing the root div:'), 65 | source: cliFileMethods.searchHtml, 66 | validate(answer) { 67 | if (!htmlList.includes(answer)) { 68 | return chalk.rgb(255, 0, 0).bold('Invalid entry point'); 69 | } 70 | return true; 71 | }, 72 | }, 73 | ]; 74 | 75 | module.exports = cliQuestions; 76 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Function that returns a string to make an index file for the SSR version of the user's app 3 | */ 4 | 5 | const index = () => { 6 | return `const md5File = require('md5-file'); 7 | const path = require('path'); 8 | 9 | // Ignore CSS styles imported on load 10 | const ignoreStyles = require('ignore-styles'); 11 | const register = ignoreStyles.default; 12 | 13 | // When running locally these will load from a standard import 14 | // When running on the server, we want to load via their hashed version in the build folder 15 | const extensions = ['.gif', '.jpeg', '.jpg', '.png', '.svg']; 16 | 17 | // Override the default style ignorer, also modifying all image requests 18 | register(ignoreStyles.DEFAULT_EXTENSIONS, (mod, filename) => { 19 | if (!extensions.find(f => filename.endsWith(f))) { 20 | // If we find a style 21 | return ignoreStyles.noOp(); 22 | } 23 | const hash = md5File.sync(filename).slice(0, 8); 24 | const bn = path.basename(filename).replace(/(\\.\\w{3})$/, \`.\${hash}$1\`); 25 | 26 | mod.exports = \`/static/media/\${bn}\`; 27 | }); 28 | 29 | // require babel to transpile JSX 30 | // allow imports and code splitting through plugins 31 | require('@babel/register')({ 32 | presets: ['@babel/preset-env', '@babel/preset-react'], 33 | plugins: [ 34 | '@babel/plugin-syntax-dynamic-import', 35 | 'dynamic-import-node', 36 | 'react-loadable/babel' 37 | ] 38 | }); 39 | 40 | // import the server 41 | require('./server');`; 42 | }; 43 | 44 | module.exports = index; 45 | -------------------------------------------------------------------------------- /lib/performanceTest/createCompareHTML.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Function that returns a string to create an HTML comparison file 3 | * @param {Object} input - Object containing user input from CLI (requires the .port property) 4 | */ 5 | 6 | const createCompareHTML = (input) => { 7 | return ` 8 | 9 |
10 | 11 |Client-Side Rendered
113 |Server-Side Rendered
125 |