Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

as-loader

Package Overview
Dependencies
Maintainers
1
Versions
46
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

as-loader

AssemblyScript loader for webpack

  • 0.2.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
27
increased by575%
Maintainers
1
Weekly downloads
 
Created
Source
AssemblyScript logo webpack logo

as-loader

AssemblyScript loader for webpack

⚠️ In development ⚠️

npm version build status

Installation

This loader requires minimum AssemblyScript 0.18, Node.js 12 and webpack 4 or webpack 5

# with npm
npm install --save-dev as-loader

# with yarn
yarn add --dev as-loader

The minimal webpack.config.js:

module.exports = {
  entry: "src/index.ts",
  resolve: {
    extensions: [".ts", ".js"],
  },
  module: {
    rules: [
      {
        test: /\.ts$/,
        include: path.resolve(__dirname, "src/assembly"),
        loader: "as-loader",
        options: {
          // optional loader and compiler options
        }
      },
      {
        test: /\.ts$/,
        exclude: path.resolve(__dirname, "src/assembly"),
        loader: "ts-loader",
      },
    ],
  },
};

Usage

By default, the loader emits .wasm file (+ .wasm.map if source maps are enabled) and creates CommonJS module that exports URL to the emitted .wasm file.

If you enable fallback option, the loader will also emit .js file (+ .js.map if source maps are enabled) and will expose async fallback() function which dynamically imports fallback module.

To simplify loading logic, you can use as-loader/runtime/ loaders which uses @assemblyscript/loader under the hood. Available loaders are: web, node. These loaders will check for WebAssembly support, and will use fallback if available. They will also ensure correct typings.

import * as myModule from "./assembly/myModule";
import { instantiate } from "as-loader/runtime/web";

async function loadAndRun() {
  const myModuleInstance = await instantiate(myModule);

  myModuleInstance.exports.myFunction(100);
}

loadAndRun();
Alternatively, you can use exported URL directly:
import * as myModule from "./assembly/myModule";
import { instantiate } from "@assemblyscript/loader";

async function loadAndRun() {
  const myModuleInstance = await instantiate<typeof myModule>(
    // workaround for TypeScript
    fetch((myModule as unknown) as string)
  );

  myModuleInstance.exports.myFunction(100);
}

loadAndRun();

Options

Loader Options
NameTypeDescription
namestringOutput asset name template, [name].[contenthash].wasm by default.
rawbooleanIf true, returns binary instead of emitting file. Use for chaining with other loaders.
fallbackbooleanIf true, creates additional JavaScript file which can be used if WebAssembly is not supported.
Compiler Options

Options passed to the AssemblyScript compiler.

NameTypeDescription
optimizeLevelnumberHow much to focus on optimizing code. [0-3]
shrinkLevelnumberHow much to focus on shrinking code size. [0-2]
coveragebooleanRe-optimizes until no further improvements can be made.
noAssertbooleanReplaces assertions with just their value without trapping.
runtimestringSpecifies the runtime variant to include in the program. Available runtimes are: "full", "half", "stub", "none"
debugbooleanEnables debug information in emitted binaries.
trapModestringSets the trap mode to use. Available modes are: "allow", "clamp", "js"
noValidatebooleanSkips validating the module using Binaryen.
importMemorybooleanImports the memory provided as 'env.memory'.
noExportMemorybooleanDoes not export the memory as 'memory'.
initialMemorynumberSets the initial memory size in pages.
maximumMemorynumberSets the maximum memory size in pages.
sharedMemorybooleanDeclare memory as shared. Requires maximumMemory.
importTablebooleanImports the function table provided as 'env.table'.
exportTablebooleanExports the function table as 'table'.
explicitStartbooleanExports an explicit '_start' function to call.
enablestring[]Enables WebAssembly features being disabled by default. Available features are: "sign-extension", "bulk-memory", "simd", "threads", "reference-types"
disablestring[]Disables WebAssembly features being enabled by default. Available features are: "mutable-globals"
lowMemoryLimitbooleanEnforces very low (<64k) memory constraints.
memoryBasenumberSets the start offset of emitted memory segments.
tableBasenumberSets the start offset of emitted table elements.

License

MIT

Keywords

FAQs

Package last updated on 11 Mar 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc