forked from jedwards1211/meteor-imports-webpack-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
176 lines (147 loc) · 5.76 KB
/
index.js
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
/* eslint-env node */
var path = require('path');
function escapeForRegEx(str) {
return str.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&');
}
function strToRegex(str) {
return new RegExp(escapeForRegEx(str));
}
var PATH_SEP_REGEX = '[/\\\\]';
// Join an array with environment agnostic path identifiers
function arrToPathForRegEx(arr) {
return arr.map(function(x) {
return escapeForRegEx(x)
}).join(PATH_SEP_REGEX);
}
var BUILD_PATH_PARTS = ['.meteor', 'local', 'build', 'programs', 'web.browser'];
var PACKAGES_PATH_PARTS = BUILD_PATH_PARTS.concat(['packages']);
var PACKAGES_REGEX_NOT_MODULES = new RegExp(
arrToPathForRegEx(PACKAGES_PATH_PARTS) +
PATH_SEP_REGEX +
'(?!modules\.js)[^/\\\\]+$'
);
function MeteorImportsPlugin(config) {
config.exclude = [
'autoupdate',
'ecmascript',
'hot-code-push',
'livedata',
].concat(config.exclude || []);
this.config = config;
}
MeteorImportsPlugin.prototype.apply = function(compiler) {
var self = this;
compiler.plugin("compile", function(params) {
// clear loaders from previous compile
for (var i = compiler.options.module.loaders.length - 1; i--;) {
if (compiler.options.module.loaders[i].meteorImports) {
compiler.options.module.loaders.splice(i, 1);
}
}
// Create path for internal build of the meteor packages.
var meteorBuild = self.config.meteorProgramsFolder
? path.resolve(params.normalModuleFactory.context, self.config.meteorProgramsFolder, 'web.browser')
: path.resolve.apply(path, [
params.normalModuleFactory.context,
self.config.meteorFolder
].concat(BUILD_PATH_PARTS));
// Create path for plugin node modules directory.
var meteorNodeModules = path.join(__dirname, 'node_modules');
// Create path for meteor app packages directory.
var meteorPackages = path.join(meteorBuild, 'packages');
// Check if module loaders is defined.
if (compiler.options.module.loaders === undefined)
throw Error('Add an empty array in module.loaders of your webpack config.');
// Check if Meteor has been run at least once.
try {
var manifest = require(meteorBuild + '/program.json').manifest;
} catch (e) {
throw Error('Run Meteor at least once and wait for startup to complete.')
}
// Create an alias so we can do the context properly using the folder
// variable from the meteor config file. If we inject the folder variable
// directly in the request.context webpack goes wild.
compiler.options.resolve.alias['meteor-build'] = meteorBuild;
compiler.options.resolve.alias['meteor-packages'] = meteorPackages;
// Create an alias for the meteor-imports require.
compiler.options.resolve.alias['meteor-imports'] = path.join(
__dirname, './meteor-imports.js');
// Add a loader to inject the meteor config in the meteor-imports require.
compiler.options.module.loaders.push({
meteorImports: true,
test: /meteor-config/,
loader: 'json-string-loader?json=' + JSON.stringify(self.config)
});
var meteorPkgsRel = PACKAGES_PATH_PARTS.join('/');
// Add a loader to inject this as window in the meteor packages.
compiler.options.module.loaders.push({
meteorImports: true,
test: new RegExp(escapeForRegEx(meteorPkgsRel) + '.*\\.js'),
loader: 'imports?this=>window'
});
compiler.options.module.loaders.push({
meteorImports: true,
test: strToRegex(path.join(meteorPkgsRel, '/modules.js')),
loader: path.join(__dirname, 'modules-loader.js')
});
compiler.options.module.loaders.push({
meteorImports: true,
test: strToRegex(meteorPkgsRel + '/global-imports.js'),
loader: path.join(__dirname, 'global-imports-loader.js'),
query: self.config
});
// Add a resolveLoader to use the loaders from this plugin's own NPM
// dependencies.
if (compiler.options.resolveLoader.modulesDirectories.indexOf(meteorNodeModules) < 0) {
compiler.options.resolveLoader.modulesDirectories.push(meteorNodeModules);
}
// Create an alias for each Meteor packages and a loader to extract its
// globals.
var excluded = new RegExp(self.config.exclude
.map(function(exclude){ return '^packages/' + exclude + '\.js$'; })
.concat('^app\/.+.js$')
.join('|'));
manifest.forEach(function(pckge){
if (excluded.test(pckge.path)) {
return;
}
var location = /^packages\/(.+)\.js$/.exec(pckge.path);
if (!location) {
return;
}
var packageName = location[1];
packageName = packageName.replace('_', ':');
compiler.options.resolve.alias['meteor/' + packageName] =
meteorBuild + '/' + pckge.path;
compiler.options.module.loaders.push({
meteorImports: true,
test: new RegExp('.meteor/local/build/programs/web.browser/' + pckge.path),
loader: 'exports?Package["' + packageName + '"]'
})
});
});
// Don't create modules and chunks for excluded packages.
compiler.plugin("normal-module-factory", function(nmf) {
var excluded = new RegExp(self.config.exclude
.map(function(exclude) { return '^\./' + exclude + '\.js$' })
.join('|'));
nmf.plugin("before-resolve", function(result, callback) {
if (!result) return callback();
if (excluded.test(result.request)) {
return callback();
}
return callback(null, result);
});
nmf.plugin("after-resolve", function(result, callback) {
// We want to parse modules.js, but that's the only one as the rest relies on internal Meteor require system
if (result && result.request.match(PACKAGES_REGEX_NOT_MODULES)) {
result.parser = {
parse: function() {
}
};
}
return callback(null, result);
});
});
};
module.exports = MeteorImportsPlugin;