Comments (5)
Seems using relative paths working, with absolute paths there is no way man (I'll try one last time to see if there's any luck):
F:\work\projects\quini\GitHub\antfu\esbuild-node-loader>pnpm run test
> [email protected] test F:\work\projects\quini\GitHub\antfu\esbuild-node-loader
> node --experimental-loader ./loader.mjs test/entry.ts
(node:23624) ExperimentalWarning: --experimental-loader is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
• • • • • • (6 / 6)
Total: 6
Passed: 6
Skipped: 0
Duration: 1146.71ms
test/entry.ts changes
@@ -1,12 +1,20 @@
import assert from 'assert'
import { test } from 'uvu'
import execa from 'execa'
+import { relative } from 'path'
+
+const cwd = process.cwd()
+function relativize(path: string) {
+ const url = relative(cwd, path)
+ // console.log(`FROM ${path} => ${url}`)
+ return `./${url}`
+}
test('register', async() => {
const { stdout } = await execa('node', [
'--experimental-loader',
- `${process.cwd()}/loader.mjs`,
- `${process.cwd()}/test/fixture.ts`,
+ relativize(`${cwd}/loader.mjs`),
+ relativize(`${cwd}/test/fixture.ts`),
])
assert(stdout === 'text')
})
@@ -14,8 +22,8 @@
test('register2', async() => {
const { stdout } = await execa('node', [
'--experimental-loader',
- `${process.cwd()}/loader.mjs`,
- `${process.cwd()}/test/fixture.arrowFunction.ts`,
+ relativize(`${cwd}/loader.mjs`),
+ relativize(`${cwd}/test/fixture.arrowFunction.ts`),
])
assert(stdout === 'hello from ts')
})
@@ -23,8 +31,8 @@
test('register3', async() => {
const { stdout } = await execa('node', [
'--experimental-loader',
- `${process.cwd()}/loader.mjs`,
- `${process.cwd()}/test/fixture.import.ts`,
+ relativize(`${cwd}/loader.mjs`),
+ relativize(`${cwd}/test/fixture.import.ts`),
])
assert(stdout === 'export')
})
@@ -32,8 +40,8 @@
test('register cjs', async() => {
const { stdout } = await execa('node', [
'--experimental-loader',
- `${process.cwd()}/loader.mjs`,
- `${process.cwd()}/test/fixture.cjs.ts`,
+ relativize(`${cwd}/loader.mjs`),
+ relativize(`${cwd}/test/fixture.cjs.ts`),
])
assert(stdout === 'fs imported')
})
@@ -41,8 +49,8 @@
test('package type module', async() => {
const { stdout } = await execa('node', [
'--experimental-loader',
- `${process.cwd()}/loader.mjs`,
- `${process.cwd()}/test/fixture-type-module/index.js`,
+ relativize(`${cwd}/loader.mjs`),
+ relativize(`${cwd}/test/fixture-type-module/index.js`),
])
assert(stdout === 'foo')
})
@@ -50,8 +58,8 @@
test('import type module', async() => {
const { stdout } = await execa('node', [
'--experimental-loader',
- `${process.cwd()}/loader.mjs`,
- `${process.cwd()}/test/import-mjs/index.js`,
+ relativize(`${cwd}/loader.mjs`),
+ relativize(`${cwd}/test/import-mjs/index.js`),
])
assert(stdout === 'foo')
})
loader.mjs changes
@@ -1,8 +1,16 @@
import { URL, pathToFileURL, fileURLToPath } from 'url'
import { transformSync } from 'esbuild'
import fs from 'fs'
+import { relative } from 'path'
-const baseURL = pathToFileURL(`${process.cwd()}/`).href
+const cwd = process.cwd()
+function relativize(path) {
+ const url = relative(cwd, path)
+ //console.log(`FROM ${path} => ${url}`)
+ return `./${url}`
+}
+
+const baseURL = pathToFileURL(relativize(`${cwd}/`)).href
const isWindows = process.platform === "win32"
const extensionsRegex = /\.ts$/
@@ -46,12 +54,12 @@
const { url, format } = context
if (extensionsRegex.test(url)) {
- let filename = fileURLToPath(url)
- if (isWindows)
- filename = filename.slice(1)
+ // let filename = fileURLToPath(url)
+ // if (isWindows)
+ // filename = filename.slice(1)
const { code: js, warnings, map: jsSourceMap } = transformSync(source.toString(), {
- sourcefile: filename,
+ sourcefile: url,
sourcemap: 'both',
loader: 'ts',
target: 'esnext',
from esbuild-node-loader.
@antfu Working, just modify transformSource
from loader.mjs
:
Adjust filename
assigment:
let filename = url
if (!isWindows)
filename = fileURLToPath(url)
To make the tests working you need to use relativize
from previous comment.
transformSource
code:
export function transformSource(source, context, defaultTransformSource) {
const { url, format } = context
if (extensionsRegex.test(url)) {
let filename = url
if (!isWindows)
filename = fileURLToPath(url)
const { code: js, warnings, map: jsSourceMap } = transformSync(source.toString(), {
sourcefile: filename,
sourcemap: 'both',
loader: 'ts',
target: 'esnext',
format: format === 'module' ? 'esm' : 'cjs',
})
if (warnings && warnings.length > 0) {
for (const warning of warnings) {
console.log(warning.location)
console.log(warning.text)
}
}
return {
source: js,
}
}
// Let Node.js handle all other sources.
return defaultTransformSource(source, context, defaultTransformSource)
}
from esbuild-node-loader.
Maybe you need to check if script is provided with absolute path and then relativize from cwd
: using esno
I've never seen provide scripts with absolute paths, always with relative paths, I think if also working previous fix on linux/macosx, just include a warning/hint for windows users to use always relative paths to root directory.
from esbuild-node-loader.
Ideally I think we would like to have absolute path working as well
from esbuild-node-loader.
Found these 2 issues, I have tried with no luck:
from esbuild-node-loader.
Related Issues (20)
- Can it work with --experimental-specifier-resolution=node ? HOT 1
- bug ERR_UNSUPPORTED_DIR_IMPORT
- ERR_INVALID_FILE_URL_HOST with --experimental-specifier-resolution=node HOT 1
- Support file extension ".json" HOT 1
- Error: the URL must be of a scheme file HOT 3
- Support for `.js` extension HOT 2
- esbuild plugin support HOT 6
- Invalid loader: "mts"
- A mode that disallows .ts and no extension files HOT 2
- Support file loader? HOT 1
- Resolve tsconfig from current working dir
- DeprecationWarning: Obsolete loader hook(s) supplied and will be ignored: getFormat, transformSource
- [Refactor] Using esbuild's new resolve api
- Update to esbuild 0.14.11
- Incorrect line numbers in stack traces
- Why `https` import support?
- Unknown file extension ".tsx" HOT 4
- Question about importing ts files with ESM .js imports HOT 4
- tsconfig paths support? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from esbuild-node-loader.