From a72c8af19a72186069465c139b72df9c353fd3d1 Mon Sep 17 00:00:00 2001 From: eric thul Date: Thu, 14 May 2015 22:13:59 -0400 Subject: [PATCH] Additional loader options Adds the `psc-make` options `comments` and `no-prefix`. Also, an internal option `src` has been added that is used to specify the source paths of `PureScript` files that will be globbed for compilation. By default the `bower_components` path is globbed, but the loader requires that the source paths be provided for the user's code. Resolves #12 --- MODULE.md | 21 +++++++++++ README.md | 47 ++++++++++++++++++------ example/webpack.config.js | 2 +- src/Loader.purs | 10 ++--- src/Options.purs | 77 ++++++++++++++++++++++++++------------- 5 files changed, 114 insertions(+), 43 deletions(-) diff --git a/MODULE.md b/MODULE.md index 39d9d3a..2d54719 100644 --- a/MODULE.md +++ b/MODULE.md @@ -177,6 +177,20 @@ instance isForeignOptions :: IsForeign Options ``` +#### `booleanLoaderOption` + +``` purescript +instance booleanLoaderOption :: LoaderOption Boolean +``` + + +#### `stringLoaderOption` + +``` purescript +instance stringLoaderOption :: LoaderOption String +``` + + #### `pscMakeOutputOption` ``` purescript @@ -191,6 +205,13 @@ pscMakeOptions :: Foreign -> [String] ``` +#### `loaderSrcOption` + +``` purescript +loaderSrcOption :: Foreign -> Maybe [String] +``` + + ## Module PursLoader.Path diff --git a/README.md b/README.md index ee96448..d243381 100644 --- a/README.md +++ b/README.md @@ -12,18 +12,41 @@ npm install purs-loader --save-dev ## Options - - **no-prelude**: Boolean value that toggles `--no-prelude` - - Do not include the Prelude in the generated Javascript. - - **no-opts**: Boolean value that toggles `--no-opts` - - Disable all optimizations. - - **no-magic-do**: Boolean value that toggles `--no-magic-do` - - Turn off optimizations which inline calls to >>= for the Eff monad. - - **no-tco**: Boolean value that toggles `--no-tco` - - Turn off tail-call elimination. - - **verbose-errors**: Boolean value that toggles `--verbose-errors` - - Generate verbose error messages. - - **output**: String value that sets `--output=` - - Write the generated Javascript to the specified file. +###### `noPrelude` (Boolean) + +Toggles `--no-prelude` that omits the Prelude. + +###### `noTco` (Boolean) + +Toggles `--no-tco` that disables tail-call optimizations. + +###### `noMagicDo` (Boolean) + +Toggles `--no-magic-do` that disables optimizations overloading the do keyword generating efficient code for the `Eff` monad. + +###### `noOpts` (Boolean) + +Toggles `--no-opts` that skips the optimization phase. + +###### `verboseErrors` (Boolean) + +Toggles `--verbose-errors` that displays verbose error messages. + +###### `comments` (Boolean) + +Toggles `--comments` that includes comments in generated code. + +###### `output` (String) + +Sets `--output=` the specifies the output directory, `output` by default. + +###### `noPrefix` (Boolean) + +Toggles `--no-prefix` that does not include the comment header. + +###### `src` (String Array) + +Specifies PureScript source paths to be globbed for `.purs` files. By default, `bower_components` is search. Additional paths may be specified using this option. This option is specified as `src[]=path`. ## Example diff --git a/example/webpack.config.js b/example/webpack.config.js index 19997f3..f67e83a 100644 --- a/example/webpack.config.js +++ b/example/webpack.config.js @@ -5,7 +5,7 @@ var config , output: { path: __dirname , filename: 'bundle.js' } - , module: { loaders: [ { test: /\.purs$/, loader: 'purs-loader' } ] } + , module: { loaders: [ { test: /\.purs$/, loader: 'purs-loader?src[]=src' } ] } , resolve: { modulesDirectories: [ 'node_modules', 'output' ] diff --git a/src/Loader.purs b/src/Loader.purs index aae51c0..fedc424 100644 --- a/src/Loader.purs +++ b/src/Loader.purs @@ -26,7 +26,7 @@ import PursLoader.Glob (Glob(), glob) import PursLoader.LoaderRef (LoaderRef(), Loader(), async, cacheable, clearDependencies, addDependency, query, resourcePath) import PursLoader.LoaderUtil (getRemainingRequest, parseQuery) import PursLoader.OS (eol) -import PursLoader.Options (pscMakeOptions, pscMakeDefaultOutput, pscMakeOutputOption) +import PursLoader.Options (loaderSrcOption, pscMakeOptions, pscMakeDefaultOutput, pscMakeOutputOption) import PursLoader.Path (dirname, join, relative, resolve) foreign import cwd "var cwd = process.cwd();" :: String @@ -43,8 +43,8 @@ indexFilename = "index.js" (!!!) = flip (!!) -pursPattern :: String -> String -pursPattern root = join [ "{" ++ joinWith "," [ bowerPattern, root ] ++ "}" +pursPattern :: [String] -> String +pursPattern srcs = join [ "{" ++ joinWith "," ([ bowerPattern ] <> srcs) ++ "}" , "**" , "*.purs" ] @@ -86,10 +86,10 @@ loader' ref source = do liftEff $ cacheable ref let request = getRemainingRequest ref - root = dirname $ relative cwd request parsed = parseQuery $ query ref + srcs = loaderSrcOption parsed opts = pscMakeOptions parsed - pattern = pursPattern root + pattern = pursPattern $ fromMaybe [] srcs key = match moduleRegex source >>= (!!!) 1 files <- glob pattern diff --git a/src/Options.purs b/src/Options.purs index b96cddc..c47bebc 100644 --- a/src/Options.purs +++ b/src/Options.purs @@ -2,6 +2,7 @@ module PursLoader.Options ( pscMakeOptions , pscMakeDefaultOutput , pscMakeOutputOption + , loaderSrcOption ) where import Data.Either (either) @@ -24,6 +25,12 @@ verboseErrorsOpt = "verbose-errors" outputOpt = "output" +commentsOpt = "comments" + +noPrefixOpt = "no-prefix" + +srcOpt = "src" + pscMakeDefaultOutput = "output" newtype Options @@ -32,29 +39,42 @@ newtype Options , noMagicDo :: NullOrUndefined Boolean , noTco :: NullOrUndefined Boolean , verboseErrors :: NullOrUndefined Boolean + , comments :: NullOrUndefined Boolean , output :: NullOrUndefined String + , noPrefix :: NullOrUndefined Boolean + , src :: NullOrUndefined [String] } instance isForeignOptions :: IsForeign Options where - read obj = (\a b c d e f -> - Options { noPrelude: a - , noOpts: b - , noMagicDo: c - , noTco: d - , verboseErrors: e - , output: f - }) <$> readProp noPreludeOpt obj - <*> readProp noOptsOpt obj - <*> readProp noMagicDoOpt obj - <*> readProp noTcoOpt obj - <*> readProp verboseErrorsOpt obj - <*> readProp outputOpt obj - -booleanOpt :: String -> NullOrUndefined Boolean -> [String] -booleanOpt key opt = maybe [] (\a -> if a then ["--" ++ key] else []) (runNullOrUndefined opt) - -stringOpt :: String -> NullOrUndefined String -> [String] -stringOpt key opt = maybe [] (\a -> ["--" ++ key ++ "=" ++ a]) (runNullOrUndefined opt) + read obj = Options <$> ({ noPrelude: _ + , noOpts: _ + , noMagicDo: _ + , noTco: _ + , verboseErrors: _ + , comments: _ + , output: _ + , noPrefix: _ + , src: _ + } <$> readProp noPreludeOpt obj + <*> readProp noOptsOpt obj + <*> readProp noMagicDoOpt obj + <*> readProp noTcoOpt obj + <*> readProp verboseErrorsOpt obj + <*> readProp commentsOpt obj + <*> readProp outputOpt obj + <*> readProp noPrefixOpt obj + <*> readProp srcOpt obj) + +class LoaderOption a where + opt :: String -> NullOrUndefined a -> [String] + +instance booleanLoaderOption :: LoaderOption Boolean where + opt key opt = maybe [] (\a -> if a then ["--" ++ key] else []) + (runNullOrUndefined opt) + +instance stringLoaderOption :: LoaderOption String where + opt key opt = maybe [] (\a -> ["--" ++ key ++ "=" ++ a]) + (runNullOrUndefined opt) pscMakeOutputOption :: Foreign -> Maybe String pscMakeOutputOption query = either (const Nothing) @@ -64,9 +84,16 @@ pscMakeOutputOption query = either (const Nothing) pscMakeOptions :: Foreign -> [String] pscMakeOptions query = either (const []) fold parsed where parsed = read query :: F Options - fold (Options a) = booleanOpt noPreludeOpt a.noPrelude <> - booleanOpt noOptsOpt a.noOpts <> - booleanOpt noMagicDoOpt a.noMagicDo <> - booleanOpt noTcoOpt a.noTco <> - booleanOpt verboseErrorsOpt a.verboseErrors <> - stringOpt outputOpt a.output + fold (Options a) = opt noPreludeOpt a.noPrelude <> + opt noOptsOpt a.noOpts <> + opt noMagicDoOpt a.noMagicDo <> + opt noTcoOpt a.noTco <> + opt verboseErrorsOpt a.verboseErrors <> + opt commentsOpt a.comments <> + opt outputOpt a.output <> + opt noPrefixOpt a.noPrefix + +loaderSrcOption :: Foreign -> Maybe [String] +loaderSrcOption query = either (const Nothing) + (\(Options a) -> runNullOrUndefined a.src) + (read query) -- 2.41.0