Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate source maps #356

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions spec/compilers/component_with_provider
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ class C extends _C {
componentDidUpdate() {
if (false) {
B._subscribe(this, new A({
moves: (a) => {
return null
},
ups: (b) => {
return null
}
}))
moves: (a) => {
return null
},
ups: (b) => {
return null
}
}))
} else {
B._unsubscribe(this)
};
Expand All @@ -58,13 +58,13 @@ class C extends _C {
componentDidMount() {
if (false) {
B._subscribe(this, new A({
moves: (a) => {
return null
},
ups: (b) => {
return null
}
}))
moves: (a) => {
return null
},
ups: (b) => {
return null
}
}))
} else {
B._unsubscribe(this)
};
Expand Down
28 changes: 14 additions & 14 deletions spec/compilers/component_with_provider_and_lifecycle_functions
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ class C extends _C {
componentDidUpdate() {
if (false) {
B._subscribe(this, new A({
moves: (a) => {
return null
},
ups: (b) => {
return null
}
}))
moves: (a) => {
return null
},
ups: (b) => {
return null
}
}))
} else {
B._unsubscribe(this)
};
Expand All @@ -73,13 +73,13 @@ class C extends _C {
componentDidMount() {
if (false) {
B._subscribe(this, new A({
moves: (a) => {
return null
},
ups: (b) => {
return null
}
}))
moves: (a) => {
return null
},
ups: (b) => {
return null
}
}))
} else {
B._unsubscribe(this)
};
Expand Down
28 changes: 14 additions & 14 deletions spec/compilers/component_with_provider_and_store
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ class C extends _C {
componentDidUpdate() {
if (false) {
B._subscribe(this, new A({
moves: (a) => {
return null
},
ups: (b) => {
return null
}
}))
moves: (a) => {
return null
},
ups: (b) => {
return null
}
}))
} else {
B._unsubscribe(this)
};
Expand All @@ -77,13 +77,13 @@ class C extends _C {

if (false) {
B._subscribe(this, new A({
moves: (a) => {
return null
},
ups: (b) => {
return null
}
}))
moves: (a) => {
return null
},
ups: (b) => {
return null
}
}))
} else {
B._unsubscribe(this)
};
Expand Down
1 change: 1 addition & 0 deletions spec/compilers/directives/format
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class A extends _C {
return (() => {
const [a,b] = [`HelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHBello`, `"HelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloH" \\
"Bello"`];

return a + b;
})();
}
Expand Down
8 changes: 4 additions & 4 deletions spec/compilers/module_access_subscriptions
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class C extends _C {
componentDidUpdate() {
if (true) {
B._subscribe(this, new A({
test: ``
}))
test: ``
}))
} else {
B._unsubscribe(this)
};
Expand All @@ -57,8 +57,8 @@ class C extends _C {
componentDidMount() {
if (true) {
B._subscribe(this, new A({
test: ``
}))
test: ``
}))
} else {
B._unsubscribe(this)
};
Expand Down
67 changes: 67 additions & 0 deletions spec/indented_string_builder_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
require "./spec_helper"

INDENT = 2

describe Mint::IndentedStringBuilder do
it "indent class with constructor and display name" do
b = Mint::IndentedStringBuilder.new

b << "class " << "A" << " extends " << "_C" << " " << "{\n"
b.indent_size += INDENT
b << "constructor" << "(" << "props" << ") "
b << "{\n"
b.indent_size += INDENT
b << "super" << "(" << "props" << ")" << ";"
# js.call
b << "\n\n"
b << "this._d" << "("
b << "{\n"
b.indent_size += INDENT
b << "a" << ": " << "[\n"
b.indent_size += INDENT
b << "null" << "," << "\n" << "`Hello`"
b.indent_size -= INDENT
b << "\n]"
b.indent_size -= INDENT
b << "\n}"
b << ")" << ";"
b.indent_size -= INDENT
b << "\n}"
b.indent_size -= INDENT
b << "\n}"
b << ";"
b << "\n\n"
b << "A.displayName = \"Test\""
b << ";"

expected =
<<-STR
class A extends _C {
constructor(props) {
super(props);

this._d({
a: [
null,
`Hello`
]
});
}
};

A.displayName = "Test";
STR

result = b.build

pos = b.get_position_for_next_input
pos[:line].should eq(13)
pos[:column].should eq(23)

