繁体   English   中英

html 模板中的 Webpack static 文件引用

[英]Webpack static file references in html template

我似乎被困住了。 这可能是一个以前被问过一百万次的问题,但我什至不知道要搜索什么才能得到答案。 我提前道歉。

我有一个 Webpack 4 VueJS 2 设置。 我有点工作。 Stuff 可以编译,Webpack-dev-server 可以向我展示我的大部分站点,并包含很多我期望的预期行为。 但是......我似乎无法让 Webpack 将文件注入到我的 html 模板中。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Kollecto</title>
    <link rel="icon" type="image/png" sizes="32x32" href="static/img/favicon-32x32.png">
    <link rel="icon" type="image/png" sizes="16x16" href="static/img/favicon-16x16.png">
    <!--[if IE]><link rel="shortcut icon" href="/static/img/icons/favicon.ico"><![endif]-->
    <!-- Add to home screen for Android and modern mobile browsers -->
    <link rel="manifest" href="static/manifest.json">
    <meta name="theme-color" content="#ffffff">

    <!-- Add to home screen for Safari on iOS -->
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="white">
    <meta name="apple-mobile-web-app-title" content="Kollecto">
    <link rel="apple-touch-icon" href="static/img/icons/apple-touch-icon-152x152.png">
    <!-- <link rel="mask-icon" href="<%= htmlWebpackPlugin.files.publicPath %>static/img/icons/safari-pinned-tab.svg" color="#ffffff"> -->
    <!-- Add to home screen for Windows -->
    <!-- <meta name="msapplication-TileImage" content="<%= htmlWebpackPlugin.files.publicPath %>static/img/icons/msapplication-icon-144x144.png"> -->
    <meta name="msapplication-TileColor" content="#ffffff">
    <!-- <% for (var chunk of webpack.chunks) {
        for (var file of chunk.files) {
        if (file.match(/\.(js|css)$/)) { %> -->
    <!-- <link rel="<%= chunk.initial?'preload':'prefetch' %>" href="<%= htmlWebpackPlugin.files.publicPath + file %>" as="<%= file.match(/\.css$/)?'style':'script' %>"><% }}} %> -->
</head>
<body>
    <style>
        <!-- inline styles.... -->
    </style>
    <noscript>
        This is your fallback content in case JavaScript fails to load.
    </noscript>
    <div id="app">
    <div class="spinner-container">
        <svg class="spinner" width="65px" height="65px" viewBox="0 0 66 66" xmlns="http://www.w3.org/2000/svg">
        <circle class="path" fill="none" stroke-width="6" stroke-linecap="round" cx="33" cy="33" r="30"></circle>
        </svg>
    </div>
    </div>
    <!-- Todo: only include in production -->
    <!-- <%= htmlWebpackPlugin.options.serviceWorkerLoader %> -->
    <!-- built files will be auto injected -->
</body>
</html>

这是从 Webpack 3 迁移到包含 VueJS 的 4 的尝试。 这是我的webpack.common.js

'use strict';

const helpers = require('./helpers');
const path = require('path');

const VueLoaderPlugin = require('vue-loader/lib/plugin')

module.exports = {
    entry: {
        polyfill: '@babel/polyfill',
        main: path.resolve(__dirname, '../src/main.js'),
        vendor: path.resolve(__dirname, '../src/vendor.js')
    },
    module: {
        rules: [
            {
                test: /\.vue$/,
                loader: 'vue-loader',
                include: [helpers.root('src')]
            },
            {
                test: /\.html$/,
                use: [
                    'html-loader'
                ]
            },
            {
                test: /\.(svg|jpe?g|gif|png)$/,
                use: {
                    loader: 'file-loader',
                    options: {
                        name: '[name].[hash].[ext]',
                        outputPath: 'static/img'
                    }
                }
            },
            {
                test: /\.(ttf|eot|woff2?|otf)$/,
                use: {
                    loader: 'file-loader',
                    options: {
                        name: '[name].[hash].[ext]',
                        outputPath: 'static/fonts'
                    }
                }
            },
            {
                test: /\.ico$/,
                use: {
                    loader: 'file-loader',
                    options: {
                        name: '[name].[hash].[ext]',
                        outputPath: 'static/img/icons'
                    }
                }
            },
        ]
    },
    plugins: [
        new VueLoaderPlugin(),
    ]
};

当然还有我的webpack.dev.js

'use strict';

const webpack = require('webpack');
const path = require('path');
const common = require('./webpack.common');
const merge = require('webpack-merge');
const fs = require('fs')
const helpers = require('./helpers');

const HtmlWebpackPlugin = require('html-webpack-plugin');
const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin');

module.exports = merge(common, {
    mode: "development",
    resolve: {
        extensions: [ '.js', '.vue' ],
        alias: {
            'vue$':'vue/dist/vue.runtime.js',
            '@': helpers.root('src')
        }
    },
    devServer: {
        port: 9000,
        hot: true,
        open: true,
        overlay: true,
        stats: {
            normal: true
        }
    },
    output: {
        filename: `[name].bundle.js`,
        path: path.resolve(__dirname, "dist")
    },
    optimization: {
        runtimeChunk: 'single',
        splitChunks: {
            chunks: "all"
        }
    },
    plugins: [
        new HtmlWebpackPlugin({
            filename: helpers.root('index.html'),
            template: helpers.root('index.html'),
            inject: true,
            serviceWorkerLoader: `<script>${fs.readFileSync(path.join(__dirname,
                './service-worker-dev.js'), 'utf-8')}</script>`
        }),
        new webpack.EnvironmentPlugin({NODE_ENV: 'development'}),
        new webpack.HotModuleReplacementPlugin(),
        new FriendlyErrorsPlugin()
    ],
    module: {
        rules: [
            {
                test: /\.(scss|sass)$/,
                use: [
                    "style-loader", 
                    "css-loader", 
                    "sass-loader"
                ]
            },
            {
                test: /\.css$/,
                use: [
                    'style-loader',
                    'css-loader'
                ]
            }
        ]
    }
});

我希望有人有比我更敏锐的眼睛。 因为我似乎无法将图标文件和脚本注入到我的模板中。 我知道index.html被评论和淘汰等。但这就是我所拥有的。 我需要帮助。 请:) 如果需要,可以提供更多代码。 (我遵循的指南: https://medium.com/js-dojo/how-to-configure-webpack-4-with-vuejs-a-complete-guide-209e943c4772

事实证明,除非明确告知,否则 webpack 会完全忽略项目中的static文件夹。 这导致我将CopyWebpackPlugin添加到我的 prod/staging 构建中,如下所示:

new CopyWebpackPlugin([
    {
        from: path.resolve(__dirname, '../static'),
        to: 'static',
        ignore: ['.*']
    }
])

这解决了我的问题。 从那里我所要做的就是让我的加载器和捆绑包寻找正确的文件。 static 文件夹是应用程序根目录中 static 文件夹的一对一副本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM