Skip to content

Commit

Permalink
fix comments in import (#964)
Browse files Browse the repository at this point in the history
* fix comments in import

* fix dynamic imports too
  • Loading branch information
FredKSchott committed Aug 31, 2020
1 parent 3c8ddd5 commit 43cf5ce
Show file tree
Hide file tree
Showing 7 changed files with 2,158 additions and 9 deletions.
4 changes: 2 additions & 2 deletions snowpack/src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ import {
transformEsmImports,
transformFileImports,
} from '../rewrite-imports';
import {matchImportSpecifier} from '../scan-imports';
import {matchDynamicImportValue} from '../scan-imports';
import {CommandOptions, ImportMap, SnowpackBuildMap, SnowpackConfig} from '../types/snowpack';
import {
BUILD_CACHE,
Expand Down Expand Up @@ -604,7 +604,7 @@ If Snowpack is having trouble detecting the import, add ${colors.bold(
const resolvedImports = rawImports.map((imp) => {
let spec = code.substring(imp.s, imp.e);
if (imp.d > -1) {
spec = matchImportSpecifier(spec) || '';
spec = matchDynamicImportValue(spec) || '';
}
spec = spec.replace(/\?mtime=[0-9]+$/, '');
return path.posix.resolve(path.posix.dirname(reqPath), spec);
Expand Down
6 changes: 3 additions & 3 deletions snowpack/src/rewrite-imports.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {SnowpackSourceFile} from './types/snowpack';
import {HTML_JS_REGEX, CSS_REGEX} from './util';
import {matchImportSpecifier} from './scan-imports';
import {matchDynamicImportValue} from './scan-imports';

const {parse} = require('es-module-lexer');

Expand All @@ -18,7 +18,7 @@ export async function scanCodeImportsExports(code: string): Promise<any[]> {
// imp.d > -1 === dynamic import
if (imp.d > -1) {
const importStatement = code.substring(imp.s, imp.e);
return !!matchImportSpecifier(importStatement);
return !!matchDynamicImportValue(importStatement);
}
return true;
});
Expand All @@ -33,7 +33,7 @@ export async function transformEsmImports(
for (const imp of imports.reverse()) {
let spec = rewrittenCode.substring(imp.s, imp.e);
if (imp.d > -1) {
spec = matchImportSpecifier(spec) || '';
spec = matchDynamicImportValue(spec) || '';
}
let rewrittenImport = replaceImport(spec);
if (imp.d > -1) {
Expand Down
8 changes: 4 additions & 4 deletions snowpack/src/scan-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ function createInstallTarget(specifier: string, all = true): InstallTarget {
};
}

export function matchImportSpecifier(importStatement: string) {
const matched = importStatement.match(/^\s*('([^']+)'|"([^"]+)")\s*$/m);
export function matchDynamicImportValue(importStatement: string) {
const matched = stripComments(importStatement).match(/^\s*('([^']+)'|"([^"]+)")\s*$/m);
return matched?.[2] || matched?.[3] || null;
}

Expand All @@ -52,7 +52,7 @@ function getWebModuleSpecifierFromCode(code: string, imp: ImportSpecifier) {
}
// Dynamic imports: a bit trickier to parse. Today, we only support string literals.
const importStatement = code.substring(imp.s, imp.e);
return matchImportSpecifier(importStatement);
return matchDynamicImportValue(importStatement);
}

/**
Expand All @@ -76,7 +76,7 @@ function parseImportStatement(code: string, imp: ImportSpecifier): null | Instal
return null;
}

const importStatement = code.substring(imp.ss, imp.se);
const importStatement = stripComments(code.substring(imp.ss, imp.se));
if (/^import\s+type/.test(importStatement)) {
return null;
}
Expand Down

0 comments on commit 43cf5ce

Please sign in to comment.