forked from bigcommerce/checkout-sdk-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack-cdn.config.js
95 lines (86 loc) · 3.04 KB
/
webpack-cdn.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const { execSync } = require('child_process');
const path = require('path');
const { major } = require('semver');
const { DefinePlugin } = require('webpack');
const WebpackAssetsManifest = require('webpack-assets-manifest');
const { getNextVersion, transformManifest } = require('./scripts/webpack');
const { babelLoaderRules, getBaseConfig, libraryEntries, libraryName, coreSrcPath } = require('./webpack-common.config');
const baseOutputPath = path.join(__dirname, 'dist-cdn');
async function getCdnConfig(options, argv) {
const baseConfig = await getBaseConfig(options, argv);
const version = await getNextVersion();
const versionDir = `v${major(version)}`;
const outputPath = path.join(baseOutputPath, versionDir);
return {
...baseConfig,
name: 'umd',
entry: libraryEntries,
output: {
filename: '[name]-[contenthash:8].js',
library: libraryName,
libraryTarget: 'umd',
path: outputPath,
},
module: {
rules: [
...babelLoaderRules,
...baseConfig.module.rules,
],
},
plugins: [
...baseConfig.plugins,
new WebpackAssetsManifest({
entrypoints: true,
output: path.join(outputPath, 'manifest.json'),
publicPath: path.join(versionDir, '/'),
transform: assets => transformManifest(assets, version),
}),
],
};
}
async function getCdnLoaderConfig(options, argv) {
const baseConfig = await getBaseConfig(options, argv);
const version = await getNextVersion();
const outputPath = path.join(baseOutputPath, `v${major(version)}`);
return {
...baseConfig,
name: 'umd-loader',
entry: {
loader: path.join(coreSrcPath, 'loader-cdn.ts'),
},
output: {
filename: `[name]-v${version}.js`,
library: `${libraryName}Loader`,
libraryTarget: 'umd',
path: outputPath,
},
module: {
rules: [
...babelLoaderRules,
...baseConfig.module.rules,
],
},
plugins: [
...baseConfig.plugins,
new DefinePlugin({
LIBRARY_NAME: JSON.stringify(libraryName),
MANIFEST_JSON: JSON.stringify(require(path.join(outputPath, 'manifest.json'))),
}),
{
apply(compiler) {
compiler.hooks.done.tap('DuplicateLoader', () => {
execSync(`cp ${path.join(outputPath, `loader-v${version}.js`)} ${path.join(outputPath, `loader.js`)}`);
});
},
},
],
};
}
// This configuration is for building distribution files for CDN deployment
async function getConfigs(options, argv) {
if (argv.configName.includes('umd-loader')) {
return await getCdnLoaderConfig(options, argv);
}
return await getCdnConfig(options, argv);
}
module.exports = getConfigs;