2017-12-11 4 views
1

에있는 webpack으로 부트 스트랩 scss 컴파일하기 CSSS 파일 대신 부트 스트랩 scss 파일을 사용하여 부트 스트랩 변수를 무시할 수 있습니다. CSS 파일을 사용하지만 scss 파일을 사용하지 않고 스타일을로드 할 수 있습니다. 저는 Visual Studio 2017 Angular 템플릿에서 생성 된 .NET Core Angular 프로젝트에서 그렇게하고 있습니다.닷넷 코어 각도 템플릿

나는 여기에 답을 시도했지만 운이 없었습니다.

  • webpack.config.vendor.js 파일 vendor.js 및 vendor.css를 생성

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

    여기 그것이 작동하는 방법에 대한 이해이다. vendor.js가 main-client.js와 분리되어 있기 때문에 공급 업체 종속성이 변경 될 때만 빌드하면 빌드 프로세스가 개선되고 프로덕션 게시에 필요할 때 캐시 무효화 만 수행하면됩니다.

  • CSS 파일 대신 bootstrap의 scss 파일을 컴파일하려는 경우 webpack.config.vendor.js의 bootstrap.css 참조가 제거되어야하며 scss 규칙이 다음과 같이 추가되어야합니다. webpack.config.js.

  • Webpack.config.js는 각 구성 요소에 필요한 모든 js 코드가 포함 된 main-client.js 파일을 생성합니다. 그리고 설정에서 scss 규칙을 inluding함으로써, 그것은 또한 자바 스크립트를 통해 html 헤더에 주입 된 스타일로 부트 스트랩 scss 파일을 컴파일해야합니다.

  • 내 응용 프로그램의 styles.scss 파일은 webpack.config.js에서 처리하며 부트 스트랩 scss 파일이 빌드되도록 @import "~ bootstrap/scss/bootstrap"을 가져야합니다. 궁극적으로 나는 변수를 무시하는 내 자신의 _custom.scss 파일을 가져올 수도 있습니다.

내 이해가 맞습니까?

또한, webpack.config.js가 내 style.scss 파일 (이미 bootstrap.scss를 가져 오는 파일) 외에도 node-modules 폴더 아래에서 bootstrap scss 파일을 처리하는 것을 어떻게 유지하는지 알지 못합니다. webpack.js에서 정의해야 할 사항입니까?

여기 내 webpack.config.js입니다. scss 규칙은 부트 스트랩 문서에서 바로 볼 수 있습니다.

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]; 
}; 

여기 내 webpack.config.vendor.js입니다. 나는이 질문을 게시 자마자, 나는 그것을 알아낼 물론

//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]; 
} 

답변

2

부트 스트랩/DIST/CSS/bootstrap.css을 주석했다.

문제점은 내 진입 점 모듈 boot.browser.ts에서 style.scss를 가져와야한다는 것이 었습니다. 또한 webpack이 styles.scss 만 컴파일하는 방법과 모든 bootstrap.scss 파일을 컴파일하는 방법에 대한 질문에 대답합니다.

관련 문제