How to add wildcard mapping in entry of webpack

前端 未结 5 1411
长发绾君心
长发绾君心 2020-12-04 16:50

I need to webpack all the js file in the script folder.I tried this

module.exports = {
  module: {
    loaders: [
      {
        test: /\\.js$/,
        ex         


        
5条回答
  •  余生分开走
    2020-12-04 16:58

    I had some problems with file paths in Webpack 2.4.1, so made this. In addition to multiple entries, this also creates multiple .html files.

    const HtmlWebpackPlugin = require('html-webpack-plugin');
    const fs = require('fs');
    
    function getEntries (){
        return fs.readdirSync('./src/pages/')
            .filter(
                (file) => file.match(/.*\.js$/)
            )
            .map((file) => {
                return {
                    name: file.substring(0, file.length - 3),
                    path: './pages/' + file
                }
            }).reduce((memo, file) => {
                memo[file.name] = file.path
                return memo;
            }, {})
    }
    
    const config = {
        entry: getEntries, 
        output: {
            path: resolve('./public'),
            filename: '[name].js'
        },
        plugins: [
            new HtmlWebpackPlugin({
                title: 'My App',
                filename: '[name].html',
                template: './pages/_template.html'
            })
        ]
    }
    

提交回复
热议问题