begin
result.should eq(expected)
rescue error
fail diff(expected, result)
end
end
end
8 changes: 7 additions & 1 deletion spec/js_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ describe "JS" do

context "object" do
it "renders object Optimized" do
optimized.object({"a" => "b", "c" => "d"}).should eq("{a:b,c:d}")
subject =
optimized.object({"a" => "b", "c" => "d"})

result =
Mint::Codegen.build(subject)[:code]

result.should eq("{a:b,c:d}")
end
end
end
Expand Down
22 changes: 16 additions & 6 deletions src/builder.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Mint
class Builder
def initialize(relative, skip_service_worker, skip_icons, optimize)
def initialize(relative, skip_service_worker, skip_icons, optimize, source_map)
json = MintJson.parse_current

if !skip_icons && !Process.find_executable("convert")
Expand All @@ -26,10 +26,17 @@ module Mint

terminal.puts "#{COG} Compiling your application:"

index_js, artifacts =
index(json.application.css_prefix, relative, optimize)
build, artifacts =
index(json.application.css_prefix, relative, optimize, source_map)

File.write Path[DIST_DIR, "index.js"], index_js
File.write Path[DIST_DIR, "index.js"], build[:code]

if source_map
terminal.puts "#{COG} Generating source map:"
build[:source_map].try do |map|
File.write Path[DIST_DIR, "index.js.map"], map.build_json
end
end

if SourceFiles.external_javascripts?
terminal.measure "#{COG} Writing external javascripts..." do
Expand Down Expand Up @@ -128,7 +135,7 @@ module Mint
end
end

def index(css_prefix, relative, optimize)
def index(css_prefix, relative, optimize, source_map)
runtime =
Assets.read("runtime.js")

Expand Down Expand Up @@ -163,7 +170,10 @@ module Mint
}
end

{runtime + compiled.to_s, type_checker.artifacts}
build_result =
Codegen.build(Codegen.join([runtime, compiled].compact), source_map)

{build_result, type_checker.artifacts}
end

def terminal
Expand Down
7 changes: 6 additions & 1 deletion src/commands/build.cr
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@ module Mint
default: true,
short: "m"

define_flag source_map : Bool,
description: "If specified generate source mappings for debugging",
default: false

def run
execute "Building for production" do
Builder.new(
flags.relative,
flags.skip_service_worker,
flags.skip_icons,
flags.minify
flags.minify,
flags.source_map
)
end
end
Expand Down
31 changes: 26 additions & 5 deletions src/commands/compile.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,36 @@ module Mint
define_flag output : String,
description: "The output file",
default: "program.js",
required: false,
short: "o"

define_flag minify : Bool,
description: "If specified the resulting JavaScript code will be minified",
default: true,
short: "m"

define_flag source_map : Bool,
description: "If specified generate source mappings for debugging",
default: false

def run
execute "Compiling" do
File.write(flags.output, compile(flags.minify))
result =
compile(flags.minify, flags.source_map)

File.write(flags.output, result[:code])

result[:source_map].try do |map|
File.write("#{flags.output}.map", map)
end
end
end

def compile(optimize)
def compile(optimize, generate_source_map : Bool)
json =
MintJson.parse_current

runtime =
Assets.read("runtime.js")
Assets.read("runtime.js").as(Codegen::Node)

sources =
Dir.glob(SourceFiles.all)
Expand Down Expand Up @@ -60,7 +70,18 @@ module Mint
}
end

runtime + compiled.to_s
result =
Codegen.build(Codegen.join([runtime, compiled].compact), generate_source_map)

source_map : String? = nil

result[:source_map].try do |map|
terminal.measure " #{ARROW} Generating source map: " do
source_map = map.build_json
end
end

{code: result[:code], source_map: source_map}
end
end
end
Expand Down
9 changes: 7 additions & 2 deletions src/commands/start.cr
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,21 @@ module Mint
default: (ENV["PORT"]? || "3000").to_i,
required: false,
short: "p"

define_flag live_reload : Bool,
description: "Whether or not to reload the browser when something changes. (Default true)",
required: false,
default: true,
short: "r"

define_flag source_map : Bool,
description: "If specified generate source mappings for debugging",
default: true,
required: false,
short: "m"

def run
execute "Running the development server" do
Reactor.start flags.host, flags.port, flags.auto_format, flags.live_reload
Reactor.start flags.host, flags.port, flags.auto_format, flags.live_reload, flags.source_map
end
end
end
Expand Down
Loading