【问题标题】:extract-text-webpack-plugin difficulties getting .css file outputextract-text-webpack-plugin 难以获取 .css 文件输出
【发布时间】:2016-09-22 00:13:51
【问题描述】:

我正在使用 AngularClass 的角种子。我有 1 个 application.scss 文件,我试图通过 webpack 和 extract-text-webpack 插件将其导出到 css 文件中。我正在关注此处的 webpack 样式表页面:http://webpack.github.io/docs/stylesheets.html#separate-css-bundle

但在运行 webpack 后,我似乎无法让我的 .css 文件出现在我的 /dist 文件夹中。

有人有什么想法吗?

我在下面包含了我的 webpack 配置文件。

/**
 * @author: @AngularClass
 */

const webpack = require('webpack');
const helpers = require('./helpers');

/*
 * Webpack Plugins
 */
// problem with copy-webpack-plugin
var CopyWebpackPlugin = (CopyWebpackPlugin = require('copy-webpack-plugin'), CopyWebpackPlugin.default || CopyWebpackPlugin);
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ForkCheckerPlugin = require('awesome-typescript-loader').ForkCheckerPlugin;
var ExtractTextPlugin = require('extract-text-webpack-plugin');
/*
 * Webpack Constants
 */
const METADATA = {
  title: 'Angular2 Webpack Starter by @gdi2290 from @AngularClass',
  baseUrl: '/'
};

/*
 * Webpack configuration
 *
 * See: http://webpack.github.io/docs/configuration.html#cli
 */
