When you use the modern build
command in an initialized project, Modern.js Module will generate corresponding build artifacts based on the current configuration.
The default configuration is as follows:
The default output files has the following characteristics.
ES6
, and more advanced syntax will be converted.dist
directory under the project, and the type file output directory is dist/types
.You may have the following questions when you see this.
buildPreset
?Then the next step is to first explain buildPreset
.
The buildPreset
represents a prepared set or sets of build-related configurations that can be used to eliminate the trouble and complexity of configuration by using the default values corresponding to the build Preset, resulting in the expected product.
Modern.js Module mainly comes with two built-in build presets, including:
It also provides some variations, such as npm-library-with-umd
and npm-library-es5
, which, as their names suggest, correspond to library presets with umd output and support for es5 syntax, respectively. For more detailed configuration, you can refer to its API.
In addition, we can also fully customize the build configuration:
buildConfig
is a configuration option that describes how to compile and generate build artifacts. What was mentioned at the beginning about "features of build artifacts" are actually properties supported by buildConfig
. The currently supported properties cover the needs of most module type projects when building artifacts. buildConfig
not only contains some properties that artifacts have, but also contains some features needed to build artifacts. The following is a brief list from a classification point of view.
The basic attributes of a build artifacts include:
buildConfig.buildType
.buildConfig.target
.buildConfig.format
.buildConfig.dts
.buildConfig.sourceMap
.buildConfig.input
.buildConfig.outDir
.buildConfig.sourceDir
.Common functions required for build artifacts include:
buildConfig.alias
.buildConfig.asset
.buildConfig.copy
.buildConfig.define
.buildConfig.jsx
](/api/config/ build-config#jsx).Some advanced properties or less frequently used functions:
buildConfig.minify
.buildConfig.splitting
buildConfig.platform
.buildConfig.umdGlobals
.buildConfig.umdModuleName
.In addition to the above categories, frequently asked questions and best practices about these APIs can be found at the following links
bundle
and bundleless
?input
and sourceDir
Once we understand buildPreset
and buildConfig
, we can use them together.
In a real project, we can use buildPreset
to quickly get a set of default build configurations. If you need to customise it, you can use buildConfig
to override and extend it.
The extension logic is as follows.
buildConfig
is an array, new configuration items are added to the original preset.This will generate an additional IIFE-formatted product that supports up to ES2020 syntax on top of the original preset, in the dist/global
directory under the project.
buildConfig
is an object, the configuration items in the object are overwritten in the preset.This will cause a sourceMap file to be generated for each build task.