Skip to content

Commit 54d7c65

Browse files
committed
feat(all): use path-based moduleIds and hook native __webpack_require__
1 parent 90a3658 commit 54d7c65

File tree

2 files changed

+73
-40
lines changed

2 files changed

+73
-40
lines changed

src/index.js

+73-23
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ class AureliaWebpackPlugin {
151151
/**
152152
* used to inject Aurelia's Origin to all build resources
153153
*/
154-
compiler.plugin('compilation', function(compilation) {
154+
compiler.plugin('compilation', function (compilation) {
155155
debug('compilation');
156156
const contextElements = compiler.__aureliaContextElements;
157157
let paths = [];
@@ -161,32 +161,82 @@ class AureliaWebpackPlugin {
161161
console.error('No context elements');
162162
}
163163

164-
compilation.plugin('normal-module-loader', function(loaderContext, module) {
165-
// this is where all the modules are loaded
166-
// one by one, no dependencies are created yet
167-
if (typeof module.resource == 'string' && /\.(js|ts)x?$/i.test(module.resource)) {
168-
let moduleId;
169-
if (module.resource.startsWith(options.src)) {
170-
moduleId = path.relative(options.src, module.resource);
164+
function customWebpackRequire(moduleId) {
165+
// Check if module is in cache
166+
if(installedModules[moduleId])
167+
return installedModules[moduleId].exports;
168+
169+
// Create a new module (and put it into the cache)
170+
var module = installedModules[moduleId] = {
171+
i: moduleId,
172+
l: false,
173+
exports: {}
174+
};
175+
176+
// Try adding .js / .ts
177+
if (!modules[moduleId] && typeof moduleId === 'string') {
178+
var newModuleId;
179+
if (modules[newModuleId = moduleId + '.js'] || modules[newModuleId = moduleId + '.ts']) {
180+
moduleId = newModuleId;
181+
// alias also installedModules:
182+
installedModules[moduleId] = module;
171183
}
172-
if (!moduleId && typeof module.userRequest == 'string') {
173-
moduleId = paths.find(originPath => contextElements[originPath].source === module.userRequest);
174-
if (moduleId) {
175-
moduleId = path.normalize(moduleId);
176-
}
184+
}
185+
186+
// Execute the module function
187+
modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
188+
189+
// Flag the module as loaded
190+
module.l = true;
191+
192+
// Return the exports of the module
193+
return module.exports;
194+
}
195+
196+
compilation.mainTemplate.plugin('require', function(source, chunk, hash) {
197+
let newSourceArray = customWebpackRequire.toString().split('\n');
198+
newSourceArray.pop(); // remove header 'function... {'
199+
newSourceArray.shift(); // remove footer '}'
200+
return newSourceArray.join('\n');
201+
});
202+
203+
compilation.plugin('before-module-ids', function (modules) {
204+
modules.forEach((module) => {
205+
if (module.id !== null) {
206+
return;
177207
}
178-
if (!moduleId && typeof module.rawRequest == 'string' && !module.rawRequest.startsWith('.')) {
179-
// requested module:
180-
let index = paths.indexOf(module.rawRequest);
181-
if (index >= 0) {
182-
moduleId = module.rawRequest;
208+
209+
if (typeof module.resource == 'string') {
210+
let moduleId;
211+
212+
if (module.resource.startsWith(options.src)) {
213+
// paths inside SRC
214+
let relativeToSrc = path.relative(options.src, module.resource);
215+
moduleId = relativeToSrc;
216+
}
217+
if (!moduleId && typeof module.userRequest == 'string') {
218+
// paths resolved as build resources
219+
let matchingModuleIds = paths
220+
.filter(originPath => contextElements[originPath].source === module.userRequest)
221+
.map(originPath => path.normalize(originPath));
222+
223+
if (matchingModuleIds.length) {
224+
matchingModuleIds.sort((a, b) => b.length - a.length);
225+
moduleId = matchingModuleIds[0];
226+
}
227+
}
228+
if (!moduleId && typeof module.rawRequest == 'string' && module.rawRequest.indexOf('.') !== 0) {
229+
// requested modules from node_modules:
230+
let index = paths.indexOf(module.rawRequest);
231+
if (index >= 0) {
232+
moduleId = module.rawRequest;
233+
}
234+
}
235+
if (moduleId) {
236+
module.id = moduleId;
183237
}
184238
}
185-
if (moduleId) {
186-
const originLoader = path.join(__dirname, 'origin-loader.js') + '?' + JSON.stringify({ moduleId });
187-
module.loaders.unshift(originLoader);
188-
}
189-
}
239+
});
190240
});
191241
});
192242
}

src/origin-loader.js

-17
This file was deleted.

0 commit comments

Comments
 (0)