Skip to content

Commit

Permalink
[dartdevc] rename legacy to ddc
Browse files Browse the repository at this point in the history
The old name is still supported as an arg.

Change-Id: I864c54b977f5ef0cc1be4779422efbd539b67a2b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/125545
Commit-Queue: Vijay Menon <vsm@google.com>
Reviewed-by: Sigmund Cherem <sigmund@google.com>
  • Loading branch information
vsmenon authored and commit-bot@chromium.org committed Nov 20, 2019
1 parent 733db09 commit ae34d6e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 78 deletions.
16 changes: 1 addition & 15 deletions pkg/dev_compiler/lib/src/analyzer/module_compiler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -314,23 +314,9 @@ class JSModuleFile {
String mapPath = jsPath + '.map';
var code = getCode(
format, p.toUri(jsPath).toString(), p.toUri(mapPath).toString());
var c = code.code;
if (format == ModuleFormat.amdConcat ||
format == ModuleFormat.legacyConcat) {
// In single-out-file mode we wrap each module in an eval statement to
// leverage sourceURL to improve the debugging experience when source maps
// are not enabled.
//
// Note: We replace all `/` with `.` so that we don't break relative urls
// to sources in the original sourcemap. The name of this file is bogus
// anyways, so it has very little effect on things.
c += '\n//# sourceURL=${name.replaceAll("/", ".")}.js\n';
c = 'eval(${json.encode(c)});\n';
}

var file = File(jsPath);
if (!file.parent.existsSync()) file.parent.createSync(recursive: true);
file.writeAsStringSync(c);
file.writeAsStringSync(code.code);

// TODO(jacobr): it is a bit strange we are writing the source map to a file
// even when options.inlineSourceMap is true. To be consistent perhaps we
Expand Down
80 changes: 18 additions & 62 deletions pkg/dev_compiler/lib/src/compiler/module_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// BSD-style license that can be found in the LICENSE file.

import 'package:args/args.dart' show ArgParser, ArgResults;
import 'package:args/command_runner.dart' show UsageException;
import 'package:path/path.dart' as p;

import '../js_ast/js_ast.dart';
Expand All @@ -20,50 +19,24 @@ enum ModuleFormat {
/// Asynchronous Module Definition (AMD, used in browsers).
amd,

/// Dart Dev Compiler's legacy format (deprecated).
legacy,

/// Like [amd] but can be concatenated into a single file.
amdConcat,

/// Like [legacy] but can be concatenated into a single file.
legacyConcat
/// Dart Dev Compiler's own format.
ddc,
}

/// Parses a string into a [ModuleFormat].
ModuleFormat parseModuleFormat(String s) => {
'es6': ModuleFormat.es6,
'common': ModuleFormat.common,
'amd': ModuleFormat.amd,
'ddc': ModuleFormat.ddc,
// Deprecated:
'node': ModuleFormat.common,
'legacy': ModuleFormat.legacy
'legacy': ModuleFormat.ddc
}[s];

/// Parse the module format option added by [addModuleFormatOptions].
List<ModuleFormat> parseModuleFormatOption(ArgResults args) {
var formats =
(args['modules'] as List<String>).map(parseModuleFormat).toList();

if (args['single-out-file'] as bool) {
for (int i = 0; i < formats.length; i++) {
var format = formats[i];
switch (formats[i]) {
case ModuleFormat.amd:
formats[i] = ModuleFormat.amdConcat;
break;
case ModuleFormat.legacy:
formats[i] = ModuleFormat.legacyConcat;
break;
default:
throw UsageException(
'Format $format cannot be combined with '
'single-out-file. Only amd and legacy modes are supported.',
'');
}
}
}
return formats;
return (args['modules'] as List<String>).map(parseModuleFormat).toList();
}

/// Adds an option to the [argParser] for choosing the module format, optionally
Expand All @@ -74,7 +47,8 @@ void addModuleFormatOptions(ArgParser argParser, {bool hide = true}) {
'es6',
'common',
'amd',
'legacy', // deprecated
'ddc',
'legacy', // renamed to ddc
'node', // renamed to commonjs
'all' // to emit all flavors for the SDK
], allowedHelp: {
Expand All @@ -84,12 +58,6 @@ void addModuleFormatOptions(ArgParser argParser, {bool hide = true}) {
}, defaultsTo: [
'amd'
]);

argParser.addFlag('single-out-file',
help: 'emit modules that can be concatenated into one file.\n'
'Only compatible with legacy and amd module formats.',
defaultsTo: false,
hide: hide);
}

