关于延迟主要styles.scss的加载,我依靠两件事:
- 首先,我使用
--extract-css=false
标志构建应用程序。这确保了样式包将是一个 JavaScript 文件。
- 其次,我推迟加载该文件。为此,我依赖于我调用的一个小脚本,该脚本
defer.styles.js
在构建过程结束时运行。该脚本只是查找加载样式的脚本标签,并在defer
其中添加一个。
const path = require('path');
const htmlPath = path.join(__dirname, 'dist', 'browser', 'index.html');
fs.readFile(htmlPath, 'utf8', (err, data) => {
if (err) {
console.log(err);
return;
}
const index = data.search(`src="styles.`);
if (index > -1) {
const output = [data.slice(0, index), 'defer ', data.slice(index)].join('');
console.log(output);
fs.writeFile(htmlPath, output, 'utf8', () => {
console.log('Styles succesfully deferred');
})
}
});
关于您的其他问题-如何加载关键的CSS-。我仍然没有找到一种舒适的方式。