Webpack 4-模块解析失败:您可能需要适当的加载器来处理此文件类型

时间:2019-05-25 08:19:03

标签: javascript webpack webpack-4

我刚刚开始使用升级后的模板将我的aspdotnet核心Aurelia项目升级到3.0,该模板用于创建项目。

我将我的文件(在上一个项目中可用)放入这个新版本中,结果导致webpack出现了一系列错误,第一个错误概述在标题中。

Webpack也已升级。这是我收到的第一个错误。

ERROR in ./ClientApp/public/public/public.css 1:0
    Module parse failed: Unexpected character '@' (1:0)
    You may need an appropriate loader to handle this file type.
    > @media (max-width: 767px) {
    |     /* On small screens, the nav menu spans the full width of the screen. Leave a space for it. */
    |     .body-content {
     @ ./ClientApp/public/public/public.html
     @ ./ClientApp/boot.ts
     @ ./node_modules/aurelia-webpack-plugin/runtime/empty-entry.js
     @ multi aurelia-webpack-plugin/runtime/empty-entry aurelia-webpack-plugin/runtime/pal-loader-entry es6-promise/auto aurelia-bootstrapper

找到了same problem,因为事实证明我没有加倍,所以还有其他东西。

我还检查了this问题,该问题的标题非常相似,但是我相信webpack文件可以正常工作,因为它适用于基本的aurelia文件。

This问题,尽管类似,但它有一个webpack开发文件,因此略有设置(可能会出现)。为此,这里是我的package.json,其脚本位于顶部:

{
    "name": "Jobsledger.API",
    "private": true,
    "version": "0.0.0",
    "scripts": {
        "lint": "tslint --project tsconfig.json",
        "webpack:Debug": "webpack --mode development",
        "webpack:Release": "webpack --mode production",
        "webpack:watch": "webpack --mode development --watch"
    },
    "devDependencies": {
        "aurelia-animator-css": "^1.0.4",
        "aurelia-api": "^3.2.1",
        "aurelia-binding": "2.3.1",
        "aurelia-bootstrap": "^0.1.20",
        "aurelia-bootstrapper": "^2.3.3",
        "aurelia-dialog": "^2.0.0-rc.5",
        "aurelia-event-aggregator": "^1.0.3",
        "aurelia-fetch-client": "^1.8.2",
        "aurelia-mask": "^2.0.1",
        "aurelia-pal": "^1.8.2",
        "aurelia-router": "^1.7.1",
        "aurelia-templating": "^1.10.2",
        "aurelia-validation": "^1.4.0",
        "aurelia-webpack-plugin": "^4.0.0",
        "au-table": "^0.1.14",
        "awesome-typescript-loader": "^5.2.1",
        "bootstrap": "^4.3.1",
        "clean-webpack-plugin": "^2.0.2",
        "css-loader": "^2.1.1",
        "es6-promise": "^4.2.6",
        "fetch": "^1.1.0",
        "file-loader": "^3.0.1",
        "font-awesome": "^4.7.0",
        "html-loader": "^0.5.5",
        "html-webpack-plugin": "^3.2.0",
        "isomorphic-fetch": "^2.2.1",
        "jquery": "^3.4.1",
        "mini-css-extract-plugin": "^0.6.0",
        "node-sass": "^4.12.0",
        "optimize-css-assets-webpack-plugin": "^5.0.1",
        "popper.js": "^1.15.0",
        "postcss-loader": "^3.0.0",
        "sass-loader": "^7.1.0",
        "style-loader": "^0.23.1",
        "tether": "^1.4.6",
        "tslint": "^5.16.0",
        "ts-loader": "^6.0.1",
        "typescript": "^3.4.5",
        "url-loader": "^1.1.2",
        "velocity-animate": "^2.0.5",
        "webpack": "^4.32.2",
        "webpack-cli": "^3.3.2"
    }
}

我认为我可能需要修改webpack文件,使其重新成为加载器。.在显示webpack文件之前,这是导致第一个错误的public.css文件:

@media (max-width: 767px) {
    /* On small screens, the nav menu spans the full width of the screen. Leave a space for it. */
    .body-content {
        padding-top: 50px;
    }
}

现在是webpack.config.js文件

const path = require("path");
const webpack = require("webpack");
const { AureliaPlugin, ModuleDependenciesPlugin, GlobDependenciesPlugin } = require("aurelia-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CleanWebpackPlugin = require("clean-webpack-plugin");

const bundleOutputDir = "./wwwroot/dist";

module.exports = (env, argv) => {
    if ((!argv || !argv.mode) && process.env.ASPNETCORE_ENVIRONMENT === "Development") {
        argv = { mode: "development" };
    }
    console.log("mode =", argv.mode);
    const isDevBuild = argv.mode !== "production";
    const cssLoaders = ["css-loader", "postcss-loader"];
    const scssLoaders = [...cssLoaders, "sass-loader"];

    return [{
        target: "web",
        mode: isDevBuild ? "development" : "production",
        entry: { "app": ["es6-promise/auto", "aurelia-bootstrapper"] },
        resolve: {
            extensions: [".ts", ".js"],
            modules: ["ClientApp", "node_modules"]
        },
        output: {
            path: path.resolve(bundleOutputDir),
            // Asp.Net JavaScriptServices does not tolerate "/" in public path, see https://github.com/aspnet/JavaScriptServices/issues/1495
            publicPath: "dist/",
            filename: "[name].[hash].js",
            chunkFilename: "[name].[chunkhash].js",
            pathinfo: false
        },
        module: {
            rules: [
                { test: /\.(woff|woff2|png|eot|ttf|svg)(\?|$)/, use: { loader: "url-loader", options: { limit: 1, publicPath: "./" } } },
                { test: /\.ts$/i, include: [/ClientApp/], loader: "ts-loader" },
                { test: /\.html$/i, use: "html-loader" },
                { test: /\.css$/i, include: [/node_modules/], issuer: /\.html$/i, use: cssLoaders },
                { test: /\.css$/i, include: [/node_modules/], exclude: [/bootstrap.css$/, /font-awesome.css$/], issuer: [{ not: [{ test: /\.html$/i }] }], use: ["style-loader", ...cssLoaders] },
                { test: /\.css$/, include: [/bootstrap.css$/, /font-awesome.css$/], use: [{ loader: MiniCssExtractPlugin.loader }, ...cssLoaders] },
                { test: /\.scss$/i, issuer: /(\.html|empty-entry\.js)$/i, use: scssLoaders },
                { test: /\.scss$/i, issuer: /\.ts$/i, use: ["style-loader", ...scssLoaders] }
            ]
        },
        optimization: {
            splitChunks: {
                chunks: "all",
                // comment the following to avoid creatin a separate bundle for each npm module
                maxInitialRequests: Infinity,
                minSize: 0,
                cacheGroups: {
                    vendor: {
                        test: /[\\/]node_modules[\\/]/,
                        name(module) {
                            // get the name. E.g. node_modules/packageName/not/this/part.js
                            // or node_modules/packageName
                            const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1];

                            // npm package names are URL-safe, but some servers don't like @ symbols
                            return `npm.${packageName.replace('@', '')}`;
                        }
                    }
                }
            }
        },
        devtool: isDevBuild ? "source-map" : false,
        performance: {
            hints: false
        },
        plugins: [
            new CleanWebpackPlugin(),
            new webpack.DefinePlugin({ IS_DEV_BUILD: JSON.stringify(isDevBuild) }),
            new webpack.ProvidePlugin({ $: "jquery", jQuery: "jquery", "window.jQuery": "jquery" }),
            new HtmlWebpackPlugin({ template: 'index.ejs', filename: "../../wwwroot/index.html", inject: false, metadata: {}, alwaysWriteToDisk: true }),
            new AureliaPlugin({ aureliaApp: "boot" }),
            new GlobDependenciesPlugin({ "boot": ["ClientApp/**/*.{ts,html}"] }),
            new ModuleDependenciesPlugin({}),
            new MiniCssExtractPlugin({
                filename: "[name].[hash].css",
                chunkFilename: "[name].[chunkhash].css"
            })
        ],
        devServer: {
            contentBase: "wwwroot/",
            compress: true,
            writeToDisk: true,
            hot: false
        }
    }];
};

对于所有其他错误,我基本上得到了相同的错误:

You may need an appropriate loader to handle this file type.

所以它是某种类型的加载器。

我不确定我需要对webpack文件做些什么。

1 个答案:

答案 0 :(得分:0)

您的CSS媒体查询语法错误。

您需要这样写:

@media {thing-you-want-to-query} and {case} {
    .body-content {
        padding-top: 50px;
    }

在您的情况下,它将是屏幕:

@media screen and (max-width: 767px) {
    .body-content {
        padding-top: 50px;
    }