-
-
Notifications
You must be signed in to change notification settings - Fork 301
/
webpack.config.js
189 lines (172 loc) · 5.51 KB
/
webpack.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
const path = require('path');
const webpack = require('webpack');
const WrapperPlugin = require('wrapper-webpack-plugin');
const packageJson = require('./package.json');
module.exports = {
entry: `${__dirname}/src/index.js`,
output: {
path: path.resolve(__dirname, 'dist'),
library: 'BoxIconElement',
libraryTarget: 'umd',
filename: 'boxicons.js',
},
devtool: 'source-map',
module: {
rules: [
{
test: /\.js$/,
loader: 'babel-loader',
options: {
babelrc: false,
presets: [
['env', { modules: false, targets: { uglify: true } }],
],
plugins: [
['babel-plugin-transform-builtin-classes', {
globals: ['Array', 'Error', 'HTMLElement'],
}],
],
},
},
{
test: /\.css$/,
use: [
{ loader: 'to-string-loader' },
{
loader: 'css-loader',
options: {
camelCase: true,
},
},
],
},
],
},
plugins: [
new webpack.DefinePlugin({
'BUILD.DATA': {
VERSION: JSON.stringify(packageJson.version),
},
}),
new WrapperPlugin({
test: /boxicons\.js$/,
header: getWrapper('header'),
footer: getWrapper('footer'),
}),
],
};
function getWrapper(type) {
if (getWrapper.header) {
return getWrapper[type];
}
const templatePieces = `(function (
DEFAULT_CDN_PREFIX,
STRING_WEB_COMPONENTS_REQUESTED,
WINDOW,
DOCUMENT,
init
) {
/**
* A Custom Elements (Web Components) polyfill loader wrapper.
* Use it to wrap modules that use CEs and it will take care of
* only executing the module initialization function when the
* polyfill is loaded.
* This loader wrapper also loads the \`core-js\` library if it
* finds that he current environment does not support Promises
* (ex. IE)
*
* The loader contains default URLs for polyfills, however, these
* can be overwritten with the following globals:
*
* - \`window.WEB_COMPONENTS_POLYFILL\`
* - \`window.ES6_CORE_POLYFILL\`
*
* In addition, this loader will add the following global
* variable, which is used to store module initialization
* functions until the environment is patched:
*
* - \`window.AWAITING_WEB_COMPONENTS_POLYFILL\`: an Array
* with callbacks. To store a new one, use
* \`window.AWAITING_WEB_COMPONENTS_POLYFILL.then(callbackHere)\`
*/
if (!('customElements' in WINDOW)) {
// If in the mist of loading the polyfills, then just add init to when that is done
if (WINDOW[STRING_WEB_COMPONENTS_REQUESTED]) {
WINDOW[STRING_WEB_COMPONENTS_REQUESTED].then(init);
return;
}
var _WEB_COMPONENTS_REQUESTED = WINDOW[STRING_WEB_COMPONENTS_REQUESTED] = getCallbackQueue();
_WEB_COMPONENTS_REQUESTED.then(init);
var WEB_COMPONENTS_POLYFILL = WINDOW.WEB_COMPONENTS_POLYFILL || "/" + "/" + DEFAULT_CDN_PREFIX + "/webcomponentsjs/2.0.2/webcomponents-bundle.js";
var ES6_CORE_POLYFILL = WINDOW.ES6_CORE_POLYFILL || "/" + "/" + DEFAULT_CDN_PREFIX + "/core-js/2.5.3/core.min.js";
if (!("Promise" in WINDOW)) {
loadScript(ES6_CORE_POLYFILL)
.then(function () {
loadScript(WEB_COMPONENTS_POLYFILL).then(function () {
_WEB_COMPONENTS_REQUESTED.isDone = true;
_WEB_COMPONENTS_REQUESTED.exec();
});
});
} else {
loadScript(WEB_COMPONENTS_POLYFILL).then(function () {
_WEB_COMPONENTS_REQUESTED.isDone = true;
_WEB_COMPONENTS_REQUESTED.exec();
});
}
} else {
init();
}
function getCallbackQueue() {
var callbacks = [];
callbacks.isDone = false;
callbacks.exec = function () {
callbacks.splice(0).forEach(function (callback) {
callback();
});
};
callbacks.then = function(callback){
if (callbacks.isDone) {
callback();
} else {
callbacks.push(callback);
}
return callbacks;
}
return callbacks;
}
function loadScript (url) {
var callbacks = getCallbackQueue();
var script = DOCUMENT.createElement("script")
script.type = "text/javascript";
if (script.readyState){ // IE
script.onreadystatechange = function(){
if (script.readyState == "loaded" || script.readyState == "complete"){
script.onreadystatechange = null;
callbacks.isDone = true;
callbacks.exec();
}
};
} else {
script.onload = function(){
callbacks.isDone = true;
callbacks.exec();
};
}
script.src = url;
DOCUMENT.getElementsByTagName("head")[0].appendChild(script);
script.then = callbacks.then;
return script;
}
})(
"cdnjs.cloudflare.com/ajax/libs",
"AWAITING_WEB_COMPONENTS_POLYFILL", // Global wait queue var name
window,
document,
function () {
____SPLIT_HERE____
}
);`.split('____SPLIT_HERE____');
getWrapper.header = templatePieces[0];
getWrapper.footer = templatePieces[1];
return getWrapper[type];
}