2017-12-11 5 views
1

Ich versuche, die Bootstrap-SCS-Dateien anstelle der CSS-Datei zu verwenden, damit ich die Bootstrap-Variablen überschreiben kann. Ich kann die Stile mit der CSS-Datei laden, aber nicht mit SCS-Dateien. Ich mache das in einem .NET Core Angular-Projekt, das aus der Visual Studio 2017 Angular-Vorlage erstellt wurde.Kompilieren Bootstrap Scss mit Webpack in .NET Kern Angular Template

Ich habe die Antworten hier versucht, aber hatten kein Glück.

Spa template .net core 2.0 angular 4 webpack 2 use sass not boostrap 4 css

Hier ist mein Verständnis, wie es funktionieren sollte:

  • Die webpack.config.vendor.js Datei erzeugt vendor.js und vendor.css. Ich verstehe, dass vendor.js getrennt von main-client.js gehalten wird, so dass es nur erstellt werden muss, wenn sich die Abhängigkeiten des Anbieters ändern, der Build-Prozess verbessert wird und nur Cache-Busting ausgeführt wird, wenn es für die Produktion benötigt wird.

  • In dem Szenario, in dem ich die scss-Dateien von Bootstrap kompilieren möchte, anstatt ihre CSS-Dateien zu verwenden, erwarte ich, dass die bootstrap.css-Referenz in webpack.config.vendor.js entfernt und stattdessen eine scss-Regel hinzugefügt wird webpack.config.js.

  • Webpack.config.js generiert eine main-client.js-Datei, die den gesamten js-Code enthält, der für die eckigen Komponenten benötigt wird. Und indem man die scss-Regel in die config einbezieht, sollte sie auch die Bootstrap-scss-Dateien in Stile kompilieren, die über Javascript in den html-Header eingefügt werden.

  • Eine styles.scss-Datei in meiner App wird von webpack.config.js verarbeitet und sollte @import "~ bootstrap/scss/bootstrap" haben, damit die Bootstrap-SCS-Dateien erstellt werden. Letztendlich würde ich auch meine eigene Datei _custom.scss importieren, in der ich die Variablen überschreibe.

Ist mein Verständnis korrekt?

Ich weiß auch nicht, was webpack.config.js davon abhält, die Bootstrap-scss-Dateien im Ordner node-modules zusätzlich zu meiner style.scss-Datei zu verarbeiten (die bootstrap.scss bereits importiert). Ist das etwas, das ich in webpack.js definieren muss?

Hier ist meine webpack.config.js. Die scss-Regel stammt direkt aus der Bootstrap-Dokumentation.

const path = require('path'); 
const webpack = require('webpack'); 
const merge = require('webpack-merge'); 
const AotPlugin = require('@ngtools/webpack').AotPlugin; 
const CheckerPlugin = require('awesome-typescript-loader').CheckerPlugin; 

