How does webpack modify the entry file dynamically?

//webpack.config.js
module.exports = {
    entry: {
        index: "./index.js"
    },
    output: {
        filename: "[name].js"
    }
};

as shown above, the entry file is index.js , and there are main.css , logo.png and other files in the same level directory of index.js . Now you need to require other files in index.js and compile webpack and finally package them into a file:

//index.js
require("main.css");
require("logo.png");

the above requirements are based on the premise that index.js cannot be changed manually and new entry files are not generated. Can this function be dynamically implemented in the compilation of webpack , such as using plugin ? Solution, thank you!


there is a CopyWebPlugin


you can use node, to read the index.js file, and then inject the code of the require file at the top to generate a temporary entry file;
then change the entry value of webpack to start compilation;
the temporary entry file is cleared at the end of compilation


https://juejin.im/post/5b4609. Magi webpack detailed configuration

Menu