client/build: true template precompiling
Rather than putting templates in DOM nodes, output JS functions themselves. This fixes transpiling for IE11, where ES6 parts of the templates wouldn't get passed through Babel.
This commit is contained in:
parent
052a7ca684
commit
98c6128829
|
@ -55,42 +55,50 @@ function getConfig() {
|
||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
function bundleHtml(config) {
|
function minifyJs(path) {
|
||||||
const minify = require('html-minifier').minify;
|
return require('uglify-js').minify(path).code;
|
||||||
const baseHtml = fs.readFileSync('./html/index.htm', 'utf-8');
|
}
|
||||||
const minifyOptions = {
|
|
||||||
|
function minifyCss(css) {
|
||||||
|
return require('csso').minify(css);
|
||||||
|
}
|
||||||
|
|
||||||
|
function minifyHtml(html) {
|
||||||
|
return require('html-minifier').minify(html, {
|
||||||
removeComments: true,
|
removeComments: true,
|
||||||
collapseWhitespace: true,
|
collapseWhitespace: true,
|
||||||
conservativeCollapse: true,
|
conservativeCollapse: true,
|
||||||
};
|
}).trim();
|
||||||
glob('./html/**/*.tpl', {}, (er, files) => {
|
|
||||||
let templates = {};
|
|
||||||
for (const file of files) {
|
|
||||||
const name = path.basename(file, '.tpl').replace(/_/g, '-');
|
|
||||||
templates[name] = minify(
|
|
||||||
fs.readFileSync(file, 'utf-8'), minifyOptions);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const templatesHolder = util.format(
|
function bundleHtml(config) {
|
||||||
'<script type=\'text/javascript\'>' +
|
const lodash = require('lodash');
|
||||||
'var templates = %s;' +
|
const babelify = require('babelify');
|
||||||
'</script>',
|
const baseHtml = fs.readFileSync('./html/index.htm', 'utf-8');
|
||||||
JSON.stringify(templates));
|
|
||||||
|
|
||||||
const finalHtml = baseHtml
|
const finalHtml = baseHtml
|
||||||
.replace(/(<\/head>)/, templatesHolder + '$1')
|
|
||||||
.replace(
|
.replace(
|
||||||
/(<title>)(.*)(<\/title>)/,
|
/(<title>)(.*)(<\/title>)/,
|
||||||
util.format('$1%s$3', config.name));
|
util.format('$1%s$3', config.name));
|
||||||
|
fs.writeFileSync('./public/index.htm', minifyHtml(finalHtml));
|
||||||
|
|
||||||
fs.writeFileSync(
|
glob('./html/**/*.tpl', {}, (er, files) => {
|
||||||
'./public/index.htm', minify(finalHtml, minifyOptions));
|
let compiledTemplateJs = '\'use strict\'\n';
|
||||||
|
compiledTemplateJs += 'let _ = require(\'lodash\');';
|
||||||
|
compiledTemplateJs += 'let templates = {};';
|
||||||
|
for (const file of files) {
|
||||||
|
const name = path.basename(file, '.tpl').replace(/_/g, '-');
|
||||||
|
const templateText = minifyHtml(fs.readFileSync(file, 'utf-8'));
|
||||||
|
const functionText = lodash.template(
|
||||||
|
templateText, {variable: 'ctx'}).source;
|
||||||
|
compiledTemplateJs += `templates['${name}'] = ${functionText};`;
|
||||||
|
}
|
||||||
|
compiledTemplateJs += 'module.exports = templates;';
|
||||||
|
fs.writeFileSync('./js/.templates.autogen.js', compiledTemplateJs);
|
||||||
console.info('Bundled HTML');
|
console.info('Bundled HTML');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function bundleCss() {
|
function bundleCss() {
|
||||||
const minify = require('csso').minify;
|
|
||||||
const stylus = require('stylus');
|
const stylus = require('stylus');
|
||||||
glob('./css/**/*.styl', {}, (er, files) => {
|
glob('./css/**/*.styl', {}, (er, files) => {
|
||||||
let css = '';
|
let css = '';
|
||||||
|
@ -98,24 +106,11 @@ function bundleCss() {
|
||||||
css += stylus.render(
|
css += stylus.render(
|
||||||
fs.readFileSync(file, 'utf-8'), {filename: file});
|
fs.readFileSync(file, 'utf-8'), {filename: file});
|
||||||
}
|
}
|
||||||
fs.writeFileSync('./public/app.min.css', minify(css));
|
fs.writeFileSync('./public/app.min.css', minifyCss(css));
|
||||||
console.info('Bundled CSS');
|
console.info('Bundled CSS');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function writeJsBundle(b, path, message, compress) {
|
|
||||||
const uglifyjs = require('uglify-js');
|
|
||||||
let outputFile = fs.createWriteStream(path);
|
|
||||||
b.bundle().pipe(outputFile);
|
|
||||||
outputFile.on('finish', function() {
|
|
||||||
if (compress) {
|
|
||||||
const result = uglifyjs.minify(path);
|
|
||||||
fs.writeFileSync(path, result.code);
|
|
||||||
}
|
|
||||||
console.info(message);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function bundleJs(config) {
|
function bundleJs(config) {
|
||||||
const browserify = require('browserify');
|
const browserify = require('browserify');
|
||||||
const external = [
|
const external = [
|
||||||
|
@ -126,6 +121,18 @@ function bundleJs(config) {
|
||||||
'page',
|
'page',
|
||||||
'nprogress',
|
'nprogress',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
function writeJsBundle(b, path, message, compress) {
|
||||||
|
let outputFile = fs.createWriteStream(path);
|
||||||
|
b.bundle().pipe(outputFile);
|
||||||
|
outputFile.on('finish', function() {
|
||||||
|
if (compress) {
|
||||||
|
fs.writeFileSync(path, minifyJs(path));
|
||||||
|
}
|
||||||
|
console.info(message);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
glob('./js/**/*.js', {}, (er, files) => {
|
glob('./js/**/*.js', {}, (er, files) => {
|
||||||
if (!process.argv.includes('--no-vendor-js')) {
|
if (!process.argv.includes('--no-vendor-js')) {
|
||||||
let b = browserify();
|
let b = browserify();
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
.config.autogen.json
|
*.autogen.*
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
module.exports = require('./.templates.autogen.js');
|
|
@ -1,7 +1,7 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
require('../util/polyfill.js');
|
require('../util/polyfill.js');
|
||||||
const lodash = require('lodash');
|
const templates = require('../templates.js');
|
||||||
const tags = require('../tags.js');
|
const tags = require('../tags.js');
|
||||||
const events = require('../events.js');
|
const events = require('../events.js');
|
||||||
const domParser = new DOMParser();
|
const domParser = new DOMParser();
|
||||||
|
@ -237,8 +237,7 @@ function getTemplate(templatePath) {
|
||||||
console.error('Missing template: ' + templatePath);
|
console.error('Missing template: ' + templatePath);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
const templateText = templates[templatePath].trim();
|
const templateFactory = templates[templatePath];
|
||||||
const templateFactory = lodash.template(templateText, {variable: 'ctx'});
|
|
||||||
return ctx => {
|
return ctx => {
|
||||||
if (!ctx) {
|
if (!ctx) {
|
||||||
ctx = {};
|
ctx = {};
|
||||||
|
|
Loading…
Reference in New Issue