Angular2 Webpack资产和全局styles.css

时间:2016-12-23 19:42:20

标签: angular webpack assets

在以前的项目中,我使用angular-cli来构建我的项目。现在我必须在.NET项目中直接使用webpack。而且我正在努力让一件简单的事情发挥作用:

  • 使用全球style.css应用范围
  • 将我的资产文件夹复制到dist

我的构建输出目录是wwwroot / dist(我们的项目要求)。

Angular-cli有一个很好的简化"配置:

"outDir": "../wwwroot/dist",
"assets": [
        "assets"
      ],
"styles": [        
        "styles.css"
      ],

然后我用一个正确的dist文件夹结束,我的资产移动到正确的输出目录,我的应用程序级别的styles.css在我的应用程序的任何地方都可用。

现在,我无法管理如何配置webpack以在新项目结构和配置中实现此功能。我认为我的团队使用VS2015模板启动了项目,该模板生成了一个通用结构:https://github.com/aspnet/JavaScriptServices/tree/dev/templates/Angular2Spa

我的项目结构:

Project Structure

我的两个webpack配置文件

  

webpack.config.js

var isDevBuild = process.argv.indexOf('--env.prod') < 0;
var path = require('path');
var webpack = require('webpack');
var nodeExternals = require('webpack-node-externals');
var merge = require('webpack-merge');
var allFilenamesExceptJavaScript = /\.(?!js(\?|$))([^.]+(\?|$))/;

// Configuration in common to both client-side and server-side bundles
var sharedConfig = {
    resolve: { extensions: [ '', '.js', '.ts' ] },
    output: {
        filename: '[name].js',
        publicPath: '/dist/' // Webpack dev middleware, if enabled, handles requests for this URL prefix
    },
    module: {
        loaders: [
            { test: /\.ts$/, include: /ClientApp/, loader: 'ts', query: { silent: true } },
            { test: /\.html$/, loader: 'raw' },
            { test: /\.css$/, loader: 'to-string!css' },
            { test: /\.(png|jpg|jpeg|gif|svg)$/, loader: 'url', query: { limit: 25000 } }
        ]
    }
};

// Configuration for client-side bundle suitable for running in browsers
var clientBundleConfig = merge(sharedConfig, {
    entry: { 'main-client': './ClientApp/boot-client.ts' },
    output: { path: path.join(__dirname, './wwwroot/dist') },
    devtool: isDevBuild ? 'inline-source-map' : null,
    plugins: [
        new webpack.DllReferencePlugin({
            context: __dirname,
            manifest: require('./wwwroot/dist/vendor-manifest.json')
        })
    ].concat(isDevBuild ? [] : [
        // Plugins that apply in production builds only
        new webpack.optimize.OccurenceOrderPlugin(),
        new webpack.optimize.UglifyJsPlugin()
    ])
});

// Configuration for server-side (prerendering) bundle suitable for running in Node
var serverBundleConfig = merge(sharedConfig, {
    entry: { 'main-server': './ClientApp/boot-server.ts' },
    output: {
        libraryTarget: 'commonjs',
        path: path.join(__dirname, './ClientApp/dist')
    },
    target: 'node',
    devtool: 'inline-source-map',
    externals: [nodeExternals({ whitelist: [allFilenamesExceptJavaScript] })] // Don't bundle .js files from node_modules
});

module.exports = [clientBundleConfig, serverBundleConfig];

还有一个供应商配置可以单独构建供应商并更少地刷新它们。我不希望我的资产和全局style.css成为供应商的一部分。

  

webpack.config.vendor.js

var isDevBuild = process.argv.indexOf('--env.prod') < 0;
var path = require('path');
var webpack = require('webpack');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var extractCSS = new ExtractTextPlugin('vendor.css');

module.exports = {
    resolve: {
        extensions: [ '', '.js' ]
    },
    module: {
        loaders: [
            { test: /\.(png|woff|woff2|eot|ttf|svg)(\?|$)/, loader: 'url-loader?limit=100000' },
            { test: /\.css(\?|$)/, loader: extractCSS.extract(['css']) }
        ]
    },
    entry: {
        vendor: [
            '@angular/common',
            '@angular/compiler',
            '@angular/core',
            '@angular/http',
            '@angular/platform-browser',
            '@angular/platform-browser-dynamic',
            '@angular/router',
            '@angular/platform-server',
            'angular2-universal',
            'angular2-universal-polyfills',
            'bootstrap',
            'bootstrap/dist/css/bootstrap.css',
            'bootstrap-filestyle',
            'font-awesome/css/font-awesome.css',
            'es6-shim',
            'es6-promise',
            'jquery',
            'zone.js',
        ]
    },
    output: {
        path: path.join(__dirname, 'wwwroot', 'dist'),
        filename: '[name].js',
        library: '[name]_[hash]',
    },
    plugins: [
        extractCSS,
        new webpack.ProvidePlugin({ $: 'jquery', jQuery: 'jquery' }), // Maps these identifiers to the jQuery package (because Bootstrap expects it to be a global variable)
        new webpack.optimize.OccurenceOrderPlugin(),
        new webpack.DllPlugin({
            path: path.join(__dirname, 'wwwroot', 'dist', '[name]-manifest.json'),
            name: '[name]_[hash]'
        })
    ].concat(isDevBuild ? [] : [
        new webpack.optimize.UglifyJsPlugin({ compress: { warnings: false } })
    ])
};

我希望能够使用styles.css应用程序范围中定义的css,并将我的资产移到wwwroot / dist中。我想通过加载/ dist / assets / xxx

在我的应用中访问它们

1 个答案:

答案 0 :(得分:2)

正在浏览一些Webpack问题并发现了这一点。尽管它确实很旧,并且您可能找到了解决方法,但我仍然想提出一个解决方案。

Webpack将加载程序用于各种类型的文件,这些文件在代码中需要/导入。 看到您已经为css和图像指定了加载程序,我猜测您没有直接在JavaScript代码中要求/导入上述任何资产,而只是在css或html中引用它们。

在这种情况下,您可以使用copy-webpack-plugin之类的webpack插件将这些资产复制到构建目录中。 会是这样的:

//webpack.config.js

var CopyWebpackPlugin = require('copy-webpack-plugin');
...
config.plugins = config.plugins.concat([
  new CopyWebpackPlugin([
    { from: 'assets', to: '../wwwroot/dist/assets' }
  ]),
...
]);