Skip to content
Closed
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
6 changes: 6 additions & 0 deletions .changeset/chubby-rocks-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@lynx-js/template-webpack-plugin": patch
"@lynx-js/web-webpack-plugin": patch
---

Delay remove of intermediate debugging assets to afterEmit, this will fix the sourcemap not being generated issue.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
// Licensed under the Apache License Version 2.0 that can be found in the
// LICENSE file in the root directory of this source tree.

import type { Compilation, Compiler } from 'webpack';
import fs from 'node:fs';
import path from 'node:path';

import type { Compiler } from 'webpack';

import { LynxTemplatePlugin } from './LynxTemplatePlugin.js';

Expand Down Expand Up @@ -121,6 +124,7 @@ export class LynxEncodePlugin {

export class LynxEncodePluginImpl {
name = 'LynxEncodePlugin';
private assets2Delete = new Set<string>();

constructor(
compiler: Compiler,
Expand All @@ -145,7 +149,7 @@ export class LynxEncodePluginImpl {

if (!isDebug() && !isDev && !isRsdoctor()) {
templateHooks.beforeEmit.tap(this.name, (args) => {
this.deleteDebuggingAssets(compilation, [
this.deleteDebuggingAssets([
encodeData.lepusCode.root,
...encodeData.lepusCode.chunks,
...Object.keys(manifest).map(name => ({ name })),
Expand Down Expand Up @@ -192,6 +196,32 @@ export class LynxEncodePluginImpl {

return args;
});
compiler.hooks.afterEmit.tap(this.name, (compilation) => {
this.assets2Delete.forEach((asset) => {
compilation.deleteAsset(asset);
if (compilation.outputOptions.path) {
const files2Delete = [
asset,
];
if (asset.endsWith('.js')) {
files2Delete.push(asset + '.map');
}
for (const file of files2Delete) {
fs.rmSync(
path.posix.join(
compilation.outputOptions.path,
file,
),
{
recursive: true,
force: true,
},
);
}
}
});
this.assets2Delete.clear();
});

templateHooks.encode.tapPromise({
name: this.name,
Expand All @@ -216,14 +246,15 @@ export class LynxEncodePluginImpl {
* The deleteDebuggingAssets delete all the assets that are inlined into the template.
*/
deleteDebuggingAssets(
compilation: Compilation,
assets: ({ name: string } | undefined)[],
): void {
this.assets2Delete = new Set();
const { assets2Delete } = this;
assets
.filter(asset => asset !== undefined)
.forEach(asset => deleteAsset(asset));
function deleteAsset({ name }: { name: string }) {
return compilation.deleteAsset(name);
return assets2Delete.add(name);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

```ts

import type { Compilation } from 'webpack';
import type { Compiler } from 'webpack';

// Warning: (ae-missing-release-tag) "isDebug" is part of the package's API, but it is missing a release tag (@alpha, @beta, @public, or @internal)
Expand All @@ -25,7 +24,7 @@ export class WebWebpackPlugin {
apply(compiler: Compiler): void;
// (undocumented)
static BEFORE_ENCODE_HOOK_STAGE: number;
deleteDebuggingAssets(compilation: Compilation, assets: ({
deleteDebuggingAssets(assets: ({
name: string;
} | undefined)[]): void;
// (undocumented)
Expand Down
38 changes: 34 additions & 4 deletions packages/webpack/web-webpack-plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Copyright 2024 The Lynx Authors. All rights reserved.
// Licensed under the Apache License Version 2.0 that can be found in the
// LICENSE file in the root directory of this source tree.
import fs from 'node:fs';
import path from 'node:path';

import type { Compilation, Compiler } from 'webpack';
import type { Compiler } from 'webpack';

import { LynxTemplatePlugin } from '@lynx-js/template-webpack-plugin';

Expand All @@ -12,6 +14,7 @@ export class WebWebpackPlugin {
static name = 'lynx-for-web-plugin';
static BEFORE_ENCODE_HOOK_STAGE = 100;
static ENCODE_HOOK_STAGE = 100;
private assets2Delete = new Set<string>();

apply(compiler: Compiler): void {
const isDev = process.env['NODE_ENV'] === 'development'
Expand All @@ -35,7 +38,7 @@ export class WebWebpackPlugin {

if (!isDebug() && !isDev && !isRsdoctor()) {
hooks.beforeEmit.tap({ name: WebWebpackPlugin.name }, (args) => {
this.deleteDebuggingAssets(compilation, [
this.deleteDebuggingAssets([
{ name },
encodeData.lepusCode.root,
...encodeData.lepusCode.chunks,
Expand All @@ -57,6 +60,32 @@ export class WebWebpackPlugin {
});
return encodeOptions;
});
compiler.hooks.afterEmit.tap(WebWebpackPlugin.name, (compilation) => {
this.assets2Delete.forEach((asset) => {
Comment thread
upupming marked this conversation as resolved.
compilation.deleteAsset(asset);
if (compilation.outputOptions.path) {
const files2Delete = [
asset,
];
if (asset.endsWith('.js')) {
files2Delete.push(asset + '.map');
}
for (const file of files2Delete) {
fs.rmSync(
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Emitting the asset to disk first and then manually deleting it seems inefficient.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, actually the right way to consume our sourcemap is to read source map from memory using compiler.

But the plugin user used now are using fs to find all sourcemap files.

image

path.posix.join(
compilation.outputOptions.path,
file,
),
{
recursive: true,
force: true,
},
);
}
}
});
this.assets2Delete.clear();
});

hooks.encode.tap({
name: WebWebpackPlugin.name,
Expand All @@ -82,14 +111,15 @@ export class WebWebpackPlugin {
* The deleteDebuggingAssets delete all the assets that are inlined into the template.
*/
deleteDebuggingAssets(
compilation: Compilation,
assets: ({ name: string } | undefined)[],
): void {
this.assets2Delete = new Set();
const { assets2Delete } = this;
assets
.filter(asset => asset !== undefined)
.forEach(asset => deleteAsset(asset));
function deleteAsset({ name }: { name: string }) {
return compilation.deleteAsset(name);
return assets2Delete.add(name);
}
}
}
Expand Down