Пример #1
0
func bundleAssets() {
	fmt.Println("-> bundle assets with require directive")
	train.Config.BundleAssets = true
	publicAssetPath := "public" + train.Config.AssetsUrl
	filepath.Walk(publicAssetPath, func(filePath string, info os.FileInfo, err error) error {
		if info.IsDir() {
			return nil
		}
		fileExt := path.Ext(filePath)
		if fileExt != ".js" && fileExt != ".css" {
			return nil
		}

		assetUrl := strings.Replace(filePath, publicAssetPath, train.Config.AssetsUrl, 1)
		b_content, err := ioutil.ReadFile(filePath)
		content := string(b_content)
		header := train.FindDirectivesHeader(&content, fileExt)
		if len(header) != 0 {
			content, err := train.ReadAsset(assetUrl)
			if err != nil {
				removeAssets()
				panic(err)
			}
			ioutil.WriteFile(filePath, []byte(content), os.ModeDevice)
		}
		return nil
	})
}
Пример #2
0
func bundleAssets() {
	fmt.Println("-> bundle and compile assets")

	train.Config.BundleAssets = true
	publicAssetPath := "public" + train.Config.AssetsUrl

	filepath.Walk(publicAssetPath, func(filePath string, info os.FileInfo, err error) error {
		if info.IsDir() {
			return nil
		}

		assetUrl := strings.Replace(filePath, publicAssetPath, train.Config.AssetsUrl, 1)
		fileExt := path.Ext(filePath)
		switch fileExt {
		case ".js", ".css":
			if hasRequireDirectives(filePath) {
				content, err := train.ReadAsset(assetUrl)
				if err != nil {
					removeAssets()
					panic(err)
				}
				ioutil.WriteFile(filePath, []byte(content), os.ModeDevice)
			}
		case ".sass", ".scss", ".coffee":
			if path.Base(filePath)[0] == '_' {
				return nil
			}

			content, err := train.ReadAsset(assetUrl)
			if err != nil {
				removeAssets()
				fmt.Println("Error when reading asset: ", assetUrl)
				panic(err)
			}
			compiledPath := strings.Replace(filePath, fileExt, mapCompiledExt[fileExt], 1)
			os.Create(compiledPath)
			ioutil.WriteFile(compiledPath, []byte(content), os.ModeDevice)
		default:
			return nil
		}
		return nil
	})
}