Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/builder/extension-builder/extension-builder.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { CleanWebpackPlugin } from "clean-webpack-plugin";
import { Pattern, PluginOptions } from "copy-webpack-plugin";
import { existsSync, readFileSync } from "fs";
import { resolve } from "path";
import { Plugin } from "webpack";
import { ExternalsPlugin } from "webpack";
import { IBuilderEnvironment } from "../../api";
import { IDataNode } from "../../api/data-node";
import { WebpackBuilder } from "../webpack-builder";
import { WebpackConfigModel } from "../webpack-builder/model";
import { LogPlugin } from "../webpack-builder/plugins/log.plugin";
import { CopyWebpackPlugin, PathOverridePlugin, QextFilePlugin, ZipWebpackPlugin } from "./plugins";
import { CopyWebpackPlugin, QextFilePlugin, ZipWebpackPlugin } from "./plugins";
import { DeployExtensionPlugin } from "./plugins/ci/ci.plugin";
import { ExtensionService } from "./service/extension.service";
import { QrsService } from "./service/qrs.service";
Expand Down Expand Up @@ -79,7 +80,7 @@ export class ExtensionBuilder extends WebpackBuilder {
* @returns {Plugin[]}
* @memberof WebpackBuilder
*/
protected loadWebpackPlugins(): Plugin[] {
protected loadWebpackPlugins(): ExternalsPlugin[] {

// let plugins = super.loadWebpackPlugins();
const config = this.webpackService.getConfig();
Expand All @@ -91,8 +92,9 @@ export class ExtensionBuilder extends WebpackBuilder {
new CleanWebpackPlugin({
cleanAfterEveryBuildPatterns: ["!**/wbfolder.wbl"],
}),
new PathOverridePlugin(/\/umd\//, "/esm/"),
new CopyWebpackPlugin(this.getBinaryFiles()),
new CopyWebpackPlugin({
patterns: this.getBinaryFiles()
}),
new QextFilePlugin(this.extensionService.getQextConfiguration()),
new ZipWebpackPlugin({
filename: `${fileName}.zip`,
Expand All @@ -111,10 +113,10 @@ export class ExtensionBuilder extends WebpackBuilder {
* get binary files which should copy to dist folder
*
* @private
* @returns {IDataNode[]}
* @returns {Pattern[]}
* @memberof ExtensionBuilder
*/
private getBinaryFiles(): IDataNode[] {
private getBinaryFiles(): Pattern[] {

const binFiles = [
{ from: "wbfolder.wbl", to: "wbfolder.wbl" },
Expand Down
4 changes: 2 additions & 2 deletions src/builder/extension-builder/plugins/ci/ci.plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readFileSync } from "fs";
import { resolve } from "path";
import { compilation, Compiler } from "webpack";
import { Compilation, Compiler } from "webpack";
import { ICiConfig } from "../../api/ci-config.interface";
import { IExtensionFile } from "../../api/extensionFile.interface";
import { DesktopService } from "../../service/desktop.service";
Expand Down Expand Up @@ -30,7 +30,7 @@ export class DeployExtensionPlugin {
// this breaks the watcher
compiler.hooks.afterEmit.tapAsync(
"ExtensionDone",
async (comp: compilation.Compilation, callback) => {
async (comp: Compilation, callback) => {

if (typeof (this.config.desktop) !== "undefined") {
const files: IExtensionFile[] = [];
Expand Down
2 changes: 0 additions & 2 deletions src/builder/extension-builder/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import * as WebpackCopyPlugin from "copy-webpack-plugin";
import * as WebpackPathOverridePlugin from "path-override-webpack-plugin";
import * as WebpackZipPlugin from "zip-webpack-plugin";

export const CopyWebpackPlugin = WebpackCopyPlugin;
export const PathOverridePlugin = WebpackPathOverridePlugin;
export const ZipWebpackPlugin = WebpackZipPlugin;
export * from "./qext/qext-file.plugin";
121 changes: 121 additions & 0 deletions src/builder/extension-builder/plugins/path-replacer/path-replpacer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { stat } from "fs";
import { join } from "path";
import { Compiler, Compilation } from "webpack";


// Check if this is a windows runtime or not
const WIN = /^win/.test(process.platform);

// Captures component id (e.g 'feedback_form' from 'feedback/feedback_form').
const COMPONENT_ID_PATTERN = WIN ? /([^\\]+)$/ : /[^\/]*$/

export class PathReplacer {

pathRegExp;
pathReplacement;
exts;
info = [];

public constructor(pathRegExp: RegExp, pathReplacement: string, exts?) {
this.pathRegExp = pathRegExp;
this.pathReplacement = pathReplacement;
this.exts = exts || ['jsx', 'js', 'scss', 'css']
console.log("PathReplacer - Construckor")
}


public apply(compiler: Compiler) {
console.log("PathReplacer - apply")
compiler.hooks.normalModuleFactory.tap("PathReplacer", (nmf) => {
nmf.hooks.beforeResolve.tapAsync("PathReplacer2", (
comp, // ToDo rebuild to Compilation Type
callback,
) => {
console.log("PathReplacer - make")

console.log("## 1 ##")
if(!comp) return callback();

console.log("pathRegExp", this.pathRegExp)
console.log("pathReplacement", this.pathReplacement)
// console.log("exts", this.exts)
console.log("test 1 ", this.pathRegExp.test(comp.request))
console.log("test 2 ", comp.request)
console.log("test 3 ", comp)

// test the request for a path match
if(this.pathRegExp.test(comp.request)) {
console.log("## 2 ##")
var filePath = comp.request.replace(this.pathRegExp, this.pathReplacement);
this.getResolvedFile(filePath, this.exts, (file) => {
if (typeof file === 'string') {
this.info.push('[path-override] '+comp.request+' => '+file);
comp.request = file;
}
return callback();
})
} else {
return callback(false, false);
}

callback();
})

})

compiler.hooks.done.tapAsync("done", () => {
console.log("PathReplacer - done")
if (this.info.length > 0) {
console.log(this.info.join("\n"));
}
});

}

private getResolvedFile(filePath, exts, callback) {
console.log("PathReplacer - getResolvedFile")

var enclosingDirPath = filePath || '';
var captured = enclosingDirPath.match(COMPONENT_ID_PATTERN);
if (captured) {
var componentId = captured[1];
var extObjs = exts.reduce(function(allExts, ext) {
allExts.push(
{ ext: ext, file: true },
{ ext: ext, file: false }
)
return allExts
}, [])

var tryToFindExtension = function (index) {
var extObj = extObjs[index];
// None of passed extensions are found
if (!extObj) {
return callback(false);
}
var componentFileName, componentFilePath;
// Try to load regular file
if (extObj.file) {
componentFilePath = enclosingDirPath;
let extension = '.' + extObj.ext;
if (componentFilePath.slice(extension.length * -1) !== extension) {
componentFilePath += extension;
}
} else {
componentFileName = componentId + '.' + extObj.ext;
componentFilePath = join(enclosingDirPath, componentFileName);
}
stat(componentFilePath, function (err, stats) {
if (err || !stats.isFile()) {
return tryToFindExtension(index + 1);
}
callback(componentFilePath)
});
};
tryToFindExtension(0);
}
}

}


13 changes: 7 additions & 6 deletions src/builder/extension-builder/plugins/qext/qext-file.plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readFileSync, statSync, existsSync } from "fs";
import { basename } from "path";
import { compilation, Compiler } from "webpack";
import { Compiler, Compilation } from "webpack";
import { IQextData } from "../../../qext-file-builder/api";
import { QextFileBuilder } from "../../../qext-file-builder/qext-file.builder";

Expand All @@ -17,7 +17,7 @@ export class QextFilePlugin {
}

public apply(compiler: Compiler) {
this.registerAfterCompilationHook(compiler);
this.registerMakeHook(compiler);
}

/**
Expand All @@ -27,24 +27,25 @@ export class QextFilePlugin {
* @param {Compiler} compiler
* @memberof QextFilePlugin
*/
private registerAfterCompilationHook(compiler: Compiler) {
compiler.hooks.afterCompile.tapAsync("QextAfterCompile", async (
comp: compilation.Compilation,
private registerMakeHook(compiler: Compiler) {
compiler.hooks.make.tapAsync("QextAfterCompile", async (
comp, // ToDo rebuild to Compilation Type
callback,
) => {
const filePath = await this.qextBuilder.run();
const fileName = basename(filePath);
const fileStats = statSync(filePath);
const fileContent = readFileSync(filePath);

comp.assets[fileName] = {
(comp as any).assets[fileName] = {
size: () => {
return fileStats.size;
},
source: () => {
/** file not exists anymore ... */
return fileContent;
},

};
callback();
});
Expand Down
20 changes: 10 additions & 10 deletions src/builder/webpack-builder/model/webpack-config.model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Module, Options, Plugin } from "webpack";
import { Module, ExternalsPlugin, ModuleOptions } from "webpack";
import { IDataNode } from "../../../api/data-node";
import { ConfigModel } from "../../../model/config.model";

Expand Down Expand Up @@ -54,7 +54,7 @@ export class WebpackConfigModel extends ConfigModel {
* @type {IDataNode}
* @memberof WebpackConfigModel
*/
private webpackModuleRules: Module;
private webpackModuleRules: ModuleOptions;

/**
* context paths to tell webpack where to find specific loaders
Expand All @@ -73,7 +73,7 @@ export class WebpackConfigModel extends ConfigModel {
* @type {Options.Optimization}
* @memberof WebpackConfigModel
*/
private optimization: Options.Optimization;
private optimization: any;

/**
* out file name where bundle is saved to
Expand All @@ -100,7 +100,7 @@ export class WebpackConfigModel extends ConfigModel {
* @type {Plugin[]}
* @memberof WebpackConfigModel
*/
private plugins: Plugin[];
private plugins: ExternalsPlugin[];

/**
* watchmode enabled or disabled
Expand Down Expand Up @@ -159,7 +159,7 @@ export class WebpackConfigModel extends ConfigModel {
* @type {IDataNode}
* @memberof WebpackConfigModel
*/
public get moduleRules(): Module {
public get moduleRules(): ModuleOptions {
return this.webpackModuleRules;
}

Expand All @@ -169,7 +169,7 @@ export class WebpackConfigModel extends ConfigModel {
* @returns {Options.Optimization}
* @memberof WebpackConfigModel
*/
public getOptimization(): Options.Optimization {
public getOptimization(): any {
return this.optimization;
}

Expand Down Expand Up @@ -199,7 +199,7 @@ export class WebpackConfigModel extends ConfigModel {
* @returns {Plugin[]}
* @memberof WebpackConfigModel
*/
public getPlugins(): Plugin[] {
public getPlugins(): ExternalsPlugin[] {
return this.plugins;
}

Expand All @@ -209,7 +209,7 @@ export class WebpackConfigModel extends ConfigModel {
* @param {Options.Optimization} optimization
* @memberof WebpackConfigModel
*/
public setOptimization(optimization: Options.Optimization) {
public setOptimization(optimization: any) {
this.optimization = optimization;
}

Expand Down Expand Up @@ -267,7 +267,7 @@ export class WebpackConfigModel extends ConfigModel {
this.loaderContextPaths = paths;
}

public set moduleRules(rules: Module) {
public set moduleRules(rules: ModuleOptions) {
this.webpackModuleRules = rules;
}

Expand All @@ -287,7 +287,7 @@ export class WebpackConfigModel extends ConfigModel {
* @param {Plugin[]} plugins
* @memberof WebpackConfigModel
*/
public setPlugins(plugins: Plugin[]) {
public setPlugins(plugins: ExternalsPlugin[]) {
this.plugins = plugins;
}

Expand Down
6 changes: 3 additions & 3 deletions src/builder/webpack-builder/plugins/log.plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Compiler, Plugin, Stats } from "webpack";
import { Compiler, AutomaticPrefetchPlugin, Stats, StatsCompilation } from "webpack";
import { IDataNode } from "../../../api/data-node";

/**
Expand All @@ -8,7 +8,7 @@ import { IDataNode } from "../../../api/data-node";
* @class LogPlugin
* @implements {Plugin}
*/
export class LogPlugin implements Plugin {
export class LogPlugin implements AutomaticPrefetchPlugin {

/**
* entry point for webpack plugins
Expand All @@ -27,7 +27,7 @@ export class LogPlugin implements Plugin {
return;
}

const statsJson: Stats.ToJsonOutput = result.toJson();
const statsJson: StatsCompilation = result.toJson();
process.stderr.write(JSON.stringify(statsJson.errors, null, 4));
});
}
Expand Down
Loading