Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
P
protobuf
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
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
protobuf
Commits
1062d985
Commit
1062d985
authored
Feb 08, 2018
by
Peter Marton
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Feat: add import-style=commonjs_strict option to the compiler
parent
25625b95
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
33 additions
and
10 deletions
+33
-10
README.md
js/README.md
+1
-0
js_generator.cc
src/google/protobuf/compiler/js/js_generator.cc
+27
-6
js_generator.h
src/google/protobuf/compiler/js/js_generator.h
+5
-4
No files found.
js/README.md
View file @
1062d985
...
@@ -144,6 +144,7 @@ Some examples:
...
@@ -144,6 +144,7 @@ Some examples:
The
`import_style`
option is left to the default, which is
`closure`
.
The
`import_style`
option is left to the default, which is
`closure`
.
-
`--js_out=import_style=commonjs,binary:protos`
: this contains the options
-
`--js_out=import_style=commonjs,binary:protos`
: this contains the options
`import_style=commonjs`
and
`binary`
and outputs to the directory
`protos`
.
`import_style=commonjs`
and
`binary`
and outputs to the directory
`protos`
.
`import_style=commonjs_strict`
doesn't expose the output on the global scope.
API
API
===
===
...
...
src/google/protobuf/compiler/js/js_generator.cc
View file @
1062d985
...
@@ -276,7 +276,8 @@ string GetEnumPath(const GeneratorOptions& options,
...
@@ -276,7 +276,8 @@ string GetEnumPath(const GeneratorOptions& options,
string
MaybeCrossFileRef
(
const
GeneratorOptions
&
options
,
string
MaybeCrossFileRef
(
const
GeneratorOptions
&
options
,
const
FileDescriptor
*
from_file
,
const
FileDescriptor
*
from_file
,
const
Descriptor
*
to_message
)
{
const
Descriptor
*
to_message
)
{
if
(
options
.
import_style
==
GeneratorOptions
::
kImportCommonJs
&&
if
((
options
.
import_style
==
GeneratorOptions
::
kImportCommonJs
||
options
.
import_style
==
GeneratorOptions
::
kImportCommonJsStrict
)
&&
from_file
!=
to_message
->
file
())
{
from_file
!=
to_message
->
file
())
{
// Cross-file ref in CommonJS needs to use the module alias instead of
// Cross-file ref in CommonJS needs to use the module alias instead of
// the global name.
// the global name.
...
@@ -1675,8 +1676,18 @@ void Generator::GenerateProvides(const GeneratorOptions& options,
...
@@ -1675,8 +1676,18 @@ void Generator::GenerateProvides(const GeneratorOptions& options,
//
//
// // Later generated code expects foo.bar = {} to exist:
// // Later generated code expects foo.bar = {} to exist:
// foo.bar.Baz = function() { /* ... */ }
// foo.bar.Baz = function() { /* ... */ }
printer
->
Print
(
"goog.exportSymbol('$name$', null, global);
\n
"
,
"name"
,
*
it
);
// Do not use global scope in strict mode
if
(
options
.
import_style
==
GeneratorOptions
::
kImportCommonJsStrict
)
{
string
namespaceObject
=
*
it
;
// Remove "proto." from the namespace object
namespaceObject
.
erase
(
0
,
6
);
printer
->
Print
(
"goog.exportSymbol('$name$', null, proto);
\n
"
,
"name"
,
namespaceObject
);
}
else
{
printer
->
Print
(
"goog.exportSymbol('$name$', null, global);
\n
"
,
"name"
,
*
it
);
}
}
}
}
}
}
}
...
@@ -3321,6 +3332,8 @@ bool GeneratorOptions::ParseFromOptions(
...
@@ -3321,6 +3332,8 @@ bool GeneratorOptions::ParseFromOptions(
import_style
=
kImportClosure
;
import_style
=
kImportClosure
;
}
else
if
(
options
[
i
].
second
==
"commonjs"
)
{
}
else
if
(
options
[
i
].
second
==
"commonjs"
)
{
import_style
=
kImportCommonJs
;
import_style
=
kImportCommonJs
;
}
else
if
(
options
[
i
].
second
==
"commonjs_strict"
)
{
import_style
=
kImportCommonJsStrict
;
}
else
if
(
options
[
i
].
second
==
"browser"
)
{
}
else
if
(
options
[
i
].
second
==
"browser"
)
{
import_style
=
kImportBrowser
;
import_style
=
kImportBrowser
;
}
else
if
(
options
[
i
].
second
==
"es6"
)
{
}
else
if
(
options
[
i
].
second
==
"es6"
)
{
...
@@ -3430,10 +3443,17 @@ void Generator::GenerateFile(const GeneratorOptions& options,
...
@@ -3430,10 +3443,17 @@ void Generator::GenerateFile(const GeneratorOptions& options,
GenerateHeader
(
options
,
printer
);
GenerateHeader
(
options
,
printer
);
// Generate "require" statements.
// Generate "require" statements.
if
(
options
.
import_style
==
GeneratorOptions
::
kImportCommonJs
)
{
if
((
options
.
import_style
==
GeneratorOptions
::
kImportCommonJs
||
options
.
import_style
==
GeneratorOptions
::
kImportCommonJsStrict
))
{
printer
->
Print
(
"var jspb = require('google-protobuf');
\n
"
);
printer
->
Print
(
"var jspb = require('google-protobuf');
\n
"
);
printer
->
Print
(
"var goog = jspb;
\n
"
);
printer
->
Print
(
"var goog = jspb;
\n
"
);
printer
->
Print
(
"var global = Function('return this')();
\n\n
"
);
// Do not use global scope in strict mode
if
(
options
.
import_style
==
GeneratorOptions
::
kImportCommonJsStrict
)
{
printer
->
Print
(
"var proto = {};
\n\n
"
);
}
else
{
printer
->
Print
(
"var global = Function('return this')();
\n\n
"
);
}
for
(
int
i
=
0
;
i
<
file
->
dependency_count
();
i
++
)
{
for
(
int
i
=
0
;
i
<
file
->
dependency_count
();
i
++
)
{
const
string
&
name
=
file
->
dependency
(
i
)
->
name
();
const
string
&
name
=
file
->
dependency
(
i
)
->
name
();
...
@@ -3477,7 +3497,8 @@ void Generator::GenerateFile(const GeneratorOptions& options,
...
@@ -3477,7 +3497,8 @@ void Generator::GenerateFile(const GeneratorOptions& options,
GenerateExtension
(
options
,
printer
,
*
it
);
GenerateExtension
(
options
,
printer
,
*
it
);
}
}
if
(
options
.
import_style
==
GeneratorOptions
::
kImportCommonJs
)
{
if
((
options
.
import_style
==
GeneratorOptions
::
kImportCommonJs
||
options
.
import_style
==
GeneratorOptions
::
kImportCommonJsStrict
))
{
printer
->
Print
(
"goog.object.extend(exports, $package$);
\n
"
,
printer
->
Print
(
"goog.object.extend(exports, $package$);
\n
"
,
"package"
,
GetFilePath
(
options
,
file
));
"package"
,
GetFilePath
(
options
,
file
));
}
}
...
...
src/google/protobuf/compiler/js/js_generator.h
View file @
1062d985
...
@@ -63,10 +63,11 @@ struct GeneratorOptions {
...
@@ -63,10 +63,11 @@ struct GeneratorOptions {
bool
binary
;
bool
binary
;
// What style of imports should be used.
// What style of imports should be used.
enum
ImportStyle
{
enum
ImportStyle
{
kImportClosure
,
// goog.require()
kImportClosure
,
// goog.require()
kImportCommonJs
,
// require()
kImportCommonJs
,
// require()
kImportBrowser
,
// no import statements
kImportCommonJsStrict
,
// require() with no global export
kImportEs6
,
// import { member } from ''
kImportBrowser
,
// no import statements
kImportEs6
,
// import { member } from ''
}
import_style
;
}
import_style
;
GeneratorOptions
()
GeneratorOptions
()
...
...
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