allow disabling async importer

This commit is contained in:
Pooya Parsa 2020-11-02 15:54:18 +01:00
parent ad4fc18ab8
commit ee27e3b5fe
3 changed files with 6 additions and 7 deletions

View File

@ -20,7 +20,6 @@ async function main () {
buildDir: '', buildDir: '',
targets: [], targets: [],
nuxt: 2, nuxt: 2,
dynamicImporter: undefined,
importSync: "require('../server/' + chunkId)", importSync: "require('../server/' + chunkId)",
importAsync: "Promise.resolve(require('../server/' + chunkId))", importAsync: "Promise.resolve(require('../server/' + chunkId))",
target: process.argv[3] && process.argv[3][0] !== '-' ? process.argv[3] : null, target: process.argv[3] && process.argv[3][0] !== '-' ? process.argv[3] : null,
@ -51,10 +50,8 @@ async function main () {
consola.info('Generated', prettyPath(htmlTemplateFileJS)) consola.info('Generated', prettyPath(htmlTemplateFileJS))
// Collect dynamic chunks // Collect dynamic chunks
if (!config.dynamicImporter) { consola.info('Collecting dynamic chunks...')
consola.info('Collecting dynamic chunks...') const dynamicImporter = await createDynamicImporter(resolve(config.buildDir, 'dist/server'))
config.dynamicImporter = await createDynamicImporter(resolve(config.buildDir, 'dist/server'))
}
// Bundle for each target // Bundle for each target
for (let target of config.targets) { for (let target of config.targets) {
@ -72,7 +69,8 @@ async function main () {
const ctx: any = defu( const ctx: any = defu(
target, target,
config, config,
tryImport(__dirname, `./targets/${target.target}`) || tryImport(config.rootDir, target.target) tryImport(__dirname, `./targets/${target.target}`) || tryImport(config.rootDir, target.target),
{ dynamicImporter }
) )
const hooks = new Hookable() const hooks = new Hookable()

View File

@ -70,7 +70,7 @@ export const getRollupConfig = (config) => {
if (config.dynamicImporter) { if (config.dynamicImporter) {
options.output.intro += config.dynamicImporter(config.importSync, config.importAsync) options.output.intro += config.dynamicImporter(config.importSync, config.importAsync)
} else { } else {
options.output.intro += 'const requireDynamic = require;' options.output.intro += `const requireDynamic = (chunkId) => ${config.importSync}`
} }
options.plugins.push(replace({ values: { 'require("./" +': 'requireDynamic(' }, delimiters: ['', ''] })) options.plugins.push(replace({ values: { 'require("./" +': 'requireDynamic(' }, delimiters: ['', ''] }))

View File

@ -2,6 +2,7 @@ import consola from 'consola'
export default { export default {
entry: require.resolve('./entry'), entry: require.resolve('./entry'),
dynamicImporter: false,
hooks: { hooks: {
'rollup:done' (_ctx) { 'rollup:done' (_ctx) {
consola.info('Run `vercel deploy` to deploy!') consola.info('Run `vercel deploy` to deploy!')