Skip to content

Commit

Permalink
edits to how we had implemented webpack
Browse files Browse the repository at this point in the history
  • Loading branch information
Carr committed Jul 5, 2018
1 parent 3e52b88 commit 1899119
Show file tree
Hide file tree
Showing 19 changed files with 4,036 additions and 242 deletions.
5 changes: 0 additions & 5 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ S3method(export,page)
S3method(export,resource)
S3method(export,svg)
S3method(export,thumbnail)
S3method(export,webpackmain)
S3method(export,webpackmodule)
S3method(fetch,character)
S3method(fetch,file)
S3method(fetch,none)
Expand Down Expand Up @@ -62,8 +60,6 @@ S3method(publish,tabular)
S3method(publish,template)
S3method(publish,thumbnail)
S3method(publish,webpack)
S3method(publish,webpackmain)
S3method(publish,webpackmodule)
S3method(readData,character)
S3method(readData,excel)
S3method(readData,filepath)
Expand Down Expand Up @@ -125,7 +121,6 @@ export(getLogPath)
export(getPreferences)
export(getProfileInfo)
export(getVizlabVersion)
export(getWebpackConfig)
export(initializeVizRepo)
export(locateTimestampFile)
export(lookupMimetype)
Expand Down
24 changes: 0 additions & 24 deletions R/export.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,30 +129,6 @@ export.thumbnail <- function(viz) {
return(file)
}

#' export webpack modules to the src/modules folder so webpack can find them
#' @rdname export
#' @export
export.webpackmodule <- function(viz) {
location <- viz[['location']]
file <- NULL
if (doExport(viz)) {
file <- paste0(exportLocation(), "src/modules/", basename(location))
}
return(file)
}

#' export the input webpack js file to the src folder so webpack can find it
#' @rdname export
#' @export
export.webpackmain <- function(viz) {
location <- viz[['location']]
file <- NULL
if (doExport(viz)) {
file <- paste0(exportLocation(), "src/", basename(location))
}
return(file)
}

### Utility functions provided by export

#' Set where exports should be placed
Expand Down
8 changes: 0 additions & 8 deletions R/getContentInfo.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,6 @@ getVizlabVersion <- function() {
return(getBlocks('vizlab')[['vizlab']][[1]][[1]])
}

#' Read the content of the webpack config block
#'
#' @export
getWebpackConfig <- function() {
# version block is unnamed list with version at first index
return(getBlocks('webpack')[['webpack']])
}

#' Read vizlab config document and return specific blocks
#'
#' @param block character vector of blocks to read
Expand Down
28 changes: 0 additions & 28 deletions R/publish.R
Original file line number Diff line number Diff line change
Expand Up @@ -509,34 +509,6 @@ publish.webpack <- function(viz) {
return(html)
}

#' publish webpack main js file
#'
#' @rdname publish
#' @export
publish.webpackmain <- function(viz) {
checkRequired(viz, "location")
destFile <- export(viz)
if(!dir.exists(dirname(destFile))) { stop("webpack dirs not setup properly") }
srcFile <- viz[['location']]
file.copy(srcFile, destFile, overwrite = TRUE)
viz[['relpath']] <- relativePath(destFile)
return(viz)
}

#' publish webpack modules
#'
#' @rdname publish
#' @export
publish.webpackmodule <- function(viz) {
checkRequired(viz, "location")
destFile <- export(viz)
if(!dir.exists(dirname(destFile))) { stop("webpack dirs not setup properly") }
srcFile <- viz[['location']]
file.copy(srcFile, destFile, overwrite = TRUE)
viz[['relpath']] <- relativePath(destFile)
return(viz)
}

#' publish template
#'
#' @rdname publish
Expand Down
9 changes: 4 additions & 5 deletions R/vizmake.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,11 @@ vizmake <- function(target_names=NULL, ...) {
source(remake.yaml$sources[i])
}

webpack.config <- getWebpackConfig()
if(length(webpack.config) > 0) {
if(file.exists('webpack.config.js')) {
# run webpack
message('Setting up webpack configs ', Sys.time())
webpacker(webpack.config)
message('Finishing webpack configs ', Sys.time())
message('Running webpack ', Sys.time())
webpacker()
message('Finished webpack ', Sys.time())
}

# run remake::make(target_name, ..., remake_file='remake.yaml')
Expand Down
64 changes: 3 additions & 61 deletions R/webpacker.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,17 @@
#' filepath for the javasript file going into webpack, and the name for the bundled file that results.
#' @md
#' @export
webpacker <- function(webpack.config) {

checkRequired(webpack.config[["context"]], c("input", "output"))
webpacker <- function() {

# check if node, npm are installed
checkInstalled("node")
checkInstalled("npm")

# create webpack.config.js
configFilepath <- file.path(exportLocation(), "webpack.config.js")
if(!file.exists(configFilepath)) {
generateWebpackConfigFile(configFilepath, webpack.config)
}

# create package.json
packagejsonFilepath <- file.path(exportLocation(), "package.json")
if(!file.exists(packagejsonFilepath)) {
generatePackageJsonFile(packagejsonFilepath, webpack.config)
}

checkAndInstallNodeModule("webpack")
checkAndInstallNodeModule("webpack-cli")
checkAndInstallNodeModule("d3", addSave=FALSE)

# setup appropriate directories
srcDir <- file.path(exportLocation(), "src")
moduleDir <- file.path(srcDir, "modules")
if(!dir.exists(srcDir)) { dir.create(srcDir, showWarnings = FALSE) }
if(!dir.exists(moduleDir)) { dir.create(moduleDir, showWarnings = FALSE) }

## have a way to fail if `start` is not the default?
system("npm run start")
}

#' Check that a system library is installed
Expand Down Expand Up @@ -73,42 +54,3 @@ checkAndInstallNodeModule <- function(module, addSave=TRUE) {

setwd(oldwd)
}

#' Create the webpack.config.js file
#' Needs an input and output in webpack.config
#'
#' @param file the file name to save the output with .js ending
#' @param webpack.config list generated by `getWebpackConfig()`
#'
#' Could switch out the template to a custom one.
generateWebpackConfigFile <- function(file, webpack.config) {

if(!is.null(webpack.config[['template-webpackconfig']])) {
template <- template(webpack.config[['template-webpackconfig']])
} else {
template <- template("webpackconfig")
}

template_output <- render(template, webpack.config[["context"]])
write(template_output, file)

}

#' Create the package.json file
#'
#' @param file the file name to save the output with .json ending
#' @param webpack.config list generated by `getWebpackConfig()`
#'
#' Could switch out the template to a custom one.
generatePackageJsonFile <- function(file, webpack.config) {

if(!is.null(webpack.config[['template-packagejson']])) {
template <- template(webpack.config[['template-packagejson']])
} else {
template <- template("packagejson")
}

template_output <- render(template, webpack.config)
write(template_output, file)

}
14 changes: 0 additions & 14 deletions inst/resource.library.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -188,20 +188,6 @@
id:
figure:
caption:
-
id: webpackconfig
location: templates/webpackconfig.mustache
publisher: template
mimetype: text/mustache
context:
input:
output:
-
id: packagejson
location: templates/packagejson.mustache
publisher: template
mimetype: text/mustache
context:
-
id: simpleSVG
location: templates/simpleSVG.mustache
Expand Down
1 change: 1 addition & 0 deletions inst/testviz/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ vizlab
target
.remake
remake.yaml
node_modules
File renamed without changes.
Loading

0 comments on commit 1899119

Please sign in to comment.