Skip to content

Commit a600093

Browse files
Make plugin remove drupal library require statements
1 parent 0991ac5 commit a600093

2 files changed

Lines changed: 55 additions & 2 deletions

File tree

lib/DrupalLibrariesPlugin.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ const path = require('path'),
22
YAML = require('yaml'),
33
DrupalLibraryEntryGenerator = require('./DrupalLibraryEntryGenerator'),
44
DrupalLibraryFile = require('./DrupalLibraryFile'),
5-
DrupalLibraryMetadata = require('./DrupalLibraryMetadata')
5+
DrupalLibraryMetadata = require('./DrupalLibraryMetadata'),
6+
DrupalLibraryModule = require('./DrupalLibraryModule')
67

78
/**
89
* A webpack plugin for generating drupal libraries files.
@@ -49,7 +50,21 @@ class DrupalLibrariesPlugin {
4950
* The webpack compiler.
5051
*/
5152
apply(compiler) {
52-
compiler.hooks.done.tapPromise('DrupalLibrariesPlugin ', async stats => {
53+
// Prevents drupal library dependencies from being included.
54+
compiler.hooks.normalModuleFactory.tap('DrupalLibrariesPlugin', cmf => {
55+
cmf.hooks.factory.tap("DrupalLibrariesPlugin", factory => (data, callback) => {
56+
const result = this.opts.requirePattern.exec(data.request)
57+
if (result) {
58+
callback(null, new DrupalLibraryModule(result[1]))
59+
}
60+
else {
61+
factory(data, callback)
62+
}
63+
})
64+
})
65+
66+
// Analyzes the final chunks to determine library dependencies.
67+
compiler.hooks.done.tapPromise('DrupalLibrariesPlugin', async stats => {
5368
this.metadata = await this._generateLibraryMetadata(stats.compilation)
5469
this.result = await this._generateLibraries(this.metadata)
5570
this.files = await Promise.resolve(this.opts.path(this.result, this.metadata))

lib/DrupalLibraryModule.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const Module = require('webpack').Module,
2+
RawSource = require('webpack-sources').RawSource
3+
4+
/**
5+
* A webpack module representing a Drupal library dependency.
6+
*/
7+
class DrupalLibraryModule extends Module {
8+
9+
constructor(name) {
10+
super("", null)
11+
12+
this.name = name
13+
}
14+
15+
identifier() {
16+
return "drupal " + this.name
17+
}
18+
19+
readableIdentifier() {
20+
return "drupal " + this.name
21+
}
22+
23+
needRebuild() {
24+
false
25+
}
26+
27+
build(options, compilation, resolver, fs, callback) {
28+
this.build = true
29+
this.buildMeta = {}
30+
this.buildInfo = {}
31+
callback()
32+
}
33+
34+
source(dependencyTemplates) {
35+
return new RawSource('')
36+
}
37+
38+
}

0 commit comments

Comments
 (0)