如何创建具有多个html页面的Fusebox项目?

时间:2017-10-24 21:18:41

标签: bundle fusebox

我是捆绑商的新手,目前正在了解Fusebox。到目前为止我真的很喜欢它,除了我无法弄清楚如何将它用于多页项目。到目前为止,我只能在how to do this using webpack找到教程,而不是找到保险箱。

在src文件夹中输入文件:

  • 的index.html
  • index2.html
  • index.ts

dist文件夹中的所需输出:

  • app.js
  • vendor.js
  • 的index.html
  • index2.html

dist文件夹中的实际输出:

  • app.js
  • vendor.js
  • 的index.html

这是我在fuse.js文件中的配置:

Sparky.task("config", () => {
    fuse = FuseBox.init({
       homeDir: "src",
       output: "dist/$name.js",
       hash: isProduction,
       sourceMaps: !isProduction,
       plugins: [
           [SassPlugin(), CSSPlugin()],
           CSSPlugin(),
           WebIndexPlugin({ 
               title: "Welcome to FuseBox index",
               template: "src/index.html"
           },
           WebIndexPlugin({ 
               title: "Welcome to FuseBox index2",
               template: "src/index2.html"
           },
           isProduction && UglifyJSPlugin()
        ]
    });

    // vendor should come first
    vendor = fuse.bundle("vendor")
        .instructions("~ index.ts");

    // out main bundle
    app = fuse.bundle("app")
        .instructions(`!> [index.ts]`);

    if (!isProduction) {
        fuse.dev();
    }
});

在插件中设置WebIndexPlugin两次不起作用。使用fusebox设置多html页面项目的正确方法是什么?

2 个答案:

答案 0 :(得分:2)

无法配置WebIndexPlugin,以输出多个html文件。

但是如果你不为生成的包使用哈希(例如:output: "dist/$name.$hash.js"),那么你就不需要WebIndexPlugin - 你可以完全从它中删除它plugins选项。因为您已经知道生成的包的名称(vendor.jsapp.js),所以您可以包含以下行

<script src="vendor.js"></script>
<script src="app.js"></script>

而不是占位符$bundles

如果您愿意,将两个html文件从src目录复制到dist目录中,可以将以下行添加到fuse.js脚本中:

const fs = require('fs-extra');
fs.copySync('src/index.html', 'dist/index.html');
fs.copySync('src/index2.html', 'dist/index2.html');

注意:不要忘记将fs-extra:^5.0.0添加到package.json

答案 1 :(得分:1)

询问问题时可能不是这种情况,但是现在可以多次指定WebIndexPlugin,并且还可以使用可选的bundles参数,在其中可以指定要包含在html中的捆绑软件列表(所有捆绑包是默认包含的)。

例如2个html文件(app1.html,app2.html),其中每个文件都包含一个公共库(vendor.js)和不同的入口点(app1.js和app2.js)

  • app1.html
    • vendor.js
    • app1.js
  • app2.html
    • vendor.js
    • app2.js

配置如下所示:

const fuse = FuseBox.init({
    homeDir : "src",
    target : 'browser@es6',
    output : "dist/$name.js",
    plugins: [
        WebIndexPlugin({
            target: 'app1.html',
            bundles:['vendor', 'app1']
        }),
        WebIndexPlugin({
            target: 'app2.html',
            bundles:['vendor', 'app2']
        })
    ]
})
// vendor bundle, extracts dependencies from index1 and index2:
fuse.bundle("vendor").instructions("~[index1.ts,index2.ts]")

// app1 and app2, bundled separately without dependencies:
fuse.bundle("app1").instructions("!>index1.ts")
fuse.bundle("app2").instructions("!>index2.ts")
相关问题