-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
151 lines (134 loc) · 5.07 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
var path = require('path');
var webpack = require('webpack');
var RuleSet = require('webpack/lib/RuleSet');
var AliasPlugin = require('enhanced-resolve/lib/AliasPlugin');
var ModulesInRootPlugin = require('enhanced-resolve/lib/ModulesInRootPlugin');
function MeteorImportsPlugin(config) {
config.exclude = [
'autoupdate',
'global-imports',
'hot-code-push',
'ecmascript',
].concat(config.exclude || []);
this.config = config;
}
MeteorImportsPlugin.prototype.apply = function(compiler) {
var self = this;
function getMeteorBuild(context) {
var webBrowser = self.config.legacy ? 'web.browser.legacy' : 'web.browser'
return self.config.meteorProgramsFolder
? path.resolve(context, self.config.meteorProgramsFolder, webBrowser)
: path.resolve(context, self.config.meteorFolder, '.meteor', 'local', 'build', 'programs', webBrowser);
}
function getManifest(context) {
// Check if Meteor has been run at least once.
try {
return require(getMeteorBuild(context) + '/program.json').manifest;
} catch (e) {
throw Error('Run Meteor at least once.')
}
}
compiler.plugin("compile", function(params) {
// Create path for internal build of the meteor packages.
var meteorBuild = getMeteorBuild(params.normalModuleFactory.context)
// Create path for plugin node moduels directory.
var meteorNodeModules = path.join(__dirname, 'node_modules');
// Create path for meteor app packages directory.
var meteorPackages = path.join(meteorBuild, 'packages');
var manifest = getManifest(params.normalModuleFactory.context)
// 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.resolvers.normal.apply(new AliasPlugin('described-resolve', {
name: 'meteor-build',
alias: meteorBuild,
}, 'resolve'));
compiler.resolvers.context.apply(new AliasPlugin('described-resolve', {
name: 'meteor-packages',
alias: meteorPackages,
onlyModule: false,
}, 'resolve'));
// Create an alias for the meteor-imports require.
compiler.resolvers.normal.apply(new AliasPlugin('described-resolve', {
name: 'meteor-imports',
alias: path.join(__dirname, './meteor-imports.js'),
}, 'resolve'));
compiler.resolvers.loader.apply(new ModulesInRootPlugin('module', meteorNodeModules, 'resolve'));
// 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)) {
var match = /^packages\/(.+)\.js$/.exec(pckge.path);
if (!match) return;
var packageName = match[1];
packageName = packageName.replace('_', ':');
compiler.resolvers.normal.apply(new AliasPlugin('described-resolve', {
name: 'meteor/' + packageName,
alias: path.join(meteorBuild, pckge.path),
}, 'resolve'));
}
});
});
// 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);
});
// Create path for internal build of the meteor packages.
var meteorBuild = getMeteorBuild(nmf.context)
// Create path for meteor app packages directory.
var meteorPackages = path.join(meteorBuild, 'packages');
var manifest = getManifest(nmf.context)
var extraRules = [
{
meteorImports: true,
test: /meteor-config$/,
include: [__dirname],
use: {
loader: 'json-string-loader',
options: {json: JSON.stringify(self.config)}
}
},
{
meteorImports: true,
test: new RegExp('.meteor/local/build/programs/web.browser/packages'),
loader: 'imports?this=>window',
},
{
meteorImports: true,
test: /\.css$/,
include: [meteorPackages],
use: [
{loader: 'style-loader'},
{loader: 'css-loader'}
]
}
]
manifest.forEach(function (pckge) {
if (!excluded.test(pckge.path)) {
var match = /^packages\/(.+)\.js$/.exec(pckge.path);
if (!match) return;
var packageName = match[1];
packageName = packageName.replace('_', ':');
extraRules.push({
meteorImports: true,
test: new RegExp('.meteor/local/build/programs/web.browser/' + pckge.path),
loader: 'exports?Package["' + packageName + '"]',
})
}
});
nmf.ruleSet = new RuleSet(nmf.ruleSet.rules.concat(extraRules))
});
};
module.exports = MeteorImportsPlugin;