forked from purescript/purescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBundle.hs
More file actions
444 lines (396 loc) · 15.6 KB
/
Bundle.hs
File metadata and controls
444 lines (396 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
-- |
-- Bundles compiled PureScript modules for the browser.
--
-- This module takes as input the individual generated modules from 'Language.PureScript.Make' and
-- performs dead code elimination, filters empty modules,
-- and generates the final JavaScript bundle.
module Language.PureScript.Bundle
( ModuleIdentifier(..)
, ModuleType(..)
, ErrorMessage(..)
, printErrorMessage
, ForeignModuleExports(..)
, getExportedIdentifiers
, ForeignModuleImports(..)
, getImportedModules
, Module
) where
import Prelude
import Control.Monad.Error.Class
import Data.Aeson ((.=))
import Data.Char (chr, digitToInt)
import Data.Foldable (fold)
import Data.Maybe (mapMaybe, maybeToList)
import qualified Data.Aeson as A
import qualified Data.Text.Lazy as LT
import Language.JavaScript.Parser
import Language.JavaScript.Parser.AST
import Language.JavaScript.Process.Minify
-- | The type of error messages. We separate generation and rendering of errors using a data
-- type, in case we need to match on error types later.
data ErrorMessage
= UnsupportedModulePath String
| InvalidTopLevel
| UnableToParseModule String
| UnsupportedImport
| UnsupportedExport
| ErrorInModule ModuleIdentifier ErrorMessage
| MissingEntryPoint String
| MissingMainModule String
deriving (Show)
-- | Modules are either "regular modules" (i.e. those generated by the PureScript compiler) or
-- foreign modules.
data ModuleType
= Regular
| Foreign
deriving (Show, Eq, Ord)
showModuleType :: ModuleType -> String
showModuleType Regular = "Regular"
showModuleType Foreign = "Foreign"
-- | A module is identified by its module name and its type.
data ModuleIdentifier = ModuleIdentifier String ModuleType deriving (Show, Eq, Ord)
instance A.ToJSON ModuleIdentifier where
toJSON (ModuleIdentifier name mt) =
A.object [ "name" .= name
, "type" .= show mt
]
data Visibility
= Public
| Internal
deriving (Show, Eq, Ord)
-- | A piece of code is identified by its module, its name, and whether it is an internal variable
-- or a public member. These keys are used to label vertices in the dependency graph.
type Key = (ModuleIdentifier, String, Visibility)
-- | An export is either a "regular export", which exports a name from the regular module we are in,
-- or a reexport of a declaration in the corresponding foreign module.
--
-- Regular exports are labelled, since they might re-export an operator with another name.
data ExportType
= RegularExport String
| ForeignReexport
deriving (Show, Eq, Ord)
-- | There are four types of module element we are interested in:
--
-- 1) Import declarations and require statements
-- 2) Member declarations
-- 3) Export lists
-- 4) Everything else
--
-- Each is labelled with the original AST node which generated it, so that we can dump it back
-- into the output during codegen.
data ModuleElement
= Import JSModuleItem String (Either String ModuleIdentifier)
| Member JSStatement Visibility String JSExpression [Key]
| ExportsList [(ExportType, String, JSExpression, [Key])]
| Other JSStatement
| Skip JSModuleItem
deriving (Show)
instance A.ToJSON ModuleElement where
toJSON = \case
(Import _ name (Right target)) ->
A.object [ "type" .= A.String "Import"
, "name" .= name
, "target" .= target
]
(Import _ name (Left targetPath)) ->
A.object [ "type" .= A.String "Import"
, "name" .= name
, "targetPath" .= targetPath
]
(Member _ visibility name _ dependsOn) ->
A.object [ "type" .= A.String "Member"
, "name" .= name
, "visibility" .= show visibility
, "dependsOn" .= map keyToJSON dependsOn
]
(ExportsList exports) ->
A.object [ "type" .= A.String "ExportsList"
, "exports" .= map exportToJSON exports
]
(Other stmt) ->
A.object [ "type" .= A.String "Other"
, "js" .= getFragment (JSAstStatement stmt JSNoAnnot)
]
(Skip item) ->
A.object [ "type" .= A.String "Skip"
, "js" .= getFragment (JSAstModule [item] JSNoAnnot)
]
where
keyToJSON (mid, member, visibility) =
A.object [ "module" .= mid
, "member" .= member
, "visibility" .= show visibility
]
exportToJSON (RegularExport sourceName, name, _, dependsOn) =
A.object [ "type" .= A.String "RegularExport"
, "name" .= name
, "sourceName" .= sourceName
, "dependsOn" .= map keyToJSON dependsOn
]
exportToJSON (ForeignReexport, name, _, dependsOn) =
A.object [ "type" .= A.String "ForeignReexport"
, "name" .= name
, "dependsOn" .= map keyToJSON dependsOn
]
getFragment = ellipsize . renderToText . minifyJS
where
ellipsize text = if LT.compareLength text 20 == GT then LT.take 19 text `LT.snoc` ellipsis else text
ellipsis = '\x2026'
-- | A module is just a list of elements of the types listed above.
data Module = Module ModuleIdentifier (Maybe FilePath) [ModuleElement] deriving (Show)
instance A.ToJSON Module where
toJSON (Module moduleId filePath elements) =
A.object [ "moduleId" .= moduleId
, "filePath" .= filePath
, "elements" .= elements
]
-- | Prepare an error message for consumption by humans.
printErrorMessage :: ErrorMessage -> [String]
printErrorMessage (UnsupportedModulePath s) =
[ "An ES or CommonJS module has an unsupported name (" ++ show s ++ ")."
, "The following file names are supported:"
, " 1) index.js (PureScript native modules)"
, " 2) foreign.js (PureScript ES foreign modules)"
, " 3) foreign.cjs (PureScript CommonJS foreign modules)"
]
printErrorMessage InvalidTopLevel =
[ "Expected a list of source elements at the top level." ]
printErrorMessage (UnableToParseModule err) =
[ "The module could not be parsed:"
, err
]
printErrorMessage UnsupportedImport =
[ "An import was unsupported."
, "Modules can be imported with ES namespace imports declarations:"
, " import * as module from \"Module.Name\""
, "Alternatively, they can be also be imported with the CommonJS require function:"
, " var module = require(\"Module.Name\")"
]
printErrorMessage UnsupportedExport =
[ "An export was unsupported."
, "Declarations can be exported as ES named exports:"
, " export var decl"
, "Existing identifiers can be exported as well:"
, " export { name }"
, "They can also be renamed on export:"
, " export { name as alias }"
, "Alternatively, CommonJS exports can be defined in one of two ways:"
, " 1) exports.name = value"
, " 2) exports = { name: value }"
]
printErrorMessage (ErrorInModule mid e) =
("Error in module " ++ displayIdentifier mid ++ ":")
: ""
: map (" " ++) (printErrorMessage e)
where
displayIdentifier (ModuleIdentifier name ty) =
name ++ " (" ++ showModuleType ty ++ ")"
printErrorMessage (MissingEntryPoint mName) =
[ "Could not find an ES module or CommonJS module for the specified entry point: " ++ mName
]
printErrorMessage (MissingMainModule mName) =
[ "Could not find an ES module or CommonJS module for the specified main module: " ++ mName
]
-- String literals include the quote chars
fromStringLiteral :: JSExpression -> Maybe String
fromStringLiteral (JSStringLiteral _ str) = Just $ strValue str
fromStringLiteral _ = Nothing
strValue :: String -> String
strValue str = go $ drop 1 str
where
go ('\\' : 'b' : xs) = '\b' : go xs
go ('\\' : 'f' : xs) = '\f' : go xs
go ('\\' : 'n' : xs) = '\n' : go xs
go ('\\' : 'r' : xs) = '\r' : go xs
go ('\\' : 't' : xs) = '\t' : go xs
go ('\\' : 'v' : xs) = '\v' : go xs
go ('\\' : '0' : xs) = '\0' : go xs
go ('\\' : 'x' : a : b : xs) = chr (a' + b') : go xs
where
a' = 16 * digitToInt a
b' = digitToInt b
go ('\\' : 'u' : a : b : c : d : xs) = chr (a' + b' + c' + d') : go xs
where
a' = 16 * 16 * 16 * digitToInt a
b' = 16 * 16 * digitToInt b
c' = 16 * digitToInt c
d' = digitToInt d
go ('\\' : x : xs) = x : go xs
go "\"" = ""
go "'" = ""
go (x : xs) = x : go xs
go "" = ""
commaList :: JSCommaList a -> [a]
commaList JSLNil = []
commaList (JSLOne x) = [x]
commaList (JSLCons l _ x) = commaList l ++ [x]
trailingCommaList :: JSCommaTrailingList a -> [a]
trailingCommaList (JSCTLComma l _) = commaList l
trailingCommaList (JSCTLNone l) = commaList l
identName :: JSIdent -> Maybe String
identName (JSIdentName _ ident) = Just ident
identName _ = Nothing
exportStatementIdentifiers :: JSStatement -> [String]
exportStatementIdentifiers (JSVariable _ jsExpressions _) =
varNames jsExpressions
exportStatementIdentifiers (JSConstant _ jsExpressions _) =
varNames jsExpressions
exportStatementIdentifiers (JSLet _ jsExpressions _) =
varNames jsExpressions
exportStatementIdentifiers (JSClass _ jsIdent _ _ _ _ _) =
maybeToList . identName $ jsIdent
exportStatementIdentifiers (JSFunction _ jsIdent _ _ _ _ _) =
maybeToList . identName $ jsIdent
exportStatementIdentifiers (JSGenerator _ _ jsIdent _ _ _ _ _) =
maybeToList . identName $ jsIdent
exportStatementIdentifiers _ = []
varNames :: JSCommaList JSExpression -> [String]
varNames = mapMaybe varName . commaList
where
varName (JSVarInitExpression (JSIdentifier _ ident) _) = Just ident
varName _ = Nothing
data ForeignModuleExports =
ForeignModuleExports
{ cjsExports :: [String]
, esExports :: [String]
} deriving (Show)
instance Semigroup ForeignModuleExports where
(ForeignModuleExports cjsExports esExports) <> (ForeignModuleExports cjsExports' esExports') =
ForeignModuleExports (cjsExports <> cjsExports') (esExports <> esExports')
instance Monoid ForeignModuleExports where
mempty = ForeignModuleExports [] []
-- Get a list of all the exported identifiers from a foreign module.
--
-- TODO: what if we assign to exports.foo and then later assign to
-- module.exports (presumably overwriting exports.foo)?
getExportedIdentifiers :: forall m. (MonadError ErrorMessage m)
=> String
-> JSAST
-> m ForeignModuleExports
getExportedIdentifiers mname top
| JSAstModule jsModuleItems _ <- top = fold <$> traverse go jsModuleItems
| otherwise = err InvalidTopLevel
where
err :: ErrorMessage -> m a
err = throwError . ErrorInModule (ModuleIdentifier mname Foreign)
go (JSModuleStatementListItem jsStatement)
| Just props <- matchExportsAssignment jsStatement
= do cjsExports <- traverse toIdent (trailingCommaList props)
pure ForeignModuleExports{ cjsExports, esExports = [] }
| Just (Public, name, _) <- matchMember jsStatement
= pure ForeignModuleExports{ cjsExports = [name], esExports = [] }
| otherwise
= pure mempty
go (JSModuleExportDeclaration _ jsExportDeclaration) =
pure ForeignModuleExports{ cjsExports = [], esExports = exportDeclarationIdentifiers jsExportDeclaration }
go _ = pure mempty
toIdent (JSPropertyNameandValue name _ [_]) =
extractLabel' name
toIdent _ =
err UnsupportedExport
extractLabel' = maybe (err UnsupportedExport) pure . extractLabel
exportDeclarationIdentifiers (JSExportFrom jsExportClause _ _) =
exportClauseIdentifiers jsExportClause
exportDeclarationIdentifiers (JSExportLocals jsExportClause _) =
exportClauseIdentifiers jsExportClause
exportDeclarationIdentifiers (JSExport jsStatement _) =
exportStatementIdentifiers jsStatement
exportClauseIdentifiers (JSExportClause _ jsExportsSpecifiers _) =
mapMaybe exportSpecifierName $ commaList jsExportsSpecifiers
exportSpecifierName (JSExportSpecifier jsIdent) = identName jsIdent
exportSpecifierName (JSExportSpecifierAs _ _ jsIdentAs) = identName jsIdentAs
data ForeignModuleImports =
ForeignModuleImports
{ cjsImports :: [String]
, esImports :: [String]
} deriving (Show)
instance Semigroup ForeignModuleImports where
(ForeignModuleImports cjsImports esImports) <> (ForeignModuleImports cjsImports' esImports') =
ForeignModuleImports (cjsImports <> cjsImports') (esImports <> esImports')
instance Monoid ForeignModuleImports where
mempty = ForeignModuleImports [] []
-- Get a list of all the imported module identifiers from a foreign module.
getImportedModules :: forall m. (MonadError ErrorMessage m)
=> String
-> JSAST
-> m ForeignModuleImports
getImportedModules mname top
| JSAstModule jsModuleItems _ <- top = pure $ foldMap go jsModuleItems
| otherwise = err InvalidTopLevel
where
err :: ErrorMessage -> m a
err = throwError . ErrorInModule (ModuleIdentifier mname Foreign)
go (JSModuleStatementListItem jsStatement)
| Just (_, mid) <- matchRequire jsStatement
= ForeignModuleImports{ cjsImports = [mid], esImports = [] }
go (JSModuleImportDeclaration _ jsImportDeclaration) =
ForeignModuleImports{ cjsImports = [], esImports = [importDeclarationModuleId jsImportDeclaration] }
go _ = mempty
importDeclarationModuleId (JSImportDeclaration _ (JSFromClause _ _ mid) _) = mid
importDeclarationModuleId (JSImportDeclarationBare _ mid _) = mid
-- Matches JS statements like this:
-- var ModuleName = require("file");
matchRequire :: JSStatement -> Maybe (String, String)
matchRequire stmt
| JSVariable _ jsInit _ <- stmt
, [JSVarInitExpression var varInit] <- commaList jsInit
, JSIdentifier _ importName <- var
, JSVarInit _ jsInitEx <- varInit
, JSMemberExpression req _ argsE _ <- jsInitEx
, JSIdentifier _ "require" <- req
, [ Just importPath ] <- map fromStringLiteral (commaList argsE)
= Just (importName, importPath)
| otherwise
= Nothing
-- Matches JS member declarations.
matchMember :: JSStatement -> Maybe (Visibility, String, JSExpression)
matchMember stmt
| Just (name, decl) <- matchInternalMember stmt
= pure (Internal, name, decl)
-- exports.foo = expr; exports["foo"] = expr;
| JSAssignStatement e (JSAssign _) decl _ <- stmt
, Just name <- exportsAccessor e
= Just (Public, name, decl)
| otherwise
= Nothing
matchInternalMember :: JSStatement -> Maybe (String, JSExpression)
matchInternalMember stmt
-- var foo = expr;
| JSVariable _ jsInit _ <- stmt
, [JSVarInitExpression var varInit] <- commaList jsInit
, JSIdentifier _ name <- var
, JSVarInit _ decl <- varInit
= pure (name, decl)
-- function foo(...args) { body }
| JSFunction a0 jsIdent a1 args a2 body _ <- stmt
, JSIdentName _ name <- jsIdent
= pure (name, JSFunctionExpression a0 jsIdent a1 args a2 body)
| otherwise
= Nothing
-- Matches exports.* or exports["*"] expressions and returns the property name.
exportsAccessor :: JSExpression -> Maybe String
exportsAccessor (JSMemberDot exports _ nm)
| JSIdentifier _ "exports" <- exports
, JSIdentifier _ name <- nm
= Just name
exportsAccessor (JSMemberSquare exports _ nm _)
| JSIdentifier _ "exports" <- exports
, Just name <- fromStringLiteral nm
= Just name
exportsAccessor _ = Nothing
-- Matches assignments to module.exports, like this:
-- module.exports = { ... }
matchExportsAssignment :: JSStatement -> Maybe JSObjectPropertyList
matchExportsAssignment stmt
| JSAssignStatement e (JSAssign _) decl _ <- stmt
, JSMemberDot module' _ exports <- e
, JSIdentifier _ "module" <- module'
, JSIdentifier _ "exports" <- exports
, JSObjectLiteral _ props _ <- decl
= Just props
| otherwise
= Nothing
extractLabel :: JSPropertyName -> Maybe String
extractLabel (JSPropertyString _ nm) = Just $ strValue nm
extractLabel (JSPropertyIdent _ nm) = Just nm
extractLabel _ = Nothing