Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Webpack 如何将posthtml与网页包和多个HTML文件一起使用?_Webpack_Posthtml - Fatal编程技术网

Webpack 如何将posthtml与网页包和多个HTML文件一起使用?

Webpack 如何将posthtml与网页包和多个HTML文件一起使用?,webpack,posthtml,Webpack,Posthtml,我正在尝试使用Webpack在多页应用程序中模拟Parcel的行为(我会使用Parcel,但Parcel 1在节点15上不起作用,Parcel 2目前仍然是测试版,有太多的bug,无法使用),其中一部分是我需要能够拥有多个带有共享标记的HTML文件。在parcel中,我使用了PostHtmlInclude,所以我想对Webpack也这样做,尽管我对其他选项持开放态度。为此,我找到了github存储库,这似乎是集成posthtml和Webpack的官方方式。有了这个,我创建了一个最小的Webpac

我正在尝试使用Webpack在多页应用程序中模拟Parcel的行为(我会使用Parcel,但Parcel 1在节点15上不起作用,Parcel 2目前仍然是测试版,有太多的bug,无法使用),其中一部分是我需要能够拥有多个带有共享标记的HTML文件。在parcel中,我使用了PostHtmlInclude,所以我想对Webpack也这样做,尽管我对其他选项持开放态度。为此,我找到了github存储库,这似乎是集成posthtml和Webpack的官方方式。有了这个,我创建了一个最小的Webpack项目来找出如何使用它,我发现它并没有像预期的那样工作。我的最小、完整、可验证的示例如下:

package.json:

{
“名称”:“posthtml”,
“脚本”:{
“生成”:“网页包”
},
“依赖性”:{
“html加载程序”:“^1.3.2”,
“posthtml包含”:“^1.6.0”,
“posthtml加载程序”:“^2.0.1”,
“网页包”:“^5.6.0”,
Web包cli“^4.2.0”
}
}
webpack.config.js(基本上是链接github存储库中示例的副本/粘贴):

const path=require('path');
module.exports={
条目:'./src/index.js',
输出:{
文件名:“main.js”,
path:path.resolve(uu dirname,'dist'),
},
模块:{
规则:[
{
测试:/\.html$/,,
使用:[
“html加载程序”,
{
加载器:“posthtml加载器”,
选项:{
标识:“posthtml”,
解析器:“PostHTML解析器”,
插件:[
/*PostHTML插件*/
require('posthtml-include')(选项)
]
}
}
]
},
]
}
};
src/index.js:

从“./index.html”导入html;
src/index.html:


试验
当我在这个项目上运行
npm run build
时,我希望它在
dist
子文件夹中输出一个
index.html
文件,该文件显示我的标题(实际上使用的是Webpack和Posthtml的功能是在它工作之后出现的),但是我得到了以下错误:

$ npm run build

> posthtml@ build <path to the project directory is redacted>
> webpack

[webpack-cli] ReferenceError: options is not defined
    at Object.<anonymous> (<path to the project directory is redacted>/webpack.config.js:22:35)
    at Module._compile (<path to the project directory is redacted>/node_modules/v8-compile-cache/v8-compile-cache.js:192:30)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1131:10)
    at Module.load (node:internal/modules/cjs/loader:967:32)
    at Function.Module._load (node:internal/modules/cjs/loader:807:14)
    at Module.require (node:internal/modules/cjs/loader:991:19)
    at require (<path to the project directory is redacted>/node_modules/v8-compile-cache/v8-compile-cache.js:159:20)
    at requireConfig (/<path to the project directory is redacted>/node_modules/webpack-cli/lib/groups/resolveConfig.js:73:18)
    at Array.map (<anonymous>)
    at resolveConfigFiles (<path to the project directory is redacted>/node_modules/webpack-cli/lib/groups/resolveConfig.js:124:40)
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! posthtml@ build: `webpack`
npm ERR! Exit status 2
npm ERR! 
npm ERR! Failed at the posthtml@ build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     <home directory is redacted>/.npm/_logs/<current time, which gives timezone, is redacted>-debug.log
$npm运行构建
>posthtml@build
>网页包
[webpack cli]引用错误:未定义选项
反对。(/webpack.config.js:22:35)
在Module.\u compile(/node\u modules/v8 compile cache/v8 compile cache.js:192:30)
在Object.Module._extensions..js(节点:internal/modules/cjs/loader:1131:10)
at Module.load(节点:内部/模块/cjs/加载器:967:32)
at功能模块加载(节点:内部/模块/cjs/加载程序:807:14)
at Module.require(节点:内部/模块/cjs/加载器:991:19)
在需要时(/node_modules/v8 compile cache/v8 compile cache.js:159:20)
在requireConfig(//node_modules/webpack cli/lib/groups/resolveConfig.js:73:18)
在Array.map()处
在resolveConfigFiles(/node_modules/webpack cli/lib/groups/resolveConfig.js:124:40)
npm错误!代码失效循环
npm错误!二号
npm错误!posthtml@build:`webpack`
npm错误!退出状态2
npm错误!
npm错误!在posthtml@build脚本中失败。
npm错误!这可能不是npm的问题。上面可能还有其他日志输出。
npm错误!此运行的完整日志可在以下位置找到:
npm错误!/。npm/_logs/-debug.log
为什么Webpack的表现不符合我的预期?我怎样才能使它表现出我所期望的样子?我试图找到其他关于如何在Webpack中使用Posthtml的源代码,但我找到的大部分只提供了如何在Webpack中使用常规HTML文件的信息。当我使用示例中给出的用于此集成的正式Github存储库的确切代码时,它不起作用是毫无意义的

版本信息:

$ npm --version            
6.14.8
$ node --version
v15.2.1
$ uname -a      
Linux blue 5.9.10-arch1-1 #1 SMP PREEMPT <date is redacted> x86_64 GNU/Linux
$npm--版本
6.14.8
$node—版本
v15.2.1
$uname-a
Linux blue 5.9.10-arch1-1#1 SMP抢占x86_64 GNU/Linux

因为您有
require('posthtml-include')(选项)
,而且
选项在您的网页配置中没有定义。

我不太理解最终目标,也许这个选项适合您

{
“名称”:“posthtml”,
“脚本”:{
“生成”:“网页包”
},
“依赖性”:{
“提取加载程序”:“^5.1.0”,
“文件加载器”:“^6.2.0”,
“html加载程序”:“^1.3.2”,
“posthtml包含”:“^1.6.0”,
“posthtml加载程序”:“^2.0.1”,
“网页包”:“^5.6.0”,
Web包cli“^4.2.0”
}
}
const path=require('path');
module.exports={
条目:'./src/index.js',
输出:{
文件名:“main.js”,
path:path.resolve(uu dirname,'dist'),
},
模块:{
规则:[
{
测试:/\.html$/,,
使用:[
“文件加载器”,
“提取加载程序”,
“html加载程序”,
{
加载器:“posthtml加载器”,
选项:{
插件:[
require('posthtml-include')()
]
}
}
]
},
]
}
};

但是如果您只需要生成html,您可以使用

谢谢!我通过将
(选项)
更改为
()
修复了这个问题,然后我必须将
posthtml include
安装到开发依赖项,它构建了一些东西,但它只输出一个main.js文件,而
dist
中没有HTML文件。如何让它输出HTML文件?我的配置似乎有不止一个错误(如果所有需要的信息