Skip to content

Commit

Permalink
Don't write legacy builds when using new-style compiler plugins.
Browse files Browse the repository at this point in the history
Also decompose Isopack.prototype._canWriteLegacyBuilds.
  • Loading branch information
Ben Newman committed Jul 16, 2015
1 parent 5bbcaed commit e8a32f4
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 21 deletions.
29 changes: 26 additions & 3 deletions tools/build-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,10 @@ export class SourceProcessorSet {
// If there is no special sourceProcessor for handling a .js file,
// we can still classify it as extension/js, only without any
// source processors. #HardcodeJs
return new SourceClassification('extension', {extension});
return new SourceClassification('extension', {
extension,
usesDefaultSourceProcessor: true
});
}

if (this._legacyHandlers.hasOwnProperty(extension)) {
Expand Down Expand Up @@ -287,8 +290,14 @@ export class SourceProcessorSet {
}

class SourceClassification {
constructor(type, {legacyHandler, extension, sourceProcessors,
legacyIsTemplate, arch} = {}) {
constructor(type, {
legacyHandler,
extension,
sourceProcessors,
usesDefaultSourceProcessor,
legacyIsTemplate,
arch,
} = {}) {
const knownTypes = ['extension', 'filename', 'legacy-handler', 'wrong-arch',
'unmatched'];
if (knownTypes.indexOf(type) === -1) {
Expand Down Expand Up @@ -347,6 +356,20 @@ class SourceClassification {
}
this.extension = extension;
}

if (usesDefaultSourceProcessor) {
if (this.extension !== 'js' &&
this.extension !== 'css') {
// We only currently hard-code support for processing .js files
// when no source processor is registered (#HardcodeJs). Default
// support could conceivably be extended to .css files too, but
// anything else is almost certainly a mistake.
throw Error('non-JS/CSS file relying on default source processor?');
}
this.usesDefaultSourceProcessor = true;
} else {
this.usesDefaultSourceProcessor = false;
}
}

isNonLegacySource() {
Expand Down
2 changes: 2 additions & 0 deletions tools/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,8 @@ var compileUnibuild = function (options) {
resources.push({
type: "source",
extension: classification.extension || null,
usesDefaultSourceProcessor:
!! classification.usesDefaultSourceProcessor,
data: contents,
path: relPath,
hash: hash,
Expand Down
50 changes: 32 additions & 18 deletions tools/isopack.js
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,8 @@ _.extend(Isopack.prototype, {
resources.push({
type: "source",
extension: resource.extension,
usesDefaultSourceProcessor:
!! resource.usesDefaultSourceProcessor,
data: data,
path: resource.path,
hash: resource.hash,
Expand Down Expand Up @@ -943,6 +945,33 @@ _.extend(Isopack.prototype, {
});
},

_canWriteLegacyBuilds(options) {
if (! options.includePreCompilerPluginIsopackVersions) {
return false;
}

function isResourceUnsafeForLegacyBuilds(resource) {
if (resource.type === "source") {
// This package cannot be represented as an isopack-1 Isopack if
// it uses a file implemented by registerCompiler other than the
// very basic JS and CSS types.
if (resource.extension === "js") {
// If this JS resource uses hard-coded support for plain old
// ES5, then it is safe to write as part of a legacy Isopack.
return ! resource.usesDefaultSourceProcessor;
}

return resource.extension !== "css";
}

return false;
}

return ! this.unibuilds.some(
unibuild => unibuild.resources.some(isResourceUnsafeForLegacyBuilds)
);
},

// options:
//
// - includeIsopackBuildInfo: If set, write an isopack-buildinfo.json file.
Expand Down Expand Up @@ -983,13 +1012,7 @@ _.extend(Isopack.prototype, {
mainJson.cordovaDependencies = self.cordovaDependencies;
}

var writeLegacyBuilds = false;
if (options.includePreCompilerPluginIsopackVersions) {
// We will reset this to false if at any point later we determine that
// this package cannot be saved in the legacy format (because it uses a
// compiler plugin other than JS or CSS).
writeLegacyBuilds = true;
}
const writeLegacyBuilds = self._canWriteLegacyBuilds(options);

var isopackBuildInfoJson = null;
if (options.includeIsopackBuildInfo) {
Expand Down Expand Up @@ -1058,17 +1081,6 @@ _.extend(Isopack.prototype, {
});

var jsResourcesForLegacyPrelink = [];
if (writeLegacyBuilds) {
if (_.any(unibuild.resources, function (resource) {
return resource.type === "source" && resource.extension !== "js"
&& resource.extension !== "css";
})) {
// This package cannot be represented as an isopack-1 Isopack
// because it uses a file implemented by registerCompiler other than
// the very basic JS and CSS types.
writeLegacyBuilds = false;
}
}

// Save unibuild dependencies. Keyed by the json path rather than thinking
// too hard about how to encode pair (name, arch).
Expand Down Expand Up @@ -1174,6 +1186,8 @@ _.extend(Isopack.prototype, {
{ data: resource.data }),
length: resource.data.length,
offset: 0,
usesDefaultSourceProcessor:
resource.usesDefaultSourceProcessor || undefined,
servePath: resource.servePath || undefined,
path: resource.path || undefined,
hash: resource.hash || undefined,
Expand Down

0 comments on commit e8a32f4

Please sign in to comment.