Skip to content

Commit 2331468

Browse files
authored
Emit ES2020 builds for newer browsers, and emit ES5 for older browsers.
1 parent 9df5d06 commit 2331468

File tree

9 files changed

+592
-77
lines changed

9 files changed

+592
-77
lines changed

.github/workflows/test.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,34 @@ jobs:
3939
- run: npm run test262
4040
env:
4141
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
42+
test-test262-opt:
43+
runs-on: ubuntu-latest
44+
steps:
45+
- uses: actions/checkout@v2
46+
with:
47+
submodules: true
48+
- name: use node.js v16.x
49+
uses: actions/setup-node@v1
50+
with:
51+
node-version: 16.x
52+
- run: npm ci
53+
- run: npm run test262
54+
env:
55+
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
56+
NODE_ENV: production
57+
test-test262-es5:
58+
runs-on: ubuntu-latest
59+
steps:
60+
- uses: actions/checkout@v2
61+
with:
62+
submodules: true
63+
- name: use node.js v16.x
64+
uses: actions/setup-node@v1
65+
with:
66+
node-version: 16.x
67+
- run: npm ci
68+
- run: npm run test262
69+
env:
70+
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
71+
NODE_ENV: production
72+
TRANSPILE: "1"

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ See [this issue](https://github.com/tc39/proposal-temporal/issues/778) for more
7070
Once JS engines start shipping with Temporal, we may decide to change this behavior to match built-in behavior more closely.
7171
See [#2](https://github.com/js-temporal/temporal-polyfill/issues/2) to provide feedback or track this issue.
7272

73+
This polyfill ships ES2020 code for both CJS and ESM bundles - if your
74+
environment does not support ES2020, then please make sure to transpile the
75+
content of this package along with the rest of your code.
76+
7377
## Contributing / Help Wanted
7478

7579
We're eagerly welcoming to contributors who want to help build and maintain this polyfill.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"scripts": {
2020
"test": "tsc && node --no-warnings --experimental-modules --experimental-specifier-resolution=node --icu-data-dir node_modules/full-icu --loader ./test/resolve.source.mjs ./test/all.mjs",
2121
"test262": "TEST262=1 npm run build && ./test/test262.sh",
22-
"build": "rm -rf dist/* && rollup -c rollup.config.js",
22+
"build": "rm -rf dist/* tsc-out/* && tsc && rollup -c rollup.config.js",
2323
"prepare": "npm run build",
2424
"prepublishOnly": "npm run build",
2525
"update": "npx npm-check-updates -u -x @pipobscure/demitasse",

rollup.config.js

Lines changed: 108 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,54 @@
11
import commonjs from '@rollup/plugin-commonjs';
2-
import resolve from '@rollup/plugin-node-resolve';
2+
import { nodeResolve } from '@rollup/plugin-node-resolve';
33
import babel from '@rollup/plugin-babel';
44
import replace from '@rollup/plugin-replace';
5-
import typescript from '@rollup/plugin-typescript';
65
import { terser } from 'rollup-plugin-terser';
76
import { env } from 'process';
87
import pkg from './package.json';
98

109
const isPlaygroundBuild = !!env.TEMPORAL_PLAYGROUND;
11-
const isTest262 = !!env.TEST262;
12-
const isProduction = env.NODE_ENV === 'production' && !isTest262;
10+
const isTest262Build = !!env.TEST262;
11+
const isProduction = env.NODE_ENV === 'production';
12+
const isTranspiledBuild = !!env.TRANSPILE;
1313
const libName = 'temporal';
1414

15-
const plugins = [
16-
typescript({
17-
typescript: require('typescript')
18-
}),
19-
replace({ exclude: 'node_modules/**', 'globalThis.__debug__': !isTest262 && !isProduction, preventAssignment: true }),
20-
resolve({ preferBuiltins: false }),
21-
commonjs(),
22-
babel({
23-
exclude: 'node_modules/**',
24-
babelHelpers: 'external',
25-
presets: [
26-
[
27-
'@babel/preset-env',
28-
{
29-
targets: '> 0.25%, not dead'
15+
function withPlugins(
16+
options = {
17+
babelConfig: undefined,
18+
optimize: false,
19+
debugBuild: true
20+
}
21+
) {
22+
const basePlugins = [
23+
replace({ exclude: 'node_modules/**', 'globalThis.__debug__': options.debugBuild, preventAssignment: true }),
24+
commonjs(),
25+
nodeResolve({ preferBuiltins: false })
26+
];
27+
if (options.babelConfig) {
28+
basePlugins.push(babel(options.babelConfig));
29+
}
30+
if (options.optimize) {
31+
basePlugins.push(
32+
terser({
33+
keep_classnames: true,
34+
keep_fnames: true,
35+
ecma: 2015,
36+
compress: {
37+
keep_fargs: true,
38+
keep_classnames: true,
39+
keep_fnames: true
40+
},
41+
mangle: {
42+
keep_classnames: true,
43+
keep_fnames: true
3044
}
31-
]
32-
]
33-
}),
34-
isProduction && terser()
35-
].filter(Boolean);
45+
})
46+
);
47+
}
48+
return basePlugins;
49+
}
3650

37-
const input = 'lib/index.ts';
51+
const input = 'tsc-out/index.js';
3852

3953
const external = [
4054
// Some dependencies (e.g. es-abstract) are imported using sub-paths, so the
@@ -53,59 +67,97 @@ function outputEntry(file, format) {
5367
};
5468
}
5569

56-
let builds = [
57-
{
58-
input,
59-
external,
60-
output: [
61-
// ESM bundle
62-
outputEntry(pkg.module, 'es'),
63-
// CJS bundle.
64-
// Note that because package.json specifies "type":"module", the name of
65-
// this file MUST end in ".cjs" in order to be treated as a CommonJS file.
66-
outputEntry(pkg.main, 'cjs')
67-
],
68-
plugins
69-
},
70-
{
71-
input,
72-
// UMD bundle for using in script tags, etc
73-
// Note that some build systems don't like reading UMD files if they end in
74-
// '.cjs', so this entry in package.json should end in a .js file extension.
75-
output: [outputEntry(pkg.browser, 'umd')],
76-
plugins
77-
}
78-
];
70+
const es5BundleBabelConfig = {
71+
babelHelpers: 'bundled',
72+
presets: [
73+
[
74+
'@babel/preset-env',
75+
{
76+
targets: '> 0.25%, not dead, ie 11'
77+
}
78+
]
79+
]
80+
};
81+
82+
let builds = [];
7983

80-
if (isTest262) {
84+
if (isTest262Build) {
8185
builds = [
8286
{
83-
input: 'lib/init.ts',
87+
input: 'tsc-out/init.js',
8488
output: {
8589
name: libName,
8690
file: 'dist/script.js',
8791
format: 'iife',
8892
sourcemap: true
8993
},
90-
plugins
94+
plugins: withPlugins({
95+
debugBuild: false, // Test262 tests don't pass in debug builds
96+
optimize: isProduction,
97+
babelConfig: isTranspiledBuild ? es5BundleBabelConfig : undefined
98+
})
9199
}
92100
];
93-
}
94-
95-
if (isPlaygroundBuild) {
101+
} else if (isPlaygroundBuild) {
96102
builds = [
97103
{
98-
input: 'lib/init.ts',
104+
input: 'tsc-out/init.js',
99105
output: {
100106
name: libName,
101107
file: 'dist/playground.cjs',
102108
format: 'cjs',
103109
exports: 'named',
104110
sourcemap: true
105111
},
106-
plugins
112+
plugins: withPlugins({
113+
debugBuild: true
114+
})
107115
}
108116
];
117+
} else {
118+
// Production / production-like builds
119+
120+
// - an ES2020 CJS bundle for "main"
121+
// - an ES2020 ESM bundle for "module"
122+
// Note that all dependencies are marked as external and won't be included in
123+
// these bundles.
124+
const modernBuildDef = {
125+
input,
126+
external,
127+
output: [
128+
// ESM bundle
129+
outputEntry(pkg.module, 'es'),
130+
// CJS bundle.
131+
// Note that because package.json specifies "type":"module", the name of
132+
// this file MUST end in ".cjs" in order to be treated as a CommonJS file.
133+
outputEntry(pkg.main, 'cjs')
134+
],
135+
plugins: withPlugins({
136+
debugBuild: !isProduction,
137+
optimize: isProduction
138+
// Here is where we could insert the JSBI -> native BigInt plugin if we
139+
// could find a way to provide a separate bundle for modern browsers
140+
// that can use native BigInt.
141+
// Maybe use node's exports + a user-defined condition?
142+
// https://nodejs.org/api/packages.html#resolving-user-conditions
143+
})
144+
};
145+
// A legacy build that
146+
// - bundles all our dependencies (big-integer) into this file
147+
// - transpiles down to ES5
148+
const legacyUMDBuildDef = {
149+
input,
150+
// UMD bundle for using in script tags, etc
151+
// Note that some build systems don't like reading UMD files if they end in
152+
// '.cjs', so this entry in package.json should end in a .js file extension.
153+
output: [outputEntry(pkg.browser, 'umd')],
154+
plugins: withPlugins({
155+
debugBuild: !isProduction,
156+
optimize: isProduction,
157+
babelConfig: es5BundleBabelConfig
158+
})
159+
};
160+
builds = [modernBuildDef, legacyUMDBuildDef];
109161
}
110162

111163
export default builds;

0 commit comments

Comments
 (0)