gwuhaolin / web-webpack-plugin Goto Github PK
View Code? Open in Web Editor NEWalternative for html-webpack-plugin
alternative for html-webpack-plugin
模板用的是ejs,需要在模板里面引用图片,如果直接用相对路径,编译后图片会直接复制到根目录,但不会被webpack当作模块来处理,所以文件名也不会改为hash方式,相应loader也不会处理
但html-webpack-plugin貌似可以这样处理
<img src="<% require('./a.png' %>" height="424" width="640" alt="" />
这个插件怎么实现上述的功能呢
现在html模板渲染之后是直接放到output的path路径下面,如果我想把html渲染之后的文件放到不同的文件夹下面,有方法配置吗?
commonsChunk在webpack4.0失效了,无法提取打包公共代码
在多页面配置下每个页面文件夹下默认是以index.js作为入口模块加载,但一些页面确实是不需要有单独的入口的,比如一个错误页面,本身没有其它逻辑的,只需要注入公共模块就够了,这种情况下是不需要单独入口的,但是如果不加index.js,打包编译时就会报错,如果加一个空的index.js,虽然不报错,但是编译后就会生成那个js文件,虽然只有一行,但是html也会引入进去,怎么解决这个问题呢
I guess this plugin depend on CommonsChunkPlugin which is deprecated with version 4.
Can you update?
使用AutoWebPlugin配置多页面,通过filename来修改生成后的html文件名无效,另外想单独配置html文件的生成路径,可以办到么
filename: (filename) => {
return 'test_' + filename
},
这个插件非常棒,以前都是要结合好几个插件才能实现的功能,这个都综合到一起了。
不过对我这种强迫症的人来说,保持好的输出结构和Url是必需的。
再次感谢你的插件。
你好,了解了你写的这个插件,非常的感兴趣,有一个问题想咨询一下你,麻烦你了,首先这个插件目录划分很明细,一个页面一个文件夹,一直是我开发最中意的目录规划,关键是你的插件能够解决 当我新增一个页面时,不用每次就往 entry加一条入口,和new HtmlWebpackPlugin让页面自动插入css和js
可是我想请问一下用这个插件,每个页面的目录下是没有html的 ,所以我所有页面都需要用js操作DOM去渲染页面,比如我开发一个企业官网,每个页面几乎都是不一样的,所以template.html可以说除了头部尾部,没有其它重用代码的地方,然后每个页面都需要用js写html字符串 然后操作DOM插入,这样子很不方便,所以我想知道,这个插件适合于哪些场景的开发,比如上面说的企业官网是否适合于用这个插件呢?
async
tag attribute gets converted to async=""
切活动页面通常会有这种需求,每个页面的布局不一样,能为每个页面设置模板吗
current code seems to just copy and paste the output filename into the script tag. need some logic added to calculate relative path based on js location and HTML location.
{
....
output: {
filename: "./js/dist/[name]-[chunkhash].min.js"
},
plugins: [
....
new WebPlugin({
template: 'js/angular/pages/reports/index.html',
filename: 'org/index.php',
requires: ['common', 'reports'],
})
]
}
plugin outputs
<script src="js/dist/common-9359363eb6cc11f14a53.min.js"></script>
<script src="js/dist/reports-c55beadeae38133a9cd5.min.js"></script>
should be
<script src="../js/dist/common-9359363eb6cc11f14a53.min.js"></script>
<script src="../js/dist/reports-c55beadeae38133a9cd5.min.js"></script>
使用_ie默认出来的IE条件注释是没有版本条件的,怎么加上,比如
<!--[if lt IE 9]>
web-webpack-plugin/lib/Resource.js
Line 336 in 0b55258
待补充测试,之前的项目实践是会的
webpack.config
const path = require("path"),
resolve = p => path.resolve(__dirname, p);
const { WebPlugin, AutoWebPlugin } = require("web-webpack-plugin");
const config = {
entry: {
A: resolve("src/pages/A.js"),
B: resolve("src/pages/B.js")
},
output: {
path: resolve("dist"),
filename: "[name].js?[chunkhash]",
publicPath:'v2/dist/'
},
module: {
loaders: [
{
test: /\.css$/,
use: ["style-loader", "css-loader"]
}
]
},
resolve: {},
plugins: [
new WebPlugin({
template:'./template.html',
filename: "index.html",
requires: ["A", "B"]
})
]
};
module.exports = config;
template
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="B"></script>
<link rel="stylesheet" href="./reset.min.css?_inline">
<script src="./test.js"></script>
</head>
<body>
<!-- SCRIPT -->
</body>
</html>
结果
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style rel="stylesheet">*{
margin: 0;
padding: 0;
color: green;
}</style>
<script src="v2/dist/test.js"></script>
</head>
<body>
<!-- SCRIPT -->
</body>
</html>
弱弱的问名字是不是可以跟专一点?好容易搞混
目前只允许传入一个页面文件夹,而 AutoWebPlugin
内部 getDirsInDir
只读取第一层文件夹,这就很难组织灵活的多页面结构。
如果这个参数能像 template
那样有 string | function
两种类型的选择就好了,这样我们就可以传入自定义的页面读取方法:
const autoWebPlugin = new AutoWebPlugin(()=>{
//偶要遍历所有子文件夹 ;
//....
return allPageNames;
},
options);
template
的参数也可能需要对应的调整。
感谢作者!
new AutoWebPlugin('./src/pages/', {
template: './src/assets/template.html',
commonsChunk: {
name: 'common',
minChunks: 2
},
outputPagemap: true
}
比如以上,模版内容可以被html相关的loader处理
// call by webpack
apply(compiler) {
global._isProduction = util.isProduction(compiler);
global._isExtractStyle = util.isExtractStyle(compiler);
const { options: compilerOptions } = compiler;
const { entryMap } = this;
const { outputPagemap, outputPagemapFilename, requires = [], ...otherOptions } = this.options;
Object.keys(entryMap).forEach((pageName) => {
// ensure entryMap from pages has been add to webpack entry
// webpack-dev-server may modify compilerOptions.entry, e.g add webpack-dev-server/client to every entry
compilerOptions.entry = Object.assign(
this.webpackEntry,
compilerOptions.entry,
);
// add an WebPlugin for every page to output an html
const {
templatePath,
templateCompiler,
htmlOutputFilename,
entryPath,
} = entryMap[pageName];
new WebPlugin({
...otherOptions,
template: templatePath,
templateCompiler,
pageName,
entryPath,
filename: `${htmlOutputFilename}.html`,
requires: requires.concat(pageName),
}).apply(compiler);
});
plugin watch and copy all asset in HTML file . to make HTML file the entry point. but the important asset of the web-component is missing. import another .html file are you can add it, please?
如题,打包出来的index.html中如何设置以添加挂载点
首先表示看到这个插件,很强大,很符合我们的使用场景
再者我提一个小功能,
假如很多模块,指向配置打包二个模块的话,我只配置
const includeList = ['index','home'];
includePages:includeList,
includePages:(pageNaem) =>includeList.include(pageNaem),
或者IgnorePages支持函数
ignorePages:(pageNaem) => !includeList.include(pageNaem)
和loader一样的机制exclude/include
同时存在的时候,选择只是用一个或者按优先级
希望作者大大能够支持,尽快发布一下,谢谢了,需要我帮忙new request一个吗
web-webpack-plugin/lib/Resource.js
Line 99 in 123736a
这里应该结合script标签是否inline,且src属性值是否有效来添加
src
- A
- a
-index.js
- B
- b
- index.js
template.html
假设我的目录结构是这样的,导到 dist 中的 html 与 index.js 同级,应该怎么做呢?
AutoWebPlugin 的首参是单目录的,只用在 A 上可行。
看了源码是希望我将 { 'src/A/a': true, 'src/B/b': true } 作为首参传进去吗。
(node:7068) DeprecationWarning: Tapable.plugin is deprecated. Use new API on .hooks
instead
throw new Error("Chunk.parents: Use ChunkGroup.add/removeParent() instead");
Error: Chunk.parents: Use ChunkGroup.add/removeParent() instead
目前 JS 注入的标签是寻找 <!--SCRIPT-->
,但其实用 prettoer 格式化之后会变成 <!-- SCRIPT -->
,这也更加规范。
web-webpack-plugin/lib/AutoWebPlugin.js
Line 167 in 0b55258
这里...otherOptions
的写法,导致plugin配置中的template
属性和WebPlugin中buildHtmlDocument方法中判断的options.template属性冲突,引起覆盖
问题
在看书进行到多页管理配置时,commonsChunk 提取公用代码无效,源代码似乎没有这个选项
Webpack4 下使用 splitChunks 不知道如何提取公用代码
webpack配置
publichPath
: '//7.url.cn/edu/user/'
插件配置
filename: (n) => `${n}/${n}`
Resource.js
// add a file to newNodes
const outToNewNodes = (fileName, fileContent) => {
// resource should load in URL relative to html output path
let resourceRelative2HTMLPath = url.resolve(webpackOutputPublicPath, fileName);
resourceRelative2HTMLPath = util.urlRelative(htmlOutputURL, resourceRelative2HTMLPath);
resourceRelative2HTMLPath = url.resolve(webpackOutputPublicPath, resourceRelative2HTMLPath);
if (type === 'script') {
// output js file only
...
} else if (type === 'style') {
// output css file only
...
} else if (type === 'other') {
...
}
};
此类情况当执行到上面Resource.js文件的outToNewNodes方法时,resourceRelative2HTMLPath最终计算出资源文件的路径为//7.url.cn/edu/[name]
,故html加载资源的路径出错。
注释掉这两行代码后,跑通
// resourceRelative2HTMLPath = util.urlRelative(htmlOutputURL, resourceRelative2HTMLPath);
// resourceRelative2HTMLPath = url.resolve(webpackOutputPublicPath, resourceRelative2HTMLPath);
const fileContent = fs.readFileSync(filePath, {
encoding: ['script', 'style'].indexOf(this.type) >= 0 ? 'utf8' : undefined
});
// 从文件的路径中解析出文件的名称
const filename = util.getFilenameByFilePath(filePath);
// 对于非inline的资源需要输出
if (!resourceAttr.inline) {
// 通过webpack把本地文件输出
util.addFileToWebpackOutput(compilation, filename, fileContent);
}
// 添加文件名称和内容到新节点 以供输出到HTML文档
outToNewNodes(filename, fileContent);
目前fileContent是直接读取的
目前 JS 注入的标签是寻找 <!--SCRIPT-->
,但其实用 prettier 格式化之后会变成 <!-- SCRIPT -->
,这也更加规范。
期望能够自定义注入的标签,或者兼容 <!-- SCRIPT -->
这种写法。
Hello! Thank you for the plugin. Could you tell me, perhaps whether I use pug-templates?
I have a template
#{extends 'main.html'/}
<div id="root"></div>
#{set 'reactScripts'}
<!--SCRIPT-->
#{/set}
so I'd like to have output.html
#{extends 'main.html'/}
<div id="root"></div>
#{set 'reactScripts'}
<script src="polyfills.ba39d5cf24f6f736654b.esm.js"></script>
<script src="main.af33690fade9e8d661fc.esm.js"></script>
#{/set}
without html/head/body wrapper tags.
Is it possible?
[email protected] build D:\app\social-h5\mall-master
node build/build.js
D:\app\social-h5\mall-master\node_modules_web-webpack-plugin@4.6.7@web-webpack-plugin\lib\WebPlugin.js:99
compiler.hooks.webPluginBeforeEmitHTML = new SyncHook(['htmlDocument']);
^
TypeError: Cannot set property 'webPluginBeforeEmitHTML' of undefined
at WebPlugin.apply (D:\app\social-h5\mall-master\node_modules_web-webpack-plugin@4.6.7@web-webpack-plugin\lib\WebPlugin.js:99:42)
at Object.keys.forEach (D:\app\social-h5\mall-master\node_modules_web-webpack-plugin@4.6.7@web-webpack-plugin\lib\AutoWebPlugin.js:179:7)
at Array.forEach ()
at AutoWebPlugin.apply (D:\app\social-h5\mall-master\node_modules_web-webpack-plugin@4.6.7@web-webpack-plugin\lib\AutoWebPlugin.js:156:25)
at Compiler.apply (D:\app\social-h5\mall-master\node_modules_tapable@0.2.9@tapable\lib\Tapable.js:375:16)
at webpack (D:\app\social-h5\mall-master\node_modules_webpack@2.7.0@webpack\lib\webpack.js:32:19)
at Object. (D:\app\social-h5\mall-master\build\build.js:24:1)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
The plugin overwrite the current entry instead of adding to it.
What about if the user has other objects instead of only html files and want all?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.