Skip to content

Commit d34a8d0

Browse files
committed
Implement better exports strategy supporting both commonjs and es6-style
Keep in mind that this commit only applies for the future v4. We noticed that various applications bundling processes expect various ways of packaging a library, some prefer relying on commonjs-style exports for dependencies, other on es6-style exports. To improve on this situation, this commit implements the following way of providing exports: - Both an ES2017 build with ES6 style exports and an ES5 build with commonJS-style exports are generated. The first one is compiled in `./dist/es2017`, the second one in `./dist/commonjs`. - In our package.json, we have a `main` property leading to the commonjs build, a `module` property leading to the es2017 one and an `exports` property listing all possible import paths, leading to one or the other, depending on properties. I sadly had to change a little the way the RxPlayer tools are declared, now always through a directory and inner `index.js` as the `exports` property syntax does not seem to allow for ambiguity on whether a path import `path.js` or `path/index.js`. I also profited from this commit to only build the demo through esbuild, not webpack anymore, as we didn't really rely on the latter anymore.
1 parent 9ff9bb9 commit d34a8d0

32 files changed

+377
-411
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = {
1010
],
1111
"parser": "@typescript-eslint/parser",
1212
"parserOptions": {
13-
"project": "tsconfig.json",
13+
"project": "tsconfig.eslint.json",
1414
"sourceType": "module"
1515
},
1616
"plugins": [

FILES.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ At the time of writing, there are two distinct demos:
2222

2323
The `dist/` directory stores the player builds of the last version released.
2424

25-
Two directories, namely ``_esm5.raw`` and ``_esm5.processed`` can also be
26-
generated in here if the right scripts are called.
25+
Directories can also be generated in here if the right scripts are called.
2726
These allow to publish more modular codebases to npm.
2827

2928

jest.config.js

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module.exports = {
66
roots: ["<rootDir>/src"],
77
preset: "ts-jest",
88
testEnvironment: "jsdom",
9-
testMatch: ["**/?(*.)+(spec|test).[jt]s?(x)"],
9+
testMatch: ["**/?(*.)+(test).[jt]s"],
1010
collectCoverageFrom: [
1111
"src/**/*.ts",
1212
"!src/**/index.ts",
@@ -16,26 +16,7 @@ module.exports = {
1616
"^.+\\.tsx?$": [
1717
"ts-jest",
1818
{
19-
tsconfig: {
20-
target: "es2017",
21-
lib: ["es2017", "dom"],
22-
forceConsistentCasingInFileNames: true,
23-
skipLibCheck: false,
24-
noImplicitAny: true,
25-
strict: true,
26-
strictNullChecks: true,
27-
strictPropertyInitialization: true,
28-
noUnusedParameters: true,
29-
noUnusedLocals: true,
30-
types: ["jest"],
31-
module: "es2015",
32-
moduleResolution: "node",
33-
esModuleInterop: true,
34-
typeRoots: [
35-
"./src/typings",
36-
"./node_modules/@types",
37-
],
38-
},
19+
tsconfig: "tsconfig.jest.json",
3920
},
4021
],
4122
},

package.json

Lines changed: 102 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,103 @@
33
"author": "Canal+",
44
"version": "4.0.0-beta.3",
55
"description": "Canal+ HTML5 Video Player",
6-
"main": "./dist/_esm5.processed/index.js",
6+
"main": "./dist/commonjs/index.js",
7+
"module": "./dist/es2017/index.js",
8+
"browser": "./dist/es2017/index.js",
9+
"exports": {
10+
".": {
11+
"node": "./dist/commonjs/index.js",
12+
"es2015": "./dist/es2017/index.js",
13+
"require": "./dist/commonjs/index.js",
14+
"import": "./dist/es2017/index.js",
15+
"default": "./dist/es2017/index.js"
16+
},
17+
"./experimental": {
18+
"node": "./dist/commonjs/experimental/index.js",
19+
"es2015": "./dist/es2017/experimental/index.js",
20+
"require": "./dist/commonjs/experimental/index.js",
21+
"import": "./dist/es2017/experimental/index.js",
22+
"default": "./dist/es2017/experimental/index.js"
23+
},
24+
"./experimental/features": {
25+
"node": "./dist/commonjs/experimental/features/index.js",
26+
"es2015": "./dist/es2017/experimental/features/index.js",
27+
"require": "./dist/commonjs/experimental/features/index.js",
28+
"import": "./dist/es2017/experimental/features/index.js",
29+
"default": "./dist/es2017/experimental/features/index.js"
30+
},
31+
"./experimental/features/*": {
32+
"node": "./dist/commonjs/experimental/features/*.js",
33+
"es2015": "./dist/es2017/experimental/features/*.js",
34+
"require": "./dist/commonjs/experimental/features/*.js",
35+
"import": "./dist/es2017/experimental/features/*.js",
36+
"default": "./dist/es2017/experimental/features/*.js"
37+
},
38+
"./experimental/tools": {
39+
"node": "./dist/commonjs/experimental/tools/index.js",
40+
"es2015": "./dist/es2017/experimental/tools/index.js",
41+
"require": "./dist/commonjs/experimental/tools/index.js",
42+
"import": "./dist/es2017/experimental/tools/index.js",
43+
"default": "./dist/es2017/experimental/tools/index.js"
44+
},
45+
"./experimental/tools/*": {
46+
"node": "./dist/commonjs/experimental/tools/*/index.js",
47+
"es2015": "./dist/es2017/experimental/tools/*/index.js",
48+
"require": "./dist/commonjs/experimental/tools/*/index.js",
49+
"import": "./dist/es2017/experimental/tools/*/index.js",
50+
"default": "./dist/es2017/experimental/tools/*/index.js"
51+
},
52+
"./features": {
53+
"node": "./dist/commonjs/features/list/index.js",
54+
"es2015": "./dist/es2017/features/list/index.js",
55+
"require": "./dist/commonjs/features/list/index.js",
56+
"import": "./dist/es2017/features/list/index.js",
57+
"default": "./dist/es2017/features/list/index.js"
58+
},
59+
"./features/*": {
60+
"node": "./dist/commonjs/features/list/*.js",
61+
"es2015": "./dist/es2017/features/list/*.js",
62+
"require": "./dist/commonjs/features/list/*.js",
63+
"import": "./dist/es2017/features/list/*.js",
64+
"default": "./dist/es2017/features/list/*.js"
65+
},
66+
"./logger": {
67+
"node": "./dist/commonjs/log.js",
68+
"es2015": "./dist/es2017/log.js",
69+
"require": "./dist/commonjs/log.js",
70+
"import": "./dist/es2017/log.js",
71+
"default": "./dist/es2017/log.js"
72+
},
73+
"./minimal": {
74+
"node": "./dist/commonjs/minimal.js",
75+
"es2015": "./dist/es2017/minimal.js",
76+
"require": "./dist/commonjs/minimal.js",
77+
"import": "./dist/es2017/minimal.js",
78+
"default": "./dist/es2017/minimal.js"
79+
},
80+
"./tools": {
81+
"node": "./dist/commonjs/tools/index.js",
82+
"es2015": "./dist/es2017/tools/index.js",
83+
"require": "./dist/commonjs/tools/index.js",
84+
"import": "./dist/es2017/tools/index.js",
85+
"default": "./dist/es2017/tools/index.js"
86+
},
87+
"./tools/*": {
88+
"node": "./dist/commonjs/tools/*/index.js",
89+
"es2015": "./dist/es2017/tools/*/index.js",
90+
"require": "./dist/commonjs/tools/*/index.js",
91+
"import": "./dist/es2017/tools/*/index.js",
92+
"default": "./dist/es2017/tools/*/index.js"
93+
},
94+
"./types": {
95+
"node": "./dist/commonjs/types/index.js",
96+
"es2015": "./dist/es2017/types/index.js",
97+
"require": "./dist/commonjs/types/index.js",
98+
"import": "./dist/es2017/types/index.js",
99+
"default": "./dist/es2017/types/index.js"
100+
},
101+
"./package.json": "./package.json"
102+
},
7103
"keywords": [
8104
"dash",
9105
"eme",
@@ -17,7 +113,6 @@
17113
"typescript",
18114
"video"
19115
],
20-
"browser": "./dist/_esm5.processed/index.js",
21116
"homepage": "https://github.com/canalplus/rx-player",
22117
"bugs": "https://github.com/canalplus/rx-player/issues",
23118
"license": "Apache-2.0",
@@ -42,9 +137,9 @@
42137
"check:types:watch": "tsc --noEmit --watch --project .",
43138
"check:demo": "npm run check:demo:types && npm run lint:demo",
44139
"check:demo:types": "tsc --noEmit --project demo/full",
45-
"demo": "node ./scripts/generate_full_demo.js --production-mode",
46-
"demo:min": "node ./scripts/generate_full_demo.js --production-mode --minify",
47-
"demo:watch": "node ./scripts/generate_full_demo.js --watch --production-mode",
140+
"demo": "node ./scripts/build_demo.mjs --production-mode",
141+
"demo:min": "node ./scripts/build_demo.mjs --production-mode --minify",
142+
"demo:watch": "node ./scripts/build_demo.mjs --watch --production-mode",
48143
"doc": "readme.doc doc/ doc/generated \"$(cat VERSION)\"",
49144
"lint": "eslint src -c .eslintrc.js",
50145
"lint:demo": "eslint -c demo/full/.eslintrc.js demo/full/scripts",
@@ -53,10 +148,8 @@
53148
"prepublishOnly": "npm run build:all",
54149
"releases:dev": "./scripts/make-dev-releases",
55150
"standalone": "node ./scripts/run_standalone_demo.js",
56-
"start": "node ./scripts/start_demo_web_server.js",
57-
"start:wasm": "node ./scripts/start_demo_web_server.js --include-wasm",
58-
"s": "node ./scripts/start_demo_web_server.js --fast",
59-
"s:wasm": "node ./scripts/start_demo_web_server.js --fast --include-wasm",
151+
"start": "node ./scripts/start_demo_web_server.mjs",
152+
"sart:wasm": "node ./scripts/start_demo_web_server.mjs --include-wasm",
60153
"wasm-strip": "node scripts/wasm-strip.js dist/mpd-parser.wasm",
61154
"test:appveyor": "npm run test:unit && npm run test:memory",
62155
"test:integration": "node tests/integration/run.js --bchromehl --bfirefoxhl",
@@ -137,8 +230,6 @@
137230
"Build a demo page (e.g. to test a code change)": {
138231
"start": "Build the \"full\" demo (with a UI) with the non-minified RxPlayer and serve it on a local server. Re-build on file updates.",
139232
"start:wasm": "Build the \"full\" demo (with a UI) with the non-minified RxPlayer including the DASH WebAssembly MPD parser and serve it on a local server. Re-build on file updates.",
140-
"s": "Very fast version of `start` which does not perform type-checking. This script can be useful for quick testing",
141-
"s:wasm": "Very fast version of `start:wasm` which does not perform type-checking. This script can be useful for quick testing",
142233
"demo": "Build the demo in demo/bundle.js",
143234
"demo:min": "Build the demo and minify it in demo/bundle.js",
144235
"demo:watch": "Build the demo in demo/bundle.js each times the files update.",

scripts/build/generate_build.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,13 @@ const os = require("os");
2424
const path = require("path");
2525
const { rimraf } = require("rimraf");
2626

27-
const BUILD_DIR_FROM_ROOT = "dist/_esm5.processed";
27+
const BUILD_DIR_FROM_ROOT_FOR_TEMPLATES = "dist/es2017";
2828

2929
const ROOT_DIR = path.join(__dirname, "../../");
3030
const TEMPLATE_DIR = path.join(__dirname, "../../scripts/build/templates");
31-
const TMP_BUILD_DIR = path.join(ROOT_DIR, "dist/_esm5.raw");
32-
const BUILD_DIR = path.join(ROOT_DIR, BUILD_DIR_FROM_ROOT);
3331
const BUILD_ARTEFACTS_TO_REMOVE = [
34-
"dist/_esm5.processed",
35-
"dist/_esm5.raw",
32+
"dist/commonjs",
33+
"dist/es2017",
3634
"features",
3735
"minimal",
3836
"experimental",
@@ -54,9 +52,6 @@ async function generateBuild() {
5452
console.log(" ⚙️ Compiling project with TypeScript...");
5553
await compile();
5654

57-
console.log(" 🚚 Moving built code to its final directory...");
58-
await fs.rename(TMP_BUILD_DIR, BUILD_DIR);
59-
6055
console.log(" 📦 Generating imported files from templates...");
6156
await generateImportFilesFromTemplates();
6257
} catch (err) {
@@ -86,15 +81,22 @@ async function removePreviousBuildArtefacts() {
8681
* @returns {Promise}
8782
*/
8883
async function compile() {
89-
// Sadly TypeScript compiler API seems to be sub-par.
90-
// I did not find for example how to exclude some files (our unit tests)
91-
// easily by running typescript directly from NodeJS.
92-
// So we just spawn a separate process running tsc:
93-
await spawnProm(
84+
// Sadly TypeScript compiler API seems to be sub-par.
85+
// I did not find for example how to exclude some files (our unit tests)
86+
// easily by running typescript directly from NodeJS.
87+
// So we just spawn a separate process running tsc:
88+
await Promise.all([
89+
spawnProm(
9490
"npx tsc -p",
95-
[path.join(ROOT_DIR, "tsconfig.modules.json")],
96-
(code) => new Error(`Compilation process exited with code ${code}`),
97-
);
91+
[path.join(ROOT_DIR, "tsconfig.json")],
92+
(code) => new Error(`CommonJS compilation process exited with code ${code}`),
93+
),
94+
spawnProm(
95+
"npx tsc -p",
96+
[path.join(ROOT_DIR, "tsconfig.commonjs.json")],
97+
(code) => new Error(`es2018 compilation process exited with code ${code}`),
98+
)
99+
]);
98100
}
99101

100102
/**
@@ -165,7 +167,7 @@ async function copyDir(src, dest) {
165167
* @returns {Promise}
166168
*/
167169
async function replaceTokensInTemplates(fileDest) {
168-
const sedDir = BUILD_DIR_FROM_ROOT.replace(/\//g, "\\/");
170+
const sedDir = BUILD_DIR_FROM_ROOT_FOR_TEMPLATES.replace(/\//g, "\\/");
169171
switch (os.type()) {
170172
case "Darwin":
171173
await spawnProm(

scripts/build/templates/experimental/tools/VideoThumbnailLoader/index.d.ts renamed to scripts/build/templates/experimental/tools/VideoThumbnailLoader.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import VideoThumbnailLoader from "../../../__BUILD_DIR__/experimental/tools/VideoThumbnailLoader/index";
17-
export * from "../../../__BUILD_DIR__/experimental/tools/VideoThumbnailLoader/index";
16+
import VideoThumbnailLoader from "../../__BUILD_DIR__/experimental/tools/VideoThumbnailLoader/index";
17+
export * from "../../__BUILD_DIR__/experimental/tools/VideoThumbnailLoader/index";
1818
export default VideoThumbnailLoader;

scripts/build/templates/experimental/tools/VideoThumbnailLoader/index.js renamed to scripts/build/templates/experimental/tools/VideoThumbnailLoader.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import VideoThumbnailLoader from "../../../__BUILD_DIR__/experimental/tools/VideoThumbnailLoader/index.js";
17-
export * from "../../../__BUILD_DIR__/experimental/tools/VideoThumbnailLoader/index.js";
16+
import VideoThumbnailLoader from "../../__BUILD_DIR__/experimental/tools/VideoThumbnailLoader/index.js";
17+
export * from "../../__BUILD_DIR__/experimental/tools/VideoThumbnailLoader/index.js";
1818
export default VideoThumbnailLoader;

scripts/build/constants.d.ts renamed to scripts/build/templates/experimental/tools/createMetaplaylist.d.ts

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,6 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
17-
/* eslint-disable @typescript-eslint/naming-convention */
18-
19-
// Define build-time constants corresponding to the regular build.
20-
21-
declare const enum __LOGGER_LEVEL__ {
22-
CURRENT_LEVEL = "NONE",
23-
}
24-
25-
declare const enum __ENVIRONMENT__ {
26-
PRODUCTION = 0,
27-
DEV = 1,
28-
CURRENT_ENV = PRODUCTION,
29-
}
30-
31-
declare const __RX_PLAYER_DEBUG_MODE__ : boolean | undefined;
16+
import createMetaplaylist from "../../__BUILD_DIR__/experimental/tools/createMetaplaylist/index";
17+
export * from "../../__BUILD_DIR__/experimental/tools/createMetaplaylist/index";
18+
export default createMetaplaylist;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Copyright 2015 CANAL+ Group
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
import createMetaplaylist from "../../__BUILD_DIR__/experimental/tools/createMetaplaylist/index.js";
17+
export * from "../../__BUILD_DIR__/experimental/tools/createMetaplaylist/index.js";
18+
export default createMetaplaylist;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Copyright 2015 CANAL+ Group
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
import mediaCapabilitiesProber from "../../__BUILD_DIR__/experimental/tools/mediaCapabilitiesProber/index";
17+
export * from "../../__BUILD_DIR__/experimental/tools/mediaCapabilitiesProber/index";
18+
export default mediaCapabilitiesProber;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Copyright 2015 CANAL+ Group
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
import mediaCapabilitiesProber from "../../__BUILD_DIR__/experimental/tools/mediaCapabilitiesProber/index.js";
17+
export * from "../../__BUILD_DIR__/experimental/tools/mediaCapabilitiesProber/index.js";
18+
export default mediaCapabilitiesProber;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Copyright 2015 CANAL+ Group
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
import parseBIFThumbnails from "../../__BUILD_DIR__/experimental/tools/parseBIFThumbnails/index";
17+
export * from "../../__BUILD_DIR__/experimental/tools/parseBIFThumbnails/index";
18+
export default parseBIFThumbnails;

0 commit comments

Comments
 (0)