2

I have multiple subpages on a site that uses webpack and grunt. Here is my webpack configuration in my Gruntfile.js.

I would like to use webpack to handle all of the subpage JS files.

    webpack: {
        dev: {
            entry: {
                test: "./src/js/test.js",
                index: "./src/js/index.js",
            },
            output: {
                path: 'dist/js/',
                filename: '[name].js',
                chunkFilename: '[id].[hash].chunk.js',
                publicPath: 'js/'
            },
            plugins: [
                new webpack.SourceMapDevToolPlugin('[file].map', null, '[absolute-resource-path]', '[absolute-resource-path]')
            ]
        },
        prod: {
            entry: {
                test: "./src/js/test.js",
                index: "./src/js/index.js",
            },
            output: {
                path: 'dist/js/',
                filename: '[name].js',
                chunkFilename: '[id].[hash].chunk.js',
                publicPath: 'js/'
            },
            plugins: [
                new webpack.optimize.UglifyJsPlugin()
            ]
        }
    },

Is there a way to use a wildcard when specifying the entry names? So that any files inside js/ folder is considered an entry point? Also, I'm I configuring webpack correctly? And why is the chunkFileName needed? That seems to create unnecessary javascript. Also, how do I handle the case where there is a common JS for all subpages? There are specific JS files for some subpages, and a common JS file for all subpages.

1
  • I'm not sure you want to create a new entry point for each file. Usually, you have one or two entry points and webpack recursively pulls in all the required modules. You should be able use your test expression to assign different loaders to different filetypes. Commented Jan 8, 2016 at 19:11

1 Answer 1

1

We had a setup with multiples entry-points as well.

Is there a way to use a wildcard when specifying the entry names? So that any files inside js/ folder is considered an entry point?

It’s node, you can use fs or anything you want to list files and create an object of entry points, following any convention you like.

Also, I'm I configuring webpack correctly?

Well, it depends on what you want to do… I don’t see any loaders so you don’t do transformation on files (like babel).

And why is the chunkFileName needed? That seems to create unnecessary javascript.

I don’t use it.

Also, how do I handle the case where there is a common JS for all subpages? There are specific JS files for some subpages, and a common JS file for all subpages.

You can use CommonsChunkPlugin for mutual code between pages. https://webpack.github.io/docs/code-splitting.html#split-app-and-vendor-code

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.