CodeCoupler Webpack First Steps Library
To deploy a library you must do the following modifications:
Include only relevant files in package
Modify the files
property in the package.json
to pack only the resulting JavaScript and
CSS-Files:
1 2 3 4 5 6 |
|
Optional and depending on requirements, additional entries must be included:
- If you have used dynamic imports you have to include the files or the directory in which the files
are build. The default directory for dynamic imports would be
dist/parts
. - If you have imported static assets (like images) you must specify the folder or the files.
- The file
LICENSE
will always packed if it exists, but maybe you have added an additional author list. In this case you have to add this file manually, likeAUTHORS
.
Add main entry point of library
You must add a property main
into the package.json
. Replace library-name.js
with the own
name of the library which is by default the package name defined in the property name
(without any
namespace prefix).
1 2 3 |
|
Disable hash values in bundle filename
You must disable the hashvalues that will be appended to your bundle files. Set these properties to
false
in the webpack.cc-config.js
:
1 2 3 4 5 6 |
|
Disable the Generation of Favicons and Manifest
You should disable the Generation of Favicons and Manifest as this is not needed for a library. To
do this set staticParser.favicon
to null
in the webpack.cc-config.js
:
1 2 3 4 5 |
|
Optional: Define you own global variable name
If you want you use your own library global variable you can set this in the webpack.cc-config.js
.
This variable allows library users to access the exported functions of the library. Otherwiese
cc-webpack will try to create automatically a camel-case name from your library name (the name
without any namespace prefix, removing dashes, underscores, dots and tilde):
1 2 3 4 5 |
|
Add main export to the index.js
The file src/index.js
should contain only all the exports you want to offer to the user of your
library. If you want to provide many exports which are splitted into many files the file yould look
like this:
1 2 3 |
|
Build and access your library
Now you can build with npm run build && npm pack
and deploy the resulting .tar.gz
file.
Accessing your library in browser environment
Your library will be accesible from a global variable named as your package name from the
package.json
converted into camel case or like defined in webpack.cc-config.js
.
1 2 3 4 5 6 |
|
Remark: If you are using multiple entrypoints the name is defined in the property names of the
object entry
(like YourLibrary
and YourLibrary_Extension
).
Accessing your library in module environment
You library can be installed with npm i your-library-name
if you have published it or with npm i
your-library-name.tgz
with the local archive generated with npm pack
. The exported methods can be
imported as follows 1:
1 |
|
-
Up to version 3.3.0, the library had to be externalized when it was imported. Otherwise, importing and bundling in a separate application led to an error. From version 3.3.1 this is not necessery anymore. If you do not externalize the library it will be bundled correctly. ↩