module.exports = {

  /*
   * Static metadata for index.html
   *
   * See: (custom attribute)
   */
  metadata: METADATA,

  /*
   * Cache generated modules and chunks to improve performance for multiple incremental builds.
   * This is enabled by default in watch mode.
   * You can pass false to disable it.
   *
   * See: http://webpack.github.io/docs/configuration.html#cache
   * cache: false,
   *
   * The entry point for the bundle
   * Our Angular.js app
   *
   * See: http://webpack.github.io/docs/configuration.html#entry
   */
  entry: {

    'polyfills': './src/polyfills.ts',
    'vendor': './src/vendor.ts',
    'main': './src/main.browser.ts'

  },
   output:{
	 path: helpers.root('dist'),
	 filename: '[name].bundle.js',
	 sourceMapFilename: '[name].map'
	 
   },
  /*
   * Options affecting the resolving of modules.
   *
   * See: http://webpack.github.io/docs/configuration.html#resolve
   */
  resolve: {

    /*
     * An array of extensions that should be used to resolve modules.
     *
     * See: http://webpack.github.io/docs/configuration.html#resolve-extensions
     */
    extensions: ['', '.ts', '.js'],

    // Make sure root is src
    root: helpers.root('src'),

    // remove other default values
    modulesDirectories: ['node_modules'],

    alias: {
      'angular2/core': helpers.root('node_modules/@angular/core/index.js'),
      'angular2/testing': helpers.root('node_modules/@angular/core/testing.js'),
      '@angular/testing': helpers.root('node_modules/@angular/core/testing.js'),
      'angular2/platform/browser': helpers.root('node_modules/@angular/platform-browser/index.js'),
      'angular2/router': helpers.root('node_modules/@angular/router-deprecated/index.js'),
      'angular2/http': helpers.root('node_modules/@angular/http/index.js'),
      'angular2/http/testing': helpers.root('node_modules/@angular/http/testing.js')
    },

  },

  /*
   * Options affecting the normal modules.
   *
   * See: http://webpack.github.io/docs/configuration.html#module
   */
  module: {

    /*
     * An array of applied pre and post loaders.
     *
     * See: http://webpack.github.io/docs/configuration.html#module-preloaders-module-postloaders
     */
    preLoaders: [

      /*
       * Tslint loader support for *.ts files
       *
       * See: https://github.com/wbuchwalter/tslint-loader
       */
       // { test: /\.ts$/, loader: 'tslint-loader', exclude: [ helpers.root('node_modules') ] },

      /*
       * Source map loader support for *.js files
       * Extracts SourceMaps for source files that as added as sourceMappingURL comment.
       *
       * See: https://github.com/webpack/source-map-loader
       */
      {
        test: /\.js$/,
        loader: 'source-map-loader',
        exclude: [
          // these packages have problems with their sourcemaps
          helpers.root('node_modules/rxjs'),
          helpers.root('node_modules/@angular2-material'),
          helpers.root('node_modules/@angular'),
        ]
      }

    ],

    /*
     * An array of automatically applied loaders.
     *
     * IMPORTANT: The loaders here are resolved relative to the resource which they are applied to.
     * This means they are not resolved relative to the configuration file.
     *
     * See: http://webpack.github.io/docs/configuration.html#module-loaders
     */
    loaders: [

      /*
       * Typescript loader support for .ts and Angular 2 async routes via .async.ts
       *
       * See: https://github.com/s-panferov/awesome-typescript-loader
       */
      {
        test: /\.ts$/,
        loader: 'awesome-typescript-loader',
        exclude: [/\.(spec|e2e)\.ts$/]
      },

      /*
       * Json loader support for *.json files.
       *
       * See: https://github.com/webpack/json-loader
       */
      {
        test: /\.json$/,
        loader: 'json-loader'
      },
	  {
		  test: /\.(png|jpg)$/, 
		  loader: 'url-loader?limit=8192'
	  },
      {
        test: /\.scss$/,
        loader: ExtractTextPlugin.extract('style-loader', 'css-loader!sass-loader')
      },
      {
        test: /\.css$/,
        loader: ExtractTextPlugin.extract('style-loader', 'css-loader')
      },
	  { test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "url-loader?limit=10000&mimetype=application/font-woff" },
      { test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "file-loader" },
      /* Raw loader support for *.html
       * Returns file content as string
       *
       * See: https://github.com/webpack/raw-loader
       */
      {
        test: /\.html$/,
        loader: 'raw-loader',
        exclude: [helpers.root('src/index.html')]
      }

    ]

  },

  /*
   * Add additional plugins to the compiler.
   *
   * See: http://webpack.github.io/docs/configuration.html#plugins
   */
  plugins: [
		new ExtractTextPlugin("[name].css"),
	
    /*
     * Plugin: ForkCheckerPlugin
     * Description: Do type checking in a separate process, so webpack don't need to wait.
     *
     * See: https://github.com/s-panferov/awesome-typescript-loader#forkchecker-boolean-defaultfalse
     */
    new ForkCheckerPlugin(),

    /*
     * Plugin: OccurenceOrderPlugin
     * Description: Varies the distribution of the ids to get the smallest id length
     * for often used ids.
     *
     * See: https://webpack.github.io/docs/list-of-plugins.html#occurrenceorderplugin
     * See: https://github.com/webpack/docs/wiki/optimization#minimize
     */
    new webpack.optimize.OccurenceOrderPlugin(true),

    /*
     * Plugin: CommonsChunkPlugin
     * Description: Shares common code between the pages.
     * It identifies common modules and put them into a commons chunk.
     *
     * See: https://webpack.github.io/docs/list-of-plugins.html#commonschunkplugin
     * See: https://github.com/webpack/docs/wiki/optimization#multi-page-app
     */
    new webpack.optimize.CommonsChunkPlugin({
      name: ['polyfills', 'vendor'].reverse()
    }),

    /*
     * Plugin: CopyWebpackPlugin
     * Description: Copy files and directories in webpack.
     *
     * Copies project static assets.
     *
     * See: https://www.npmjs.com/package/copy-webpack-plugin
    
    new CopyWebpackPlugin([{
      from: 'src/assets',
      to: 'assets'
    }]),
	 */


    /*
     * Plugin: HtmlWebpackPlugin
     * Description: Simplifies creation of HTML files to serve your webpack bundles.
     * This is especially useful for webpack bundles that include a hash in the filename
     * which changes every compilation.
     *
     * See: https://github.com/ampedandwired/html-webpack-plugin
     */
    new HtmlWebpackPlugin({
      template: 'src/index.html',
      chunksSortMode: 'dependency'
    })

  ],

  /*
   * Include polyfills or mocks for various node stuff
   * Description: Node configuration
   *
   * See: https://webpack.github.io/docs/configuration.html#node
   */
  node: {
    global: 'window',
    crypto: 'empty',
    module: false,
    clearImmediate: false,
    setImmediate: false
  }

};

