-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
43 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
/** | ||
* @file loader | ||
* @desc Insert route of skeleton into router.js, so that developer can | ||
* visit route path in dev mode to debug skeleton components | ||
* @author panyuqi <[email protected]> | ||
*/ | ||
|
||
|
@@ -8,34 +10,47 @@ | |
const loaderUtils = require('loader-utils'); | ||
const insertAt = require('./util').insertAt; | ||
|
||
const DEFAULT_LOADER_OPTIONS = { | ||
// template of importing skeleton component | ||
importTemplate: 'import [nameCap] from \'@/pages/[nameCap].vue\';', | ||
// template of route path | ||
routePathTemplate: '/skeleton-[name]', | ||
// position to insert route object in router.js file | ||
insertAfter: 'routes: [' | ||
}; | ||
|
||
const ENTRY_NAME_HOLDER = /\[name\]/gi; | ||
const ENTRY_NAME_CAP_HOLDER = /\[nameCap\]/gi; | ||
|
||
module.exports = function (source) { | ||
const options = loaderUtils.getOptions(this); | ||
const options = Object.assign({}, DEFAULT_LOADER_OPTIONS, loaderUtils.getOptions(this)); | ||
let {entry, importTemplate, routePathTemplate, insertAfter} = options; | ||
|
||
// position to insert in router.js | ||
// find position to insert in router.js | ||
let routesPos = source.indexOf(insertAfter) + insertAfter.length; | ||
|
||
if (!Array.isArray(entry)) { | ||
entry = [entry]; | ||
} | ||
entry = Array.isArray(entry) ? entry : [entry]; | ||
|
||
entry.forEach(entryName => { | ||
// capitalize first letter | ||
// capitalize first letter in entryName eg.skeleton -> Skeleton | ||
let entryCap = entryName.replace(/([a-z])(.*)/, (w, firstLetter, rest) => firstLetter.toUpperCase() + rest); | ||
// route path | ||
let skeletonRoutePath = routePathTemplate.replace(ENTRY_NAME_HOLDER, entryName) | ||
.replace(ENTRY_NAME_CAP_HOLDER, entryCap); | ||
let importExpression = importTemplate.replace(ENTRY_NAME_HOLDER, entryName) | ||
.replace(ENTRY_NAME_CAP_HOLDER, entryCap); | ||
|
||
// replace placeholder in routeTpl and importTpl | ||
let [skeletonRoutePath, importExpression] = [routePathTemplate, importTemplate] | ||
.map(pathStr => pathStr.replace(ENTRY_NAME_HOLDER, entryName) | ||
.replace(ENTRY_NAME_CAP_HOLDER, entryCap)); | ||
|
||
// route object to insert | ||
let routeExpression = `{ | ||
path: '${skeletonRoutePath}', | ||
name: '${entryName}-skeleton', | ||
component: ${entryCap} | ||
},`; | ||
|
||
// insert route object into routes array | ||
source = insertAt(source, routeExpression, routesPos); | ||
|
||
// insert import sentence in the head | ||
source += importExpression; | ||
}); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
/** | ||
* @file ssr | ||
* @desc Use vue ssr to render skeleton components. The result contains html and css. | ||
* @author panyuqi <[email protected]> | ||
*/ | ||
|
||
|
@@ -20,7 +21,7 @@ module.exports = serverWebpackConfig => new Promise((resolve, reject) => { | |
|
||
console.log(`Generate skeleton for ${outputBasename}...`); | ||
|
||
// extract css | ||
// extract css into a single file | ||
serverWebpackConfig.plugins.push(new ExtractTextPlugin({ | ||
filename: outputCssBasename | ||
})); | ||
|
@@ -47,8 +48,9 @@ module.exports = serverWebpackConfig => new Promise((resolve, reject) => { | |
|
||
let bundle = mfs.readFileSync(outputPath, 'utf-8'); | ||
let skeletonCss = mfs.readFileSync(outputCssPath, 'utf-8'); | ||
// create renderer with bundle | ||
let renderer = createBundleRenderer(bundle); | ||
// ssr skeleton | ||
// use vue ssr to render skeleton | ||
renderer.renderToString({}, (err, skeletonHtml) => { | ||
if (err) { | ||
reject(err); | ||
|