Re-enable strict CJS interop
[incubator-annotator.git] / babel.config.js
1 /**
2 * @license
3 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 * use this file except in compliance with the License. You may obtain a copy of
5 * the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 * License for the specific language governing permissions and limitations under
13 * the License.
14 */
15
16 module.exports = api => {
17 const ENV = api.env() || 'development';
18 const DEV = ENV === 'development';
19 const TEST = ENV === 'test';
20 const CJS = ENV === 'cjs';
21
22 // Options for the @babel/transform-modules-commonjs plugin.
23 const cjsOptions = {
24 // Disable require default interop.
25 noInterop: true,
26 // Disable export default interop.
27 strict: true,
28 };
29
30 // Options for the @babel/env preset.
31 const envOptions = {
32 // Do not enable automatic module transformation.
33 modules: false,
34 // Enable proposals that have shipped in browsers.
35 shippedProposals: true,
36 // Set target environments.
37 targets: {
38 // Browsers: > 1%, last 2 versions, Firefox ESR
39 browsers: ['defaults'],
40 // Node: LTS
41 node: '6.0',
42 },
43 // Use a minimal @babel/polyfill.
44 useBuiltIns: 'entry',
45 };
46
47 // Options for the module-resolver plugin.
48 // Used for resolving source files during development and testing.
49 let resolverOptions = {
50 alias: {
51 '^(@annotator/.+?)(/|$)': '\\1/src',
52 },
53 };
54
55 // Options for the @babel/transform-runtime plugin.
56 const runtimeOptions = {
57 // Do not polyfill; leave that to applications.
58 polyfill: false,
59 // Do not import polyfills for helpers.
60 useBuiltIns: true,
61 // Use the module format of the target environment.
62 // TODO: make this work again
63 // useESModules: !CJS,
64 };
65
66 return {
67 plugins: [
68 'preserve-comment-header',
69 ['@babel/transform-runtime', runtimeOptions],
70 ...(CJS ? [['@babel/transform-modules-commonjs', cjsOptions]] : []),
71 ...(DEV || TEST ? [['module-resolver', resolverOptions]] : []),
72 ...(TEST ? ['istanbul'] : []),
73 ],
74 presets: [['@babel/env', envOptions]],
75 };
76 };