Skip to content

Commit

Permalink
build: approve builder (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
xtx1130 authored and PengXing committed May 14, 2018
1 parent 87112ea commit 92c18f1
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 39 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"build:tb-lavas-core": "babel packages/tb-lavas-core/core --out-dir packages/tb-lavas-core/dist --copy-files",
"build:lavas-cli": "babel packages/lavas-cli/src --out-dir packages/lavas-cli/dist --copy-files",
"lint": "fecs ./ --rule --type 'vue,js,css'",
"test": "nyc ava -v && rimraf packages/lavas-core-vue/test/temp",
"test": "nyc ava -v && rimraf packages/tb-lavas-core/test/temp",
"nyc:report": "nyc report --reporter=html",
"publish:core": "lerna publish --scope=tb-lavas-core"
},
Expand Down
58 changes: 29 additions & 29 deletions packages/tb-lavas-core/core/builder/base-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import template from 'lodash.template';
import {readFile, pathExistsSync, copySync} from 'fs-extra';
import {readFileSync, pathExistsSync, copySync} from 'fs-extra';
import {join, basename, normalize} from 'path';

import HtmlWebpackPlugin from 'html-webpack-plugin';
Expand Down Expand Up @@ -97,47 +97,47 @@ export default class BaseBuilder {
* @param {string} content content of file
* @return {string} resolvedPath absolute path of file
*/
async writeFileToLavasDir(path, content) {
writeFileToLavasDir(path, content) {
let resolvedPath = this.lavasPath(path);
await this.writeFile(resolvedPath, content);
this.writeFile(resolvedPath, content);
return resolvedPath;
}

/**
* write config used in runtime
*/
async writeRuntimeConfig() {
writeRuntimeConfig() {
let filteredConfig = JsonUtil.deepPick(this.config, RUMTIME_ITEMS);
await this.writeFileToLavasDir(CONFIG_FILE, JsonUtil.stringify(filteredConfig, null, 4));
this.writeFileToLavasDir(CONFIG_FILE, JsonUtil.stringify(filteredConfig, null, 4));
}

async writeMiddleware() {
writeMiddleware() {
const middlewareTemplate = this.templatesPath('middleware.tmpl');
let isEmpty = !(pathExistsSync(join(this.config.globals.rootDir, 'middlewares')));

await this.writeFileToLavasDir(
this.writeFileToLavasDir(
'middleware.js',
template(await readFile(middlewareTemplate, 'utf8'))({
template(readFileSync(middlewareTemplate, 'utf8'))({
isEmpty
})
);
}

async writeStore() {
writeStore() {
const storeTemplate = this.templatesPath('store.tmpl');
let isEmpty = !(pathExistsSync(join(this.config.globals.rootDir, 'store')));

await this.writeFileToLavasDir(
this.writeFileToLavasDir(
STORE_FILE,
template(await readFile(storeTemplate, 'utf8'))({
template(readFileSync(storeTemplate, 'utf8'))({
isEmpty
})
);
}

async writeLavasLink() {
let lavasLinkTemplate = await readFile(this.templatesPath('LavasLink.js.tmpl'), 'utf8');
await this.writeFileToLavasDir('LavasLink.js', template(lavasLinkTemplate)({
writeLavasLink() {
let lavasLinkTemplate = readFileSync(this.templatesPath('LavasLink.js.tmpl'), 'utf8');
this.writeFileToLavasDir('LavasLink.js', template(lavasLinkTemplate)({
entryConfig: JsonUtil.stringify(this.config.entries.map(entry => {
// only select necessary keys
return {
Expand All @@ -156,11 +156,11 @@ export default class BaseBuilder {
* @param {Array} skeleton routes
* @return {string} entryPath
*/
async writeSkeletonEntry(skeletons, entryName) {
writeSkeletonEntry(skeletons, entryName) {
const skeletonEntryTemplate = this.templatesPath('entry-skeleton.tmpl');
return await this.writeFileToLavasDir(
return this.writeFileToLavasDir(
`${entryName}/skeleton.js`,
template(await readFile(skeletonEntryTemplate, 'utf8'))({skeletons})
template(readFileSync(skeletonEntryTemplate, 'utf8'))({skeletons})
);
}

Expand All @@ -181,7 +181,7 @@ export default class BaseBuilder {
* @param {boolean} watcherEnabled enable watcher
* @param {?string} entryName entry name in MPA, undefined in SPA
*/
async addHtmlPlugin(spaConfig, baseUrl = '/', watcherEnabled, isSPA, entryName) {
addHtmlPlugin(spaConfig, baseUrl = '/', watcherEnabled, isSPA, entryName) {
let rootDir = this.config.globals.rootDir;
let htmlFilename;
let templatePath;
Expand All @@ -207,9 +207,9 @@ export default class BaseBuilder {
: {};

// write HTML template used by html-webpack-plugin which doesn't support template STRING
let resolvedTemplatePath = await this.writeFileToLavasDir(
let resolvedTemplatePath = this.writeFileToLavasDir(
tempTemplatePath,
templateUtil.client(await readFile(templatePath, 'utf8'), baseUrl, templateObject)
templateUtil.client(readFileSync(templatePath, 'utf8'), baseUrl, templateObject)
);

// add html webpack plugin
Expand All @@ -231,10 +231,10 @@ export default class BaseBuilder {

// watch template in development mode
if (watcherEnabled) {
this.addWatcher(templatePath, 'change', async () => {
await this.writeFileToLavasDir(
this.addWatcher(templatePath, 'change', () => {
this.writeFileToLavasDir(
tempTemplatePath,
templateUtil.client(await readFile(templatePath, 'utf8'), baseUrl, templateObject)
templateUtil.client(readFileSync(templatePath, 'utf8'), baseUrl, templateObject)
);
});
}
Expand All @@ -246,7 +246,7 @@ export default class BaseBuilder {
* @param {Object} spaConfig spaConfig
* @param {?string} entryName entry name in MPA, undefined in SPA
*/
async addSkeletonPlugin(spaConfig, isSPA) {
addSkeletonPlugin(spaConfig, isSPA) {
let {router, skeleton, entries} = this.config;
// if skeleton provided, we need to create an entry
let skeletonConfig;
Expand Down Expand Up @@ -278,7 +278,7 @@ export default class BaseBuilder {
}];
}
// check if all the componentPaths are existed first
let error = await this.validateSkeletonRoutes(skeleton.routes, spaConfig.resolve.alias);
let error = this.validateSkeletonRoutes(skeleton.routes, spaConfig.resolve.alias);
if (error && error.msg) {
console.error(error.msg);
}
Expand All @@ -305,7 +305,7 @@ export default class BaseBuilder {
});

// in MPA
skeletonEntries[name] = [await this.writeSkeletonEntry(skeletons, name)];
skeletonEntries[name] = [this.writeSkeletonEntry(skeletons, name)];

routes = routes.concat(skeleton.routes);
}
Expand Down Expand Up @@ -339,7 +339,7 @@ export default class BaseBuilder {
* @param {Object} alias alias in webpack
* @return {boolean|Object} error error
*/
async validateSkeletonRoutes(routes, alias) {
validateSkeletonRoutes(routes, alias) {
let currentRoute;
let resolvedPaths = [];
let isComponentPathResolved;
Expand Down Expand Up @@ -417,11 +417,11 @@ export default class BaseBuilder {
];

// 1. add html-webpack-plugin
await this.addHtmlPlugin(spaConfig, router.base, watcherEnabled, isSPA, entryName);
this.addHtmlPlugin(spaConfig, router.base, watcherEnabled, isSPA, entryName);
}));

// 2. add vue-skeleton-webpack-plugin
await this.addSkeletonPlugin(spaConfig, isSPA);
this.addSkeletonPlugin(spaConfig, isSPA);

return spaConfig;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/tb-lavas-core/core/builder/prod-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @author lavas
*/

import {emptyDir, outputFile, copy, remove} from 'fs-extra';
import {emptyDir, outputFileSync, copy, remove} from 'fs-extra';
import {join} from 'path';

import {copyWorkboxLibraries} from 'workbox-build';
Expand All @@ -17,7 +17,7 @@ import BaseBuilder from './base-builder';
export default class ProdBuilder extends BaseBuilder {
constructor(core) {
super(core);
this.writeFile = outputFile;
this.writeFile = outputFileSync;
}

/**
Expand Down
14 changes: 7 additions & 7 deletions packages/tb-lavas-core/core/utils/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
import webpack from 'webpack';
import {join} from 'path';
import {utimes, outputFile, readFile} from 'fs-extra';
import {utimesSync, outputFileSync, readFileSync} from 'fs-extra';
import template from 'lodash.template';

/**
Expand Down Expand Up @@ -48,15 +48,15 @@ export function webpackCompile(config) {
* @param {string} path file path
* @param {string} content file content
*/
export async function writeFileInDev(path, content) {
await outputFile(path, content, 'utf8');
export function writeFileInDev(path, content) {
outputFileSync(path, content, 'utf8');

/**
* hack for watchpack, solve the rebuilding problem in dev mode
* https://github.com/webpack/watchpack/issues/25#issuecomment-287789288
*/
let then = Date.now() / 1000 - 10;
await utimes(path, then, then);
utimesSync(path, then, then);
}

/**
Expand All @@ -66,18 +66,18 @@ export async function writeFileInDev(path, content) {
* @param {Object} config webpack config
* @param {boolean} subscribeReload whether subscribe reload action
*/
export async function enableHotReload(dir, config, subscribeReload = false) {
export function enableHotReload(dir, config, subscribeReload = false) {
let {entry, plugins, name: compilerName} = config;

let hotReloadEntryTemplate = join(__dirname, '../templates/entry-hot-reload.tmpl');
let hotReloadEntryPath = join(dir, `${compilerName}-hot-reload.js`);
let templateContent = template(await readFile(hotReloadEntryTemplate, 'utf8'))({
let templateContent = template(readFileSync(hotReloadEntryTemplate, 'utf8'))({
compilerName,
subscribeReload
});

// generate .lavas/xxx-hot-reload.js
await writeFileInDev(hotReloadEntryPath, templateContent);
writeFileInDev(hotReloadEntryPath, templateContent);

// add hot-reload entry in every entry
Object.keys(entry).forEach(entryName => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ test('it should merge middlewares defined in lavas.config.js and defaults correc
* lavas.config.js all: ['both']
* merged all: ['both']
*/
await core.build();
t.deepEqual(core.config.middleware.all, ['both']);
});

Expand Down

0 comments on commit 92c18f1

Please sign in to comment.