【发布时间】:2020-05-01 12:40:49
【问题描述】:
我正在尝试将一些数据异步加载到我的 Vue.js 应用程序的配置中,以供 Chris Fritz 的 PrerenderSPA webpack 插件使用,但是 - 它似乎没有预渲染任何路由。
当我对这些值进行硬编码时 - 它们被预渲染得很好,但在尝试异步加载 webpackConfiguration 时似乎失败了。
这是我的淡化尝试:
const configPromise = new Promise(async (resolve, reject) => {
// API Fetch for Prismic Routes:
const blogRoutes = await prismicRoutes;
let renderRoutes = [
...generalRoutes,
...blogRoutes,
];
// then at some point call `resolve()` with the config object:
resolve({
pwa: {
name: 'App',
themeColor: '#000000',
msTileColor: '#000000',
appleMobileWebAppCapable: 'yes',
appleMobileWebAppStatusBarStyle: 'black',
// configure the workbox plugin
workboxPluginMode: 'InjectManifest',
workboxOptions: {
// swSrc is required in InjectManifest mode.
swSrc: 'src/service-worker.js',
// ...other Workbox options...
}
},
configureWebpack: {
plugins: [
new PrerenderSPAPlugin({
staticDir: path.join(__dirname, 'dist'),
routes: renderRoutes,
}),
],
},
});
});
module.exports = configPromise;
这可以通过以下方式实现吗:
async function exportConfig() {
module.exports = await configPromise;
}
exportConfig();
有没有人在构建时实现了 vue.config.js 这样的异步加载?
【问题讨论】:
-
为什么你不直接抓取 nuxt.js
-
@Ifaruki Yeh - 我愿意,但这是一个遗留项目。
-
也许我正在关注这个问题,但如果您需要异步加载的数据在您的 webpack 配置中可用,这是受支持的。 Webpack 配置函数可以返回 Promise
标签: javascript vue.js