gpt4 book ai didi

webpack - 如何使用 webpack 将所有内容整合到一个文件中

转载 作者:行者123 更新时间:2023-12-04 03:11:35 25 4
gpt4 key购买 nike

我需要在一个文件中创建所有内容。
我正在编写的网页将从我无法访问相对(和绝对)路径的地方提供。
所以我需要将 js 放在 index.html 文件中的标签之间。

我从 vue webpack 模板开始,将其修改为所以我只有两个文件 - js 和 html 文件。
我可以使用 sed 等自动化它,但有更好的方法吗?

最佳答案

你可以使用 webpack 插件 web-webpack-plugin , script-ext-html-webpack-pluginhtml-webpack-plugin

html-webpack-plugin 和 extract-text-webpack-plugin 方式

webpack 配置:

sample https://github.com/jantimon/html-webpack-plugin/blob/master/examples/inline/webpack.config.js

var path = require('path');
var HtmlWebpackPlugin = require('../..');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var webpackMajorVersion = require('webpack/package.json').version.split('.')[0];

module.exports = {
context: __dirname,
entry: './example.js',
output: {
path: path.join(__dirname, 'dist/webpack-' + webpackMajorVersion),
publicPath: '',
filename: 'bundle.js'
},
module: {
loaders: [
{ test: /\.css$/, loader: ExtractTextPlugin.extract('style-
loader', 'css-loader') }
]
},
plugins: [
new HtmlWebpackPlugin({
inject: false,
cache: false,
filename: 'index.html',
favicon: 'favicon.ico',
title: 'demo'
}),
new ExtractTextPlugin('styles.css')
]
};

web-webpack-plugin 方式

webpack 配置:
module.exports = {
entry: {
A: './a',
B: './b',
},
plugins: [
new WebPlugin({
// file name or full path for output file, required.
// pay attention not to duplication of name,as is will cover
other file
filename: 'index.html',
// this html's requires entry,must be an array.dependent resource will inject into html use the order entry in array.
requires: {
'inline': {
_inline: true,
_dist: true
}
}
}),
]
};

关于webpack - 如何使用 webpack 将所有内容整合到一个文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44839837/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com