Skip to content

Commit

Permalink
Merge branch 'master' into webpack-5-extras
Browse files Browse the repository at this point in the history
  • Loading branch information
alvesmiguel1 committed Oct 26, 2023
2 parents a1c8331 + b978c94 commit e5357a5
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 64 deletions.
2 changes: 1 addition & 1 deletion ViewGenerator/ViewGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<AssemblyTitle>ViewGenerator</AssemblyTitle>
<Product>ViewGenerator</Product>
<Description>Generates .NET View bindings from typescript</Description>
<Version>1.1.10</Version>
<Version>1.1.17</Version>
<PackageId>ViewGenerator</PackageId>
<PackageTags>Library</PackageTags>

Expand Down
34 changes: 4 additions & 30 deletions ViewGenerator/build/ViewGenerator.targets
Original file line number Diff line number Diff line change
@@ -1,52 +1,26 @@
<Project>
<PropertyGroup>

<CleanDependsOn>
VG_UnlinkNodeModules;
VG_CleanEntryFiles;
VG_CleanJSFiles;
VG_CleanCSSFiles;
VG_Clean;
$(CleanDependsOn);
</CleanDependsOn>
<NodeVersion>12.18.4</NodeVersion>
</PropertyGroup>

<ItemGroup>
<None Remove="**\*.cache"/>
</ItemGroup>

<Target Name="VG_SetVariables">
<ItemGroup Condition="'$(OS)' == 'Windows_NT'">
<_ViewGeneratorNodeJsExe Include="$(MSBuildThisFileDirectory)..\..\..\Node.js.redist\$(NodeVersion)\tools\win-x64\node.exe"/>
<_ViewGeneratorNodeJsExe Include="$(MSBuildThisFileDirectory)..\..\Node.js.redist.$(NodeVersion)\tools\win-x64\node.exe"/>
</ItemGroup>
<ItemGroup Condition="'$(OS)' != 'Windows_NT'">
<_ViewGeneratorNodeJsExe Include="$(MSBuildThisFileDirectory)..\..\..\Node.js.redist\$(NodeVersion)\tools\osx-x64\node"/>
<_ViewGeneratorNodeJsExe Include="$(MSBuildThisFileDirectory)..\..\Node.js.redist.$(NodeVersion)\tools\osx-x64\node"/>
</ItemGroup>
<ItemGroup>
<_ViewGeneratorCoreNodeJsExeAfterCheckedPaths Include="@(_ViewGeneratorCoreNodeJsExe)" Condition="Exists('%(FullPath)')" />
</ItemGroup>
<PropertyGroup>
<ViewGeneratorPath>$([System.IO.Path]::GetFullPath(&quot;$(MSBuildThisFileDirectory)..\&quot;))</ViewGeneratorPath>
<ViewGeneratorToolsPath>$(ViewGeneratorPath)tools</ViewGeneratorToolsPath>
<ViewGeneratorContentFilesPath>$(ViewGeneratorPath)contentFiles\</ViewGeneratorContentFilesPath>
<__ViewGeneratorNodeJsExe>@(_ViewGeneratorCoreNodeJsExeAfterCheckedPaths);</__ViewGeneratorNodeJsExe>
<ViewGeneratorNodeJsExe>$(__ViewGeneratorNodeJsExe.Substring(0, $(__ViewGeneratorNodeJsExe.IndexOf(';'))))</ViewGeneratorNodeJsExe>
</PropertyGroup>
</Target>

<Target Name="VG_LinkNodeModules" Condition="'$(PluginsRelativePath)' != '' and !Exists('$(ProjectDir)node_modules\webview.plugins')" >
<Exec Condition="'$(OS)' == 'Windows_NT'" Command="mklink /J &quot;$(ProjectDir)node_modules\webview.plugins&quot; &quot;$(ProjectDir)$(PluginsRelativePath)node_modules&quot;" />
<!-- Take into account that ln -s takes the arguments in a different order from mklink e.g. first is destination and second is source -->
<Exec Condition="'$(OS)' != 'Windows_NT'" Command="ln -s &quot;$(ProjectDir)$(PluginsRelativePath)node_modules&quot; &quot;$(ProjectDir)node_modules/webview.plugins&quot;" />
</Target>

<Target Name="VG_UnlinkNodeModules">
<RemoveDir Condition="Exists('$(ProjectDir)node_modules\webview.plugins')" Directories="$(ProjectDir)node_modules\webview.plugins" />
</Target>

<Target Name="VG_CleanEntryFiles" >
<ItemGroup>
<EntryFilesToDelete Include="**\*.entry" Exclude="node_modules\**\*.entry" />
Expand Down Expand Up @@ -127,7 +101,7 @@
</ItemGroup>

