Skip to content

Commit aef82c2

Browse files
authored
Switch dev environment to native ESM to support Node 12+ (#10367)
* use native ES modules for unit tests * update CircleCI Node image to v14.15 * bump yarn cache key on Circle CI * expose bundles as CommonJS in Node * upgrade postcss and use CJS for config * convert harness and some scripts to esm * fix lint and flow * make sure testem runs with native esm * testem fixups * use custom json loader to support named exports * use a valid access token in unit tests * work around testem refusing to load cjs configs * fix lint * fix testem runs on CI hopefully * fix webpack build
1 parent 2fc685b commit aef82c2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+966
-425
lines changed

.circleci/config.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,11 @@ workflows:
102102
only: /v[0-9]+.[0-9]+.[0-9]+(-.+)?/
103103
branches:
104104
ignore: /.*/
105-
105+
106106

107107
defaults: &defaults
108108
docker:
109-
- image: circleci/node:10.16-browsers
109+
- image: circleci/node:14.15-browsers
110110
working_directory: ~/mapbox-gl-js
111111

112112
jobs:
@@ -116,10 +116,10 @@ jobs:
116116
- checkout
117117
- restore_cache:
118118
keys:
119-
- v3-yarn-{{ checksum "yarn.lock" }}
119+
- v4-yarn-{{ checksum "yarn.lock" }}
120120
- run: yarn
121121
- save_cache:
122-
key: v3-yarn-{{ checksum "yarn.lock" }}
122+
key: v4-yarn-{{ checksum "yarn.lock" }}
123123
paths:
124124
- '~/.yarn'
125125
- 'node_modules'

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/rollup/build/
2-
/dist/
2+
/dist/mapbox-gl*
3+
/dist/versions*
4+
/dist/style-spec
35
*.es.js
46
*.js.map
57
node_modules

bench/rollup_config_benchmarks.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import fs from 'fs';
22
import sourcemaps from 'rollup-plugin-sourcemaps';
3-
import replace from 'rollup-plugin-replace';
3+
import replace from '@rollup/plugin-replace';
44
import {plugins} from '../build/rollup_plugins.js';
5-
import resolve from 'rollup-plugin-node-resolve';
6-
import commonjs from 'rollup-plugin-commonjs';
5+
import resolve from '@rollup/plugin-node-resolve';
6+
import commonjs from '@rollup/plugin-commonjs';
77

88
let styles = ['mapbox://styles/mapbox/streets-v10'];
99

build/check-bundle-size.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
#!/usr/bin/env node
22

3-
/* eslint-disable */
4-
const { Octokit } = require("@octokit/rest");
5-
const { createAppAuth } = require("@octokit/auth-app");
6-
const prettyBytes = require('pretty-bytes');
7-
const fs = require('fs');
8-
const {execSync} = require('child_process');
9-
const zlib = require('zlib');
3+
import { Octokit } from "@octokit/rest";
4+
import { createAppAuth } from "@octokit/auth-app";
5+
import prettyBytes from 'pretty-bytes';
6+
import fs from 'fs';
7+
import {execSync} from 'child_process';
8+
import zlib from 'zlib';
109

1110
process.on('unhandledRejection', error => {
1211
// don't log `error` directly, because errors from child_process.execSync

build/diff-tarball.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const packlist = require('npm-packlist')
2-
const npmContent = require('list-npm-contents');
1+
import packlist from 'npm-packlist';
2+
import npmContent from 'list-npm-contents';
33

44
npmContent('mapbox-gl').then(function(last_version_files) {
55
packlist({ path: '.' }).then(function(new_version_files) {
@@ -15,4 +15,4 @@ npmContent('mapbox-gl').then(function(last_version_files) {
1515
console.log('-', file);
1616
});
1717
});
18-
});
18+
});

build/generate-access-token-script.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
/* eslint-disable */
2-
'use strict';
3-
const fs = require('fs');
4-
const path = require('path');
5-
const script = fs.readFileSync(path.join(__dirname, '../debug/access_token.js'), 'utf-8')
1+
import fs from 'fs';
2+
3+
const script = fs.readFileSync(new URL('../debug/access_token.js', import.meta.url), 'utf-8')
64
.replace('process.env.MapboxAccessToken',
75
JSON.stringify(process.env.MapboxAccessToken))
86
.replace('process.env.MAPBOX_ACCESS_TOKEN',
97
JSON.stringify(process.env.MAPBOX_ACCESS_TOKEN));
108

11-
fs.writeFileSync(path.join(__dirname, '../debug/access_token_generated.js'), script);
9+
fs.writeFileSync(new URL('../debug/access_token_generated.js', import.meta.url), script);

build/generate-flow-typed-style-spec.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
const spec = require('../src/style-spec/reference/v8.json');
2-
const properties = require('../src/style-spec/util/properties');
3-
const fs = require('fs');
1+
2+
import fs from 'fs';
3+
import path from 'path';
4+
import {supportsPropertyExpression, supportsZoomExpression} from '../src/style-spec/util/properties.js';
5+
import spec from '../src/style-spec/reference/v8.json';
46

57
function flowEnum(values) {
68
if (Array.isArray(values)) {
@@ -43,9 +45,9 @@ function flowType(property) {
4345
}
4446
})();
4547

46-
if (properties.supportsPropertyExpression(property)) {
48+
if (supportsPropertyExpression(property)) {
4749
return `DataDrivenPropertyValueSpecification<${baseType}>`;
48-
} else if (properties.supportsZoomExpression(property)) {
50+
} else if (supportsZoomExpression(property)) {
4951
return `PropertyValueSpecification<${baseType}>`;
5052
} else if (property.expression) {
5153
return `ExpressionSpecification`;

build/generate-release-list.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const octokit = require('@octokit/rest')();
2-
const fs = require('fs');
1+
import octokit from '@octokit/rest';
2+
import fs from 'fs';
33

44
const list = {};
55

6-
octokit
6+
octokit()
77
.paginate(octokit.repos.listReleases.endpoint({
88
owner: 'mapbox',
99
repo: 'mapbox-gl-js'

build/node-loader.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import flowRemoveTypes from '@mapbox/flow-remove-types';
2+
import {dataToEsm} from '@rollup/pluginutils';
3+
4+
const glslRe = /\.glsl$/;
5+
const jsonRe = /\.json$/;
6+
7+
export function resolve(specifier, context, defaultResolve) {
8+
if (glslRe.test(specifier)) {
9+
const url = new URL(specifier, context.parentURL).href;
10+
return {url};
11+
}
12+
return defaultResolve(specifier, context, defaultResolve);
13+
}
14+
15+
export function getFormat(url, context, defaultGetFormat) {
16+
if (glslRe.test(url) || jsonRe.test(url)) {
17+
return {format: 'module'};
18+
}
19+
return defaultGetFormat(url, context, defaultGetFormat);
20+
}
21+
22+
export function transformSource(source, context, defaultTransformSource) {
23+
if (source.indexOf('@flow') >= 0) {
24+
source = flowRemoveTypes(source.toString()).toString();
25+
return {source};
26+
}
27+
if (glslRe.test(context.url)) {
28+
return {source: `export default \`${source}\``};
29+
}
30+
if (jsonRe.test(context.url)) {
31+
source = dataToEsm(JSON.parse(source), {
32+
preferConst: true,
33+
namedExports: true,
34+
indent: ' '
35+
});
36+
return {source};
37+
}
38+
return defaultTransformSource(source, context, defaultTransformSource);
39+
}

build/rollup_plugins.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11

22
import flowRemoveTypes from '@mapbox/flow-remove-types';
3-
import resolve from 'rollup-plugin-node-resolve';
4-
import commonjs from 'rollup-plugin-commonjs';
3+
import resolve from '@rollup/plugin-node-resolve';
4+
import commonjs from '@rollup/plugin-commonjs';
55
import unassert from 'rollup-plugin-unassert';
6-
import json from 'rollup-plugin-json';
6+
import json from '@rollup/plugin-json';
77
import {terser} from 'rollup-plugin-terser';
8-
import minifyStyleSpec from './rollup_plugin_minify_style_spec';
9-
import {createFilter} from 'rollup-pluginutils';
8+
import minifyStyleSpec from './rollup_plugin_minify_style_spec.js';
9+
import {createFilter} from '@rollup/pluginutils';
1010
import strip from '@rollup/plugin-strip';
11-
import replace from 'rollup-plugin-replace';
11+
import replace from '@rollup/plugin-replace';
1212

1313
// Common set of plugins/transformations shared across different rollup
1414
// builds (main mapboxgl bundle, style-spec package, benchmarks bundle)

build/run-node

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#!/usr/bin/env bash
22

3-
node -r @mapbox/flow-remove-types/register -r esm ${@}
3+
node --no-warnings --experimental-loader ./build/node-loader.js ${@}

build/run-tap

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ else
55
arg="${@}"
66
fi
77

8-
node_modules/.bin/tap --node-arg -r --node-arg @mapbox/flow-remove-types/register --node-arg -r --node-arg esm $arg --node-arg
8+
node_modules/.bin/tap --node-arg --no-warnings --node-arg --experimental-loader --node-arg ./build/node-loader.js $arg --node-arg

build/test/build-tape.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
/* eslint-disable import/no-commonjs */
21
/* eslint-disable flowtype/require-valid-file-annotation */
3-
const browserify = require('browserify');
4-
const fs = require('fs');
2+
import browserify from 'browserify';
3+
import fs from 'fs';
4+
import {fileURLToPath} from 'url';
55

6-
module.exports = function() {
6+
export default function() {
77
return new Promise((resolve, reject) => {
8-
browserify(require.resolve('../../test/util/tape_config.js'), { standalone: 'tape' })
8+
browserify(fileURLToPath(new URL('../../test/util/tape_config.js', import.meta.url)), { standalone: 'tape' })
99
.transform("babelify", {presets: ["@babel/preset-env"], global: true, compact: true})
1010
.bundle((err, buff) => {
1111
if (err) { throw err; }

dist/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{
2+
}

package.json

+17-17
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"main": "dist/mapbox-gl.js",
66
"style": "dist/mapbox-gl.css",
77
"license": "SEE LICENSE IN LICENSE.txt",
8+
"type": "module",
89
"repository": {
910
"type": "git",
1011
"url": "git://github.com/mapbox/mapbox-gl-js.git"
@@ -39,13 +40,17 @@
3940
},
4041
"devDependencies": {
4142
"@babel/core": "^7.9.0",
42-
"@mapbox/flow-remove-types": "^1.3.0-await.upstream.2",
43+
"@mapbox/flow-remove-types": "^2.0.0",
4344
"@mapbox/gazetteer": "^4.0.4",
4445
"@mapbox/mapbox-gl-rtl-text": "^0.2.1",
4546
"@mapbox/mvt-fixtures": "^3.6.0",
4647
"@octokit/auth-app": "^2.4.7",
4748
"@octokit/rest": "^18.0.0",
48-
"@rollup/plugin-strip": "^1.3.1",
49+
"@rollup/plugin-commonjs": "^17.1.0",
50+
"@rollup/plugin-json": "^4.1.0",
51+
"@rollup/plugin-node-resolve": "^11.1.1",
52+
"@rollup/plugin-replace": "^2.3.4",
53+
"@rollup/plugin-strip": "^2.0.0",
4954
"address": "^1.1.2",
5055
"babel-eslint": "^10.0.1",
5156
"babelify": "^10.0.0",
@@ -66,7 +71,6 @@
6671
"eslint-plugin-import": "^2.16.0",
6772
"eslint-plugin-jsdoc": "^17.1.2",
6873
"eslint-plugin-react": "^7.12.4",
69-
"esm": "~3.0.84",
7074
"flow-bin": "^0.100.0",
7175
"gl": "^4.5.3",
7276
"glob": "^7.1.4",
@@ -86,21 +90,18 @@
8690
"nyc": "^13.3.0",
8791
"pirates": "^4.0.1",
8892
"pixelmatch": "^5.1.0",
89-
"postcss-cli": "^6.1.2",
90-
"postcss-inline-svg": "^3.1.1",
93+
"postcss": "^8.2.4",
94+
"postcss-cli": "^8.3.1",
95+
"postcss-inline-svg": "^5.0.0",
9196
"pretty-bytes": "^5.1.0",
9297
"puppeteer": "^1.18.0",
9398
"qrcode-terminal": "^0.12.0",
9499
"react": "^16.8.6",
95100
"react-dom": "^16.8.6",
96101
"request": "^2.88.0",
97-
"rollup": "^1.23.1",
98-
"rollup-plugin-commonjs": "^10.1.0",
99-
"rollup-plugin-json": "^4.0.0",
100-
"rollup-plugin-node-resolve": "^5.2.0",
101-
"rollup-plugin-replace": "^2.2.0",
102-
"rollup-plugin-sourcemaps": "^0.4.2",
103-
"rollup-plugin-terser": "^5.1.2",
102+
"rollup": "^2.38.4",
103+
"rollup-plugin-sourcemaps": "^0.6.3",
104+
"rollup-plugin-terser": "^7.0.2",
104105
"rollup-plugin-unassert": "^0.3.0",
105106
"selenium-webdriver": "^4.0.0-alpha.5",
106107
"shuffle-seed": "^1.1.6",
@@ -118,7 +119,6 @@
118119
"./src/util/window.js": "./src/util/browser/window.js",
119120
"./src/util/web_worker.js": "./src/util/browser/web_worker.js"
120121
},
121-
"esm": true,
122122
"scripts": {
123123
"build-dev": "rollup -c --environment BUILD:dev",
124124
"watch-dev": "rollup -c --environment BUILD:dev --watch",
@@ -150,10 +150,10 @@
150150
"test-unit": "build/run-tap --reporter classic --no-coverage test/unit",
151151
"test-build": "build/run-tap --no-coverage test/build/**/*.test.js",
152152
"test-browser": "build/run-tap --reporter spec --no-coverage test/browser/**/*.test.js",
153-
"watch-render": "SUITE_NAME=render testem -f test/integration/testem.js",
154-
"watch-query": "SUITE_NAME=query testem -f test/integration/testem.js",
155-
"test-render": "SUITE_NAME=render CI=true testem ci -f test/integration/testem.js",
156-
"test-query": "SUITE_NAME=query CI=true testem ci -f test/integration/testem.js",
153+
"watch-render": "SUITE_NAME=render testem -f test/integration/testem/testem.js",
154+
"watch-query": "SUITE_NAME=query testem -f test/integration/testem/testem.js",
155+
"test-render": "SUITE_NAME=render CI=true testem ci -f test/integration/testem/testem.js",
156+
"test-query": "SUITE_NAME=query CI=true testem ci -f test/integration/testem/testem.js",
157157
"test-expressions": "build/run-node test/expression.test.js",
158158
"test-flow": "build/run-node build/generate-flow-typed-style-spec && flow .",
159159
"test-cov": "nyc --require=@mapbox/flow-remove-types/register --reporter=text-summary --reporter=lcov --cache run-s test-unit test-expressions test-query test-render",
File renamed without changes.

rollup.config.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import fs from 'fs';
22
import sourcemaps from 'rollup-plugin-sourcemaps';
3-
import {plugins} from './build/rollup_plugins';
4-
import banner from './build/banner';
3+
import {plugins} from './build/rollup_plugins.js';
4+
import banner from './build/banner.js';
5+
import {fileURLToPath} from 'url';
56

67
const {BUILD, MINIFY} = process.env;
78
const minified = MINIFY === 'true';
@@ -10,14 +11,14 @@ const bench = BUILD === 'bench';
1011

1112
function buildType(build, minified) {
1213
switch (build) {
13-
case 'production':
14+
case 'production':
1415
if (minified) return 'dist/mapbox-gl.js';
1516
return 'dist/mapbox-gl-unminified.js';
1617
case 'bench':
1718
return 'dist/mapbox-gl-bench.js';
1819
case 'dev':
1920
return 'dist/mapbox-gl-dev.js';
20-
default:
21+
default:
2122
return 'dist/mapbox-gl-dev.js';
2223
}
2324
}
@@ -53,7 +54,7 @@ export default [{
5354
format: 'umd',
5455
sourcemap: production ? true : 'inline',
5556
indent: false,
56-
intro: fs.readFileSync(require.resolve('./rollup/bundle_prelude.js'), 'utf8'),
57+
intro: fs.readFileSync(fileURLToPath(new URL('./rollup/bundle_prelude.js', import.meta.url)), 'utf8'),
5758
banner
5859
},
5960
treeshake: false,

src/render/painter.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import rasterBoundsAttributes from '../data/raster_bounds_attributes.js';
1414
import posAttributes from '../data/pos_attributes.js';
1515
import ProgramConfiguration from '../data/program_configuration.js';
1616
import CrossTileSymbolIndex from '../symbol/cross_tile_symbol_index.js';
17-
import * as shaders from '../shaders/index.js';
17+
import shaders from '../shaders/shaders.js';
1818
import Program from './program.js';
1919
import {programUniforms} from './program/program_uniforms.js';
2020
import Context from '../gl/context.js';

src/render/program.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @flow
22

3-
import {prelude, preludeTerrain} from '../shaders/index.js';
3+
import {prelude, preludeTerrain} from '../shaders/shaders.js';
44
import assert from 'assert';
55
import ProgramConfiguration from '../data/program_configuration.js';
66
import VertexArrayObject from './vertex_array_object.js';

src/shaders/index.js

-20
This file was deleted.

0 commit comments

Comments
 (0)