File tree 5 files changed +70
-1
lines changed
fixtures/es-modules/package-type-module
5 files changed +70
-1
lines changed Original file line number Diff line number Diff line change 2
2
3
3
const {
4
4
RegExpPrototypeExec,
5
+ Uint8Array,
5
6
} = primordials ;
6
7
const { getOptionValue } = require ( 'internal/options' ) ;
7
8
9
+ const { closeSync, openSync, readSync } = require ( 'fs' ) ;
10
+
8
11
const experimentalWasmModules = getOptionValue ( '--experimental-wasm-modules' ) ;
9
12
10
13
const extensionFormatMap = {
@@ -35,7 +38,27 @@ function mimeToFormat(mime) {
35
38
return null ;
36
39
}
37
40
41
+ function guessExtensionlessModule ( url ) {
42
+ if ( ! experimentalWasmModules )
43
+ return 'module' ;
44
+
45
+ const magic = new Uint8Array ( 4 ) ;
46
+ let fd ;
47
+ try {
48
+ fd = openSync ( url ) ;
49
+ readSync ( fd , magic ) ;
50
+ if ( magic [ 0 ] === 0x00 && magic [ 1 ] === 0x61 && magic [ 2 ] === 0x73 && magic [ 3 ] === 0x6d ) {
51
+ return 'wasm' ;
52
+ }
53
+ } finally {
54
+ if ( fd ) closeSync ( fd ) ;
55
+ }
56
+
57
+ return 'module' ;
58
+ }
59
+
38
60
module . exports = {
39
61
extensionFormatMap,
62
+ guessExtensionlessModule,
40
63
mimeToFormat,
41
64
} ;
Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ const {
10
10
const { getOptionValue } = require ( 'internal/options' ) ;
11
11
const {
12
12
extensionFormatMap,
13
+ guessExtensionlessModule,
13
14
mimeToFormat,
14
15
} = require ( 'internal/modules/esm/formats' ) ;
15
16
@@ -73,10 +74,17 @@ function extname(url) {
73
74
*/
74
75
function getFileProtocolModuleFormat ( url , context , ignoreErrors ) {
75
76
const ext = extname ( url ) ;
76
- if ( ext === '.js' || ext === '' ) {
77
+ if ( ext === '.js' ) {
77
78
return getPackageType ( url ) === 'module' ? 'module' : 'commonjs' ;
78
79
}
79
80
81
+ if ( ext === '' ) {
82
+ if ( getPackageType ( url ) === 'module' ) {
83
+ return guessExtensionlessModule ( url ) ;
84
+ }
85
+ return 'commonjs' ;
86
+ }
87
+
80
88
const format = extensionFormatMap [ ext ] ;
81
89
if ( format ) return format ;
82
90
Original file line number Diff line number Diff line change
1
+ import { strictEqual } from 'assert' ;
2
+
3
+ export function jsFn ( ) {
4
+ state = 'WASM JS Function Executed' ;
5
+ return 42 ;
6
+ }
7
+
8
+ export let state = 'JS Function Executed' ;
9
+
10
+ export function jsInitFn ( ) {
11
+ strictEqual ( state , 'JS Function Executed' ) ;
12
+ state = 'WASM Start Executed' ;
13
+ }
Original file line number Diff line number Diff line change
1
+ // Flags: --experimental-wasm-modules
2
+ import { mustCall } from '../common/index.mjs' ;
3
+ import { path } from '../common/fixtures.mjs' ;
4
+ import { strictEqual } from 'assert' ;
5
+ import { spawn } from 'child_process' ;
6
+
7
+ {
8
+ const entry = path ( '/es-modules/package-type-module/noext-wasm' ) ;
9
+
10
+ // Run a module that does not have extension.
11
+ // This is to ensure that "type": "module" applies to extensionless files.
12
+
13
+ const child = spawn ( process . execPath , [ '--experimental-wasm-modules' , entry ] ) ;
14
+
15
+ let stdout = '' ;
16
+ child . stdout . setEncoding ( 'utf8' ) ;
17
+ child . stdout . on ( 'data' , ( data ) => {
18
+ stdout += data ;
19
+ } ) ;
20
+ child . on ( 'close' , mustCall ( ( code , signal ) => {
21
+ strictEqual ( code , 0 ) ;
22
+ strictEqual ( signal , null ) ;
23
+ strictEqual ( stdout , '' ) ;
24
+ } ) ) ;
25
+ }
You can’t perform that action at this time.
0 commit comments