<!-- Webpack bundle generation targets-->
<Target Name="VG_GenerateBundle" Inputs="@(VG_Inputs);@(VG_Workers_Inputs)" Outputs="VG_Build.cache" Condition="'$(UsingMicrosoftNETSdk)' != 'true' Or '$(DesignTimeBuild)' != 'true'" AfterTargets="AfterResolveReferences" DependsOnTargets="VG_SetVariables;VG_SetupViewPacker;VG_LinkNodeModules;VG_Setup">
<Target Name="VG_GenerateBundle" Inputs="@(VG_Inputs);@(VG_Workers_Inputs)" Outputs="VG_Build.cache" Condition="'$(UsingMicrosoftNETSdk)' != 'true' Or '$(DesignTimeBuild)' != 'true'" AfterTargets="AfterResolveReferences" DependsOnTargets="VG_SetVariables;VG_SetupViewPacker;VG_Setup">
<CallTarget Targets="VG_GenerateBundleInner" />
</Target>

Expand All @@ -141,19 +115,19 @@
<CallTarget Condition="'@(VG_Workers_Inputs)' != ''" Targets="VG_GenerateWorkersBundle" />

<!-- Main bundle generation -->
<Exec Command="node &quot;$(ViewPackerToolScript)&quot; --config='$(ViewGeneratorToolsPath)/webpack/$(WP_ConfigFileName).config.js' --mode=$(WP_Mode) --devtool=$(WP_DevTool) --env useCache=$(WP_UseCache) --env pluginsRelativePath='$(PluginsRelativePath)' --env assemblyName='$(AssemblyName)'" />
<Exec Command="node &quot;$(ViewPackerToolScript)&quot; --config=&quot;$(ViewGeneratorToolsPath)/webpack/$(WP_ConfigFileName).config.js&quot; --mode=$(WP_Mode) --devtool=$(WP_DevTool) --env useCache=$(WP_UseCache) --env pluginsRelativePath='$(PluginsRelativePath)' --env assemblyName='$(AssemblyName)'" />

<Touch Files="VG_Build.cache" AlwaysCreate="true" />
</Target>

<!-- Default stylesheet related target -->
<Target Name="VG_GenerateDefaultStyleSheet" >
<Exec Command="node &quot;$(ViewPackerToolScript)&quot; --config='$(ViewGeneratorToolsPath)/webpack/webpack_stylesheets.config.js' --mode=$(WP_Mode) --devtool=$(WP_DevTool) --env entryPath='@(DefaultStyleSheet)' --env assemblyName='$(AssemblyName)'" />
<Exec Command="node &quot;$(ViewPackerToolScript)&quot; --config=&quot;$(ViewGeneratorToolsPath)/webpack/webpack_stylesheets.config.js&quot; --mode=$(WP_Mode) --devtool=$(WP_DevTool) --env entryPath='@(DefaultStyleSheet)' --env assemblyName='$(AssemblyName)'" />
</Target>

<!-- Workers related target -->
<Target Name="VG_GenerateWorkersBundle" >
<Exec Command="node &quot;$(ViewPackerToolScript)&quot; --config='$(ViewGeneratorToolsPath)/webpack/webpack_workers.config.js' --mode=$(WP_Mode) --devtool=$(WP_DevTool) --env useCache=$(WP_UseCache)" />
<Exec Command="node &quot;$(ViewPackerToolScript)&quot; --config=&quot;$(ViewGeneratorToolsPath)/webpack/webpack_workers.config.js&quot; --mode=$(WP_Mode) --devtool=$(WP_DevTool) --env useCache=$(WP_UseCache)" />
</Target>

<!-- /// -->
Expand Down
6 changes: 3 additions & 3 deletions ViewGenerator/tools/webpack/Plugins/CommonConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import SassRuleSet from "../Rules/Sass";
import getTypeScriptRuleSet from "../Rules/TypeScript";
import ForkTsCheckerWebpackPlugin from "fork-ts-checker-webpack-plugin";

