Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
58d47361
Commit
58d47361
authored
Sep 28, 2022
by
Tim Neutkens
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Revert "Fix"
This reverts commit
112589e2
.
parent
cafb5ff8
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
120 additions
and
129 deletions
+120
-129
minify.rs
packages/next-swc/crates/napi/src/minify.rs
+11
-13
parse.rs
packages/next-swc/crates/napi/src/parse.rs
+33
-38
transform.rs
packages/next-swc/crates/napi/src/transform.rs
+76
-78
No files found.
packages/next-swc/crates/napi/src/minify.rs
View file @
58d47361
...
...
@@ -35,7 +35,7 @@ use serde::Deserialize;
use
std
::
sync
::
Arc
;
use
swc_core
::{
base
::{
config
::
JsMinifyOptions
,
try_with_handler
,
TransformOutput
},
common
::{
errors
::
ColorConfig
,
sync
::
Lrc
,
FileName
,
SourceFile
,
SourceMap
,
GLOBALS
},
common
::{
errors
::
ColorConfig
,
sync
::
Lrc
,
FileName
,
SourceFile
,
SourceMap
},
};
struct
MinifyTask
{
...
...
@@ -85,17 +85,15 @@ impl Task for MinifyTask {
skip_filename
:
true
,
},
|
handler
|
{
GLOBALS
.set
(
&
Default
::
default
(),
||
{
let
fm
=
self
.code
.to_file
(
self
.c.cm
.clone
());
self
.c
.minify
(
fm
,
handler
,
&
JsMinifyOptions
{
..
self
.opts
.clone
()
},
)
})
let
fm
=
self
.code
.to_file
(
self
.c.cm
.clone
());
self
.c
.minify
(
fm
,
handler
,
&
JsMinifyOptions
{
..
self
.opts
.clone
()
},
)
},
)
.convert_err
()
...
...
@@ -133,7 +131,7 @@ pub fn minify_sync(cx: CallContext) -> napi::Result<JsObject> {
color
:
ColorConfig
::
Never
,
skip_filename
:
true
,
},
|
handler
|
GLOBALS
.set
(
&
Default
::
default
(),
||
c
.minify
(
fm
,
handler
,
&
opts
)
),
|
handler
|
c
.minify
(
fm
,
handler
,
&
opts
),
)
.convert_err
()
?
;
...
...
packages/next-swc/crates/napi/src/parse.rs
View file @
58d47361
...
...
@@ -4,9 +4,7 @@ use napi::{CallContext, Either, Env, JsObject, JsString, JsUndefined, Task};
use
std
::
sync
::
Arc
;
use
swc_core
::{
base
::{
config
::
ParseOptions
,
try_with_handler
},
common
::{
comments
::
Comments
,
errors
::
ColorConfig
,
FileName
,
FilePathMapping
,
SourceMap
,
GLOBALS
,
},
common
::{
comments
::
Comments
,
errors
::
ColorConfig
,
FileName
,
FilePathMapping
,
SourceMap
},
};
pub
struct
ParseTask
{
...
...
@@ -24,44 +22,41 @@ impl Task for ParseTask {
type
JsValue
=
JsString
;
fn
compute
(
&
mut
self
)
->
napi
::
Result
<
Self
::
Output
>
{
GLOBALS
.set
(
&
Default
::
default
(),
||
{
let
c
=
swc_core
::
base
::
Compiler
::
new
(
Arc
::
new
(
SourceMap
::
new
(
FilePathMapping
::
empty
())));
let
c
=
swc_core
::
base
::
Compiler
::
new
(
Arc
::
new
(
SourceMap
::
new
(
FilePathMapping
::
empty
())));
let
options
:
ParseOptions
=
deserialize_json
(
&
self
.options
)
.convert_err
()
?
;
let
comments
=
c
.comments
()
.clone
();
let
comments
:
Option
<&
dyn
Comments
>
=
if
options
.comments
{
Some
(
&
comments
)
}
else
{
None
};
let
fm
=
c
.cm
.new_source_file
(
self
.filename
.clone
(),
self
.src
.clone
());
let
program
=
try_with_handler
(
c
.cm
.clone
(),
swc_core
::
base
::
HandlerOpts
{
color
:
ColorConfig
::
Never
,
skip_filename
:
false
,
},
|
handler
|
{
c
.parse_js
(
fm
,
handler
,
options
.target
,
options
.syntax
,
options
.is_module
,
comments
,
)
},
)
.convert_err
()
?
;
let
options
:
ParseOptions
=
deserialize_json
(
&
self
.options
)
.convert_err
()
?
;
let
comments
=
c
.comments
()
.clone
();
let
comments
:
Option
<&
dyn
Comments
>
=
if
options
.comments
{
Some
(
&
comments
)
}
else
{
None
};
let
fm
=
c
.cm
.new_source_file
(
self
.filename
.clone
(),
self
.src
.clone
());
let
program
=
try_with_handler
(
c
.cm
.clone
(),
swc_core
::
base
::
HandlerOpts
{
color
:
ColorConfig
::
Never
,
skip_filename
:
false
,
},
|
handler
|
{
c
.parse_js
(
fm
,
handler
,
options
.target
,
options
.syntax
,
options
.is_module
,
comments
,
)
},
)
.convert_err
()
?
;
let
ast_json
=
serde_json
::
to_string
(
&
program
)
.context
(
"failed to serialize Program"
)
.convert_err
()
?
;
let
ast_json
=
serde_json
::
to_string
(
&
program
)
.context
(
"failed to serialize Program"
)
.convert_err
()
?
;
Ok
(
ast_json
)
})
Ok
(
ast_json
)
}
fn
resolve
(
self
,
env
:
Env
,
result
:
Self
::
Output
)
->
napi
::
Result
<
Self
::
JsValue
>
{
...
...
packages/next-swc/crates/napi/src/transform.rs
View file @
58d47361
...
...
@@ -44,7 +44,7 @@ use std::{
};
use
swc_core
::{
base
::{
try_with_handler
,
Compiler
,
TransformOutput
},
common
::{
errors
::
ColorConfig
,
FileName
,
GLOBALS
},
common
::{
errors
::
ColorConfig
,
FileName
},
ecma
::
transforms
::
base
::
pass
::
noop
,
};
...
...
@@ -68,85 +68,83 @@ impl Task for TransformTask {
type
JsValue
=
JsObject
;
fn
compute
(
&
mut
self
)
->
napi
::
Result
<
Self
::
Output
>
{
GLOBALS
.set
(
&
Default
::
default
(),
||
{
let
eliminated_packages
:
Rc
<
RefCell
<
fxhash
::
FxHashSet
<
String
>>>
=
Default
::
default
();
let
res
=
catch_unwind
(
AssertUnwindSafe
(||
{
try_with_handler
(
self
.c.cm
.clone
(),
swc_core
::
base
::
HandlerOpts
{
color
:
ColorConfig
::
Never
,
skip_filename
:
true
,
},
|
handler
|
{
self
.c
.run
(||
{
let
options
:
TransformOptions
=
deserialize_json
(
&
self
.options
)
?
;
let
fm
=
match
&
self
.input
{
Input
::
Source
{
src
}
=>
{
let
filename
=
if
options
.swc.filename
.is_empty
()
{
FileName
::
Anon
}
else
{
FileName
::
Real
(
options
.swc.filename
.clone
()
.into
())
};
self
.c.cm
.new_source_file
(
filename
,
src
.to_string
())
let
eliminated_packages
:
Rc
<
RefCell
<
fxhash
::
FxHashSet
<
String
>>>
=
Default
::
default
();
let
res
=
catch_unwind
(
AssertUnwindSafe
(||
{
try_with_handler
(
self
.c.cm
.clone
(),
swc_core
::
base
::
HandlerOpts
{
color
:
ColorConfig
::
Never
,
skip_filename
:
true
,
},
|
handler
|
{
self
.c
.run
(||
{
let
options
:
TransformOptions
=
deserialize_json
(
&
self
.options
)
?
;
let
fm
=
match
&
self
.input
{
Input
::
Source
{
src
}
=>
{
let
filename
=
if
options
.swc.filename
.is_empty
()
{
FileName
::
Anon
}
else
{
FileName
::
Real
(
options
.swc.filename
.clone
()
.into
())
};
self
.c.cm
.new_source_file
(
filename
,
src
.to_string
())
}
Input
::
FromFilename
=>
{
let
filename
=
&
options
.swc.filename
;
if
filename
.is_empty
()
{
bail!
(
"no filename is provided via options"
);
}
Input
::
FromFilename
=>
{
let
filename
=
&
options
.swc.filename
;
if
filename
.is_empty
()
{
bail!
(
"no filename is provided via options"
);
}
self
.c.cm
.new_source_file
(
FileName
::
Real
(
filename
.into
()),
read_to_string
(
filename
)
.with_context
(||
{
format!
(
"Failed to read source code from {}"
,
filename
)
})
?
,
)
}
};
let
options
=
options
.patch
(
&
fm
);
let
cm
=
self
.c.cm
.clone
();
let
file
=
fm
.clone
();
self
.c
.process_js_with_custom_pass
(
fm
,
None
,
handler
,
&
options
.swc
,
|
_
,
comments
|
{
custom_before_pass
(
cm
,
file
,
&
options
,
comments
.clone
(),
eliminated_packages
.clone
(),
)
},
|
_
,
_
|
noop
(),
)
})
},
)
}))
.map_err
(|
err
|
{
if
let
Some
(
s
)
=
err
.downcast_ref
::
<
String
>
()
{
anyhow!
(
"failed to process {}"
,
s
)
}
else
{
anyhow!
(
"failed to process"
)
}
});
match
res
{
Ok
(
res
)
=>
res
.map
(|
o
|
(
o
,
eliminated_packages
.replace
(
Default
::
default
())))
.convert_err
(),
Err
(
err
)
=>
Err
(
napi
::
Error
::
new
(
Status
::
GenericFailure
,
format!
(
"{:?}"
,
err
),
)),
self
.c.cm
.new_source_file
(
FileName
::
Real
(
filename
.into
()),
read_to_string
(
filename
)
.with_context
(||
{
format!
(
"Failed to read source code from {}"
,
filename
)
})
?
,
)
}
};
let
options
=
options
.patch
(
&
fm
);
let
cm
=
self
.c.cm
.clone
();
let
file
=
fm
.clone
();
self
.c
.process_js_with_custom_pass
(
fm
,
None
,
handler
,
&
options
.swc
,
|
_
,
comments
|
{
custom_before_pass
(
cm
,
file
,
&
options
,
comments
.clone
(),
eliminated_packages
.clone
(),
)
},
|
_
,
_
|
noop
(),
)
})
},
)
}))
.map_err
(|
err
|
{
if
let
Some
(
s
)
=
err
.downcast_ref
::
<
String
>
()
{
anyhow!
(
"failed to process {}"
,
s
)
}
else
{
anyhow!
(
"failed to process"
)
}
})
});
match
res
{
Ok
(
res
)
=>
res
.map
(|
o
|
(
o
,
eliminated_packages
.replace
(
Default
::
default
())))
.convert_err
(),
Err
(
err
)
=>
Err
(
napi
::
Error
::
new
(
Status
::
GenericFailure
,
format!
(
"{:?}"
,
err
),
)),
}
}
fn
resolve
(
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment