Skip to content

Commit

Permalink
Process js after compiling to find svelte internals.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimafisk committed Apr 24, 2020
1 parent a915a50 commit e88e146
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 68 deletions.
139 changes: 71 additions & 68 deletions cmd/build/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,81 +43,84 @@ func Client(buildPath string) string {
if layoutFileInfo.IsDir() {
// Create any sub directories need for filepath.
os.MkdirAll(destFile, os.ModePerm)
}
// Make list of files not to copy to build.
excludedFiles := []string{
"layout/ejected/build.js",
"layout/ejected/build_client.js",
"layout/ejected/build_static.js",
"layout/ejected/server_router.js",
}
// Check if the current file is in the excluded list.
excluded := false
for _, excludedFile := range excludedFiles {
if excludedFile == layoutPath {
excluded = true
} else {
// Make list of files not to copy to build.
excludedFiles := []string{
"layout/ejected/build.js",
"layout/ejected/build_client.js",
"layout/ejected/build_static.js",
"layout/ejected/server_router.js",
}
}
// If the file is already in .js format just copy it straight over to build dir.
if filepath.Ext(layoutPath) == ".js" && !excluded {
from, err := os.Open(layoutPath)
if err != nil {
fmt.Printf("Could not open source .js file for copying: %s\n", err)
// Check if the current file is in the excluded list.
excluded := false
for _, excludedFile := range excludedFiles {
if excludedFile == layoutPath {
excluded = true
}
}
defer from.Close()
// If the file is already in .js format just copy it straight over to build dir.
if filepath.Ext(layoutPath) == ".js" && !excluded {
from, err := os.Open(layoutPath)
if err != nil {
fmt.Printf("Could not open source .js file for copying: %s\n", err)
}
defer from.Close()

to, err := os.Create(destFile)
if err != nil {
fmt.Printf("Could not create destination .js file for copying: %s\n", err)
}
defer to.Close()

_, fileCopyErr := io.Copy(to, from)
if err != nil {
fmt.Printf("Could not copy .js from source to destination: %s\n", fileCopyErr)
}

copiedSourceCounter++

to, err := os.Create(destFile)
if err != nil {
fmt.Printf("Could not create destination .js file for copying: %s\n", err)
}
defer to.Close()

_, fileCopyErr := io.Copy(to, from)
if err != nil {
fmt.Printf("Could not copy .js from source to destination: %s\n", fileCopyErr)
}
// If the file is in .svelte format, compile it to .js
if filepath.Ext(layoutPath) == ".svelte" {

fileContentByte, readFileErr := ioutil.ReadFile(layoutPath)
if readFileErr != nil {
fmt.Printf("Could not read contents of svelte source file: %s\n", readFileErr)
}
fileContentStr := string(fileContentByte)
// Convert file extensions to be snowpack friendly.
fileContentStr = strings.Replace(fileContentStr, ".svelte", ".js", -1)
//fileContentStr = strings.Replace(fileContentStr, "from \"svelte/internal\";", "from \"../web_modules/svelte/internal/index.js\";", -1)
fileContentStr = strings.Replace(fileContentStr, "from \"navaid\";", "from \"../web_modules/navaid.js\";", -1)

// Encode HTML so it can be represented as a string.
fileContentStr = html.EscapeString(fileContentStr)

// Remove newlines.
reN := regexp.MustCompile(`\r?\n`)
fileContentStr = reN.ReplaceAllString(fileContentStr, " ")
// Remove tabs.
reT := regexp.MustCompile(`\t`)
fileContentStr = reT.ReplaceAllString(fileContentStr, " ")
// Reduce extra whitespace to a single space.
reS := regexp.MustCompile(`\s+`)
fileContentStr = reS.ReplaceAllString(fileContentStr, " ")

// Convert opening curly brackets to HTML escape character.
fileContentStr = strings.Replace(fileContentStr, "{", "{", -1)
// Convert closing curly brackets to HTML escape character.
fileContentStr = strings.Replace(fileContentStr, "}", "}", -1)

// Replace .svelte file extension with .js.
destFile = strings.TrimSuffix(destFile, filepath.Ext(destFile)) + ".js"

// Create string representing array of objects to be passed to nodejs.
clientBuildStr = clientBuildStr + "{ \"component\": \"" + fileContentStr + "\", \"destPath\": \"" + destFile + "\", \"stylePath\": \"" + stylePath + "\"},"

compiledComponentCounter++

copiedSourceCounter++

}
// If the file is in .svelte format, compile it to .js
if filepath.Ext(layoutPath) == ".svelte" {
fileContentByte, readFileErr := ioutil.ReadFile(layoutPath)
if readFileErr != nil {
fmt.Printf("Could not read contents of svelte source file: %s\n", readFileErr)
}
fileContentStr := string(fileContentByte)
// Convert file extensions to be snowpack friendly.
fileContentStr = strings.Replace(fileContentStr, ".svelte", ".js", -1)
fileContentStr = strings.Replace(fileContentStr, "from \"svelte/internal\";", "from \"../web_modules/svelte/internal/index.js\";", -1)
fileContentStr = strings.Replace(fileContentStr, "from \"navaid\";", "from \"../web_modules/navaid.js\";", -1)

// Encode HTML so it can be represented as a string.
fileContentStr = html.EscapeString(fileContentStr)

// Remove newlines.
reN := regexp.MustCompile(`\r?\n`)
fileContentStr = reN.ReplaceAllString(fileContentStr, " ")
// Remove tabs.
reT := regexp.MustCompile(`\t`)
fileContentStr = reT.ReplaceAllString(fileContentStr, " ")
// Reduce extra whitespace to a single space.
reS := regexp.MustCompile(`\s+`)
fileContentStr = reS.ReplaceAllString(fileContentStr, " ")

// Convert opening curly brackets to HTML escape character.
fileContentStr = strings.Replace(fileContentStr, "{", "{", -1)
// Convert closing curly brackets to HTML escape character.
fileContentStr = strings.Replace(fileContentStr, "}", "}", -1)

// Replace .svelte file extension with .js.
destFile = strings.TrimSuffix(destFile, filepath.Ext(destFile)) + ".js"

// Create string representing array of objects to be passed to nodejs.
clientBuildStr = clientBuildStr + "{ \"component\": \"" + fileContentStr + "\", \"destPath\": \"" + destFile + "\", \"stylePath\": \"" + stylePath + "\"},"

compiledComponentCounter++

}
return nil
})
Expand Down
6 changes: 6 additions & 0 deletions defaults/layout/ejected/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ clientBuildStr.forEach(arg => {
css: false
});

// Find svelte internals in snowpack directory.
js.code = js.code.replace(/from "svelte\/internal"\;/g, 'from "../web_modules/svelte/internal/index.js";');

// Write JS to build directory.
ensureDirExists(arg.destPath);
fs.promises.writeFile(arg.destPath, js.code);
Expand All @@ -60,6 +63,7 @@ clientBuildStr.forEach(arg => {
let staticBuildStr = JSON.parse(args[1]);
let allNodes = JSON.parse(args[2]);

// Create the component that wraps all nodes.
let htmlWrapper = path.join(path.resolve(), 'layout/global/html.svelte')
const component = relative(htmlWrapper, process.cwd()).default;

Expand All @@ -68,8 +72,10 @@ staticBuildStr.forEach(arg => {
let componentPath = path.join(path.resolve(), arg.componentPath);
let destPath = path.join(path.resolve(), arg.destPath);

// Set route used in svelte:component as "this" value.
const route = relative(componentPath, process.cwd()).default;

// Set props so component can access field values, etc.
let props = {
Route: route,
node: arg.node,
Expand Down
6 changes: 6 additions & 0 deletions generated/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ clientBuildStr.forEach(arg => {
css: false
});
// Find svelte internals in snowpack directory.
js.code = js.code.replace(/from "svelte\/internal"\;/g, 'from "../web_modules/svelte/internal/index.js";');
// Write JS to build directory.
ensureDirExists(arg.destPath);
fs.promises.writeFile(arg.destPath, js.code);
Expand All @@ -170,6 +173,7 @@ clientBuildStr.forEach(arg => {
let staticBuildStr = JSON.parse(args[1]);
let allNodes = JSON.parse(args[2]);
// Create the component that wraps all nodes.
let htmlWrapper = path.join(path.resolve(), 'layout/global/html.svelte')
const component = relative(htmlWrapper, process.cwd()).default;
Expand All @@ -178,8 +182,10 @@ staticBuildStr.forEach(arg => {
let componentPath = path.join(path.resolve(), arg.componentPath);
let destPath = path.join(path.resolve(), arg.destPath);
// Set route used in svelte:component as "this" value.
const route = relative(componentPath, process.cwd()).default;
// Set props so component can access field values, etc.
let props = {
Route: route,
node: arg.node,
Expand Down

0 comments on commit e88e146

Please sign in to comment.