Skip to content

Commit 7ce800b

Browse files
committed
fix(css-vars): fix css vars polyfill for ie11 using defineCustomElements()
1 parent 2e7ef9f commit 7ce800b

12 files changed

Lines changed: 114 additions & 60 deletions

File tree

scripts/build-core-esm.js

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,29 @@ const fs = require('fs-extra');
22
const path = require('path');
33
const rollup = require('rollup');
44

5-
function buildCoreEsm(inputFile, outputFile) {
6-
return rollup.rollup({
5+
6+
async function buildCoreEsm(inputFile, outputFile) {
7+
const build = await rollup.rollup({
78
input: inputFile,
89
onwarn: (message) => {
910
if (/top level of an ES module/.test(message)) return;
1011
console.error( message );
1112
}
12-
})
13-
.then(bundle => {
14-
generateBundle(inputFile, outputFile, bundle, 'es');
15-
})
16-
.catch(err => {
17-
console.log(err);
18-
console.log(err.stack);
19-
process.exit(1);
2013
});
21-
}
22-
2314

24-
function generateBundle(inputFile, outputFile, bundle, format) {
25-
bundle.generate({
26-
format: format
15+
const clientCore = await build.generate({
16+
format: 'es'
17+
});
2718

28-
}).then(clientCore => {
29-
let code = clientCore.code.trim();
30-
code = dynamicImportFnHack(code);
31-
code = polyfillsHack(code);
19+
let code = clientCore.code.trim();
20+
code = dynamicImportFnHack(code);
21+
code = polyfillsHack(code);
3222

33-
fs.writeFile(outputFile, code, (err) => {
34-
if (err) {
35-
console.log(err);
36-
process.exit(1);
37-
}
38-
});
23+
fs.writeFile(outputFile, code, (err) => {
24+
if (err) {
25+
console.log(err);
26+
process.exit(1);
27+
}
3928
});
4029
}
4130

scripts/build-core.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const outputLoaderFile = path.join(DST_DIR, 'client', 'loader.js');
3131
const inputCoreEsmFile = path.join(TRANSPILED_DIR, 'client', 'core-esm.js');
3232
const outputCoreEsmFile = path.join(DIST_CLIENT_DIR, 'core.esm.js');
3333
const outputPolyfillsDir = path.join(DIST_CLIENT_DIR, 'polyfills');
34+
const transpiledPolyfillsDir = path.join(TRANSPILED_DIR, 'client', 'polyfills');
3435

3536

3637
const success = transpile(path.join('..', 'src', 'tsconfig.json'));
@@ -51,7 +52,7 @@ if (success) {
5152
copyMain();
5253
createCoreDts();
5354
copyUtilDir();
54-
buildPolyfills(outputPolyfillsDir);
55+
buildPolyfills(transpiledPolyfillsDir, outputPolyfillsDir);
5556

5657

5758
function bundleClientCore(inputCoreFile, outputCoreFile) {

scripts/build-polyfills.js

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
const fs = require('fs-extra');
22
const path = require('path');
3+
const rollup = require('rollup');
4+
const ts = require('typescript');
5+
const terser = require('terser');
36

47
const ROOT_DIR = path.join(__dirname, '..');
58
const SRC_DIR = path.join(ROOT_DIR, 'src', 'client', 'polyfills');
69

710

8-
module.exports = function buildPolyfills(outputPolyfillsDir) {
9-
11+
module.exports = async function buildPolyfills(transpiledPolyfillsDir, outputPolyfillsDir) {
1012
fs.emptyDirSync(outputPolyfillsDir);
1113

1214
const esmDir = path.join(outputPolyfillsDir, 'esm');
@@ -26,9 +28,41 @@ module.exports = function buildPolyfills(outputPolyfillsDir) {
2628

2729
const esmWrapped = (fileName === 'tslib.js')
2830
? polyfillContent
29-
: `export function applyPolyfill(window, document) {${polyfillContent}}`;
31+
: esmWrap(polyfillContent);
3032

3133
fs.writeFileSync(esmFilePath, esmWrapped);
3234
fs.writeFileSync(es5FilePath, polyfillContent);
3335
});
36+
37+
38+
const build = await rollup.rollup({
39+
input: path.join(transpiledPolyfillsDir, 'css-shim', 'index.js'),
40+
onwarn: (message) => {
41+
if (/top level of an ES module/.test(message)) return;
42+
console.error( message );
43+
}
44+
});
45+
46+
const bundleResults = await build.generate({
47+
format: 'es'
48+
});
49+
50+
const transpile = ts.transpileModule(bundleResults.code, {
51+
compilerOptions: {
52+
target: ts.ScriptTarget.ES5
53+
}
54+
});
55+
56+
const minify = terser.minify(transpile.outputText);
57+
58+
const esmFilePath = path.join(esmDir, 'css-shim.js');
59+
const es5FilePath = path.join(es5Dir, 'css-shim.js');
60+
61+
fs.writeFileSync(esmFilePath, esmWrap(minify.code));
62+
fs.writeFileSync(es5FilePath, minify.code);
3463
};
64+
65+
66+
function esmWrap(polyfillContent) {
67+
return `export function applyPolyfill(window, document) {${polyfillContent}}`;
68+
}

src/client/core-esm.ts

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -55,34 +55,45 @@ export function defineCustomElement(win: Window, cmpData: d.ComponentHostData |
5555
);
5656
}
5757

58-
// polyfills have been applied if need be
59-
(cmpData as d.ComponentHostData[]).forEach(c => {
60-
let HostElementConstructor: any;
61-
62-
if (isNative(win.customElements.define)) {
63-
// native custom elements supported
64-
const createHostConstructor = new Function('w', 'return class extends w.HTMLElement{}');
65-
HostElementConstructor = createHostConstructor(win);
66-
67-
} else {
68-
// using polyfilled custom elements
69-
HostElementConstructor = function(self: any) {
70-
return (win as any).HTMLElement.call(this, self);
71-
};
72-
73-
HostElementConstructor.prototype = Object.create(
74-
(win as any).HTMLElement.prototype,
75-
{ constructor: { value: HostElementConstructor, configurable: true } }
58+
function defineComponents() {
59+
// polyfills have been applied if need be
60+
(cmpData as d.ComponentHostData[]).forEach(c => {
61+
let HostElementConstructor: any;
62+
63+
if (isNative(win.customElements.define)) {
64+
// native custom elements supported
65+
const createHostConstructor = new Function('w', 'return class extends w.HTMLElement{}');
66+
HostElementConstructor = createHostConstructor(win);
67+
68+
} else {
69+
// using polyfilled custom elements
70+
HostElementConstructor = function(self: any) {
71+
return (win as any).HTMLElement.call(this, self);
72+
};
73+
74+
HostElementConstructor.prototype = Object.create(
75+
(win as any).HTMLElement.prototype,
76+
{ constructor: { value: HostElementConstructor, configurable: true } }
77+
);
78+
}
79+
80+
// convert the static constructor data to cmp metadata
81+
// define the component as a custom element
82+
pltMap[namespace].defineComponent(
83+
buildComponentLoader(c),
84+
HostElementConstructor
7685
);
77-
}
86+
});
87+
}
7888

79-
// convert the static constructor data to cmp metadata
80-
// define the component as a custom element
81-
pltMap[namespace].defineComponent(
82-
buildComponentLoader(c),
83-
HostElementConstructor
84-
);
85-
});
89+
if (_BUILD_.cssVarShim && (window as any).customStyleShim) {
90+
pltMap[namespace].customStyle = (window as any).customStyleShim;
91+
92+
pltMap[namespace].customStyle.initShim().then(defineComponents);
93+
94+
} else {
95+
defineComponents();
96+
}
8697

8798
});
8899
}

src/client/platform-main-legacy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ export function createPlatformMainLegacy(namespace: string, Context: d.CoreConte
284284

285285
let requestBundleQueue: Function[] = [];
286286
if (_BUILD_.cssVarShim && customStyle) {
287-
customStyle.init().then(() => {
287+
customStyle.initShim().then(() => {
288288
// loaded all the css, let's run all the request bundle callbacks
289289
while (requestBundleQueue.length) {
290290
requestBundleQueue.shift()();

src/client/polyfills/css-shim/custom-style.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class CustomStyle {
2222
private doc: Document,
2323
) {}
2424

25-
init() {
25+
initShim() {
2626
return new Promise(resolve => {
2727
this.win.requestAnimationFrame(() => {
2828
loadDocument(this.doc, this.globalScopes).then(() => resolve());
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
import { CustomStyle } from './custom-style';
3+
4+
(window as any).customStyleShim = new CustomStyle(window, document);

src/client/polyfills/index.js

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/compiler/app/app-polyfills.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,6 @@ const INLINE_POLYFILLS = [
4646

4747
const POLYFILLS = [
4848
...INLINE_POLYFILLS,
49+
'css-shim.js',
4950
'tslib.js'
5051
];

src/compiler/app/reserved-properties.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,5 +234,11 @@ export const RESERVED_PROPERTIES: string[] = [
234234
'warn',
235235
'webkitMatchesSelector',
236236
'window',
237-
'HTMLElement'
237+
'HTMLElement',
238+
239+
/** CSS Vars Shim */
240+
'createHostStyle',
241+
'initShim',
242+
'customStyleShim',
243+
'updateHost'
238244
];

0 commit comments

Comments
 (0)