我试图自动化资产进入/dist。我有以下config.js:
module.exports = {
context: __dirname + "/lib",
entry: {
main: [
"./baa.ts"
]
},
output: {
path: __dirname + "/dist",
filename: "foo.js"
},
devtool: "source-map",
module: {
loaders: [
{
test: /\.ts$/,
loader: 'awesome-typescript-loader'
},
{ test: /\.css$/, loader: "style-loader!css-loader" }
]
},
resolve: {
// you can now require('file') instead of require('file.js')
extensions: ['', '.js', '.json']
}
}
我还想包括main.html从目录,坐在旁边/lib,到/dist文件夹时运行webpack。我该怎么做呢?
更新1 2017_____________
现在我最喜欢的方法是使用带有模板文件的html-webpack-plugin。也感谢公认的答案!这种方法的优点是,索引文件也将有cachbusted js链接添加开箱即用!
我将在VitalyB的答案中添加一个选项:
选项3
Via npm. If you run your commands via npm, then you could add this setup to your package.json (check out also the webpack.config.js there too). For developing run npm start, no need to copy index.html in this case because the web server will be run from the source files directory, and the bundle.js will be available from the same place (the bundle.js will live in memory only but will available as if it was located together with index.html). For production run npm run build and a dist folder will contain your bundle.js and index.html gets copied with good old cp-command, as you can see below:
"scripts": {
"test": "NODE_ENV=test karma start",
"start": "node node_modules/.bin/webpack-dev-server --content-base app",
"build": "NODE_ENV=production node node_modules/.bin/webpack && cp app/index.html dist/index.html"
}
更新:选项4
有一个copy-webpack-plugin,在这个Stackoverflow的答案中描述
但一般来说,除了非常“第一个”文件(如index.html)和更大的资产(如大图像或视频),包括css, html,图像等直接在你的应用程序通过require和webpack将包括它为你(好吧,在你设置正确的加载器和可能的插件)。
选项1
在你的index.js文件(即webpack条目)中,通过文件加载器插件在index.html中添加一个require,例如:
require(“file-loader ? name = [name]。[请拨分机!’…- index . html);
一旦你用webpack构建了你的项目,index.html就会在输出文件夹中。
选项2
使用html-webpack-plugin来避免使用index.html。简单地让webpack为你生成文件。
在这种情况下,如果你想保留自己的index.html文件作为模板,你可以使用这样的配置:
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
// ...
plugins: [
new HtmlWebpackPlugin({
template: 'src/index.html'
})
]
}
更多信息请参见文档。
我将在VitalyB的答案中添加一个选项:
选项3
Via npm. If you run your commands via npm, then you could add this setup to your package.json (check out also the webpack.config.js there too). For developing run npm start, no need to copy index.html in this case because the web server will be run from the source files directory, and the bundle.js will be available from the same place (the bundle.js will live in memory only but will available as if it was located together with index.html). For production run npm run build and a dist folder will contain your bundle.js and index.html gets copied with good old cp-command, as you can see below:
"scripts": {
"test": "NODE_ENV=test karma start",
"start": "node node_modules/.bin/webpack-dev-server --content-base app",
"build": "NODE_ENV=production node node_modules/.bin/webpack && cp app/index.html dist/index.html"
}
更新:选项4
有一个copy-webpack-plugin,在这个Stackoverflow的答案中描述
但一般来说,除了非常“第一个”文件(如index.html)和更大的资产(如大图像或视频),包括css, html,图像等直接在你的应用程序通过require和webpack将包括它为你(好吧,在你设置正确的加载器和可能的插件)。