【问题标题】:Issues with Migration from Webpack 1.x to Webpack 2.x从 Webpack 1.x 迁移到 Webpack 2.x 的问题
【发布时间】:2017-06-01 08:41:08
【问题描述】:

我最近尝试将我的应用程序从 Webpack 1.x 升级到 2.x (Angular@4.1.3),在完成了从 here 和 here 的所有迁移更改之后,我能够运行该应用程序,但是引导程序被完全剥离。当我检查元素时,css 类没有加载(没有出现在开发人员工具中)。是装载机的问题吗?

webpack.common.js

const webpack = require('webpack');
const helpers = require('./helpers');
const ExtractTextPlugin = require("extract-text-webpack-plugin");

const CopyWebpackPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');

const METADATA = {
  title: 'App',
  baseUrl: '.'
};

var jQuery = require('jquery');

module.exports = {
  entry: {
    'polyfills': './src/polyfills.ts',
    'vendor': './src/vendor.ts',
    'main': './src/main.browser.ts'
  },

  externals: {
    "jQuery": "jQuery"
  },
  resolve: {

    alias: {
      jquery: "jquery/dist/jquery.min"
    },

    extensions: ['.ts', '.js'],

    modules: [
      helpers.root('src'),
      'node_modules'
    ]

  },

 module: {
    rules: [
      {
        test: /\.js$/,
        enforce: "pre",
        loader: 'source-map-loader',
        exclude: [
          helpers.root('node_modules/rxjs'),
          helpers.root('node_modules/primeng')
        ]
      },
      {
        test: /\.js$/,
        loader: 'babel-loader'
      },
      {
        test: /\.ts$/,
        loader: 'awesome-typescript-loader'
      },
      {
        test: /\.css$/,
        loader: 'raw-loader'
      },
      {
        test: /\.html$/,
        loader: 'raw-loader'
        // exclude: [helpers.root('src/index.html')]
      },
      {
        test: /\.scss$/,
        exclude: /node_modules/,
        use: [
                "raw-loader",
                 "sass-loader"
               ]
      },
      {
        test: /\.(woff|woff2|ttf|eot|svg)$/,
        loader: "url-loader",
          options: {
            limit: 10000
          }
      }
    ]
  },
  plugins: [
    new webpack.LoaderOptionsPlugin({
      options: {
        metadata: METADATA,
        tslint: {
          emitErrors: false,
          failOnHint: false,
          resourcePath: 'src'
        }
      }
    }),

    new webpack.ContextReplacementPlugin(
      /angular(\\|\/)core(\\|\/)@angular/,
      helpers.root('src'),
      {}
    ),

    new webpack.ProvidePlugin({
      "window.Tether": "tether",
      _: 'lodash',
      $: "jquery",
      jQuery: "jquery"
    }),

    new webpack.optimize.CommonsChunkPlugin({
      name: helpers.reverse(['polyfills', 'vendor'])
    }),

    new ExtractTextPlugin({
      filename: "node_modules/fullcalendar/dist/fullcalendar.min.css",
      allChunks: true
    }),

    new CopyWebpackPlugin(
      [
        {
          from: 'src/server-components',
          to: 'server-components'
        },
        {
          from: 'src/assets',
          to: 'assets'
        },
        {
          from: 'src/shared-files',
          to: 'shared-files'
        }
      ]
    ),
    new HtmlWebpackPlugin({
      template: 'src/index.html',
      chunksSortMode: helpers.packageSort(['polyfills', 'vendor', 'main'])
    })

  ],
  node: {
    global: true,
    crypto: 'empty',
    module: false,
    clearImmediate: false,
    setImmediate: false
  }

};

【问题讨论】:

    标签: css angularjs angular webpack webpack-2


    【解决方案1】:

    对于 css 文件,您需要 css-loader。像这样的:

    {
      test: /\.css$/,
      loader: ExtractTextPlugin.extract(
          {
            fallback: 'style-loader',
            use: [
              {
                loader: 'css-loader',
                options: {
                  importLoaders: 1,
                },
              },
              {
                loader: 'postcss-loader',
                options: {
                  ident: 'postcss', // https://webpack.js.org/guides/migrating/#complex-options
                  plugins: () => [
                    autoprefixer({
                      browsers: [
                        '>1%',
                        'last 4 versions',
                        'Firefox ESR',
                        'not ie < 9', // React doesn't support IE8 anyway
                      ],
                    }),
                  ],
                },
              },
            ],
          }
      ),
      // Note: this won't work without `new ExtractTextPlugin()` in `plugins`.
    },
    

    总而言之,通过阅读 Create React App 的 webpack 配置,我学到了很多关于 webpack 的知识https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/config/webpack.config.prod.js

    【讨论】:

    • 我在 webpack.common.js 中做了这些更改,但是没有任何更改。 Css 或 bootstrap 仍然无法加载
    • 你需要在你的插件部分添加这个github.com/facebookincubator/create-react-app/blob/master/…,你做到了吗?
    • Angular 应用也一样吗?
    • 是的,样式加载是一样的AFAIK
    【解决方案2】:

    捆绑引导程序:

    1. create a src/styles.ts
    2. within styles.ts import the bootstrap CSS stylesheet
    

    src/styles.ts

    import 'css!./bootstrap/dist/css/bootstrap.css';
    import ... // other stylesheets
    

    Webpack.config.js

        entry: {
          app: __dirname + '/src/main.ts',
          polyfills: __dirname + '/src/polyfills.ts',
          styles: __dirname + '/src/styles.ts'
        },
        output: {
          path: path.resolve(__dirname, 'dist'),
          filename: 'bundles/[hash]-[name].bundle.js',
          publicPath: '/'
        },
    
        module: {
            loaders: [
                { test: /\.css$/, loader: "style-loader!css-loader" }
            ]
        },
        plugins: [
            new CommonsChunkPlugin({
               names: [ "app", "polyfills", "styles"],
               minChunks: Infinity }),
            new HtmlWebpackPlugin({
               filename: __dirname + '/dist/index.html',
               template: __dirname + '/src/index.html'
            })
        ]
    

    索引.html

    <!DOCTYPE html>
    <html>
      <head>
        <title>Angular Quickstart</title>
        <base href="/" />
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <!-- Latest compiled and minified CSS -->
        ...
    
      </head>
    
      <body>
        <app>Loading...</app>
        <!-- webpack will auto insert JS and CSS links here -->
      </body>
    
    </html>
    

    【讨论】:

    • CSS 样式仍未在应用程序中加载。 webpack 2 的样式有哪些变化?因为我从来没有遇到过 webpack 1 的这个问题
    猜你喜欢
    • 2017-03-11
    • 2017-05-19
    • 2017-08-23
    • 1970-01-01
    • 2018-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-02
    相关资源
    最近更新 更多