2016-05-24 3 views
1

Я использую угловое семя AngularClass. У меня есть 1 файл application.scss, который я пытаюсь экспортировать в файл css через webpack и плагин extract-text-webpack. Я следую странице таблицы стилей webpack, найденной здесь: http://webpack.github.io/docs/stylesheets.html#separate-css-bundleextract-text-webpack-plugin трудности с получением файла .css-файла

, но я не могу заставить мой файл .css появляться в папке/dist после запуска webpack.

У кого-нибудь есть идеи?

Я включил мой конфигурационный файл для 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:{ 
 
\t path: helpers.root('dist'), 
 
\t filename: '[name].bundle.js', 
 
\t sourceMapFilename: '[name].map' 
 
\t 
 
    }, 
 
    /* 
 
    * 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' 
 
     }, 
 
\t { 
 
\t \t test: /\.(png|jpg)$/, 
 
\t \t loader: 'url-loader?limit=8192' 
 
\t }, 
 
     { 
 
     test: /\.scss$/, 
 
     loader: ExtractTextPlugin.extract('style-loader', 'css-loader!sass-loader') 
 
     }, 
 
     { 
 
     test: /\.css$/, 
 
     loader: ExtractTextPlugin.extract('style-loader', 'css-loader') 
 
     }, 
 
\t { 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: [ 
 
\t \t new ExtractTextPlugin("[name].css"), 
 
\t 
 
    /* 
 
    * 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' 
 
    }]), 
 
\t */ 
 

 

 
    /* 
 
    * 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 
 
    } 
 

 
};

ответ

1

Я исправил свою проблему. Я не понимаю, что мне нужно включать исходный путь моего файла SCSS, так как это было несколько папок глубоко, так вот что я добавил к моему webpack.config.js

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

в разделе входа.

0

Я использую стиль! CSS! SASS погрузчики в расслоении моих C и 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 файл.

+0

Вот почему я использую ExtractTextPlugin, хотя я не хочу, чтобы мой css в моем файле main.js мне нужен отдельный файл application.css. –

+0

Я вижу. Я отредактировал свой ответ, используя экстракт с конфигурацией, которая работает для меня. Добавление 'new ExtractPlugin ('main.css')' в ваш раздел плагинов должно извлечь ваш css в файл main.css. Извините за задержку. – Kadoo

+0

Спасибо - см. Мой ответ выше. Я исправил свою проблему, и она не была связана с плагином. У меня не было правильного каталога для моего файла SASS. –

Смежные вопросы