Webpack4 Series Tutorial (a): Packaging JS

Source: Internet
Author: User

Webpack itself is designed to be packaged js , as the first section describes how to js package .

1. Inspection webpackSpecification support

webpackSupport es6 , CommonJS , AMD .

Create vendor folders, which minus.js , multi.js and sum.js respectively, are written in CommonJS, AMD, and ES6 specifications.

>>> Vendor Folder code address

In the portal file app.js , we refer to vendor the JS file in the folder using the 3 specification.

// ES6import sum from "./vendor/sum";console.log("sum(1, 2) = ", sum(1, 2));// CommonJsvar minus = require("./vendor/minus");console.log("minus(1, 2) = ", minus(1, 2));// AMDrequire(["./vendor/multi"], function(multi) {  console.log("multi(1, 2) = ", multi(1, 2));});
2. Writing the configuration file

webpack.config.jsIs the Webpack default profile name,>>> webpack.config.js code address, which is configured as follows:

const path = require("path");module.exports = {  entry: {    app: "./app.js"  },  output: {    publicPath: __dirname + "/dist/", // js引用路径或者CDN地址    path: path.resolve(__dirname, "dist"), // 打包文件的输出目录    filename: "bundle.js"  }};

Note output.publicPath The parameter, which represents: js the path to the file within which other files are referenced .

3. Closure

The packaged js files will be placed in the directory according to our configuration, at which dist point we need to create a html file that references the packaged js files .

Then open it in Chrome ( This lesson is just packaging js , not compiling es6 ) and you can see the results of our code running.

4. More

The Code address of this section:>>> point I entered

Project Code Warehouse:>>> point I entered

Welcome to the technical Exchange, quote please indicate the source.
Personal website: Yuan Xin
Original link: Webpack4 series tutorial (a): Packaging JS

Webpack4 Series Tutorial (a): Packaging JS

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.