Giter VIP home page Giter VIP logo

Comments (7)

wxiaoyun avatar wxiaoyun commented on August 28, 2024

If you are trying to append and access additional information to AssetInfo object across compilation stages, please use compilation.updateAsset to update assetInfo, compilation.getAsset or compilation.getAssets to retrieve assetInfo.

These getters and setters are necessary for bidirectional conversion between Js Object and their corresponding Rust structs under the hood. Otherwise, only knownAssetInfo will be persisted across compilation stages:

image

Let me know if this helped

from rspack.

iusesoftware avatar iusesoftware commented on August 28, 2024

This helped, thank you. I was able to move forward a little bit.

1) Do you know if there is a way to replace compilation.assets[ filePath ].source()?
I searched for updateAsset in the repo and found that there is also getAssetSource, but seems like it's internal only.

2) Also, I wasn't able to confirm this fully, because I'm getting Process finished with exit code -1073741571 (0xC00000FD) (stack overflow?).
I understand this is probably not related, but may be you know what may be causing this?

from rspack.

wxiaoyun avatar wxiaoyun commented on August 28, 2024
  1. Do you know if there is a way to replace compilation.assets[ filePath ].source()? I searched for updateAsset in the repo and found that there is also getAssetSource, but seems like it's internal only.

Calling compilation.getAsset(<name>) or compilation.getAssets() gives you Asset object with source

	getAssets(): ReadonlyArray<Asset> {
		const assets = this.#inner.getAssets();

		return assets.map(asset => {
			return Object.defineProperties(asset, {
				info: {
					value: JsAssetInfo.__from_binding(asset.info)
				},
				source: {
					get: () => this.__internal__getAssetSource(asset.name)
				}
			}) as unknown as Asset;
		});
	}

from rspack.

wxiaoyun avatar wxiaoyun commented on August 28, 2024
  1. Also, I wasn't able to confirm this fully, because I'm getting Process finished with exit code -1073741571 (0xC00000FD) (stack overflow?). I understand this is probably not related, but may be you know what may be causing this?

I am not sure what it means. Can you create a minimal reproduction that triggers this error?

from rspack.

iusesoftware avatar iusesoftware commented on August 28, 2024
  1. Do you know if there is a way to replace compilation.assets[ filePath ].source()? I searched for updateAsset in the repo and found that there is also getAssetSource, but seems like it's internal only.

Calling compilation.getAsset(<name>) or compilation.getAssets() gives you Asset object with source

	getAssets(): ReadonlyArray<Asset> {
		const assets = this.#inner.getAssets();

		return assets.map(asset => {
			return Object.defineProperties(asset, {
				info: {
					value: JsAssetInfo.__from_binding(asset.info)
				},
				source: {
					get: () => this.__internal__getAssetSource(asset.name)
				}
			}) as unknown as Asset;
		});
	}

One custom plugin emits a new asset with:

const fileContents = JSON.stringify( fileListJson );

compilation.emitAsset( fullPath, new compiler.webpack.sources.RawSource( fileContents ) );

And in another custom plugin I tried:

compilation.getAsset( filePath ).source(); // didn't work
compilation.getAsset( filePath ).source.get(); // didn't work
compilation.getAsset( filePath ).source.toString(); // didn't work

Only this worked, but seems like it's a band-aid:

const fileMapSource = compilation.getAsset( filePath ).source._valueAsString; // did work

Its usage is:

const fileMap = JSON.parse( fileMapSource );

I'm on v1.0.0-alpha.2 now.

from rspack.

iusesoftware avatar iusesoftware commented on August 28, 2024

Is there access to compilation.valueCacheVersions?

I only found this:

My usage in the same custom plugin:

const foundAsset = findAsset( pathInMap, compilation.assetsInfo );

if ( foundAsset ) {
    let assetPathPrefix = '';

    for ( const [ key, value ] of compilation.valueCacheVersions ) {
        // @todo Find less error-prone way.
        if ( key.includes( 'webpack/DefinePlugin process.env.ASSET_PATH' ) ) {
            assetPathPrefix = value
                // “/” gets (re)added later
                // @todo This is all dirty. Find a better way.
                .replace( '/', '' )
                .replace( /"/gu, '' );
        }
    }

    filePath = `${ assetPathPrefix }${ foundAsset }`;
}

from rspack.

h-a-n-a avatar h-a-n-a commented on August 28, 2024

And in another custom plugin I tried:

compilation.getAsset( filePath ).source(); // didn't work
compilation.getAsset( filePath ).source.get(); // didn't work
compilation.getAsset( filePath ).source.toString(); // didn't work

Only this worked, but seems like it's a band-aid:

const fileMapSource = compilation.getAsset( filePath ).source._valueAsString; // did work

You were using the wrong API. Check out webpack-sources.

from rspack.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.