Skip to content

使用multi-page模式时chunks无法注入script #4944

@zwlijun

Description

@zwlijun

Version

3.11.0

Environment info

windows 10

Steps to reproduce

  1. 安装 vue-cli 工具
  2. 使用cli创建一个工程
  3. 配置pages参数
const path = require('path');
const fs = require('fs');
const glob = require('glob');

const mode = require('./mode.config');

function resolve(dir) {
    return path.join(__dirname, dir);
}
const ROOT_PATH = resolve('../src/modules');

const entries = glob.sync(`${ROOT_PATH}/**/main.js`) || [];
const useMultiplePages = ((entries.length > 0) && (mode.multiple === true));
const _pages = useMultiplePages ? {} : undefined;

console.log('PAGE MODE', useMultiplePages ? 'multi-page' : 'single');

if (useMultiplePages) {
    // 默认入口文件
    _pages.app = {
        // page 的入口
        entry: resolve('../src/main.js'),
        // 模板来源
        template: resolve('../public/index.html'),
        // 在 dist/index.html 的输出
        filename: 'index.html',
        // 当使用 title 选项时,
        // template 中的 title 标签需要是 <title><%= htmlWebpackPlugin.options.title %></title>
        title: '',
        // 在这个页面中包含的块,默认情况下会包含
        // 提取出来的通用 chunk 和 vendor chunk。
        // chunks: ['vendors', 'chunk-vendors', 'chunk-common', 'app'],
    };

    // 子模块入口文件
    entries.forEach((item, index) => {
        const name = path.basename(path.dirname(item));
        const template = path.join(path.dirname(item), 'public/index.html');
        const title = path.join(path.dirname(item), 'title');

        console.log(fs.readFileSync(title));

        _pages[name] = {
            // page 的入口
            entry: item,
            // 模板来源
            template: fs.existsSync(template) ? template : resolve('../public/index.html'),
            // 在 dist/index.html 的输出
            filename: `${name}/index.html`,
            // 当使用 title 选项时,
            // template 中的 title 标签需要是 <title><%= htmlWebpackPlugin.options.title %></title>
            title: fs.existsSync(title) ? fs.readFileSync(title, { encoding: 'utf8' }).replace(/^([\s]+)|([\s]+)$/g, '') : '',
            // 在这个页面中包含的块,默认情况下会包含
            // 提取出来的通用 chunk 和 vendor chunk。
            // chunks: ['vendors', 'chunk-vendors', 'chunk-common', name],
        };
    });

    console.log(_pages);
}

const pages = _pages;

// 多页配置
module.exports = pages;
  1. 执行 serve 或 build

What is expected?

能够正常渲染出页面

What is actually happening?

  1. chunks 没有注入到 HTML 页面中
  2. 由于第1点,导致页面不能渲染显示。

在使用vue-cli创建一个工程时,使用multi-page模式。配置 pages 参数。
通执 serve 和 build 后,页面相关的 chunks 不会被注入到 HTML文件中,导致页面无法执行渲染。
在排查过程中,发现 html-webpack-plugin 插件在处理 chunks 参数时采用的是 绝对匹配 的方式来过滤 chunk。 我在 html-webpack-plugin ##的github也提了issue( #jantimon/html-webpack-plugin#1319 ),如果 html-webpack-plugin的作者不采纳,cli 这边能否实现呢?

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs reproductionThis issue is missing a minimal runnable reproduction, provided by the author

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions