git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Fix #1794, preserve external require expressions when minifying
70ddf2bPhil Freeman
src/Language/PureScript/Bundle.hs+14−11
@@ -95,7 +95,7 @@ data ExportType
95-- Each is labelled with the original AST node which generated it, so that we can dump it back
95-- Each is labelled with the original AST node which generated it, so that we can dump it back
96-- into the output during codegen.
96-- into the output during codegen.
97data ModuleElement
97data ModuleElement
98− = Require JSNode String ModuleIdentifier
98+ = Require JSNode String (Either String ModuleIdentifier)
99 | Member JSNode Bool String [JSNode] [Key]
99 | Member JSNode Bool String [JSNode] [Key]
100 | ExportsList [(ExportType, String, JSNode, [Key])]
100 | ExportsList [(ExportType, String, JSNode, [Key])]
101 | Other JSNode
101 | Other JSNode
@@ -137,13 +137,13 @@ node (NN n) = n
137node (NT n _ _) = n
137node (NT n _ _) = n
138
138
139-- | Calculate the ModuleIdentifier which a require(...) statement imports.
139-- | 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
141checkImportPath _ "./foreign" m _ =
141checkImportPath _ "./foreign" m _ =
142− Just (ModuleIdentifier (moduleName m) Foreign)
142+ Right (ModuleIdentifier (moduleName m) Foreign)
143checkImportPath requirePath name _ names
143checkImportPath requirePath name _ names
144 | Just name' <- stripPrefix (fromMaybe "" requirePath) name
144 | Just name' <- stripPrefix (fromMaybe "" requirePath) name
145− , name' `S.member` names = Just (ModuleIdentifier name' Regular)
145+ , name' `S.member` names = Right (ModuleIdentifier name' Regular)
146−checkImportPath _ _ _ _ = Nothing
146+checkImportPath _ name _ _ = Left name
147
147
148-- | Compute the dependencies of all elements in a module, and add them to the tree.
148-- | Compute the dependencies of all elements in a module, and add them to the tree.
149--
149--
@@ -166,7 +166,7 @@ withDeps (Module modulePath es) = Module modulePath (map expandDeps es)
166 imports = mapMaybe toImport es
166 imports = mapMaybe toImport es
167 where
167 where
168 toImport :: ModuleElement -> Maybe (String, ModuleIdentifier)
168 toImport :: ModuleElement -> Maybe (String, ModuleIdentifier)
169− toImport (Require _ nm mid) = Just (nm, mid)
169+ toImport (Require _ nm (Right mid)) = Just (nm, mid)
170 toImport _ = Nothing
170 toImport _ = Nothing
171
171
172 -- | Collects all member names in scope, so that we can identify dependencies of the second type.
172 -- | 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
226 , JSIdentifier "require" <- node req
226 , JSIdentifier "require" <- node req
227 , JSArguments _ [ impS ] _ <- node impP
227 , JSArguments _ [ impS ] _ <- node impP
228 , JSStringLiteral _ importPath <- node impS
228 , JSStringLiteral _ importPath <- node impS
229− , Just importPath' <- checkImportPath requirePath importPath mid mids
229+ , importPath' <- checkImportPath requirePath importPath mid mids
230 = pure (Require n importName importPath')
230 = pure (Require n importName importPath')
231 toModuleElement n
231 toModuleElement n
232 | JSVariables var [ varIntro ] _ <- node n
232 | JSVariables var [ varIntro ] _ <- node n
@@ -371,7 +371,7 @@ sortModules modules = map (\v -> case nodeFor v of (n, _, _) -> n) (reverse (top
371 return (m, mid, mapMaybe getKey els)
371 return (m, mid, mapMaybe getKey els)
372
372
373 getKey :: ModuleElement -> Maybe ModuleIdentifier
373 getKey :: ModuleElement -> Maybe ModuleIdentifier
374− getKey (Require _ _ mi) = Just mi
374+ getKey (Require _ _ (Right mi)) = Just mi
375 getKey _ = Nothing
375 getKey _ = Nothing
376
376
377-- | A module is empty if it contains no exported members (in other words,
377-- | A module is empty if it contains no exported members (in other words,
@@ -416,9 +416,7 @@ codeGen optionsMainModule optionsNamespace ms = renderToString (NN (JSSourceElem
416 declToJS (Require _ nm req) =
416 declToJS (Require _ nm req) =
417 [ NN (JSVariables (NT (JSLiteral "var") tokenPosnEmpty [ WhiteSpace tokenPosnEmpty "\n " ])
417 [ NN (JSVariables (NT (JSLiteral "var") tokenPosnEmpty [ WhiteSpace tokenPosnEmpty "\n " ])
418 [ NN (JSVarDecl (sp (JSIdentifier nm))
418 [ NN (JSVarDecl (sp (JSIdentifier nm))
419− [ sp (JSLiteral "=")
420− , moduleReference sp (moduleName req)
419+ (sp (JSLiteral "=") : either require (return . moduleReference sp . moduleName) req))
421− ])
422 ]
420 ]
423 (nt (JSLiteral ";"))) ]
421 (nt (JSLiteral ";"))) ]
424 declToJS (ExportsList exps) = map toExport exps
422 declToJS (ExportsList exps) = map toExport exps
@@ -467,6 +465,11 @@ codeGen optionsMainModule optionsNamespace ms = renderToString (NN (JSSourceElem
467 , lf
465 , lf
468 ]
466 ]
469
467
468+ require :: String -> [JSNode]
469+ require mn = [ sp (JSIdentifier "require")
470+ , NN (JSArguments (nt (JSLiteral "(")) [ nt (JSStringLiteral '"' mn) ] (nt (JSLiteral ")")))
471+ ]
472+
470 moduleReference :: (Node -> JSNode) -> String -> JSNode
473 moduleReference :: (Node -> JSNode) -> String -> JSNode
471 moduleReference f mn =
474 moduleReference f mn =
472 NN (JSMemberSquare [ f (JSIdentifier optionsNamespace) ]
475 NN (JSMemberSquare [ f (JSIdentifier optionsNamespace) ]