Convert app-context-init to typescript and fix server.go

Typescript doesn't add .js extension to the imported file name. As a
result, the browser imports the same file twice with different names:
for example app-context and app-context.js. After the fix, server.go
patches the typescript output and adds .js extension. Rollup handles
such cases correctly.

Change-Id: I3290f9244bb6d6dd8547fbceed8ed57186c6f638
This commit is contained in:
Dmitrii Filippov
2020-07-29 13:16:12 +02:00
parent f1014d1fcf
commit c02c096ad2
2 changed files with 48 additions and 31 deletions

View File

@@ -168,17 +168,20 @@ func handleSrcRequest(compiledSrcPath string, dirListingMux *http.ServeMux, writ
writer.Header().Set("Content-Type", "text/html")
} else if isJsFile {
// The following code updates import statements.
// 1. Keep all imports started with '.' character unchanged (i.e. all relative
// imports like import ... from './a.js' or import ... from '../b/c/d.js'
// 2. For other imports it adds '/node_modules/' prefix. Additionally,
// if an in imported file has .js or .mjs extension, the code keeps
// the file extension unchanged. Otherwise, it adds .js extension.
// 1. if an in imported file has .js or .mjs extension, the code keeps
// the file extension unchanged. Otherwise, it adds .js extension
// 2. For module imports it adds '/node_modules/' prefix.
// Examples:
// '@polymer/polymer.js' -> '/node_modules/@polymer/polymer.js'
// 'page/page.mjs' -> '/node_modules/page.mjs'
// '@polymer/iron-icon' -> '/node_modules/@polymer/iron-icon.js'
moduleImportRegexp := regexp.MustCompile("(?m)^(import.*)'([^/.].*?)(\\.(m?)js)?';$")
data = moduleImportRegexp.ReplaceAll(data, []byte("$1 '/node_modules/$2.${4}js';"))
// './element/file' -> './element/file.js'
moduleImportRegexp := regexp.MustCompile("(?m)^(import.*)'(.*?)(\\.(m?)js)?';$")
data = moduleImportRegexp.ReplaceAll(data, []byte("$1 '$2.${4}js';"))
moduleImportRegexp = regexp.MustCompile("(?m)^(import.*)'([^/.].*)';$")
data = moduleImportRegexp.ReplaceAll(data, []byte("$1 '/node_modules/$2';"))
writer.Header().Set("Content-Type", "application/javascript")
} else if strings.HasSuffix(normalizedContentPath, ".css") {
writer.Header().Set("Content-Type", "text/css")