@@ -8,7 +8,12 @@ import { createCompilerHost } from "./compiler-host";
8
8
import { mapAscOptionsToArgs , Options } from "./options" ;
9
9
import { AssemblyScriptError } from "./error" ;
10
10
import * as schema from "./schema.json" ;
11
- import { addErrorToModule , addWarningToModule } from "./webpack" ;
11
+ import {
12
+ addErrorToModule ,
13
+ addWarningToModule ,
14
+ isModuleCompiledToWasm ,
15
+ markModuleAsCompiledToWasm ,
16
+ } from "./webpack" ;
12
17
13
18
const SUPPORTED_EXTENSIONS = [ ".wasm" , ".js" ] ;
14
19
@@ -21,7 +26,7 @@ interface LoaderOptions {
21
26
type CompilerOptions = OptionObject ;
22
27
23
28
// eslint-disable-next-line @typescript-eslint/no-explicit-any
24
- function loader ( this : any , buffer : Buffer ) {
29
+ function loader ( this : any , content : string , map ?: any , meta ?: any ) {
25
30
const options = getOptions ( this ) ;
26
31
validate ( schema as Schema , options , {
27
32
name : "AssemblyScript Loader" ,
@@ -43,6 +48,35 @@ function loader(this: any, buffer: Buffer) {
43
48
...userAscOptions
44
49
} = options as LoaderOptions & CompilerOptions ;
45
50
51
+ if ( isModuleCompiledToWasm ( module ) ) {
52
+ // skip asc compilation - forward request to a fallback loader
53
+ return callback ( null , content , map , meta ) ;
54
+ }
55
+
56
+ if ( ! SUPPORTED_EXTENSIONS . some ( ( extension ) => name . endsWith ( extension ) ) ) {
57
+ throw new Error (
58
+ `Unsupported extension in name: "${ name } " option in as-loader. ` +
59
+ `Supported extensions are ${ SUPPORTED_EXTENSIONS . join ( ", " ) } `
60
+ ) ;
61
+ }
62
+
63
+ if ( fallback ) {
64
+ if ( module . type ?. startsWith ( "webassembly" ) ) {
65
+ throw new Error (
66
+ `Cannot use fallback option together with module type "${ module . type } ". ` +
67
+ `Use standard module type or disable fallback option.`
68
+ ) ;
69
+ } else if ( raw ) {
70
+ throw new Error ( `Cannot use fallback option together with raw option.` ) ;
71
+ }
72
+ }
73
+
74
+ if ( name . endsWith ( ".js" ) ) {
75
+ throw new Error (
76
+ `Cannot use .js extension directly. Please use fallback option instead.`
77
+ ) ;
78
+ }
79
+
46
80
const ascOptions : Options = {
47
81
// default options
48
82
// when user imports wasm with webassembly type, it's not possible to pass env
@@ -58,44 +92,20 @@ function loader(this: any, buffer: Buffer) {
58
92
...userAscOptions ,
59
93
} ;
60
94
61
- if ( ! SUPPORTED_EXTENSIONS . some ( ( extension ) => name . endsWith ( extension ) ) ) {
62
- throw new Error (
63
- `Unsupported extension in name: "${ name } " option in as-loader. ` +
64
- `Supported extensions are ${ SUPPORTED_EXTENSIONS . join ( ", " ) } `
65
- ) ;
66
- }
67
-
68
- if ( bind && name . endsWith ( ".wasm" ) ) {
95
+ if ( bind ) {
69
96
// overwrite options for bind
70
97
ascOptions . exportRuntime = true ;
71
98
ascOptions . transform = "as-bind" ;
72
99
}
73
100
74
- if ( name . endsWith ( ".js" ) ) {
75
- // overwrite options for js
76
- ascOptions . runtime = "stub" ;
77
- ascOptions . exportRuntime = false ;
78
- }
79
-
80
101
const shouldGenerateSourceMap = this . sourceMap ;
81
102
const baseDir = path . dirname ( this . resourcePath ) ;
82
103
const outFileName = interpolateName ( this , name , {
83
104
context,
84
- content : buffer . toString ( ) ,
105
+ content,
85
106
} ) ;
86
107
const sourceMapFileName = outFileName + ".map" ;
87
108
88
- if ( fallback ) {
89
- if ( module . type ?. startsWith ( "webassembly" ) ) {
90
- throw new Error (
91
- `Cannot use fallback option together with module type "${ module . type } ". ` +
92
- `Use standard module type or disable fallback option.`
93
- ) ;
94
- } else if ( raw ) {
95
- throw new Error ( `Cannot use fallback option together with raw option.` ) ;
96
- }
97
- }
98
-
99
109
const host = createCompilerHost ( this ) ;
100
110
101
111
if ( shouldGenerateSourceMap ) {
@@ -176,6 +186,8 @@ function loader(this: any, buffer: Buffer) {
176
186
}
177
187
178
188
if ( outFileName . endsWith ( ".wasm" ) ) {
189
+ markModuleAsCompiledToWasm ( module ) ;
190
+
179
191
if ( module . type ?. startsWith ( "webassembly" ) || raw ) {
180
192
// uses module type: "webassembly/sync" or "webasssembly/async" or raw: true -
181
193
// return binary instead of emitting files
@@ -266,6 +278,5 @@ function loader(this: any, buffer: Buffer) {
266
278
callback ( error ) ;
267
279
} ) ;
268
280
}
269
- loader . raw = true ;
270
281
271
282
module . exports = loader ;
0 commit comments