diff --git a/package.json b/package.json index 6adbbb2f..e1b29856 100644 --- a/package.json +++ b/package.json @@ -5,13 +5,24 @@ "ng": "ng", "start": "ng serve", "build": "ng build", + "build:prod": "ng build --prod", + "build:client-and-server-bundles": "ng build --prod && ng run ngx-linkifyjs-demo:server:production --bundleDependencies all", + "build:client-and-server-bundles:ghpages": "npm run build:prod:ghpages && ng run ngx-linkifyjs-demo:server:production --bundleDependencies all", + "build:docs": "npx compodoc -p projects/ngx-linkifyjs/tsconfig.lib.json --hideGenerator --disableCoverage -d dist/browser/doc/", + "build:prod:ghpages": "ng build --preserve-symlinks --prod --aot --build-optimizer --base-href /ngx-linkifyjs/ --deploy-url /ngx-linkifyjs/", + "build:prerender": "npm run build:client-and-server-bundles && npm run compile:server && npm run generate:prerender", + "build:prerender:ghpages": "npm run build:client-and-server-bundles:ghpages && npm run compile:server && npm run generate:prerender", + "deploy": "ng deploy --base-href=/ngx-linkifyjs/ --no-build", + "generate:prerender": "cd dist && node prerender", "test": "ng test", "lint": "ng lint", "e2e": "ng e2e", "compile:server": "webpack --config webpack.server.config.js --progress --colors", + "serve:prerender": "cd dist/browser && http-server", "serve:ssr": "node dist/server", "build:ssr": "npm run build:client-and-server-bundles && npm run compile:server", - "build:client-and-server-bundles": "ng build --prod && ng run ngx-linkifyjs-demo:server:production" + "release": "release-it", + "test:watch": "jest --watch" }, "private": true, "dependencies": { diff --git a/prerender.ts b/prerender.ts new file mode 100644 index 00000000..ad5c127a --- /dev/null +++ b/prerender.ts @@ -0,0 +1,48 @@ +// Load zone.js for the server. +import 'zone.js/dist/zone-node'; +import 'reflect-metadata'; +import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs'; +import { join } from 'path'; + +import { enableProdMode } from '@angular/core'; +// Import module map for lazy loading +import { provideModuleMap } from '@nguniversal/module-map-ngfactory-loader'; +import { renderModuleFactory } from '@angular/platform-server'; +import { ROUTES } from './static.paths'; + +// (global as any).WebSocket = require('ws'); +// (global as any).XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; + +// Faster server renders w/ Prod mode (dev mode never needed) +enableProdMode(); + +// * NOTE :: leave this as require() since this file is built Dynamically from webpack +const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require('./dist/server/main'); + +const BROWSER_FOLDER = join(process.cwd(), 'browser'); + +// Load the index.html file containing referances to your application bundle. +const index = readFileSync(join(BROWSER_FOLDER, 'index.html'), 'utf8'); + +let previousRender = Promise.resolve(); + +// Iterate each route path +ROUTES.forEach(route => { + const fullPath = join(BROWSER_FOLDER, route); + + // Make sure the directory structure is there + if (!existsSync(fullPath)) { + mkdirSync(fullPath); + } + + // Writes rendered HTML to index.html, replacing the file if it already exists. + previousRender = previousRender + .then(_ => + renderModuleFactory(AppServerModuleNgFactory, { + document: index, + url: route, + extraProviders: [provideModuleMap(LAZY_MODULE_MAP)] + }) + ) + .then(html => writeFileSync(join(fullPath, 'index.html'), html)); +}); diff --git a/webpack.config.js b/webpack.config.js deleted file mode 100644 index 85484e61..00000000 --- a/webpack.config.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = require('./config/webpack.test.js');