Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: multi-page different usage of filename and template #127

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ThornWalli
Copy link

Hello,

I am currently trying Multi-Entrypoints with multi Page templates.

Unfortunately I found that something is difficult to get running. After I had it then however, I had to determine... That certain things in the Plugin are not correctly assigned.

This is about the two properties template and filename. According to the code there seems to be inconsistencies. When and where the template or filename is accessed, is somewhat inconsistent.

I have expanded the places in this PR, but somehow there seems to be an inconsistency in it.

const page = pages.filter((page) => {
return path.resolve('/' + page.template) === path.resolve('/' + htmlName)
})?.[0]

function createRewire(
reg: string,
page: any,
baseUrl: string,
proxyUrlKeys: string[],
) {
return {
from: new RegExp(`^/${reg}*`),
to({ parsedUrl }: any) {
const pathname: string = parsedUrl.pathname
const excludeBaseUrl = pathname.replace(baseUrl, '/')
const template = path.resolve(baseUrl, page.template)
if (excludeBaseUrl === '/') {
return template
}
const isApiUrl = proxyUrlKeys.some((item) =>
pathname.startsWith(path.resolve(baseUrl, item)),
)
return isApiUrl ? excludeBaseUrl : template
},
}
}

What is this? If I replace the template there with filename, this then works and copies the HTML files to the root level, where folder structure is needed.

async closeBundle() {
const outputDirs: string[] = []
if (isMpa(viteConfig) || pages.length) {
for (const page of pages) {
const dir = path.dirname(page.template)
if (!ignoreDirs.includes(dir)) {
outputDirs.push(dir)
}
}
} else {
const dir = path.dirname(template)
if (!ignoreDirs.includes(dir)) {
outputDirs.push(dir)
}
}
const cwd = path.resolve(viteConfig.root, viteConfig.build.outDir)
const htmlFiles = await fg(
outputDirs.map((dir) => `${dir}/*.html`),
{ cwd: path.resolve(cwd), absolute: true },
)
await Promise.all(
htmlFiles.map((file) =>
fs.move(file, path.resolve(cwd, path.basename(file)), {
overwrite: true,
}),
),
)
const htmlDirs = await fg(
outputDirs.map((dir) => dir),
{ cwd: path.resolve(cwd), onlyDirectories: true, absolute: true },
)
await Promise.all(
htmlDirs.map(async (item) => {
const isEmpty = await isDirEmpty(item)
if (isEmpty) {
return fs.remove(item)
}
}),
)
},
}
}


My Example:

Config

{
  minify: false,
  pages: [
    {
      entry: './main.js',
      filename: 'src/banners/300x250/index.html',
      template: 'banners/300x250/index.html',
      injectOptions: {
        data: {
          title: 'Index'
        }
      }
    },
    {
      entry: './main.js',
      filename: 'src/banners/800x250/index.html',
      template: 'banners/800x250/index.html',
      injectOptions: {
        data: {
          title: 'Index'
        }
      }
    }
  ]
}
  1. in dev the redirection to filename is correct. (e.g. http://localhost:3000/banners/300x250/index.html)
  2. in the build as long as closeBundle is not adjusted, I get now the output in the build, unfortunately I do not know how to remove the src path in the generated files. (Background: Everything that is programmed is in the src folder).

image

@ThornWalli ThornWalli changed the title fix: wrong pages file path usage fix: multi-page different usage of filename and template Aug 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant