UNPKG

4.41 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.transformFileImports = exports.transformEsmImports = exports.scanCodeImportsExports = void 0;
4const util_1 = require("./util");
5const scan_imports_1 = require("./scan-imports");
6const { parse } = require('es-module-lexer');
7const WEBPACK_MAGIC_COMMENT_REGEX = /\/\*[\s\S]*?\*\//g;
8function spliceString(source, withSlice, start, end) {
9 return source.slice(0, start) + (withSlice || '') + source.slice(end);
10}
11async function scanCodeImportsExports(code) {
12 const [imports] = await parse(code);
13 return imports.filter((imp) => {
14 //imp.d = -2 = import.meta.url = we can skip this for now
15 if (imp.d === -2) {
16 return false;
17 }
18 // imp.d > -1 === dynamic import
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}
26exports.scanCodeImportsExports = scanCodeImportsExports;
27async 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 // Extracting comments from spec as they are stripped in `matchDynamicImportValue`
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}
48exports.transformEsmImports = transformEsmImports;
49async 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 // Only transform a script element if it contains inlined code / is not empty.
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 // Only transform a script element if it contains inlined code / is not empty.
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}
70async 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 // Only transform a script element if it contains inlined code / is not empty.
77 rewrittenCode = spliceString(rewrittenCode,
78 // CSS doesn't support proxy files, so always point to the original file
79 `@import "${replaceImport(spec).replace('.proxy.js', '')}";`, match.index, match.index + fullMatch.length);
80 }
81 return rewrittenCode;
82}
83async 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}
95exports.transformFileImports = transformFileImports;