|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// Flags: --experimental-modules |
| 4 | +// This test ensures that JavaScript file that includes |
| 5 | +// a reserved Windows word can be loaded as ESM module |
| 6 | + |
| 7 | +const common = require('../common'); |
| 8 | +const tmpdir = require('../common/tmpdir'); |
| 9 | +const assert = require('assert'); |
| 10 | +const fs = require('fs').promises; |
| 11 | +const path = require('path'); |
| 12 | + |
| 13 | +const imp = (file) => { |
| 14 | + return import(path.relative(__dirname, file).replace(/\\/g, '/')); |
| 15 | +}; |
| 16 | + |
| 17 | +(async () => { |
| 18 | + const tmp = tmpdir.path; |
| 19 | + await fs.mkdir(tmp).catch(() => {}); |
| 20 | + const rel = (file) => path.join(tmp, file); |
| 21 | + |
| 22 | + { // Load a single script |
| 23 | + const file = rel('con.mjs'); |
| 24 | + await fs.writeFile(file, 'export default "ok"'); |
| 25 | + assert.strictEqual((await imp(file)).default, 'ok'); |
| 26 | + await fs.unlink(file); |
| 27 | + } |
| 28 | + |
| 29 | + { // Load a module |
| 30 | + const entry = rel('entry.mjs'); |
| 31 | + const nmDir = rel('node_modules'); |
| 32 | + const mDir = rel('node_modules/con'); |
| 33 | + const pkg = rel('node_modules/con/package.json'); |
| 34 | + const script = rel('node_modules/con/index.mjs'); |
| 35 | + |
| 36 | + await fs.writeFile(entry, 'export {default} from "con"'); |
| 37 | + await fs.mkdir(nmDir); |
| 38 | + await fs.mkdir(mDir); |
| 39 | + await fs.writeFile(pkg, '{"main":"index.mjs"}'); |
| 40 | + await fs.writeFile(script, 'export default "ok"'); |
| 41 | + |
| 42 | + assert.strictEqual((await imp(entry)).default, 'ok'); |
| 43 | + await fs.unlink(script); |
| 44 | + await fs.unlink(pkg); |
| 45 | + await fs.rmdir(mDir); |
| 46 | + await fs.rmdir(nmDir); |
| 47 | + await fs.unlink(entry); |
| 48 | + } |
| 49 | +})().then(common.mustCall()); |
0 commit comments