source

vue 파일 스타일을 하나의 개별 style.css 파일로 추출하는 방법

goodcode 2022. 8. 28. 09:34
반응형

vue 파일 스타일을 하나의 개별 style.css 파일로 추출하는 방법

vue-loader를 사용하고 있으며 기본적으로는 vue 파일마다 하나의 스타일 태그가 뷰에 표시되므로 vue-loader 설명서에 따르면 이 작업을 수행할 수 있습니다.

https://vue-loader.vuejs.org/en/configurations/extract-css.html my webpack-config.filename 파일

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

module.exports = {
  entry: './src/main.js',
  output: {
    path: path.resolve(__dirname, './dist'),
    publicPath: '/dist/',
    filename: 'build.js'
  },
  module: {
    rules: [
      {
        // this one extracts css files that imported to styles.css
        test: /\.css$/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader' ,
          use: "css-loader"
        })
      },
      {
         // and this dude here extracts component's styles into styles.css
        test: /\.vue$/,
        loader: 'vue-loader',
        options: {
          loaders: {
                  css: ExtractTextPlugin.extract({
          use: 'css-loader',
          fallback: 'vue-style-loader' 
        })
          }
        }
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      },
      {
        test: /\.(png|jpg|gif|svg)$/,
        loader: 'file-loader',
        options: {
          name: '[name].[ext]?[hash]'
        }
      },
      {
       test: /\.(woff|woff2|eot|ttf|svg)(\?.*$|$)/,
       loader: 'url-loader?importLoaders=1&limit=100000'
     }
    ]
  },

      plugins: [
    new ExtractTextPlugin("styles.css"),
  ],
  resolve: {
    alias: {
      'vue$': 'vue/dist/vue.esm.js'
    }
  },
  devServer: {
    historyApiFallback: true,
    noInfo: true
  },
  performance: {
    hints: false
  },
  devtool: '#eval-source-map'
}

if (process.env.NODE_ENV === 'production') {
  module.exports.devtool = '#source-map'
  // http://vue-loader.vuejs.org/en/workflow/production.html
  module.exports.plugins = (module.exports.plugins || []).concat([
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),
    new webpack.optimize.UglifyJsPlugin({
      sourceMap: true,
      compress: {
        warnings: false
      }
    }),
    new webpack.LoaderOptionsPlugin({
      minimize: true
    })
  ])
}

하지만 이 코드는 전혀 작동하지 않고 동작도 동일합니다.

여기 당신의 문제와 유사한 문제가 하나 있습니다. 확인해 보세요.

URL(https://stackoverflow.com/a/40199096/6381510))

하지만 이게 더 낫다

 less: ExtractTextPlugin.extract({
              loader: 'css-loader!less-loader?indentedSyntax',
              fallbackLoader: 'vue-style-loader',
            }),

ExterTextPlugin을 정의 및 Import했습니까?

  plugins: [
   new ExtractTextPlugin('style.css'),
  ],

여기에 전체 코드를 입력해 주시면 감사하겠습니다.

언급URL : https://stackoverflow.com/questions/43864750/how-to-extract-vue-files-style-into-one-seperate-style-css-file

반응형