Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/425.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 仅angular2捆绑节点_模块,不包括应用程序代码_Javascript_Node.js_Angular_Webpack_Webpack 2 - Fatal编程技术网

Javascript 仅angular2捆绑节点_模块,不包括应用程序代码

Javascript 仅angular2捆绑节点_模块,不包括应用程序代码,javascript,node.js,angular,webpack,webpack-2,Javascript,Node.js,Angular,Webpack,Webpack 2,Angular v.4和网页包 我在寻找一个非常简单的问题的答案 如何将node_模块文件夹绑定为vendor.js,而不是您的应用程序代码。页面加载似乎有超过300个来自angular代码的请求来加载内部文件 如果我能够合并所有节点模块库,我将节省大量的浏览器请求 我遵循这一点,虽然它能够运行,但它也捆绑了库和应用程序代码。我只能成功生成vendor.js,但我不确定在index.html中添加vendor.js后,我的应用程序代码将如何识别@angular lib引用。目前正在使用类似这样的

Angular v.4和网页包

我在寻找一个非常简单的问题的答案

如何将node_模块文件夹绑定为vendor.js,而不是您的应用程序代码。页面加载似乎有超过300个来自angular代码的请求来加载内部文件

如果我能够合并所有节点模块库,我将节省大量的浏览器请求

我遵循这一点,虽然它能够运行,但它也捆绑了库和应用程序代码。我只能成功生成vendor.js,但我不确定在
index.html
中添加vendor.js后,我的应用程序代码将如何识别@angular lib引用。目前正在使用类似这样的东西来解决这个问题

@angular/code : 'npm:@angular/code/bundle/core.umd.js' 
..
..
这正是我认为应用程序崩溃时发生的事情,因为它无法从我的应用程序代码中找到对@angular的引用

是否可以将
node\u module
文件夹捆绑到
vendor.js
中,并且仍然能够在我的应用程序代码中使用而不捆绑应用程序代码

添加网页包配置

webpack.config.js

module.exports = require('./config/webpack.prod.js');
var webpack = require('webpack');
var webpackMerge = require('webpack-merge');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var commonConfig = require('./webpack.common.js');
var helpers = require('./helpers');

const ENV = process.env.NODE_ENV = process.env.ENV = 'production';

module.exports = webpackMerge(commonConfig, {
  devtool: 'source-map',

  output: {
    path: helpers.root('dist'),
    publicPath: '/',
    filename: '[name].[hash].js',
    chunkFilename: '[id].[hash].chunk.js'
  },

  plugins: [
    new webpack.NoEmitOnErrorsPlugin(),
    new webpack.optimize.UglifyJsPlugin({ // https://github.com/angular/angular/issues/10618
      mangle: {
        keep_fnames: true
      }
    }),
    new ExtractTextPlugin('[name].[hash].css'),
    new webpack.DefinePlugin({
      'process.env': {
        'ENV': JSON.stringify(ENV)
      }
    }),
    new webpack.LoaderOptionsPlugin({
      htmlLoader: {
        minimize: false // workaround for ng2
      }
    })
  ]
});
webpack.common.js

var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var helpers = require('./helpers');

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

  resolve: {
    extensions: ['.ts', '.js']
  },

  module: {
    rules: [
      {
        test: /\.ts$/,
        loaders: [
          {
            loader: 'awesome-typescript-loader',
            options: { configFileName: helpers.root('src', 'tsconfig.json') }
          } , 'angular2-template-loader'
        ]
      },
      {
        test: /\.html$/,
        loader: 'html-loader'
      },
      {
        test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
        loader: 'file-loader?name=assets/[name].[hash].[ext]'
      },
      {
        test: /\.css$/,
        exclude: helpers.root('src', 'app'),
        loader: ExtractTextPlugin.extract({ fallbackLoader: 'style-loader', loader: 'css-loader?sourceMap' })
      },
      {
        test: /\.css$/,
        include: helpers.root('src', 'app'),
        loader: 'raw-loader'
      }
    ]
  },

  plugins: [
    // Workaround for angular/angular#11580
    new webpack.ContextReplacementPlugin(
      // The (\\|\/) piece accounts for path separators in *nix and Windows
      /angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/,
      helpers.root('./src'), // location of your src
      {} // a map of your routes
    ),

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

    new HtmlWebpackPlugin({
      template: 'src/index.html'
    })
  ]
};
webpack.prod.js

module.exports = require('./config/webpack.prod.js');
var webpack = require('webpack');
var webpackMerge = require('webpack-merge');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var commonConfig = require('./webpack.common.js');
var helpers = require('./helpers');

const ENV = process.env.NODE_ENV = process.env.ENV = 'production';

module.exports = webpackMerge(commonConfig, {
  devtool: 'source-map',

  output: {
    path: helpers.root('dist'),
    publicPath: '/',
    filename: '[name].[hash].js',
    chunkFilename: '[id].[hash].chunk.js'
  },

  plugins: [
    new webpack.NoEmitOnErrorsPlugin(),
    new webpack.optimize.UglifyJsPlugin({ // https://github.com/angular/angular/issues/10618
      mangle: {
        keep_fnames: true
      }
    }),
    new ExtractTextPlugin('[name].[hash].css'),
    new webpack.DefinePlugin({
      'process.env': {
        'ENV': JSON.stringify(ENV)
      }
    }),
    new webpack.LoaderOptionsPlugin({
      htmlLoader: {
        minimize: false // workaround for ng2
      }
    })
  ]
});

非常确定
ng build
(或者类似的东西-记不清了)会帮你完成并存储在dist文件夹中。但这是在使用角度cli。你的网页配置是什么样子的?你说的“300个请求”是什么意思?Webpack不会有300个请求,只有1个或多个。您想要vendor.js的确切原因是什么?这只有在每个站点上有几个应用程序共享相同的第三方模块时才有意义。@estus我的意思是没有webpack,我想避免使用将node_模块捆绑到供应商中的大量资源需求。js@estus所以我现在只有使用angular的项目,所以你认为我不需要捆绑node_模块并创建vendor.js?