-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathEnactFrameworkRefPlugin.js
More file actions
184 lines (164 loc) · 6.84 KB
/
EnactFrameworkRefPlugin.js
File metadata and controls
184 lines (164 loc) · 6.84 KB
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
177
178
179
180
181
182
183
184
const fs = require('fs');
const path = require('path');
const gracefulFs = require('graceful-fs');
const DelegatedSourceDependency = require('webpack/lib/dependencies/DelegatedSourceDependency');
const DelegatedModule = require('webpack/lib/DelegatedModule');
const ExternalsPlugin = require('webpack/lib/ExternalsPlugin');
const app = require('../../option-parser');
const findParentMain = function (dir) {
const currPkg = path.join(dir, 'package.json');
if (fs.existsSync(currPkg)) {
const meta = JSON.parse(fs.readFileSync(currPkg, {encoding: 'utf8'}));
if (meta.main) return {path: dir, pointsTo: path.join(dir, meta.main).replace(/\.js/, '')};
}
if (dir === '/' || dir === '' || dir === '.') return null;
return findParentMain(path.dirname(dir));
};
// Custom DelegateFactoryPlugin designed to redirect Enact framework require() calls
// to the external framework
class DelegatedEnactFactoryPlugin {
constructor(options = {}) {
this.options = options;
this.options.local = this.options.libraries.includes('.');
if (this.options.local) this.options.libraries.splice(this.options.libraries.indexOf('.'), 1);
}
apply(normalModuleFactory) {
const {name, libraries, ignore, local, polyfill} = this.options;
const libReg = new RegExp('^(' + libraries.join('|') + ')(?=[\\\\\\/]|$)');
const ignReg =
ignore && new RegExp('^(' + ignore.map(p => p.replace('/', '[\\\\\\/]')).join('|') + ')(?=[\\\\\\/]|$)');
normalModuleFactory.hooks.factorize.tapAsync('DelegatedEnactFactoryPlugin', (data, callback) => {
const dependency = data.dependencies[0];
const {context} = data;
const {request} = dependency;
if (request === polyfill) {
const polyID = '@enact/polyfills';
return callback(null, new DelegatedModule(name, {id: polyID}, 'require', polyID, polyID));
} else if (local && request && context && request.startsWith('.')) {
let resource = path.join(context, request);
// Check if the resource path contains any ignored package path
// This prevents externalizing internal imports from ignored packages
if (ignReg) {
const pathSegments = resource.split(/[\\/]node_modules[\\/]/);
for (const segment of pathSegments) {
if (ignReg.test(segment)) {
return callback(); // Don't externalize - bundle it instead
}
}
}
if (
resource.startsWith(app.context) &&
!/[\\/]tests[\\/]/.test('./' + path.relative(app.context, resource)) &&
(!ignReg || !ignReg.test(resource.replace(/^(.*[\\/]node_modules[\\/])+/, '')))
) {
const parent = findParentMain(path.dirname(resource));
if (parent.pointsTo === resource) resource = parent.path;
let localID = resource
.replace(app.context, app.name)
.replace(/\.js$/, '')
.replace(/\\/g, '/')
.replace(/.*[\\/]node_modules[\\/]/, '')
.replace(/[\\/]$/, '');
return callback(null, new DelegatedModule(name, {id: localID}, 'require', localID, localID));
}
}
if (request && libReg.test(request) && (!ignReg || !ignReg.test(request))) {
return callback(null, new DelegatedModule(name, {id: request}, 'require', request, request));
}
return callback();
});
}
}
// Form a correct filepath that can be used within the build's output directory
function normalizePath(dir, file, compiler) {
if (path.isAbsolute(dir)) {
return path.join(dir, file);
} else {
return path.relative(path.resolve(compiler.outputPath), path.join(process.cwd(), dir, file));
}
}
// Determine if it's a NodeJS output filesystem or if it's a foreign/virtual one.
// The internal webpack5 implementation of outputFileSystem is graceful-fs.
function isNodeOutputFS(compiler) {
return compiler.outputFileSystem && JSON.stringify(compiler.outputFileSystem) === JSON.stringify(gracefulFs);
}
// Reference plugin to handle rewiring the external Enact framework requests
class EnactFrameworkRefPlugin {
constructor(options = {}) {
this.options = options;
this.options.name = this.options.name || 'enact_framework';
this.options.libraries = this.options.libraries || [
'@enact',
'react',
'react-dom',
'react-dom/client',
'react-dom/server',
'ilib'
];
this.options.ignore = this.options.ignore || [
'@enact/dev-utils',
'@enact/storybook-utils',
'@enact/ui-test-utils',
'@enact/screenshot-test-utils',
'readable-stream', // ignore for screenshot test build
'react-is' // ignore for ui test build
];
this.options.external = this.options.external || {};
this.options.external.publicPath =
this.options.publicPath || this.options.external.publicPath || this.options.external.path;
if (!this.options.htmlPlugin) this.options.htmlPlugin = require('html-webpack-plugin');
if (!process.env.ILIB_BASE_PATH) {
// Backwards support for Enact <3
const context = options.context || process.cwd();
if (fs.existsSync(path.join(context, 'node_modules', '@enact', 'i18n', 'ilib'))) {
process.env.ILIB_BASE_PATH = path.join(
this.options.external.publicPath,
'node_modules',
'@enact',
'i18n',
'ilib'
);
} else {
process.env.ILIB_BASE_PATH = path.join(this.options.external.publicPath, 'node_modules', 'ilib');
}
}
}
apply(compiler) {
const {name, libraries, ignore, external, polyfill, htmlPlugin, webOSMetaPlugin} = this.options;
// Declare enact_framework as an external dependency
const externals = {};
externals[this.options.name] = this.options.name;
new ExternalsPlugin(this.options.libraryTarget || 'var', externals).apply(compiler);
compiler.hooks.compilation.tap('EnactFrameworkRefPlugin', (compilation, {normalModuleFactory}) => {
const htmlPluginHooks = htmlPlugin.getHooks(compilation);
const webOSMetaPluginHooks = webOSMetaPlugin.getHooks(compilation);
compilation.dependencyFactories.set(DelegatedSourceDependency, normalModuleFactory);
htmlPluginHooks.beforeAssetTagGeneration.tapAsync('EnactFrameworkRefPlugin', (htmlPluginData, callback) => {
htmlPluginData.assets.js.unshift(
normalizePath(external.publicPath, 'enact.js', compiler).replace(/\\+/g, '/')
);
htmlPluginData.assets.css.unshift(
normalizePath(external.publicPath, 'enact.css', compiler).replace(/\\+/g, '/')
);
callback(null, htmlPluginData);
});
if (external.snapshot && isNodeOutputFS(compiler) && webOSMetaPluginHooks.webosMetaRootAppinfo) {
webOSMetaPluginHooks.webosMetaRootAppinfo.tap('EnactFrameworkRefPlugin', meta => {
const relSnap = normalizePath(external.publicPath, 'snapshot_blob.bin', compiler);
meta.v8SnapshotFile = relSnap.replace(/\\+/g, '/');
return meta;
});
}
});
// Apply the Enact factory plugin to handle the require() delagation/rerouting
compiler.hooks.compile.tap('EnactFrameworkRefPlugin', ({normalModuleFactory}) => {
new DelegatedEnactFactoryPlugin({
name,
libraries,
ignore,
polyfill
}).apply(normalModuleFactory);
});
}
}
module.exports = EnactFrameworkRefPlugin;