【发布时间】:2019-05-25 18:52:05
【问题描述】:
我使用 Vue Cli 创建了一个项目 typescript 项目。
我想要一个单独的 commands.html 文件,其中应包含来自 commands.ts 文件的 javascript 代码。
我把commands.html文件放在public文件夹下,所以在构建项目的时候复制到dist文件夹下。
commands.ts 文件位于根文件夹中(与 public、src、node_modules 等文件夹位于同一文件夹中)。
Webpack 应该做以下事情:
- 缩小
commands.html文件(就像缩小index.html一样) - 将
commands.ts文件编译成JavaScript - 将生成的JavaScript文件放到
dist/js文件夹中 - 在指向生成的 JavaScript 文件的
commands.html文件中放置一个脚本标签
我尝试了以下方法:
我安装了html-webpack-plugin
我使用以下代码创建了一个vue.config.js 文件:
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
configureWebpack: {
entry: {
commands: './commands.ts',
},
plugins: [
new HtmlWebpackPlugin({
filename: 'commands.html',
template: './public/commands.html',
chunks: ['commands'],
minify: {
collapseWhitespace: true,
},
}),
],
},
};
这一切都是正确的,但它还在index.html 文件中创建了一个脚本标记,该标记指向生成的JavaScript 文件。我认为 Vue 的默认 Webpack 配置正在编译注入到 index.html 文件中的包中的 commands.ts。我怎样才能阻止它这样做?
【问题讨论】:
-
您是否尝试过此处示例中使用的“文件”选项:github.com/jantimon/html-webpack-plugin#options 您可以指定哪些 JS 文件或任何文件,可能会有所帮助。
标签: vue.js webpack vuejs2 vue-cli