module.exports = (env) => { 
    // Configuration in common to both client-side and server-side bundles 
    const isDevBuild = !(env && env.prod); 
    const sharedConfig = { 
     stats: { modules: false }, 
     context: __dirname, 
     resolve: { extensions: ['.js', '.ts'] }, 
     output: { 
      filename: '[name].js', 
      publicPath: 'dist/' // Webpack dev middleware, if enabled, handles requests for this URL prefix 
     }, 
     module: { 
      rules: [ 
       { test: /\.ts$/, include: /ClientApp/, use: isDevBuild ? ['awesome-typescript-loader?silent=true', 'angular2-template-loader'] : '@ngtools/webpack' }, 
       { test: /\.html$/, use: 'html-loader?minimize=false' }, 
       { test: /\.css$/, use: ['to-string-loader', isDevBuild ? 'css-loader' : 'css-loader?minimize'] }, 
       { test: /\.(png|jpg|jpeg|gif|svg)$/, use: 'url-loader?limit=25000' }, 
       { 
        test: /\.(scss)$/, 
        use: [{ 
         loader: 'style-loader', // inject CSS to page 
        }, { 
         loader: 'css-loader', // translates CSS into CommonJS modules 
        }, { 
         loader: 'postcss-loader', // Run post css actions 
         options: { 
          plugins: function() { // post css plugins, can be exported to postcss.config.js 
           return [ 
            require('precss'), 
            require('autoprefixer') 
           ]; 
          } 
         } 
        }, { 
         loader: 'sass-loader' // compiles SASS to CSS 
        }] 
       }, 
      ] 
     }, 
     plugins: [new CheckerPlugin()] 
    }; 

    // Configuration for client-side bundle suitable for running in browsers 
    const clientBundleOutputDir = './wwwroot/dist'; 
    const clientBundleConfig = merge(sharedConfig, { 
     entry: { 'main-client': './ClientApp/boot.browser.ts' }, 
     output: { path: path.join(__dirname, clientBundleOutputDir) }, 
     plugins: [ 
      new webpack.DllReferencePlugin({ 
       context: __dirname, 
       manifest: require('./wwwroot/dist/vendor-manifest.json') 
      }), 
      new webpack.ProvidePlugin({ 
       $: 'jquery', 
       jQuery: 'jquery', 
       'window.jQuery': 'jquery', 
       Popper: ['popper.js', 'default'] 
       // In case you imported plugins individually, you must also require them here: 
       //Util: "exports-loader?Util!bootstrap/js/dist/util", 
       //Dropdown: "exports-loader?Dropdown!bootstrap/js/dist/dropdown" 
      }) 
     ].concat(isDevBuild ? [ 
      // Plugins that apply in development builds only 
      new webpack.SourceMapDevToolPlugin({ 
       filename: '[file].map', // Remove this line if you prefer inline source maps 
       moduleFilenameTemplate: path.relative(clientBundleOutputDir, '[resourcePath]') // Point sourcemap entries to the original file locations on disk 
      }) 
     ] : [ 
       // Plugins that apply in production builds only 
       new webpack.optimize.UglifyJsPlugin(), 
       new AotPlugin({ 
        tsConfigPath: './tsconfig.json', 
        entryModule: path.join(__dirname, 'ClientApp/app/app-browser.module#AppModule'), 
        exclude: ['./**/*.server.ts'] 
       }) 
      ]) 
    }); 

    // Configuration for server-side (prerendering) bundle suitable for running in Node 
    const serverBundleConfig = merge(sharedConfig, { 
     resolve: { mainFields: ['main'] }, 
     entry: { 'main-server': './ClientApp/boot.server.ts' }, 
     plugins: [ 
      new webpack.DllReferencePlugin({ 
       context: __dirname, 
       manifest: require('./ClientApp/dist/vendor-manifest.json'), 
       sourceType: 'commonjs2', 
       name: './vendor' 
      }) 
     ].concat(isDevBuild ? [] : [ 
      // Plugins that apply in production builds only 
      new AotPlugin({ 
       tsConfigPath: './tsconfig.json', 
       entryModule: path.join(__dirname, 'ClientApp/app/app-server.module#AppModule'), 
       exclude: ['./**/*.browser.ts'] 
      }) 
     ]), 
     output: { 
      libraryTarget: 'commonjs', 
      path: path.join(__dirname, './ClientApp/dist') 
     }, 
     target: 'node', 
     devtool: 'inline-source-map' 
    }); 

    return [clientBundleConfig, serverBundleConfig]; 
}; 

Hier ist meine webpack.config.vendor.js. Ich habe auf Kommentar Bootstrap/dist/css/bootstrap.css

//Run from Command Line: webpack --config webpack.config.vendor.js 

const path = require('path'); 
const webpack = require('webpack'); 
const ExtractTextPlugin = require('extract-text-webpack-plugin'); 
const merge = require('webpack-merge'); 
const treeShakableModules = [ 
    '@angular/animations', 
    '@angular/common', 
    '@angular/compiler', 
    '@angular/core', 
    '@angular/forms', 
    '@angular/http', 
    '@angular/platform-browser', 
    '@angular/platform-browser-dynamic', 
    '@angular/router', 
    'zone.js', 
]; 
const nonTreeShakableModules = [ 
    'bootstrap', 
    //'bootstrap/dist/css/bootstrap.css', 
    'ag-grid/dist/styles/ag-grid.css', 
    'ag-grid/dist/styles/ag-theme-blue.css', 
    'es6-promise', 
    'es6-shim', 
    'event-source-polyfill', 
    'jquery', 
]; 
const allModules = treeShakableModules.concat(nonTreeShakableModules); 

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' } 
      ] 
     }, 
     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, { 
     entry: { 
      // To keep development builds fast, include all vendor dependencies in the vendor bundle. 
      // But for production builds, leave the tree-shakable ones out so the AOT compiler can produce a smaller bundle. 
      vendor: isDevBuild ? allModules : nonTreeShakableModules 
     }, 
     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'] }, 
     entry: { vendor: allModules.concat(['aspnet-prerendering']) }, 
     output: { 
      path: path.join(__dirname, 'ClientApp', 'dist'), 
      libraryTarget: 'commonjs2', 
     }, 
     module: { 
      rules: [{ test: /\.css(\?|$)/, use: ['to-string-loader', isDevBuild ? 'css-loader' : 'css-loader?minimize'] }] 
     }, 
     plugins: [ 
      new webpack.DllPlugin({ 
       path: path.join(__dirname, 'ClientApp', 'dist', '[name]-manifest.json'), 
       name: '[name]_[hash]' 
      }) 
     ] 
    }); 

    return [clientBundleConfig, serverBundleConfig]; 
} 

Antwort

2

Natürlich sobald ich die Frage posten, ich es herausfinden.

Das Problem war, dass ich die style.scss in meinem Einstiegspunktmodul boot.browser.ts importieren musste. Das beantwortet auch die Frage, wie webpack nur die styles.scss und nicht alle bootstrap.scss-Dateien kompilieren kann.