UNPKG

5.99 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var path_1 = require("path");
4var Constants = require("../util/constants");
5var helpers_1 = require("../util/helpers");
6var logger_1 = require("../logger/logger");
7var hybrid_file_system_factory_1 = require("../util/hybrid-file-system-factory");
8var watch_memory_system_1 = require("./watch-memory-system");
9var ContextElementDependency = require('webpack/lib/dependencies/ContextElementDependency');
10var IonicEnvironmentPlugin = (function () {
11 function IonicEnvironmentPlugin(context, writeToDisk) {
12 this.context = context;
13 this.writeToDisk = writeToDisk;
14 }
15 IonicEnvironmentPlugin.prototype.apply = function (compiler) {
16 var _this = this;
17 compiler.plugin('context-module-factory', function (contextModuleFactory) {
18 contextModuleFactory.plugin('after-resolve', function (result, callback) {
19 if (!result) {
20 return callback();
21 }
22 var deepLinkConfig = helpers_1.getParsedDeepLinkConfig();
23 var webpackDeepLinkModuleDictionary = convertDeepLinkConfigToWebpackFormat(deepLinkConfig);
24 var ionicAngularDir = helpers_1.getStringPropertyValue(Constants.ENV_VAR_IONIC_ANGULAR_DIR);
25 var ngModuleLoaderDirectory = path_1.join(ionicAngularDir, 'util');
26 if (!result.resource.endsWith(ngModuleLoaderDirectory)) {
27 return callback(null, result);
28 }
29 result.resource = _this.context.srcDir;
30 result.recursive = true;
31 result.dependencies.forEach(function (dependency) { return dependency.critical = false; });
32 result.resolveDependencies = function (p1, p2, p3, p4, cb) {
33 var dependencies = Object.keys(webpackDeepLinkModuleDictionary)
34 .map(function (key) {
35 var value = webpackDeepLinkModuleDictionary[key];
36 if (value) {
37 return new ContextElementDependency(value, key);
38 }
39 return null;
40 }).filter(function (dependency) { return !!dependency; });
41 cb(null, dependencies);
42 };
43 return callback(null, result);
44 });
45 });
46 compiler.plugin('environment', function (otherCompiler, callback) {
47 logger_1.Logger.debug('[IonicEnvironmentPlugin] apply: creating environment plugin');
48 var hybridFileSystem = hybrid_file_system_factory_1.getInstance(_this.writeToDisk);
49 hybridFileSystem.setInputFileSystem(compiler.inputFileSystem);
50 hybridFileSystem.setOutputFileSystem(compiler.outputFileSystem);
51 compiler.inputFileSystem = hybridFileSystem;
52 compiler.outputFileSystem = hybridFileSystem;
53 compiler.watchFileSystem = new watch_memory_system_1.WatchMemorySystem(_this.context.fileCache, _this.context.srcDir);
54 // do a bunch of webpack specific stuff here, so cast to an any
55 // populate the content of the file system with any virtual files
56 // inspired by populateWebpackResolver method in Angular's webpack plugin
57 var webpackFileSystem = hybridFileSystem;
58 var fileStatsDictionary = hybridFileSystem.getAllFileStats();
59 var dirStatsDictionary = hybridFileSystem.getAllDirStats();
60 _this.initializeWebpackFileSystemCaches(webpackFileSystem);
61 for (var _i = 0, _a = Object.keys(fileStatsDictionary); _i < _a.length; _i++) {
62 var filePath = _a[_i];
63 var stats = fileStatsDictionary[filePath];
64 webpackFileSystem._statStorage.data[filePath] = [null, stats];
65 webpackFileSystem._readFileStorage.data[filePath] = [null, stats.content];
66 }
67 for (var _b = 0, _c = Object.keys(dirStatsDictionary); _b < _c.length; _b++) {
68 var dirPath = _c[_b];
69 var stats = dirStatsDictionary[dirPath];
70 var fileNames = hybridFileSystem.getFileNamesInDirectory(dirPath);
71 var dirNames = hybridFileSystem.getSubDirs(dirPath);
72 webpackFileSystem._statStorage.data[dirPath] = [null, stats];
73 webpackFileSystem._readdirStorage.data[dirPath] = [null, fileNames.concat(dirNames)];
74 }
75 });
76 };
77 IonicEnvironmentPlugin.prototype.initializeWebpackFileSystemCaches = function (webpackFileSystem) {
78 if (!webpackFileSystem._statStorage) {
79 webpackFileSystem._statStorage = {};
80 }
81 if (!webpackFileSystem._statStorage.data) {
82 webpackFileSystem._statStorage.data = [];
83 }
84 if (!webpackFileSystem._readFileStorage) {
85 webpackFileSystem._readFileStorage = {};
86 }
87 if (!webpackFileSystem._readFileStorage.data) {
88 webpackFileSystem._readFileStorage.data = [];
89 }
90 if (!webpackFileSystem._readdirStorage) {
91 webpackFileSystem._readdirStorage = {};
92 }
93 if (!webpackFileSystem._readdirStorage.data) {
94 webpackFileSystem._readdirStorage.data = [];
95 }
96 };
97 return IonicEnvironmentPlugin;
98}());
99exports.IonicEnvironmentPlugin = IonicEnvironmentPlugin;
100function convertDeepLinkConfigToWebpackFormat(parsedDeepLinkConfigs) {
101 var dictionary = {};
102 if (!parsedDeepLinkConfigs) {
103 parsedDeepLinkConfigs = new Map();
104 }
105 parsedDeepLinkConfigs.forEach(function (parsedDeepLinkConfig) {
106 if (parsedDeepLinkConfig.userlandModulePath && parsedDeepLinkConfig.absolutePath) {
107 dictionary[parsedDeepLinkConfig.userlandModulePath] = parsedDeepLinkConfig.absolutePath;
108 }
109 });
110 return dictionary;
111}
112exports.convertDeepLinkConfigToWebpackFormat = convertDeepLinkConfigToWebpackFormat;