【问题讨论】:

    标签: angularjs webpack


    【解决方案1】:

    我正在使用 style!css!sass 加载器来捆绑我的 css 和 scss 文件。你可以尝试从 NPM 安装这个加载器,以防你已经没有,并删除 ExtractTextPlugin。你的 webpack.config.js 应该是这样的:

    /**
     * @author: @AngularClass
     */
    
    const webpack = require('webpack');
    const helpers = require('./helpers');
    
    /*
     * Webpack Plugins
     */
    // problem with copy-webpack-plugin
    var CopyWebpackPlugin = (CopyWebpackPlugin = require('copy-webpack-plugin'), CopyWebpackPlugin.default || CopyWebpackPlugin);
    const HtmlWebpackPlugin = require('html-webpack-plugin');
    const ForkCheckerPlugin = require('awesome-typescript-loader').ForkCheckerPlugin;
    
    /*
     * Webpack Constants
     */
    const METADATA = {
      title: 'Angular2 Webpack Starter by @gdi2290 from @AngularClass',
      baseUrl: '/'
    };
    
    /*
     * Webpack configuration
     *
     * See: http://webpack.github.io/docs/configuration.html#cli
     */
    module.exports = {
    
      /*
       * Static metadata for index.html
       *
       * See: (custom attribute)
       */
      metadata: METADATA,
    
      /*
       * Cache generated modules and chunks to improve performance for multiple incremental builds.
       * This is enabled by default in watch mode.
       * You can pass false to disable it.
       *
       * See: http://webpack.github.io/docs/configuration.html#cache
       * cache: false,
       *
       * The entry point for the bundle
       * Our Angular.js app
       *
       * See: http://webpack.github.io/docs/configuration.html#entry
       */
      entry: {
    
        'polyfills': './src/polyfills.ts',
        'vendor': './src/vendor.ts',
        'main': './src/main.browser.ts'
    
      },
       output:{
         path: helpers.root('dist'),
         filename: '[name].bundle.js',
         sourceMapFilename: '[name].map'
    
       },
      /*
       * Options affecting the resolving of modules.
       *
       * See: http://webpack.github.io/docs/configuration.html#resolve
       */
      resolve: {
    
        /*
         * An array of extensions that should be used to resolve modules.
         *
         * See: http://webpack.github.io/docs/configuration.html#resolve-extensions
         */
        extensions: ['', '.ts', '.js'],
    
        // Make sure root is src
        root: helpers.root('src'),
    
        // remove other default values
        modulesDirectories: ['node_modules'],
    
        alias: {
          'angular2/core': helpers.root('node_modules/@angular/core/index.js'),
          'angular2/testing': helpers.root('node_modules/@angular/core/testing.js'),
          '@angular/testing': helpers.root('node_modules/@angular/core/testing.js'),
          'angular2/platform/browser': helpers.root('node_modules/@angular/platform-browser/index.js'),
          'angular2/router': helpers.root('node_modules/@angular/router-deprecated/index.js'),
          'angular2/http': helpers.root('node_modules/@angular/http/index.js'),
          'angular2/http/testing': helpers.root('node_modules/@angular/http/testing.js')
        },
    
      },
    
      /*
       * Options affecting the normal modules.
       *
       * See: http://webpack.github.io/docs/configuration.html#module
       */
      module: {
    
        /*
         * An array of applied pre and post loaders.
         *
         * See: http://webpack.github.io/docs/configuration.html#module-preloaders-module-postloaders
         */
        preLoaders: [
    
          /*
           * Tslint loader support for *.ts files
           *
           * See: https://github.com/wbuchwalter/tslint-loader
           */
           // { test: /\.ts$/, loader: 'tslint-loader', exclude: [ helpers.root('node_modules') ] },
    
          /*
           * Source map loader support for *.js files
           * Extracts SourceMaps for source files that as added as sourceMappingURL comment.
           *
           * See: https://github.com/webpack/source-map-loader
           */
          {
            test: /\.js$/,
            loader: 'source-map-loader',
            exclude: [
              // these packages have problems with their sourcemaps
              helpers.root('node_modules/rxjs'),
              helpers.root('node_modules/@angular2-material'),
              helpers.root('node_modules/@angular'),
            ]
          }
    
        ],
    
        /*
         * An array of automatically applied loaders.
         *
         * IMPORTANT: The loaders here are resolved relative to the resource which they are applied to.
         * This means they are not resolved relative to the configuration file.
         *
         * See: http://webpack.github.io/docs/configuration.html#module-loaders
         */
        loaders: [
    
          /*
           * Typescript loader support for .ts and Angular 2 async routes via .async.ts
           *
           * See: https://github.com/s-panferov/awesome-typescript-loader
           */
          {
            test: /\.ts$/,
            loader: 'awesome-typescript-loader',
            exclude: [/\.(spec|e2e)\.ts$/]
          },
    
          /*
           * Json loader support for *.json files.
           *
           * See: https://github.com/webpack/json-loader
           */
          {
            test: /\.json$/,
            loader: 'json-loader'
          },
          {
            test: /\.(png|jpg)$/, 
            loader: 'url-loader?limit=8192'
          },
          {
            test: /\.scss$/,
            loader: ExtractPlugin.extract('style', 'css!sass'),
          },
          { test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "url-loader?limit=10000&mimetype=application/font-woff" },
          { test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "file-loader" },
          /* Raw loader support for *.html
           * Returns file content as string
           *
           * See: https://github.com/webpack/raw-loader
           */
          {
            test: /\.html$/,
            loader: 'raw-loader',
            exclude: [helpers.root('src/index.html')]
          }
    
        ]
    
      },
    
      /*
       * Add additional plugins to the compiler.
       *
       * See: http://webpack.github.io/docs/configuration.html#plugins
       */
      plugins: [
         /*Try using this*/
         new ExtractPlugin('main.css'),
    
         /*
         * Plugin: ForkCheckerPlugin
         * Description: Do type checking in a separate process, so webpack don't need to wait.
         *
         * See: https://github.com/s-panferov/awesome-typescript-loader#forkchecker-boolean-defaultfalse
         */
        new ForkCheckerPlugin(),
    
        /*
         * Plugin: OccurenceOrderPlugin
         * Description: Varies the distribution of the ids to get the smallest id length
         * for often used ids.
         *
         * See: https://webpack.github.io/docs/list-of-plugins.html#occurrenceorderplugin
         * See: https://github.com/webpack/docs/wiki/optimization#minimize
         */
        new webpack.optimize.OccurenceOrderPlugin(true),
    
        /*
         * Plugin: CommonsChunkPlugin
         * Description: Shares common code between the pages.
         * It identifies common modules and put them into a commons chunk.
         *
         * See: https://webpack.github.io/docs/list-of-plugins.html#commonschunkplugin
         * See: https://github.com/webpack/docs/wiki/optimization#multi-page-app
         */
        new webpack.optimize.CommonsChunkPlugin({
          name: ['polyfills', 'vendor'].reverse()
        }),
    
        /*
         * Plugin: CopyWebpackPlugin
         * Description: Copy files and directories in webpack.
         *
         * Copies project static assets.
         *
         * See: https://www.npmjs.com/package/copy-webpack-plugin
    
        new CopyWebpackPlugin([{
          from: 'src/assets',
          to: 'assets'
        }]),
         */
    
    
        /*
         * Plugin: HtmlWebpackPlugin
         * Description: Simplifies creation of HTML files to serve your webpack bundles.
         * This is especially useful for webpack bundles that include a hash in the filename
         * which changes every compilation.
         *
         * See: https://github.com/ampedandwired/html-webpack-plugin
         */
        new HtmlWebpackPlugin({
          template: 'src/index.html',
          chunksSortMode: 'dependency'
        })
    
      ],
    
      /*
       * Include polyfills or mocks for various node stuff
       * Description: Node configuration
       *
       * See: https://webpack.github.io/docs/configuration.html#node
       */
      node: {
        global: 'window',
        crypto: 'empty',
        module: false,
        clearImmediate: false,
        setImmediate: false
      }
    
    };
    

    这应该将 css 捆绑到您的主 js 文件中。

    【讨论】:

    • 这就是我使用 ExtractTextPlugin 的原因,我不希望我的 css 在我的 main.js 文件中我想要一个单独的 application.css 文件。
    • 我明白了。我使用 extract 和适合我的配置编辑了我的答案。将 new ExtractPlugin('main.css') 添加到您的插件部分应该将您的 css 提取到 main.css 文件中。抱歉耽搁了。
    • 谢谢 - 见我上面的回答。我解决了我自己的问题,它与插件无关。我的 SASS 文件没有正确的目录。
    【解决方案2】:

    我解决了我自己的问题。我没有意识到我需要包含我的 scss 文件的源路径,因为它有几个文件夹深,所以这是我添加到我的 webpack.config.js 的内容

    path.join(__dirname, 'src', 'assets', 'sass', 'application.scss')
    

    在入口部分。

    【讨论】:

      猜你喜欢
      • 2023-03-22
      • 1970-01-01
      • 2018-05-29
      • 1970-01-01
      • 1970-01-01
      • 2016-03-11
      • 2018-01-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多