【问题标题】:Angular2 Webpack assets and global styles.cssAngular2 Webpack 资产和全局样式.css
【发布时间】:2016-12-23 19:42:19
【问题描述】:

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

  • 在应用范围内使用全局 style.css
  • 将我的 assets 文件夹复制到 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

我的项目结构:

我的两个 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 在我的应用程序中访问它们

【问题讨论】:

  • 我使用了这个webback starter link。该项目有src/assets 文件夹,在构建时复制到dist/assets 文件夹。对于全局样式,它使用角度特征:@Component({ selector: 'app', encapsulation:ViewEncapsulation.None, styleUrls: ['./app.component.scss'],
  • Nikolai,我不能使用其他首发,我的团队已经做出了选择,我无法干预......

标签: angular webpack assets


【解决方案1】:

正在查看一些 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' }
  ]),
...
]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-05
    • 2019-12-08
    • 2018-05-01
    • 2023-03-08
    • 2020-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多