Skip to content

Commit 70ddf2b

Browse files
committed
Fix purescript#1794, preserve external require expressions when minifying
1 parent 20ecf43 commit 70ddf2b

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

src/Language/PureScript/Bundle.hs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ data ExportType
9595
-- Each is labelled with the original AST node which generated it, so that we can dump it back
9696
-- into the output during codegen.
9797
data ModuleElement
98-
= Require JSNode String ModuleIdentifier
98+
= Require JSNode String (Either String ModuleIdentifier)
9999
| Member JSNode Bool String [JSNode] [Key]
100100
| ExportsList [(ExportType, String, JSNode, [Key])]
101101
| Other JSNode
@@ -137,13 +137,13 @@ node (NN n) = n
137137
node (NT n _ _) = n
138138

139139
-- | Calculate the ModuleIdentifier which a require(...) statement imports.
140-
checkImportPath :: Maybe FilePath -> String -> ModuleIdentifier -> S.Set String -> Maybe ModuleIdentifier
140+
checkImportPath :: Maybe FilePath -> String -> ModuleIdentifier -> S.Set String -> Either String ModuleIdentifier
141141
checkImportPath _ "./foreign" m _ =
142-
Just (ModuleIdentifier (moduleName m) Foreign)
142+
Right (ModuleIdentifier (moduleName m) Foreign)
143143
checkImportPath requirePath name _ names
144144
| Just name' <- stripPrefix (fromMaybe "" requirePath) name
145-
, name' `S.member` names = Just (ModuleIdentifier name' Regular)
146-
checkImportPath _ _ _ _ = Nothing
145+
, name' `S.member` names = Right (ModuleIdentifier name' Regular)
146+
checkImportPath _ name _ _ = Left name
147147

148148
-- | Compute the dependencies of all elements in a module, and add them to the tree.
149149
--
@@ -166,7 +166,7 @@ withDeps (Module modulePath es) = Module modulePath (map expandDeps es)
166166
imports = mapMaybe toImport es
167167
where
168168
toImport :: ModuleElement -> Maybe (String, ModuleIdentifier)
169-
toImport (Require _ nm mid) = Just (nm, mid)
169+
toImport (Require _ nm (Right mid)) = Just (nm, mid)
170170
toImport _ = Nothing
171171

172172
-- | Collects all member names in scope, so that we can identify dependencies of the second type.
@@ -226,7 +226,7 @@ toModule requirePath mids mid top
226226
, JSIdentifier "require" <- node req
227227
, JSArguments _ [ impS ] _ <- node impP
228228
, JSStringLiteral _ importPath <- node impS
229-
, Just importPath' <- checkImportPath requirePath importPath mid mids
229+
, importPath' <- checkImportPath requirePath importPath mid mids
230230
= pure (Require n importName importPath')
231231
toModuleElement n
232232
| JSVariables var [ varIntro ] _ <- node n
@@ -371,7 +371,7 @@ sortModules modules = map (\v -> case nodeFor v of (n, _, _) -> n) (reverse (top
371371
return (m, mid, mapMaybe getKey els)
372372

373373
getKey :: ModuleElement -> Maybe ModuleIdentifier
374-
getKey (Require _ _ mi) = Just mi
374+
getKey (Require _ _ (Right mi)) = Just mi
375375
getKey _ = Nothing
376376

377377
-- | A module is empty if it contains no exported members (in other words,
@@ -416,9 +416,7 @@ codeGen optionsMainModule optionsNamespace ms = renderToString (NN (JSSourceElem
416416
declToJS (Require _ nm req) =
417417
[ NN (JSVariables (NT (JSLiteral "var") tokenPosnEmpty [ WhiteSpace tokenPosnEmpty "\n " ])
418418
[ NN (JSVarDecl (sp (JSIdentifier nm))
419-
[ sp (JSLiteral "=")
420-
, moduleReference sp (moduleName req)
421-
])
419+
(sp (JSLiteral "=") : either require (return . moduleReference sp . moduleName) req))
422420
]
423421
(nt (JSLiteral ";"))) ]
424422
declToJS (ExportsList exps) = map toExport exps
@@ -467,6 +465,11 @@ codeGen optionsMainModule optionsNamespace ms = renderToString (NN (JSSourceElem
467465
, lf
468466
]
469467

468+
require :: String -> [JSNode]
469+
require mn = [ sp (JSIdentifier "require")
470+
, NN (JSArguments (nt (JSLiteral "(")) [ nt (JSStringLiteral '"' mn) ] (nt (JSLiteral ")")))
471+
]
472+
470473
moduleReference :: (Node -> JSNode) -> String -> JSNode
471474
moduleReference f mn =
472475
NN (JSMemberSquare [ f (JSIdentifier optionsNamespace) ]

0 commit comments

Comments
 (0)