forked from meteor/meteor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisopack.js
More file actions
1848 lines (1656 loc) · 70.2 KB
/
isopack.js
File metadata and controls
1848 lines (1656 loc) · 70.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var compiler = require('./compiler.js');
var archinfo = require('../utils/archinfo.js');
var _ = require('underscore');
var linker = require('./linker.js');
var buildmessage = require('../utils/buildmessage.js');
var Builder = require('./builder.js');
var bundler = require('./bundler.js');
var watch = require('../fs/watch.js');
var files = require('../fs/files.js');
var isopackets = require('../tool-env/isopackets.js');
var colonConverter = require('../utils/colon-converter.js');
var utils = require('../utils/utils.js');
var buildPluginModule = require('./build-plugin.js');
var Console = require('../console/console.js').Console;
var Profile = require('../tool-env/profile.js').Profile;
var rejectBadPath = function (p) {
if (p.match(/\.\./))
throw new Error("bad path: " + p);
};
///////////////////////////////////////////////////////////////////////////////
// Unibuild
///////////////////////////////////////////////////////////////////////////////
// Options:
// - kind [required] (main/plugin/app)
// - arch [required]
// - uses
// - implies
// - watchSet
// - nodeModulesPath
// - declaredExports
// - resources
var nextBuildId = 1;
var Unibuild = function (isopack, options) {
var self = this;
options = options || {};
self.pkg = isopack;
self.kind = options.kind;
self.arch = options.arch;
self.uses = options.uses;
self.implies = options.implies || [];
// This WatchSet will end up having the watch items from the
// SourceArch (such as package.js or .meteor/packages), plus all of
// the actual source files for the unibuild (including items that we
// looked at to find the source files, such as directories we
// scanned).
self.watchSet = options.watchSet || new watch.WatchSet();
// Each Unibuild is given a unique id when it's loaded (it is
// not saved to disk). This is just a convenience to make it easier
// to keep track of Unibuilds in a map; it's used by bundler
// and compiler. We put some human readable info in here too to make
// debugging easier.
self.id = self.pkg.name + "." + self.kind + "@" + self.arch + "#" +
(nextBuildId ++);
// 'declaredExports' are the variables which are exported from this package.
// A list of objects with keys 'name' (required) and 'testOnly' (boolean,
// defaults to false).
self.declaredExports = options.declaredExports;
// All of the data provided for eventual inclusion in the bundle,
// other than JavaScript that still needs to be fed through the
// final link stage. A list of objects with these keys:
//
// type: "source", "head", "body", "asset". (resources produced by
// legacy source handlers can also be "js" or "css".
//
// data: The contents of this resource, as a Buffer. For example,
// for "head", the data to insert in <head>; for "js", the
// JavaScript source code (which may be subject to further
// processing such as minification); for "asset", the contents of a
// static resource such as an image.
//
// servePath: The (absolute) path at which the resource would prefer
// to be served. Interpretation varies by type. For example, always
// honored for "asset", ignored for "head" and "body", sometimes
// honored for CSS but ignored if we are concatenating.
//
// sourceMap: Allowed only for "js". If present, a string.
//
// fileOptions: for "source", the options passed to `api.addFiles`.
// plugin-specific.
//
// extension: for "source", the file extension that this matched
// against at build time. null if matched against a specific filename.
self.resources = options.resources;
// Absolute path to the node_modules directory to use at runtime to
// resolve Npm.require() calls in this unibuild. null if this unibuild
// does not have a node_modules.
self.nodeModulesPath = options.nodeModulesPath;
};
///////////////////////////////////////////////////////////////////////////////
// Isopack
///////////////////////////////////////////////////////////////////////////////
// Meteor has a packaging system called "Isobuild". Isobuild knows how to
// compile the same JavaScript code-base to different architectures: browser,
// node.js-like server environment (could be Rhino or other) or a webview in a
// Cordova mobile app.
//
// Each package used by Isobuild forms an Isopack. Isopack is a package format
// containing source code for each architecture it can be ran on.
// Each separate part built for a separate architecture is called "Unibuild".
//
// There are multiple reasons why we can't call it just "build" and historically
// the name "Unibuild" has been associated with parts of Isopacks. We also can't
// call it "Isobuild" because this is the brand-name of the whole
// build/packaging system.
var Isopack = function () {
var self = this;
// These have the same meaning as in PackageSource.
self.name = null;
self.metadata = {};
self.version = null;
self.isTest = false;
self.debugOnly = false;
self.prodOnly = false;
// Unibuilds, an array of class Unibuild.
self.unibuilds = [];
// Plugins in this package. Map from plugin name to {arch -> JsImage}.
// Plugins are package-supplied classes and functions that can change the
// build process: introduce a new source processor (compiler, minifier,
// linter)
self.plugins = {};
self.cordovaDependencies = {};
// isobuild:* pseudo-packages which this package depends on.
self.isobuildFeatures = [];
// -- Information for up-to-date checks --
// Data in this section is only set if the Isopack was directly created by
// compiler.compile or read from a package compiled by IsopackCache (with its
// isopack-buildinfo.json file). They are not set for Isopacks read from
// the tropohouse.
// XXX this is likely to change once we have build versions
//
// A WatchSet for the full transitive dependencies for all plugins in this
// package, as well as this package's package.js. If any of these dependencies
// change, our plugins need to be rebuilt... but also, any package that
// directly uses this package needs to be rebuilt in case the change to
// plugins affected compilation.
self.pluginWatchSet = new watch.WatchSet();
// -- Loaded plugin state --
// True if plugins have been initialized (if ensurePluginsInitialized has
// been called)
self._pluginsInitialized = false;
// The SourceProcessors registered by plugins defined by this package. Each
// value is a SourceProcessorSet. sourceProcessors.compiler includes the
// legacy source handlers as well.
// Valid when self._pluginsInitialized is true.
self.sourceProcessors = {
compiler: null,
linter: null,
minifier: null
};
// See description in PackageSource. If this is set, then we include a copy of
// our own source, in addition to any other tools that were originally in the
// isopack.
self.includeTool = false;
// This is tools to copy from trees on disk. This is used by the
// isopack-merge code in tropohouse.
self.toolsOnDisk = [];
// A map of package dependencies that can provide a plugin for this isopack.
// In practice, it is every direct dependency and implied packages.
self.pluginProviderPackageMap = null;
// A directory on disk that plugins can use for caching. Should be created
// by the code that initializes the Isopack. If not provided, plugins don't
// get a disk cache.
self.pluginCacheDir = null;
// An in-memory only buildmessage.MessageSet object that is printed by the
// build tool when the app is linted. Is also printed when a package
// represented by Isopack is published.
self.lintingMessages = null;
};
Isopack.knownFormats = ["unipackage-pre2", "isopack-1", "isopack-2"];
// These functions are designed to convert isopack metadata between
// versions. They were designed to convert between unipackage-pre2 and
// isopack-1. The differences between these formats are essentially syntactical,
// not semantic, and occur entirely in the isopack.json file, not in the
// individual unibuild json files. These functions are written assuming those
// constraints, and were not actually useful in the isopack-1/isopack-2
// transition,where most of the changes are in the unibuild level, and there's
// actual semantic changes involved. So they are not actually used as much as
// they were before.
Isopack.convertOneStepForward = function (data, fromFormat) {
var convertedData = _.clone(data);
// XXX COMPAT WITH 0.9.3
if (fromFormat === "unipackage-pre2") {
convertedData.builds = convertedData.unibuilds;
delete convertedData.unibuilds;
return convertedData;
}
if (fromFormat === "isopack-1") {
// For now, there's no difference in this direction at the isopack level.
return convertedData;
}
};
Isopack.convertOneStepBackward = function (data, fromFormat) {
var convertedData = _.clone(data);
if (fromFormat === "isopack-1") {
convertedData.unibuilds = convertedData.builds;
convertedData.format = "unipackage-pre2";
delete convertedData.builds;
return convertedData;
}
if (fromFormat === "isopack-2") {
// The conversion from isopack-2 requires converting the nested
// unibuild data as well. This shouldn't happen.
throw Error("Can't automatically convert backwards from isopack-2");
}
};
Isopack.convertIsopackFormat = Profile(
"Isopack.convertIsopackFormat", function (data, fromFormat, toFormat) {
var fromPos = _.indexOf(Isopack.knownFormats, fromFormat);
var toPos = _.indexOf(Isopack.knownFormats, toFormat);
var step = fromPos < toPos ? 1 : -1;
if (fromPos === -1)
throw new Error("Can't convert from unknown Isopack format: " + fromFormat);
if (toPos === -1)
throw new Error("Can't convert to unknown Isopack format: " + toFormat);
while (fromPos !== toPos) {
if (step > 0) {
data = Isopack.convertOneStepForward(data, fromFormat);
} else {
data = Isopack.convertOneStepBackward(data, fromFormat);
}
fromPos += step;
fromFormat = Isopack.knownFormats[fromPos];
}
return data;
});
// Read the correct file from isopackDirectory and convert to current format
// of the isopack metadata. Returns null if there is no package here.
Isopack.readMetadataFromDirectory =
Profile("Isopack.readMetadataFromDirectory", function (isopackDirectory) {
var metadata = null;
let originalVersion = null;
// deal with different versions of "isopack.json", backwards compatible
var isopackJsonPath = files.pathJoin(isopackDirectory, "isopack.json");
var unipackageJsonPath = files.pathJoin(isopackDirectory, "unipackage.json");
if (files.exists(isopackJsonPath)) {
var isopackJson = JSON.parse(files.readFile(isopackJsonPath));
if (isopackJson['isopack-2']) {
metadata = isopackJson['isopack-2'];
originalVersion = 'isopack-2';
} else if (isopackJson['isopack-1']) {
metadata = Isopack.convertIsopackFormat(
isopackJson['isopack-1'], 'isopack-1', 'isopack-2');
originalVersion = 'isopack-1';
} else {
// This file is from the future and no longer supports this version
throw new Error("Could not find isopack data supported any supported format (isopack-1 or isopack-2).\n" +
"This isopack was likely built with a much newer version of Meteor.");
}
} else if (files.exists(unipackageJsonPath)) {
// super old version with different file name
// XXX COMPAT WITH 0.9.3
if (files.exists(unipackageJsonPath)) {
metadata = JSON.parse(files.readFile(unipackageJsonPath));
metadata = Isopack.convertIsopackFormat(metadata,
"unipackage-pre2", "isopack-2");
originalVersion = 'unipackage-pre2';
}
if (metadata.format !== "unipackage-pre2") {
// We don't support pre-0.9.0 isopacks, but we do know enough to delete
// them if we find them in an isopack cache somehow (rather than crash).
if (metadata.format === "unipackage-pre1") {
throw new exports.OldIsopackFormatError();
}
throw new Error("Unsupported isopack format: " +
JSON.stringify(metadata.format));
}
}
return {metadata, originalVersion};
});
_.extend(Isopack.prototype, {
// Make a dummy (empty) package that contains nothing of interest.
// XXX used?
initEmpty: function (name) {
var self = this;
self.name = name;
},
// This is primarily intended to be used by the compiler. After
// calling this, call addUnibuild to add the unibuilds.
initFromOptions: function (options) {
var self = this;
self.name = options.name;
self.metadata = options.metadata;
self.version = options.version;
self.isTest = options.isTest;
self.plugins = options.plugins;
self.cordovaDependencies = options.cordovaDependencies;
self.pluginWatchSet = options.pluginWatchSet;
self.npmDiscards = options.npmDiscards;
self.includeTool = options.includeTool;
self.debugOnly = options.debugOnly;
self.prodOnly = options.prodOnly;
self.pluginCacheDir = options.pluginCacheDir || null;
self.isobuildFeatures = options.isobuildFeatures;
},
// Programmatically add a unibuild to this Isopack. Should only be
// called as part of building up a new Isopack using
// initFromOptions. 'options' are the options to the Unibuild
// constructor.
addUnibuild: function (options) {
var self = this;
self.unibuilds.push(new Unibuild(self, options));
},
setPluginProviderPackageMap: function (pluginProviderPackageMap) {
var self = this;
self.pluginProviderPackageMap = pluginProviderPackageMap;
},
getSourceFilesUnderSourceRoot: Profile(
"Isopack#getSourceFilesUnderSourceRoot", function (sourceRoot) {
var self = this;
var sourceFiles = {};
var anySourceFiles = false;
var addSourceFilesFromWatchSet = function (watchSet) {
_.each(watchSet.files, function (hash, filename) {
anySourceFiles = true;
var relativePath = files.pathRelative(sourceRoot, filename);
// We only want files that are actually under sourceRoot.
if (relativePath.substr(0, 3) === '..' + files.pathSep)
return;
sourceFiles[relativePath] = true;
});
};
addSourceFilesFromWatchSet(self.pluginWatchSet);
_.each(self.unibuilds, function (u) {
addSourceFilesFromWatchSet(u.watchSet);
});
// Were we actually built from source or loaded from an IsopackCache? If so
// then there should be at least one source file in some WatchSet. If not,
// return null.
if (! anySourceFiles)
return null;
return _.keys(sourceFiles);
}),
// An sorted array of all the architectures included in this package.
architectures: Profile("Isopack#architectures", function () {
var self = this;
var archSet = {};
_.each(self.unibuilds, function (unibuild) {
archSet[unibuild.arch] = true;
});
_.each(self._toolArchitectures(), function (arch) {
archSet[arch] = true;
});
_.each(self.plugins, function (plugin, name) {
_.each(plugin, function (plug, arch) {
archSet[arch] = true;
});
});
var arches = _.keys(archSet).sort();
// Ensure that our buildArchitectures string does not look like
// web+os+os.osx.x86_64
// This would happen if there is an 'os' unibuild but a platform-specific
// tool (eg, in meteor-tool). This would confuse catalog.getBuildsForArches
// into thinking that it would work for Linux, since the 'os' means
// 'works on any Node server'.
if (_.any(arches, function (a) { return a.match(/^os\./); })) {
arches = _.without(arches, 'os');
}
return arches;
}),
// A sorted plus-separated string of all the architectures included in this
// package.
buildArchitectures: function () {
var self = this;
return self.architectures().join('+');
},
// Returns true if we think that this isopack is platform specific (contains
// binary builds)
platformSpecific: function () {
var self = this;
return _.any(self.architectures(), function (arch) {
return arch.match(/^os\./);
});
},
tarballName: function () {
var self = this;
return colonConverter.convert(self.name) + '-' + self.version;
},
_toolArchitectures: function () {
var self = this;
var toolArches = _.pluck(self.toolsOnDisk, 'arch');
self.includeTool && toolArches.push(archinfo.host());
return _.uniq(toolArches).sort();
},
// Return the unibuild of the package to use for a given target architecture
// (eg, 'os.linux.x86_64' or 'web'), or throw an exception if that
// packages can't be loaded under these circumstances.
getUnibuildAtArch: Profile("Isopack#getUnibuildAtArch", function (
arch, {allowWrongPlatform} = {}) {
var self = this;
let chosenArch = archinfo.mostSpecificMatch(
arch, _.pluck(self.unibuilds, 'arch'));
if (! chosenArch && allowWrongPlatform && arch.match(/^os\./)) {
// Special-case: we're looking for a specific server platform and it's
// not available. (eg, we're deploying from a Mac to Linux and are
// processing a local package with binary npm deps). If we have "allow
// wrong platform" turned on, search again for the host version, which
// might find the Mac version. We'll detect this case later and provide
// package.json instead of Mac binaries.
chosenArch =
archinfo.mostSpecificMatch(archinfo.host(), _.pluck(self.unibuilds, 'arch'));
}
if (! chosenArch) {
buildmessage.error(
(self.name || "this app") +
" is not compatible with architecture '" + arch + "'",
{ secondary: true });
// recover by returning by no unibuilds
return null;
}
return _.findWhere(self.unibuilds, { arch: chosenArch });
}),
_checkPluginsInitialized: function () {
var self = this;
if (self._pluginsInitialized)
return;
throw Error("plugins not yet initialized?");
},
// If this package has plugins, initialize them (run the startup
// code in them so that they register their extensions). Idempotent.
ensurePluginsInitialized: Profile("Isopack#ensurePluginsInitialized", function () {
var self = this;
buildmessage.assertInJob();
if (self._pluginsInitialized)
return;
self.sourceProcessors.compiler = new buildPluginModule.SourceProcessorSet(
self.displayName(), { hardcodeJs: true, singlePackage: true });
self.sourceProcessors.linter = new buildPluginModule.SourceProcessorSet(
self.displayName(), { singlePackage: true, allowConflicts: true });
self.sourceProcessors.minifier = new buildPluginModule.SourceProcessorSet(
self.displayName(), { singlePackage: true });
_.each(self.plugins, function (pluginsByArch, name) {
var arch = archinfo.mostSpecificMatch(
archinfo.host(), _.keys(pluginsByArch));
if (! arch) {
buildmessage.error("package `" + name + "` is built for incompatible " +
"architecture");
// Recover by ignoring plugin
// XXX does this recovery work?
return;
}
var plugin = pluginsByArch[arch];
buildmessage.enterJob({
title: "loading plugin `" + name +
"` from package `" + self.name + "`"
// don't necessarily have rootPath anymore
// (XXX we do, if the isopack was locally built, which is
// the important case for debugging. it'd be nice to get this
// case right.)
}, function () {
// Make a new Plugin API object for this plugin.
var Plugin = self._makePluginApi();
plugin.load({ Plugin: Plugin });
});
});
// Instantiate each of the registered batch plugins. Note that we don't
// do this directly in the registerCompiler (etc) call, because we want
// to allow people to do something like:
// Plugin.registerCompiler({...}, function () { return new C; });
// var C = function () {...}
// and so we want to wait for C to be defined.
_.each(self.sourceProcessors, (sourceProcessorSet) => {
_.each(sourceProcessorSet.allSourceProcessors, (sourceProcessor) => {
sourceProcessor.instantiatePlugin();
});
});
self._pluginsInitialized = true;
}),
_makePluginApi: function () {
var isopack = this;
/**
* @global
* @namespace Plugin
* @summary The namespace that is exposed inside build plugin files.
*/
var Plugin = {
// 'extension' is a file extension without the separation dot
// (eg 'js', 'coffee', 'coffee.md')
//
// 'options' can be omitted. The only known option is 'isTemplate', which
// is a bit of a hack meaning "in an app, these files should be loaded
// before non-templates".
//
// 'handler' is a function that takes a single argument, a
// CompileStep (#CompileStep)
/**
* @summary Inside a build plugin source file specified in
* [Package.registerBuildPlugin](#Package-registerBuildPlugin),
* add a handler to compile files with a certain file extension.
* @param {String} fileExtension The file extension that this plugin
* should handle, without the first dot.
* Examples: `"coffee"`, `"coffee.md"`.
* @param {Function} handler A function that takes one argument,
* a CompileStep object.
*
* Documentation for CompileStep is available [on the GitHub Wiki](https://github.com/meteor/meteor/wiki/CompileStep-API-for-Build-Plugin-Source-Handlers).
* @memberOf Plugin
* @locus Build Plugin
* @deprecated since 1.2
* XXX COMPAT WITH 1.1
*/
registerSourceHandler: function (extension, options, handler) {
if (!handler) {
handler = options;
options = {};
}
// The popular package mquandalle:bower has a call to
// `registerSourceHandler('json', null)` for some reason, to
// work around some behavior of Meteor believed to be a bug. We
// think that new features of registerCompiler like being able
// to register for filenames will allow them to drop that line,
// but in the meantime we might as well not choke on it. (The
// old implementation coincidentally didn't choke.)
if (!handler) {
handler = function () {};
}
isopack.sourceProcessors.compiler.addLegacyHandler({
extension,
handler,
packageDisplayName: isopack.displayName(),
isTemplate: !!options.isTemplate,
archMatching: options.archMatching
});
},
_registerSourceProcessor: function (
{extensions, filenames, archMatching, isTemplate},
factory,
{sourceProcessorSet, methodName, featurePackage}) {
if (!isopack.featureEnabled(featurePackage)) {
// This error is OK because we only define 1.0.0 for each of these
// feature packages (in compiler.KNOWN_ISOBUILD_FEATURE_PACKAGES).
buildmessage.error(
`your package must \`api.use('${ featurePackage }@1.0.0')\` in ` +
`order for its plugins to call Plugin.${ methodName }`);
return;
}
const hasExtensions = (extensions &&
extensions instanceof Array &&
extensions.length > 0);
const hasFilenames = (filenames &&
filenames instanceof Array &&
filenames.length > 0);
if (! (hasExtensions || hasFilenames)) {
buildmessage.error("Plugin." + methodName + " must specify a " +
"non-empty array of extensions or filenames",
{ useMyCaller: 3 });
// recover by ignoring
return;
}
// Don't let extensions or filenames try to look for directories (in the
// way that WatchSet expresses them).
if (extensions && extensions.some(e => e.endsWith('/'))) {
buildmessage.error(
`Plugin.${methodName}: extensions may not end in /`);
// recover by ignoring
return;
}
if (filenames && filenames.some(f => f.endsWith('/'))) {
buildmessage.error(
`Plugin.${methodName}: filenames may not end in /`);
// recover by ignoring
return;
}
if (typeof factory !== 'function') {
buildmessage.error(methodName + " call must "
+ "specify a factory function",
{ useMyCaller: 3 });
// recover by ignoring
return;
}
const sp = new buildPluginModule.SourceProcessor({
isopack: isopack,
extensions: extensions,
filenames: filenames,
archMatching: archMatching,
isTemplate: isTemplate,
factoryFunction: factory,
methodName: methodName
});
// This logs a buildmessage on conflicts.
sourceProcessorSet.addSourceProcessor(sp);
},
// Compilers are part of the Batch Plugins API.
//
// A compiler plugin is provided by packages to participate in
// the build process. A compiler can register file extensions and
// filenames it handles and the build tool will call the compiler's
// `processFilesForTarget` method once for each target (eg, the server
// or client program) with all of the files in the target.
//
// Compilers are run on application bundling (in bundle.js).
// This is different from the legacy registerSourceHandler API,
// which runs on a single file at a time when a *package* is built.
// Published Isopack packages contain the original sources of
// files handled by registerCompiler, not the generated output,
// so compilers can be involved in the very end, when the app is bundled
// (not in package publish time). (Note that this requires a new
// Isopack format, 'isopack-2'; versions of packages published with new
// compilers cannot be used with previous releases of Meteor, but
// Version Solver knows this and will select an older compatible
// version if possible.
//
// Unlike the legacy API called "source handlers" (deprecated in
// Meteor 1.2), compiler plugins can handle all files for the target,
// making independent decisions about caching and dependencies resolution.
//
// The factory function must return an instance of a compiler.
//
// Note: It's important to ensure that all plugins that want to call
// plugin compiler use the isobuild:compiler-plugin fake package, so that
// Version Solver will not let you use registerCompiler plugins with old
// versions of the tool.
/**
* @summary Inside a build plugin source file specified in
* [Package.registerBuildPlugin](#Package-registerBuildPlugin),
* add a compiler that will handle files with certain extensions or
* filenames.
* @param {Object} options
* @param {String[]} options.extensions The file extensions that this
* plugin should handle, without the first dot.
* Examples: `["coffee", "coffee.md"]`.
* @param {String[]} options.filenames The list of filenames
* that this plugin should handle. Examples: `["config.json"]`.
* @param {Function} factory A function that returns an instance
* of a compiler class.
*
* More detailed documentation for build plugins is available [on the GitHub Wiki](https://github.com/meteor/meteor/wiki/Build-Plugins-API).
* @memberOf Plugin
* @locus Build Plugin
*/
registerCompiler: function (options, factory) {
Plugin._registerSourceProcessor(options || {}, factory, {
sourceProcessorSet: isopack.sourceProcessors.compiler,
methodName: "registerCompiler",
featurePackage: "isobuild:compiler-plugin"
});
},
// Linters are part of the Batch Plugin API.
//
// A linter plugin provides a Linter instance. The linter is
// given a batch of source files for the target according to
// linter's declared file extensions and filenames (e.g.: '*.js',
// '.jshintrc').
//
// Linters don't output any files. They can only raise an error
// message on one of the source files to force the build tool to
// print a linting message.
//
// The factory function must return an instance of the linter.
// The linter must have the `processFilesForPackage` method that
// has two arguments:
// - inputFiles - LinterFile - sources instances
// - options - Object
// - globals - a list of strings - global variables that can be
// used in the target's scope as they are dependencies of the
// package or the app. e.g.: "Minimongo" or "Webapp".
//
// Unlike compilers and minifiers, linters run on one package
// at a time. Linters are run by `meteor run`, `meteor publish`,
// and `meteor lint`.
/**
* @summary Inside a build plugin source file specified in
* [Package.registerBuildPlugin](#Package-registerBuildPlugin),
* add a linter that will handle files with certain extensions or
* filenames.
* @param {Object} options
* @param {String[]} options.extensions The file extensions that this
* plugin should handle, without the first dot.
* Examples: `["js", "es6", "jsx"]`.
* @param {Function} factory A function that returns an instance
* of a linter class.
*
* More detailed documentation for build plugins is available [on the GitHub Wiki](https://github.com/meteor/meteor/wiki/Build-Plugins-API).
* @memberOf Plugin
* @locus Build Plugin
*/
registerLinter: function (options, factory) {
Plugin._registerSourceProcessor(options || {}, factory, {
sourceProcessorSet: isopack.sourceProcessors.linter,
methodName: "registerLinter",
featurePackage: "isobuild:linter-plugin"
});
},
// Minifiers are part of the Batch Plugin API.
//
// The minifiers are applied in the very end of the bundling
// process, after the linters and compilers. Unlike linters and
// compilers, minifiers are given the output of compilers and not
// the source files the application developer supplied.
//
// The minifier plugins can fill into 2 types of minifiers: CSS or JS.
// When the minifier is added to an app, it is used during "bundling" to
// compress the app code and each package's code separately.
// Only minifier packages directly used by an app (or implied by a package
// directly used by the app) are active: using a minifer's package in
// another package does nothing.
//
// So far, the minifiers are only run on client targets such as
// web.browser and web.cordova.
//
// The factory function must return an instance of a
// minifier. The method `processFilesForBundle` is passed a list of
// files, possibly a linked file per target (for JavaScript files).
//
// - files - processed files to minify
// - options - Object
// - minifyMode - string - 'development' or 'production', based
// on the bundling mode
/**
* @summary Inside a build plugin source file specified in
* [Package.registerBuildPlugin](#Package-registerBuildPlugin),
* add a linter that will handle files with certain extensions or
* filenames.
* @param {Object} options
* @param {String[]} options.extensions The file extensions that this
* plugin should handle, without the first dot. Can only be "js" or "css".
* Examples: `["js", "css"]`.
* @param {String[]} options.filenames The list of filenames
* that this plugin should handle. Examples: `["config.json"]`.
* @param {Function} factory A function that returns an instance
* of a minifier class.
*
* More detailed documentation for build plugins is available [on the GitHub Wiki](https://github.com/meteor/meteor/wiki/Build-Plugins-API).
* @memberOf Plugin
* @locus Build Plugin
*/
registerMinifier: function (options, factory) {
var badUsedExtension = _.find(options.extensions, function (ext) {
return ! _.contains(['js', 'css'], ext);
});
if (badUsedExtension !== undefined) {
buildmessage.error(badUsedExtension + ': Minifiers are only allowed to register "css" or "js" extensions.');
return;
}
if (options.filenames) {
buildmessage.error("Plugin.registerMinifier does not accept `filenames`");
return;
}
Plugin._registerSourceProcessor(options || {}, factory, {
sourceProcessorSet: isopack.sourceProcessors.minifier,
methodName: "registerMinifier",
featurePackage: "isobuild:minifier-plugin"
});
},
nudge: function () {
Console.nudge(true);
},
convertToOSPath: files.convertToOSPath,
convertToStandardPath: files.convertToStandardPath,
path: {
join: files.pathJoin,
normalize: files.pathNormalize,
relative: files.pathRelative,
resolve: files.pathResolve,
dirname: files.pathDirname,
basename: files.pathBasename,
extname: files.pathExtname,
sep: files.pathSep
},
fs: files.fsFixPath
};
return Plugin;
},
// Load a Isopack on disk.
//
// options:
// - isopackBuildInfoJson: parsed isopack-buildinfo.json object,
// if loading from an IsopackCache.
initFromPath: Profile(
"Isopack#initFromPath", function (name, dir, options) {
var self = this;
options = _.clone(options || {});
options.firstIsopack = true;
if (options.pluginCacheDir) {
self.pluginCacheDir = options.pluginCacheDir;
}
return self._loadUnibuildsFromPath(name, dir, options);
}),
_loadUnibuildsFromPath: function (name, dir, options) {
var self = this;
options = options || {};
// In the tropohouse, isopack paths are symlinks (which can be updated if
// more unibuilds are merged in). For any given call to
// _loadUnibuildsFromPath, let's ensure we see a consistent isopack by
// realpath'ing dir.
dir = files.realpath(dir);
var {metadata: mainJson} = Isopack.readMetadataFromDirectory(dir);
if (! mainJson) {
throw new Error("No metadata files found for isopack at: " + dir);
}
// isopacks didn't used to know their name, but they should.
if (_.has(mainJson, 'name') && name !== mainJson.name) {
throw new Error("isopack " + name + " thinks its name is " +
mainJson.name);
}
// If we're loading from an IsopackCache, we need to load the WatchSets
// which will be used by the bundler. (builtBy is only used by
// IsopackCache._checkUpToDate. pluginProviderPackageMap will actually be
// set by IsopackCache afterwards, because it has access to an appropriate
// PackageMap which can be subset to create a new PackageMap object.)
var unibuildWatchSets = {};
if (options.isopackBuildInfoJson) {
if (! options.firstIsopack)
throw Error("can't merge isopacks with buildinfo");
// XXX should comprehensively sanitize (eg, typecheck) everything
// read from json files
// Read the watch sets for each unibuild
_.each(
options.isopackBuildInfoJson.unibuildDependencies,
function (watchSetJSON, unibuildTag) {
unibuildWatchSets[unibuildTag] =
watch.WatchSet.fromJSON(watchSetJSON);
});
// Read pluginWatchSet. (In the multi-sub-isopack case, these are
// guaranteed to be trivial (since we check that there's no
// isopackBuildInfoJson), so no need to merge.)
self.pluginWatchSet = watch.WatchSet.fromJSON(
options.isopackBuildInfoJson.pluginDependencies);
}
// If we are loading multiple isopacks, only take this stuff from the
// first one.
if (options.firstIsopack) {
self.name = name;
self.metadata = {
summary: mainJson.summary
};
self.version = mainJson.version;
self.isTest = mainJson.isTest;
self.debugOnly = !!mainJson.debugOnly;
self.prodOnly = !!mainJson.prodOnly;
}
_.each(mainJson.plugins, function (pluginMeta) {
rejectBadPath(pluginMeta.path);
var plugin = bundler.readJsImage(files.pathJoin(dir, pluginMeta.path));
if (!_.has(self.plugins, pluginMeta.name)) {
self.plugins[pluginMeta.name] = {};
}
// If we already loaded a plugin of this name/arch, just ignore this one.
if (!_.has(self.plugins[pluginMeta.name], plugin.arch)) {
self.plugins[pluginMeta.name][plugin.arch] = plugin;
}
});
self.pluginsBuilt = true;
_.each(mainJson.builds, function (unibuildMeta) {
// aggressively sanitize path (don't let it escape to parent
// directory)
rejectBadPath(unibuildMeta.path);
// Skip unibuilds we already have.
var alreadyHaveUnibuild = _.find(self.unibuilds, function (unibuild) {
return unibuild.arch === unibuildMeta.arch;
});
if (alreadyHaveUnibuild)
return;
var unibuildJson = JSON.parse(
files.readFile(files.pathJoin(dir, unibuildMeta.path)));
var unibuildBasePath =
files.pathDirname(files.pathJoin(dir, unibuildMeta.path));
if (unibuildJson.format !== "unipackage-unibuild-pre1" &&
unibuildJson.format !== "isopack-2-unibuild") {
throw new Error("Unsupported isopack unibuild format: " +
JSON.stringify(unibuildJson.format));
}
// Is this unibuild the legacy pre-"compiler plugin" format which contains
// "prelink" resources of pre-processed JS files (as well as the
// "packageVariables" field) instead of individual "source" resources (and
// a "declaredExports" field)?
var unibuildHasPrelink =
unibuildJson.format === "unipackage-unibuild-pre1";
var nodeModulesPath = null;
if (unibuildJson.node_modules) {
rejectBadPath(unibuildJson.node_modules);
nodeModulesPath =
files.pathJoin(unibuildBasePath, unibuildJson.node_modules);
}
var resources = [];
_.each(unibuildJson.resources, function (resource) {
rejectBadPath(resource.file);
var data = files.readBufferWithLengthAndOffset(
files.pathJoin(unibuildBasePath, resource.file),
resource.length, resource.offset);
if (resource.type === "prelink") {
if (! unibuildHasPrelink) {
throw Error("Unexpected prelink resource in " +
unibuildJson.format + " at " + dir);
}
// We found a "prelink" resource, because we're processing a package
// published with an older version of Meteor which did not create
// isopack-2 isopacks and which always preprocessed and linked all JS
// files instead of leaving that until bundle time. Let's pretend it
// was just a single js source file, but leave a "legacyPrelink" field
// on it so we can not re-link that part (and not re-analyze for
// assigned variables).
var prelinkResource = {