-
Notifications
You must be signed in to change notification settings - Fork 26.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add basic webpack loader support (vercel/turborepo#3284)
add the minimum to run a simple raw-loader (see test case) * add `experimental.turbopackWebpackLoaders` to next.config.js * key is extension like `".mdx"`, value is an array of loaders like `["mdx-loader"]`
- Loading branch information
Showing
22 changed files
with
449 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
crates/next-dev/tests/integration/turbopack/basic/webpack-loaders/hello.raw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello World |
5 changes: 5 additions & 0 deletions
5
crates/next-dev/tests/integration/turbopack/basic/webpack-loaders/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import source from "./hello.raw"; | ||
|
||
it("runs a simple loader", () => { | ||
expect(source).toBe("Hello World"); | ||
}); |
7 changes: 7 additions & 0 deletions
7
crates/next-dev/tests/integration/turbopack/basic/webpack-loaders/next.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module.exports = { | ||
experimental: { | ||
turbopackWebpackLoaders: { | ||
".raw": ["raw-loader"], | ||
}, | ||
}, | ||
}; |
3 changes: 3 additions & 0 deletions
3
...xt-dev/tests/integration/turbopack/basic/webpack-loaders/node_modules/raw-loader/index.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"name": "@vercel/turbopack-node", | ||
"version": "0.0.0", | ||
"description": "turbopack node runtime", | ||
"license": "UNLICENSED", | ||
"private": true, | ||
"scripts": { | ||
"check": "tsc --noEmit" | ||
}, | ||
"dependencies": { | ||
"loader-runner": "^4.3.0" | ||
}, | ||
"devDependencies": { | ||
"@types/loader-runner": "^2.2.4", | ||
"@types/node": "^18.11.11" | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
crates/turbopack-node/js/src/transforms/webpack-loaders.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
declare const __turbopack_external_require__: (id: string) => any; | ||
|
||
import type { Ipc } from "../ipc/evaluate"; | ||
import { | ||
relative, | ||
isAbsolute, | ||
sep, | ||
dirname, | ||
resolve as pathResolve, | ||
} from "path"; | ||
|
||
const { runLoaders } = __turbopack_external_require__( | ||
"loader-runner" | ||
) as typeof import("loader-runner"); | ||
|
||
const contextDir = process.cwd(); | ||
const toPath = (file: string) => { | ||
const relPath = relative(contextDir, file); | ||
if (isAbsolute(relPath)) { | ||
throw new Error( | ||
`Cannot depend on path (${file}) outside of root directory (${contextDir})` | ||
); | ||
} | ||
return sep !== "/" ? relPath.replaceAll(sep, "/") : relPath; | ||
}; | ||
|
||
const transform = (ipc: Ipc, content: string, name: string, loaders: any[]) => { | ||
return new Promise((resolve, reject) => { | ||
const resource = pathResolve(contextDir, name); | ||
const resourceDir = dirname(resource); | ||
// TODO this should be handled in turbopack instead to ensure it's watched | ||
loaders = loaders.map((loader: any) => { | ||
return require.resolve(loader, { paths: [resourceDir] }); | ||
}); | ||
runLoaders( | ||
{ | ||
resource, | ||
context: { | ||
rootContext: contextDir, | ||
}, | ||
loaders, | ||
readResource: (_filename, callback) => { | ||
// TODO assuming the filename === resource, but loaders might change that | ||
callback(null, Buffer.from(content, "utf-8")); | ||
}, | ||
}, | ||
(err, result) => { | ||
if (err) return reject(err); | ||
for (const dep of result.contextDependencies) { | ||
ipc.send({ | ||
type: "dirDependency", | ||
path: toPath(dep), | ||
glob: "**", | ||
}); | ||
} | ||
for (const dep of result.fileDependencies) { | ||
ipc.send({ | ||
type: "fileDependency", | ||
path: toPath(dep), | ||
}); | ||
} | ||
if (!result.result) return reject(new Error("No result from loaders")); | ||
const [source, map] = result.result; | ||
resolve({ source, map }); | ||
} | ||
); | ||
}); | ||
}; | ||
|
||
export { transform as default }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"compilerOptions": { | ||
// type checking | ||
"strict": true, | ||
"noFallthroughCasesInSwitch": true, | ||
"skipLibCheck": true, | ||
|
||
// interop constraints | ||
"allowSyntheticDefaultImports": true, | ||
"esModuleInterop": true, | ||
|
||
// js support | ||
"allowJs": true, | ||
"checkJs": false, | ||
|
||
// environment | ||
"lib": ["ESNext"], | ||
"target": "esnext", | ||
|
||
// modules | ||
"baseUrl": ".", | ||
"module": "esnext", | ||
"moduleResolution": "node", | ||
|
||
// emit | ||
"noEmit": true, | ||
"stripInternal": true | ||
}, | ||
"include": ["src"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
pub mod postcss; | ||
mod util; | ||
pub mod webpack; |
Oops, something went wrong.