1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.transformFileImports = exports.transformEsmImports = exports.scanCodeImportsExports = void 0;
|
4 | const util_1 = require("./util");
|
5 | const scan_imports_1 = require("./scan-imports");
|
6 | const { parse } = require('es-module-lexer');
|
7 | const WEBPACK_MAGIC_COMMENT_REGEX = /\/\*[\s\S]*?\*\//g;
|
8 | function spliceString(source, withSlice, start, end) {
|
9 | return source.slice(0, start) + (withSlice || '') + source.slice(end);
|
10 | }
|
11 | async function scanCodeImportsExports(code) {
|
12 | const [imports] = await parse(code);
|
13 | return imports.filter((imp) => {
|
14 |
|
15 | if (imp.d === -2) {
|
16 | return false;
|
17 | }
|
18 |
|
19 | if (imp.d > -1) {
|
20 | const importStatement = code.substring(imp.s, imp.e);
|
21 | return !!scan_imports_1.matchDynamicImportValue(importStatement);
|
22 | }
|
23 | return true;
|
24 | });
|
25 | }
|
26 | exports.scanCodeImportsExports = scanCodeImportsExports;
|
27 | async function transformEsmImports(_code, replaceImport) {
|
28 | const imports = await scanCodeImportsExports(_code);
|
29 | let rewrittenCode = _code;
|
30 | for (const imp of imports.reverse()) {
|
31 | let spec = rewrittenCode.substring(imp.s, imp.e);
|
32 | let webpackMagicCommentMatches;
|
33 | if (imp.d > -1) {
|
34 |
|
35 | webpackMagicCommentMatches = spec.match(WEBPACK_MAGIC_COMMENT_REGEX);
|
36 | spec = scan_imports_1.matchDynamicImportValue(spec) || '';
|
37 | }
|
38 | let rewrittenImport = replaceImport(spec);
|
39 | if (imp.d > -1) {
|
40 | rewrittenImport = webpackMagicCommentMatches
|
41 | ? `${webpackMagicCommentMatches.join(' ')} ${JSON.stringify(rewrittenImport)}`
|
42 | : JSON.stringify(rewrittenImport);
|
43 | }
|
44 | rewrittenCode = spliceString(rewrittenCode, rewrittenImport, imp.s, imp.e);
|
45 | }
|
46 | return rewrittenCode;
|
47 | }
|
48 | exports.transformEsmImports = transformEsmImports;
|
49 | async function transformHtmlImports(code, replaceImport) {
|
50 | let rewrittenCode = code;
|
51 | let match;
|
52 | const jsImportRegex = new RegExp(util_1.HTML_JS_REGEX);
|
53 | while ((match = jsImportRegex.exec(rewrittenCode))) {
|
54 | const [, scriptTag, scriptCode] = match;
|
55 |
|
56 | if (scriptCode.trim()) {
|
57 | rewrittenCode = spliceString(rewrittenCode, await transformEsmImports(scriptCode, replaceImport), match.index + scriptTag.length, match.index + scriptTag.length + scriptCode.length);
|
58 | }
|
59 | }
|
60 | const cssImportRegex = new RegExp(util_1.HTML_STYLE_REGEX);
|
61 | while ((match = cssImportRegex.exec(rewrittenCode))) {
|
62 | const [, styleTag, styleCode] = match;
|
63 |
|
64 | if (styleCode.trim()) {
|
65 | rewrittenCode = spliceString(rewrittenCode, await transformCssImports(styleCode, replaceImport), match.index + styleTag.length, match.index + styleTag.length + styleCode.length);
|
66 | }
|
67 | }
|
68 | return rewrittenCode;
|
69 | }
|
70 | async function transformCssImports(code, replaceImport) {
|
71 | let rewrittenCode = code;
|
72 | let match;
|
73 | const importRegex = new RegExp(util_1.CSS_REGEX);
|
74 | while ((match = importRegex.exec(rewrittenCode))) {
|
75 | const [fullMatch, spec] = match;
|
76 |
|
77 | rewrittenCode = spliceString(rewrittenCode,
|
78 |
|
79 | `@import "${replaceImport(spec).replace('.proxy.js', '')}";`, match.index, match.index + fullMatch.length);
|
80 | }
|
81 | return rewrittenCode;
|
82 | }
|
83 | async function transformFileImports({ baseExt, contents }, replaceImport) {
|
84 | if (baseExt === '.js') {
|
85 | return transformEsmImports(contents, replaceImport);
|
86 | }
|
87 | if (baseExt === '.html') {
|
88 | return transformHtmlImports(contents, replaceImport);
|
89 | }
|
90 | if (baseExt === '.css') {
|
91 | return transformCssImports(contents, replaceImport);
|
92 | }
|
93 | throw new Error(`Incompatible filetype: cannot scan ${baseExt} files for ESM imports. This is most likely an error within Snowpack.`);
|
94 | }
|
95 | exports.transformFileImports = transformFileImports;
|