我正在尝试使用 HTML Webpack 插件创建一个带有静态 HTML 部分的设置,但遇到了一些错误。这是我当前的配置:
webpack.config.js
const webpack = require('webpack');
const path = require('path');
const ExtractTextWebpackPlugin = require('extract-text-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const OptimizeCSSAssets = require('optimize-css-assets-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
let config = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, './public'),
filename: 'app.js'
},
module: {
loaders: [{
test: /\.html$/,
loader: 'html-loader'
}],
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader'
},
{
test: /\.scss$/,
use: ['css-hot-loader'].concat(ExtractTextWebpackPlugin.extract({
fallback: 'style-loader',
use: ['css-loader', 'sass-loader', 'postcss-loader'],
})),
},
{
test: /\.(jpe?g|png|gif|svg)$/i,
loaders: ['file-loader?context=src/assets/images/&name=images/[path][name].[ext]', {
loader: 'image-webpack-loader',
query: {
mozjpeg: {
progressive: true,
},
gifsicle: {
interlaced: false,
},
optipng: {
optimizationLevel: 4,
},
pngquant: {
quality: '75-90',
speed: 3,
},
},
}],
exclude: /node_modules/,
include: __dirname,
},
]
},
plugins: [
new HtmlWebpackPlugin({
template: './src/template.html.ejs'
}),
new ExtractTextWebpackPlugin('main.css')
],
devServer: {
contentBase: path.resolve(__dirname, './public'),
historyApiFallback: true,
inline: true,
open: true
},
devtool: 'eval-source-map'
}
module.exports = config;
if (process.env.NODE_ENV === 'production') {
module.exports.plugins.push(
new webpack.optimize.UglifyJsPlugin(),
new OptimizeCSSAssets()
);
}
template.html.ejs(位于./src下)
<%=require('./header.html')%>
<body>
testing schmesting
</body>
<%=require('./footer.html')%>
</html>
(footer.html 和header.html 位于./src 下)
编辑:更新代码,问题依旧:
“错误中的错误:子编译失败:
模块解析失败:意外标记 (1:0)
您可能需要适当的加载程序来处理此文件类型。
语法错误:意外标记 (1:0)
模块解析失败:意外标记 (1:2)
您可能需要一个合适的加载器来处理这种文件类型。”
最佳答案
编辑
(编辑:2017-12-20 将“装载机”移至“规则”)
默认情况下,“html-webpack-plugin”解析模板为“underscore”(也叫lodash)模板,你的“template.html”没有错,错误是webpack failed to resolve 'html -loader' 为你的“template.html”
<%= require('html-loader!./footer.html') %> ,所以你需要为webpack安装“html-loader”,并配置它:
在命令行中:
npm install html-loader
并为 webpack 配置它,编辑 webpack.config.js :
...
module: {
rules: [
// ...
{
test: /\.html$/, // tells webpack to use this loader for all ".html" files
loader: 'html-loader'
}
]
}
现在你可以运行“webpack”,你不会看到任何错误,但是生成的“index.html”不是你所期望的,因为你的模板文件有“.html”扩展名,webpack现在使用“html-loader”加载“template.html”而不是默认的“lodash loader”,为了解决这个问题,您可以将“template.html”重命名为“template.html.ejs”(或任何其他扩展名)以制作“html-webpack-plugin”回退。除了在“template.html”上有更多变化外,删除“html-loader!”来自它:
<%= require('./footer.html') %>
现在它应该可以工作了。
编辑 发布我的代码以供引用:
/src/template.html.ejs
<!DOCTYPE html>
<html lang="en">
<head>
<title>test</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h1>template</h1>
<%=require('./footer.html')%>
</body>
</html>
/src/footer.html
<footer>this is a footer</footer>
/webpack.config.js
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
let config = {
entry: {
index: './src/js/index'
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js'
},
module: {
rules: [
{
test: /\.html$/,
loader: 'html-loader'
}
],
},
plugins: [
new HtmlWebpackPlugin({
template: './src/template.html.ejs'
})
]
}
module.exports = config;
/package.json
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"html-loader": "^0.5.1",
"html-webpack-plugin": "^2.30.1",
"webpack": "^3.10.0"
}
}
/src/js/index.js
console.log("A test page!");
环境:
运行 webpack 后“/dist/index.html”的内容:
<!DOCTYPE html>
<html lang="en">
<head>
<title>test</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h1>template</h1>
<footer>this is a footer</footer>
<script type="text/javascript" src="index.js"></script></body>
</html>
如您所见,“footer.html”的内容已正确插入。
旧答案
方法一: Using "es6" template
npm install html-loader 安装“html-loader” module: { rules: [{ test: /\.html$/, loader: 'html-loader' }], }
interpolate为 ES6 模板字符串启用插值语法的标志,如下所示:plugins: [ new HtmlWebpackPlugin({ template: '!!html-loader?interpolate!src/template.html' }) ]
template.html匹配 ES6 模板:<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> template </body> ${require('./footer.html')} </html>
webpack , 它会起作用方法二:使用“下划线”模板
按照“方法 1”的第 1 步和第 2 步,然后:
template: './src/template.html'至 template: './src/template.html.ejs'在“webpack.config.js”中关于javascript - 在 HTML Webpack 插件中使用 partials 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47776459/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.