1 | import { Parser } from 'acorn';
|
2 | import acornJsx from 'acorn-jsx';
|
3 | import acornDynamicImport from 'acorn-dynamic-import';
|
4 | import Program from './program/Program.js';
|
5 | import { features, matrix } from './support.js';
|
6 | import getSnippet from './utils/getSnippet.js';
|
7 |
|
8 | const parser = Parser.extend(acornDynamicImport, acornJsx());
|
9 |
|
10 | const dangerousTransforms = ['dangerousTaggedTemplateString', 'dangerousForOf'];
|
11 |
|
12 | export function target(target) {
|
13 | const targets = Object.keys(target);
|
14 | let bitmask = targets.length
|
15 | ? 0b11111111111111111111111
|
16 | : 0b00010000000000000000001;
|
17 |
|
18 | Object.keys(target).forEach(environment => {
|
19 | const versions = matrix[environment];
|
20 | if (!versions)
|
21 | throw new Error(
|
22 | `Unknown environment '${environment}'. Please raise an issue at https://github.com/bublejs/buble/issues`
|
23 | );
|
24 |
|
25 | const targetVersion = target[environment];
|
26 | if (!(targetVersion in versions))
|
27 | throw new Error(
|
28 | `Support data exists for the following versions of ${environment}: ${Object.keys(
|
29 | versions
|
30 | ).join(
|
31 | ', '
|
32 | )}. Please raise an issue at https://github.com/bublejs/buble/issues`
|
33 | );
|
34 | const support = versions[targetVersion];
|
35 |
|
36 | bitmask &= support;
|
37 | });
|
38 |
|
39 | const transforms = Object.create(null);
|
40 | features.forEach((name, i) => {
|
41 | transforms[name] = !(bitmask & (1 << i));
|
42 | });
|
43 |
|
44 | dangerousTransforms.forEach(name => {
|
45 | transforms[name] = false;
|
46 | });
|
47 |
|
48 | return transforms;
|
49 | }
|
50 |
|
51 | export function transform(source, options = {}) {
|
52 | let ast;
|
53 | let jsx = null;
|
54 |
|
55 | try {
|
56 | ast = parser.parse(source, {
|
57 | ecmaVersion: 10,
|
58 | preserveParens: true,
|
59 | sourceType: 'module',
|
60 | allowAwaitOutsideFunction: true,
|
61 | allowReturnOutsideFunction: true,
|
62 | allowHashBang: true,
|
63 | onComment: (block, text) => {
|
64 | if (!jsx) {
|
65 | const match = /@jsx\s+([^\s]+)/.exec(text);
|
66 | if (match) jsx = match[1];
|
67 | }
|
68 | }
|
69 | });
|
70 | options.jsx = jsx || options.jsx;
|
71 | } catch (err) {
|
72 | err.snippet = getSnippet(source, err.loc);
|
73 | err.toString = () => `${err.name}: ${err.message}\n${err.snippet}`;
|
74 | throw err;
|
75 | }
|
76 |
|
77 | const transforms = target(options.target || {});
|
78 | Object.keys(options.transforms || {}).forEach(name => {
|
79 | if (name === 'modules') {
|
80 | if (!('moduleImport' in options.transforms))
|
81 | transforms.moduleImport = options.transforms.modules;
|
82 | if (!('moduleExport' in options.transforms))
|
83 | transforms.moduleExport = options.transforms.modules;
|
84 | return;
|
85 | }
|
86 |
|
87 | if (!(name in transforms)) throw new Error(`Unknown transform '${name}'`);
|
88 | transforms[name] = options.transforms[name];
|
89 | });
|
90 | if (options.objectAssign === true) options.objectAssign = 'Object.assign';
|
91 | return new Program(source, ast, transforms, options).export(options);
|
92 | }
|
93 |
|
94 | export { version as VERSION } from '../package.json';
|