/// Transforms an ES6 [module] into a given module [format].
Expand All @@ -102,16 +70,12 @@ void addModuleFormatOptions(ArgParser argParser, {bool hide = true}) {
/// [ExportDeclaration]s.
Program transformModuleFormat(ModuleFormat format, Program module) {
switch (format) {
case ModuleFormat.legacy:
case ModuleFormat.legacyConcat:
// Legacy format always generates output compatible with single file mode.
return LegacyModuleBuilder().build(module);
case ModuleFormat.ddc:
return DdcModuleBuilder().build(module);
case ModuleFormat.common:
return CommonJSModuleBuilder().build(module);
case ModuleFormat.amd:
return AmdModuleBuilder().build(module);
case ModuleFormat.amdConcat:
return AmdModuleBuilder(singleOutFile: true).build(module);
case ModuleFormat.es6:
default:
return module;
Expand Down Expand Up @@ -161,9 +125,9 @@ abstract class _ModuleBuilder {
}
}

/// Generates modules for with our legacy `dart_library.js` loading mechanism.
/// Generates modules for with our DDC `dart_library.js` loading mechanism.
// TODO(jmesserly): remove this and replace with something that interoperates.
class LegacyModuleBuilder extends _ModuleBuilder {
class DdcModuleBuilder extends _ModuleBuilder {
Program build(Program module) {
// Collect imports/exports/statements.
visitProgram(module);
Expand All @@ -180,7 +144,8 @@ class LegacyModuleBuilder extends _ModuleBuilder {
TemporaryId(pathToJSIdentifier(import.from.valueWithoutQuotes));
parameters.add(moduleVar);
for (var importName in import.namedImports) {
assert(!importName.isStar); // import * not supported in legacy modules.
assert(!importName
.isStar); // import * not supported in ddc format modules.
var asName = importName.asName ?? importName.name;
var fromName = importName.name.name;
// Load non-SDK modules on demand (i.e., deferred).
Expand All @@ -204,7 +169,7 @@ class LegacyModuleBuilder extends _ModuleBuilder {
// TODO(jmesserly): make these immutable in JS?
for (var export in exports) {
var names = export.exportedNames;
assert(names != null); // export * not supported in legacy modules.
assert(names != null); // export * not supported in ddc modules.
for (var name in names) {
var alias = name.asName ?? name.name;
statements.add(
Expand Down Expand Up @@ -280,9 +245,7 @@ class CommonJSModuleBuilder extends _ModuleBuilder {

/// Generates AMD modules (used in browsers with RequireJS).
class AmdModuleBuilder extends _ModuleBuilder {
final bool singleOutFile;

AmdModuleBuilder({this.singleOutFile = false});
AmdModuleBuilder();

Program build(Program module) {
var importStatements = <Statement>[];
Expand Down Expand Up @@ -326,16 +289,9 @@ class AmdModuleBuilder extends _ModuleBuilder {
}

// TODO(vsm): Consider using an immediately invoked named function pattern
// (see legacy code above).
var block = singleOutFile
? js.statement("define(#, #, function(#) { 'use strict'; #; });", [
js.string(module.name, "'"),
ArrayInitializer(dependencies),
fnParams,
statements
])
: js.statement("define(#, function(#) { 'use strict'; #; });",
[ArrayInitializer(dependencies), fnParams, statements]);
// (see ddc module code above).
var block = js.statement("define(#, function(#) { 'use strict'; #; });",
[ArrayInitializer(dependencies), fnParams, statements]);

return Program([block]);
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/dev_compiler/tool/kernel_sdk.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ Future main(List<String> args) async {
'amd': ModuleFormat.amd,
'common': ModuleFormat.common,
'es6': ModuleFormat.es6,
'legacy': ModuleFormat.legacy,
'legacy': ModuleFormat.ddc,
};

for (var name in moduleFormats.keys) {
Expand Down

0 comments on commit ae34d6e

Please sign in to comment.