let getCommonConfiguration = (isProductionBuild: boolean, cacheName: string, libraryName: string, assemblyName?: string, pluginsRelativePath?: string): Configuration => {
let getCommonConfiguration = (isProductionBuild: boolean, cacheName: string, libraryName: string, assemblyName?: string, pluginsRelativePath?: string, forHotReload?: boolean): Configuration => {

const entryMap: Dictionary<string> = {}
const outputMap: Dictionary<string> = {};
Expand Down Expand Up @@ -82,7 +82,7 @@ let getCommonConfiguration = (isProductionBuild: boolean, cacheName: string, lib
libraryTarget: "window",
globalObject: "window",
devtoolNamespace: libraryName,
publicPath: "/"
publicPath: "/" + assemblyName + "/"
},

node: false,
Expand All @@ -99,7 +99,7 @@ let getCommonConfiguration = (isProductionBuild: boolean, cacheName: string, lib
rules: [
SassRuleSet,
getResourcesRuleSet(assemblyName, pluginsAssembly),
getTypeScriptRuleSet()
getTypeScriptRuleSet(forHotReload)
]
},

Expand Down
5 changes: 4 additions & 1 deletion ViewGenerator/tools/webpack/Plugins/Resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ export const CssChunkPlaceholder: string = "chunk_[contenthash].css";
export const NamePlaceholder: string = "[name]";
export const JsChunkPlaceholder: string = "chunk_[contenthash].js";
export const JsPlaceholder: string = "[name].js";
export const JsMapPlaceholder: string = "[name].js.map";
export const JsMapPlaceholder: string = "[name].js.map";
export const IdPlaceholder: string = "[id]";
export const RuntimePlaceholder: string = "[runtime]";
export const FullHashPlaceholder: string = "[fullhash]";
16 changes: 12 additions & 4 deletions ViewGenerator/tools/webpack/Plugins/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,21 @@ export function generateManifest(
export function getCurrentDirectory() {
return resolve(".");
}

/*
* Gets the filename from an array.
* */
export function getFileName(relativePaths: Dictionary<string>, chunkData: any) {
const Directory: string = relativePaths[chunkData.chunk.name];
if (Directory) {
return Directory + JsPlaceholder;
export function getFileName(relativePaths: Dictionary<string>, chunkData: any): string {
const directory: string = relativePaths[chunkData.chunk.name];
if (directory) {
return directory + JsPlaceholder;
}
return OutputDirectoryDefault + JsChunkPlaceholder;
}

/*
* Sanitizes a command-line parameter
* */
export function sanitizeCommandLineParam(parameter: string): string {
return parameter.replaceAll("'", "");
}
9 changes: 5 additions & 4 deletions ViewGenerator/tools/webpack/Rules/Files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const getResourcesRuleSet = (assemblyName?: string, pluginsBase? : string): Rule
type: 'asset/resource',
generator: {
emit: false,
publicPath: "/",
filename: (pathData: PathData, _assetInfo?: AssetInfo): string => {
const fileName: string = pathData.filename;
if (fileName.indexOf(`/.pnpm/`) > 0) {
Expand All @@ -21,15 +22,15 @@ const getResourcesRuleSet = (assemblyName?: string, pluginsBase? : string): Rule
}

if (pathData.module) {
let module: NormalModule = pathData.module as NormalModule;
const module: NormalModule = pathData.module as NormalModule;
if (module.resourceResolveData) {
let resourceResolveData = module.resourceResolveData;
let assemblyFileName = resourceResolveData.descriptionFileData.name;
const resourceResolveData = module.resourceResolveData;
const assemblyFileName = resourceResolveData.descriptionFileData.name;
if (assemblyFileName.toUpperCase() === assemblyName.toUpperCase()) {
return assemblyName + '/' + pathData.filename;
}

if (!!pluginsBase && assemblyFileName.toUpperCase() === pluginsBase.toUpperCase()) {
if (pluginsBase && assemblyFileName.toUpperCase() === pluginsBase.toUpperCase()) {
return pluginsBase + '/' + pathData.filename;
}

Expand Down
4 changes: 2 additions & 2 deletions ViewGenerator/tools/webpack/Rules/TypeScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { RuleSetRule, RuleSetUseItem } from "webpack";

// .ts / .tsx files
const getTypeScriptRuleSet = (): RuleSetRule => {
const getTypeScriptRuleSet = (forHotReload: boolean): RuleSetRule => {

const TypeScriptRule: RuleSetRule = {
test: /\.tsx?$/i,
Expand All @@ -27,7 +27,7 @@ const getTypeScriptRuleSet = (): RuleSetRule => {
};

// thread-loader, ts-loader
TypeScriptRule.use = ruleSet.concat(threadLoaderRule, tsLoaderRule);
TypeScriptRule.use = forHotReload ? ruleSet.concat(tsLoaderRule) : ruleSet.concat(threadLoaderRule, tsLoaderRule);
return TypeScriptRule;
}

Expand Down
5 changes: 4 additions & 1 deletion ViewGenerator/tools/webpack/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
"module": "commonjs",
"target": "es6",
"esModuleInterop": true,
"skipLibCheck": true
"skipLibCheck": true,
"lib": [
"ES2021.String"
]
},
"compileOnSave": true,
"exclude": [
Expand Down
7 changes: 3 additions & 4 deletions ViewGenerator/tools/webpack/webpack_plugins.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ import getCommonConfiguration from "./Plugins/CommonConfiguration";
import DtsCleanupPlugin from "./Plugins/DtsCleanupPlugin";
import DtsGeneratorPlugin from "./Plugins/DtsGeneratorPlugin";
import { DtsFileName } from "./Plugins/Resources";
import { getCurrentDirectory } from "./Plugins/Utils";
import { getCurrentDirectory, sanitizeCommandLineParam } from "./Plugins/Utils";

const config = (env, argv) => {

let standardConfig: Configuration = getCommonConfiguration(
const standardConfig: Configuration = getCommonConfiguration(
argv.mode === "production",
"pluginsCache",
"Plugins",
env.assemblyName,
env.pluginsRelativePath);
sanitizeCommandLineParam(env.assemblyName));

standardConfig.optimization.runtimeChunk = {
name: "PluginsRuntime"
Expand Down
12 changes: 6 additions & 6 deletions ViewGenerator/tools/webpack/webpack_stylesheets.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ import { Configuration } from "webpack";

import MiniCssExtractPluginCleanup from "./Plugins/MiniCssExtractPluginCleanup";
import { CssPlaceholder, JsMapPlaceholder, OutputDirectoryDefault } from "./Plugins/Resources";
import { Dictionary, getCurrentDirectory } from "./Plugins/Utils"
import { Dictionary, getCurrentDirectory, sanitizeCommandLineParam } from "./Plugins/Utils"

import getResourcesRuleSet from "./Rules/Files";
import SassRuleSet from "./Rules/Sass";

const config = (env) => {

const getEntryName = (entryPath: string): string => {
let fileExtensionLen: number = entryPath.length - entryPath.lastIndexOf(".");
const fileExtensionLen: number = entryPath.length - entryPath.lastIndexOf(".");
return entryPath.slice(entryPath.replace(/\//g, '\\').lastIndexOf("\\") + 1, -fileExtensionLen);
};

let entries: string = env.entryPath;
let entries: string = sanitizeCommandLineParam(env.entryPath);
let entryMap: Dictionary<string> = {};
entries.split(";").map(entryPath => entryMap[getEntryName(entryPath)] = './' + entryPath)
let stylesheetsConfig: Configuration = {

const stylesheetsConfig: Configuration = {
entry: entryMap,

output: {
Expand All @@ -42,7 +42,7 @@ const config = (env) => {
module: {
rules: [
SassRuleSet,
getResourcesRuleSet(env.assemblyName)
getResourcesRuleSet(sanitizeCommandLineParam(env.assemblyName))
]
},

Expand Down
26 changes: 18 additions & 8 deletions ViewGenerator/tools/webpack/webpack_views.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { resolve } from "path";
import { Configuration } from "webpack";

import getCommonConfiguration from "./Plugins/CommonConfiguration";
import { Dictionary } from "./Plugins/Utils";
import { Dictionary, sanitizeCommandLineParam } from "./Plugins/Utils";
import {FullHashPlaceholder, IdPlaceholder, OutputDirectoryDefault, RuntimePlaceholder} from "./Plugins/Resources";

const config = (env, argv) => {

Expand Down Expand Up @@ -34,14 +35,17 @@ const config = (env, argv) => {
throw new Error("Extended configuration file not found.");
}
};

const sanitizedPluginsRelativePath: string = sanitizeCommandLineParam(env.pluginsRelativePath);

let standardConfig: Configuration = getCommonConfiguration(
const standardConfig: Configuration = getCommonConfiguration(
argv.mode === "production",
"viewsCache",
"Views",
env.assemblyName,
env.pluginsRelativePath);

sanitizeCommandLineParam(env.assemblyName),
sanitizedPluginsRelativePath,
env.forHotReload);

standardConfig.optimization.runtimeChunk = {
name: "ViewsRuntime"
};
Expand All @@ -57,7 +61,7 @@ const config = (env, argv) => {
}
};

generateExtendedConfig(env.pluginsRelativePath || ".", !!env.pluginsRelativePath);
generateExtendedConfig(sanitizedPluginsRelativePath || ".", !!sanitizedPluginsRelativePath);

// resolve.alias
if (Object.keys(aliasMap).length > 0) {
Expand All @@ -78,10 +82,16 @@ const config = (env, argv) => {
];
}

if (env.useCache) {
if (env.forHotReload) {
standardConfig.output.hotUpdateChunkFilename = OutputDirectoryDefault + IdPlaceholder + "." + FullHashPlaceholder + ".hot-update.js";
standardConfig.output.hotUpdateMainFilename = OutputDirectoryDefault + RuntimePlaceholder + "." + FullHashPlaceholder + ".hot-update.json";

// @ts-ignore
standardConfig.devServer = {
disableHostCheck: true
client: {
overlay: false
},
allowedHosts: "all"
};
}

Expand Down

0 comments on commit e5357a5

Please sign in to comment.