从生成的 Microsoft SPA 模板 VS2017 webpack 中删除特定供应商 css

Removing specific vendor css from generated Microsoft SPA Template VS2017 webpack

我已经使用 Microsoft 模板创建了几个 SPA 项目。

dotnet new --install Microsoft.AspNetCore.SpaTemplates::*

然后运行:

dotnet new angular

dotnet new aurelia

两个项目都生成名为 webpack.config.vendor.js 的文件,如下所示(来自 angular 示例):

const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const merge = require('webpack-merge');

module.exports = (env) => {
    const extractCSS = new ExtractTextPlugin('vendor.css');
    const isDevBuild = !(env && env.prod);
    const sharedConfig = {
        stats: { modules: false },
        resolve: { extensions: [ '.js' ] },
        module: {
            rules: [
                { test: /\.(png|woff|woff2|eot|ttf|svg)(\?|$)/, use: 'url-loader?limit=100000' }
            ]
        },
        entry: {
            vendor: [
                '@angular/animations',
                '@angular/common',
                '@angular/compiler',
                '@angular/core',
                '@angular/forms',
                '@angular/http',
                '@angular/platform-browser',
                '@angular/platform-browser-dynamic',
                '@angular/router',
                'bootstrap',
                'bootstrap/dist/css/bootstrap.css',
                'es6-shim',
                'es6-promise',
                'event-source-polyfill',
                'jquery',
                'zone.js',
            ]
        },
        output: {
            publicPath: '/dist/',
            filename: '[name].js',
            library: '[name]_[hash]'
        },
        plugins: [
            new webpack.ProvidePlugin({ $: 'jquery', jQuery: 'jquery' }), // Maps these identifiers to the jQuery package (because Bootstrap expects it to be a global variable)
            new webpack.ContextReplacementPlugin(/\@angular\b.*\b(bundles|linker)/, path.join(__dirname, './ClientApp')), // Workaround for https://github.com/angular/angular/issues/11580
            new webpack.ContextReplacementPlugin(/angular(\|\/)core(\|\/)@angular/, path.join(__dirname, './ClientApp')), // Workaround for https://github.com/angular/angular/issues/14898
            new webpack.IgnorePlugin(/^vertx$/) // Workaround for https://github.com/stefanpenner/es6-promise/issues/100
        ]
    };

    const clientBundleConfig = merge(sharedConfig, {
        output: { path: path.join(__dirname, 'wwwroot', 'dist') },
        module: {
            rules: [
                { test: /\.css(\?|$)/, use: extractCSS.extract({ use: isDevBuild ? 'css-loader' : 'css-loader?minimize' }) }
            ]
        },
        plugins: [
            extractCSS,
            new webpack.DllPlugin({
                path: path.join(__dirname, 'wwwroot', 'dist', '[name]-manifest.json'),
                name: '[name]_[hash]'
            })
        ].concat(isDevBuild ? [] : [
            new webpack.optimize.UglifyJsPlugin()
        ])
    });

    const serverBundleConfig = merge(sharedConfig, {
        target: 'node',
        resolve: { mainFields: ['main'] },
        output: {
            path: path.join(__dirname, 'ClientApp', 'dist'),
            libraryTarget: 'commonjs2',
        },
        module: {
            rules: [ { test: /\.css(\?|$)/, use: ['to-string-loader', isDevBuild ? 'css-loader' : 'css-loader?minimize' ] } ]
        },
        entry: { vendor: ['aspnet-prerendering'] },
        plugins: [
            new webpack.DllPlugin({
                path: path.join(__dirname, 'ClientApp', 'dist', '[name]-manifest.json'),
                name: '[name]_[hash]'
            })
        ]
    });

    return [clientBundleConfig, serverBundleConfig];
}

现在我想从等式中删除 Bootstrap。我尝试从 entry->bootstrap:

中删除以下行
'bootstrap',
'bootstrap/dist/css/bootstrap.css',

但是bootstrap还是出现在最后的vendor.css。我仍在(慢慢地)跟上 WebPack 的步伐。

我需要做什么才能从 vendor.css 输出中删除 bootstrap

在您的根文件夹中 package.json。

添加

  "scripts": {
    "build": "npm install && npm run webpack",
    "webpack": "webpack --config webpack.config.vendor.js && webpack"
  },

在webpack.config.vendor.js

        entry: {
            vendor: [
                'aurelia-event-aggregator',
                'aurelia-fetch-client',
                'aurelia-framework',
                'aurelia-history-browser',
                'aurelia-logging-console',
                'aurelia-pal-browser',
                'aurelia-polyfills',
                'aurelia-route-recognizer',
                'aurelia-router',
                'aurelia-templating-binding',
                'aurelia-templating-resources',
                'aurelia-templating-router',
                **//Remove the following two lines**
                'bootstrap',
                'bootstrap/dist/css/bootstrap.css',
                'jquery'
            ],
        },

然后运行下面的命令。

npm run webpack

user8454147 给出了正确的答案。但它不适用于 dotnet new angular 脚手架,例如。文件 vendor.css 只是没有重新生成。尝试这样做以使其工作:从 vendor 数组中删除这两行:

    'bootstrap',
    'bootstrap/dist/css/bootstrap.css',

并添加新行 css。这非常重要!比如我添加material design css file:

'material-design-lite/material.css',

只有在那之后运行 webpack --config webpack.config.vendor.js。魔法,vendor.css 重现。我认为这只是一个错误或类似的问题。