【问题标题】:How to include node_modules using webpack?如何使用 webpack 包含 node_modules?
【发布时间】:2019-03-29 22:41:46
【问题描述】:

在我的 angularJs 1.3 应用程序中,我之前使用的是 bower 和 grunt,它运行良好。我在 index.html 中添加文件,如下图所示。但现在我已经使用 NPM 安装了所有包,并使用 WEBPack 4.21.0 来捆绑和运行应用程序。但是现在如果我从 Index.html 文件中删除包链接,我的应用程序将停止工作。但我不想要 Index.html 中的所有这些链接,只想从这些文件中生成一个捆绑文件。请指导我如何实现这一目标?目前,它只是在 vendor.js 中添加 angular.js 文件和一些其他文件。

Index.html

Package.json

webpack.config.js

更新问题:

现在我正在使用以下 webpack.config.js 但它正在创建 bootstrap_and_some_plugin.css.js 。它必须创建css文件但不知道为什么要创建js文件?

module.exports = {
  context: __dirname + '/app/scripts',
  resolve: {
    modules: ['bower_components', 'node_modules'],
    alias: {
      bower_components: __dirname + '/app/bower_components',
      assets: __dirname + '/app/assets'
    },
    extensions: ['.js', '.jsx', '.css']
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          { loader: "style-loader" },
          { loader: "css-loader" }
        ]
      },
      {
        test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
        use: [{
          loader: 'file-loader',
          options: {
            name: '[name].[ext]',
            outputPath: 'fonts/'
          }
        }]
      }
    ]
  },
  entry: {
    app: './main-app.js',
    'bootstrap_and_some_plugin.css': [
      'bower_components/font-awesome/css/font-awesome.css',
      'bower_components/seiyria-bootstrap-slider/dist/css/bootstrap-slider.min.css',
      'bower_components/angular-ui-tree/dist/angular-ui-tree.min.css',
    ]
  },
  output: {
    filename: '[name].js',
    path: __dirname + '/app/scripts',
    //chunkFilename: '[id].[chunkhash].js',
  },
  devServer: {
    contentBase: './app',
    host: 'localhost',
    port: '9000',
    inline: true,
    compress: true,
    proxy: {
      '/api/**': {
        //target: 'http://10.189.1.159:8080',
        target: 'http://localhost:9100',
        secure: false,
        changeOrigin: true,
        cookieDomainRewrite: true
      }
    },
    open: true
  },
  plugins: [

  ]
};

【问题讨论】:

    标签: angularjs webpack webpack-4


    【解决方案1】:

    在文件webpack.config.js 中,将此属性添加到resolve 属性中:

    resolve: {
        alias: {
            bower_components: __dirname + '/app/bower_components'
        }
    }
    

    main-app.js文件中,如果你想使用一些js文件,你可以这样调用:

    require('bower_components/jquery/dist/jquery.js');
    require('bower_components/angular/angular.js');
    require('bower_components/bootstrap/dist/js/bootstrap.js');
    // ...
    

    您需要指定文件webpack.config.js 的路径。在我的示例中,所有路径如下所示:

    your_project
        webpack.config.js
        app
            bower_components
                jquery
                    ...
                angular
                    ...
                bootstrap
                    ...
    

    __dirname 指的是使用它的 js 文件的当前路径。如果您在webpack.config.js 文件中使用__dirname,它将呈现your_project。或者在jquery.js中使用它,它会渲染your_project\app\bowser_components\jquery\dist

    然后,构建到bundle.js文件并删除Index.cshtml文件中的所有路径。

    希望这会有所帮助!

    更新:如果您的 js 目标文件太大。您可以将模块拆分为多个部分,如下所示:

    entry: {
        'bootstrap_and_some_plugin.css': [
            './app/bower_components/bootstrap/dist/css/bootstrap.min.css',
            './app/bower_components/some-plugin/css/some-plugin.css'
        ],
        'jquery_and_angular.js': [
            './app/bower_components/jquery/dist/jquery.js', 
            './app/bower_components/angular/angular.js'
        ],
        'site.js': ['./js/site']
    }
    

    那么,在你的Index.cshtml

    <link href="bootstrap_and_some_plugin.css" rel="stylesheet" />
    
    <!-- body content -->
    
    <script src="jquery_and_angular.js"></script>
    <script src="site.js"></script>
    

    更新2:你需要安装babili-webpack-pluginextract-text-webpack-plugin这两个包

    在文件webpack.config.js

    // define these variables before "module.exports"
    var BabiliPlugin = require('babili-webpack-plugin');
    var ExtractTextPlugin = require('extract-text-webpack-plugin');
    
    module.exports = {...};
    

    然后,设置插件选项:

    plugins: [
        new BabiliPlugin({}, { test: /\.js$/, comments: false }),
        new ExtractTextPlugin('[name]'),
        ... and other options
    ]
    

    和输出选项:

    output: {
        filename: '[name]',
        ... and other options
    }
    

    【讨论】:

    • 我已经按照你的描述在 main-app.js 中添加了 .js。现在我的 app.js 文件大小达到了 3.51 MB。如何制作该文件的块?
    • @Manjit 我已经更新了我对您问题的回答。请再次检查!
    • 感谢您的回复,您的回复对我很有帮助,但请您查看我更新的问题。它正在创建 bootstrap_and_some_plugin.css.js 文件而不是 cs 文件。你能告诉我我在做什么错吗?
    • @Manjit 完成您的问题,请检查!
    • 它正在生成bootstrap_and_some_plugin.css,但应用程序文件没有js扩展名,并且bootstrap_and_some_plugin.css文件中有js代码。
    猜你喜欢
    • 2018-12-29
    • 2017-07-21
    • 2017-12-01
    • 2018-07-07
    • 2018-10-09
    • 2018-06-02
    • 1970-01-01
    • 2019-05-23
    • 2017-07-16
    相关资源
    最近更新 更多