Webpack Introduction (iii) Webpack API

Source: Internet
Author: User

Finally to the Webpack API this article, I can not wait for 0.0;

Webpack is fed a configuration object. Depending on your usage of webpack there is and ways to pass this configuration object:

Webpack provides a configuration object, depending on how you use Webpack, there are two ways to pass this configuration object.

Cli

If you use the CLI it would read a file webpack.config.js (or the file passed by the --config option). This file should export the configuration object:

If you use the CLI, it only webpack.config.js this file, (or get a file with a--config mark), this file will export this configuration object.

Module.exports = {    //  configuration};
node. js API

If you use the node. js API need to pass the configuration object as parameter: With node, you need to pass configuration objects in parameters, as follows:

Webpack ({    //  configuration}, callback);
multipleConfigurations

In both cases you can also with an array of configurations, which is processed in parallel.      They share filesystem cache and watchers so this is more efficent than calling Webpack multiple times. In both cases, parallel processing, you can use a configuration array, they share the file system cache and watchers, more efficient than multiple webpack calls

CONFIGURATION OBJECT CONTENT

Hint: Keep in mind so you don't need to write a pure JSON into the configuration. Use any JavaScript want. It ' s just a node. js module ...

Note: Remember, do not write a pure JSON file in the configuration, casually with JS, it is a Nodejs module

Very Simple Configuration Object Example:

{    + "/app",    "./entry",    output: {        + "/dist",        "Bundle.js"     }}
context

The base directory (absolute path!) for resolving the entry option. If output.pathinfo is set, the included PathInfo is shortened to this directory.

The underlying directory (absolute path) is used to resolve the entry option, and if Output.pathinfo is set, it contains shortened directories, (equivalent to the public directory, all directories below this common directory)

Default:process.cwd()

entry

The entry point for the bundle. The entrance to the packaged file

If you pass a string:the string was resolved to a module which is loaded upon startup. If a string is passed in, the string is parsed to the module that was loaded at startup.

If you pass an array:all modules is loaded upon startup.  The last one is exported. If the array is passed in, all modules are loaded at startup, and the last one is exported

Entry: ["./entry1", "./entry2"]

If you pass an object:multiple entry bundles is created. The key is the chunk name. The value can be a string or an array.

If an object is passed in, multiple input package files are created, the object key value is chunk name, and the value can be a string or an array.

{    entry: {        "./page1",        page2: ["./entry1", "./entry2"]    },    output: {         // Make sure to use [name] or [id] in output.filename        // When   using multiple entry points        filename: "[name].bundle.js",        "[id]." Bundle.js "    }}

There is an object, Page1 and Page2 will generate two packaging files, the name is Page1.bundle.js and page2.bundle.js, the entry file is 3, Page1 module, Entry1 and Entry2 module.

output

Options affecting the output of the compilation. outputoptions tell Webpack how to write the compiled files to disk. Note, that and there can multiple entry points, only one of the output configuration is specified.

If you use any hashing ( [hash] or [chunkhash] ) Make sure to a consistent ordering of modules. Use the OccurenceOrderPlugin or recordsPath .

Output is an option that affects the compilation output. The output option tells Webpack how to write the compiled file to disk. Note that although there can be many input ports, there is only one output configuration

If you use a lot of hashing, make sure there is a consistent module order. With OccurenceOrderPlugin plugins orrecordsPath

output.filename

Specifies the name of each output file on disk. You must not specify an absolute path here! output.paththe option determines the location of the files is written to, was filename used solely for naming the individual files.

Specify the name of each output file, you must specify an absolute path, the option determines the path to which the output.path file is written on disk, and filename is used only to name a single file

Single entry

Not finish ...

Webpack Introduction (iii) Webpack API

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.