1 | "use strict";
|
2 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3 | if (k2 === undefined) k2 = k;
|
4 | Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
5 | }) : (function(o, m, k, k2) {
|
6 | if (k2 === undefined) k2 = k;
|
7 | o[k2] = m[k];
|
8 | }));
|
9 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
10 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
11 | }) : function(o, v) {
|
12 | o["default"] = v;
|
13 | });
|
14 | var __importStar = (this && this.__importStar) || function (mod) {
|
15 | if (mod && mod.__esModule) return mod;
|
16 | var result = {};
|
17 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
18 | __setModuleDefault(result, mod);
|
19 | return result;
|
20 | };
|
21 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
22 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
23 | };
|
24 | Object.defineProperty(exports, "__esModule", { value: true });
|
25 | exports.transform = void 0;
|
26 | const meriyah = __importStar(require("meriyah"));
|
27 | const magic_string_1 = __importDefault(require("magic-string"));
|
28 | const periscopic_1 = require("periscopic");
|
29 | const estree_walker_1 = require("estree-walker");
|
30 | const is_reference_1 = __importDefault(require("is-reference"));
|
31 | function transform(data) {
|
32 | const code = new magic_string_1.default(data);
|
33 | const ast = meriyah.parseModule(data, {
|
34 | ranges: true,
|
35 | next: true,
|
36 | });
|
37 | const { map, scope } = periscopic_1.analyze(ast);
|
38 | const all_identifiers = new Set();
|
39 |
|
40 | estree_walker_1.walk(ast, {
|
41 | enter(node, parent) {
|
42 | if (is_reference_1.default(node, parent)) {
|
43 | all_identifiers.add(node.name);
|
44 | }
|
45 | },
|
46 | });
|
47 |
|
48 | function deconflict(name) {
|
49 | while (all_identifiers.has(name))
|
50 | name += '_';
|
51 | return name;
|
52 | }
|
53 | const exports = deconflict('exports');
|
54 | const __import = deconflict('__import');
|
55 | const __import_meta = deconflict('__import_meta');
|
56 | const __export = deconflict('__export');
|
57 | const __export_all = deconflict('__export_all');
|
58 |
|
59 | let uid = 0;
|
60 | const get_import_name = () => deconflict(`__import${uid++}`);
|
61 | const replacements = new Map();
|
62 | const deps = [];
|
63 | const css = [];
|
64 | ast.body.forEach((node) => {
|
65 | if (node.type === 'ImportDeclaration') {
|
66 | const is_namespace = node.specifiers[0] && node.specifiers[0].type === 'ImportNamespaceSpecifier';
|
67 | const default_specifier = node.specifiers.find((specifier) => !specifier.imported);
|
68 | const name = is_namespace
|
69 | ? node.specifiers[0].local.name
|
70 | : default_specifier
|
71 | ? default_specifier.local.name
|
72 | : get_import_name();
|
73 | const source = node.source.value;
|
74 | if (source.endsWith('.css.proxy.js')) {
|
75 | css.push(source.replace(/\.proxy\.js$/, ''));
|
76 | }
|
77 | else {
|
78 | deps.push({ name, source });
|
79 | if (!is_namespace) {
|
80 | node.specifiers.forEach((specifier) => {
|
81 | const prop = specifier.imported ? specifier.imported.name : 'default';
|
82 | replacements.set(specifier.local.name, `${name}.${prop}`);
|
83 | });
|
84 | }
|
85 | }
|
86 | code.remove(node.start, node.end);
|
87 | }
|
88 | if (node.type === 'ExportAllDeclaration') {
|
89 | const source = node.source.value;
|
90 | const name = get_import_name();
|
91 | deps.push({ name, source });
|
92 | code.overwrite(node.start, node.end, `${__export_all}(${name})`);
|
93 | }
|
94 | if (node.type === 'ExportDefaultDeclaration') {
|
95 | code.overwrite(node.start, node.declaration.start, `${exports}.default = `);
|
96 | }
|
97 | if (node.type === 'ExportNamedDeclaration') {
|
98 | if (node.source) {
|
99 | const name = get_import_name();
|
100 | const source = node.source.value;
|
101 | deps.push({ name, source });
|
102 | const export_block = node.specifiers
|
103 | .map((specifier) => {
|
104 | return `${__export}('${specifier.exported.name}', () => ${name}.${specifier.local.name})`;
|
105 | })
|
106 | .join('; ');
|
107 | code.overwrite(node.start, node.end, export_block);
|
108 | }
|
109 | else if (node.declaration) {
|
110 |
|
111 | code.remove(node.start, node.declaration.start);
|
112 | let suffix;
|
113 | if (node.declaration.type === 'VariableDeclaration') {
|
114 | const names = [];
|
115 | node.declaration.declarations.forEach((declarator) => {
|
116 | names.push(...periscopic_1.extract_names(declarator.id));
|
117 | });
|
118 | suffix = names.map((name) => ` ${__export}('${name}', () => ${name});`).join('');
|
119 | }
|
120 | else {
|
121 | const { name } = node.declaration.id;
|
122 | suffix = ` ${__export}('${name}', () => ${name});`;
|
123 | }
|
124 | code.appendLeft(node.end, suffix);
|
125 | }
|
126 | else {
|
127 | if (node.specifiers.length > 0) {
|
128 | code.remove(node.start, node.specifiers[0].start);
|
129 | node.specifiers.forEach((specifier) => {
|
130 | code.overwrite(specifier.start, specifier.end, `${__export}('${specifier.exported.name}', () => ${specifier.local.name})`);
|
131 | });
|
132 | code.remove(node.specifiers[node.specifiers.length - 1].end, node.end);
|
133 | }
|
134 | else {
|
135 |
|
136 | code.remove(node.start, node.end);
|
137 | }
|
138 | }
|
139 | }
|
140 | });
|
141 |
|
142 | if (replacements.size) {
|
143 | let current_scope = scope;
|
144 | estree_walker_1.walk(ast, {
|
145 | enter(node, parent) {
|
146 | if (map.has(node)) {
|
147 | current_scope = map.get(node) || current_scope;
|
148 | }
|
149 | if (node.type === 'ImportDeclaration') {
|
150 | this.skip();
|
151 | return;
|
152 | }
|
153 | if (!is_reference_1.default(node, parent))
|
154 | return;
|
155 | if (!replacements.has(node.name))
|
156 | return;
|
157 | if (current_scope.find_owner(node.name) === scope) {
|
158 | let replacement = replacements.get(node.name);
|
159 | if (parent.type === 'Property' && node === parent.key && node === parent.value) {
|
160 | replacement = `${node.name}: ${replacement}`;
|
161 | }
|
162 | code.overwrite(node.start, node.end, replacement);
|
163 | }
|
164 | },
|
165 | leave(node) {
|
166 | if (map.has(node)) {
|
167 | if (!current_scope.parent) {
|
168 | throw new Error('Unexpected: (!current_scope.parent.parent)');
|
169 | }
|
170 | current_scope = current_scope.parent;
|
171 | }
|
172 | },
|
173 | });
|
174 | }
|
175 |
|
176 | if (/import\s*\.\s*meta/.test(data) || /import\s*\(/.test(data)) {
|
177 | estree_walker_1.walk(ast.body, {
|
178 | enter(node) {
|
179 | if (node.type === 'MetaProperty' && node.meta.name === 'import') {
|
180 | code.overwrite(node.start, node.end, __import_meta);
|
181 | }
|
182 | else if (node.type === 'ImportExpression') {
|
183 | code.overwrite(node.start, node.start + 6, __import);
|
184 | }
|
185 | },
|
186 | });
|
187 | }
|
188 | return {
|
189 | code: code.toString(),
|
190 | deps,
|
191 | css,
|
192 | names: { exports, __import, __import_meta, __export, __export_all },
|
193 | };
|
194 | }
|
195 | exports.transform = transform;
|