-
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #806 from preactjs/less
- Loading branch information
Showing
70 changed files
with
821 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'wmr': minor | ||
--- | ||
|
||
Add support for `.less` stylesheets. |
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
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,116 @@ | ||
import path from 'path'; | ||
import { createCodeFrame } from 'simple-code-frame'; | ||
import { resolveAlias } from '../lib/aliasing.js'; | ||
|
||
/** @type {import('less') | undefined} */ | ||
let less; | ||
|
||
const lessFileLoader = (resolve, root) => | ||
class LessPluginWmr { | ||
static install(less, pluginManager) { | ||
class LessFileManagerWmr extends less.FileManager { | ||
async loadFile(filename, currentDirectory, options, environment) { | ||
let file = filename; | ||
if (!file.endsWith('.less')) file = file + '.less'; | ||
|
||
if (!path.isAbsolute(currentDirectory)) { | ||
currentDirectory = path.join(root, currentDirectory); | ||
} | ||
|
||
// Supply fake importer for relative resolution | ||
const importer = path.join(currentDirectory, 'fake.less'); | ||
const resolved = await resolve(file, importer, { skipSelf: true }); | ||
|
||
let resolvedId = resolved ? resolved.id : file; | ||
|
||
// Support bare imports: `@import "bar"` | ||
if (!path.isAbsolute(resolvedId)) { | ||
resolvedId = path.join(currentDirectory, filename); | ||
} | ||
|
||
// Pass loading to less | ||
return less.FileManager.prototype.loadFile.call(this, resolvedId, '', options, environment); | ||
} | ||
} | ||
pluginManager.addFileManager(new LessFileManagerWmr()); | ||
} | ||
}; | ||
|
||
/** | ||
* @param {string} code | ||
* @param {{id: string, resolve: any, sourcemap: boolean }} options | ||
* @returns {Promise<{ css: string, map?: string, imports: string[] }>} | ||
*/ | ||
export async function renderLess(code, { id, resolve, sourcemap }) { | ||
if (!less) { | ||
if (process.env.DISABLE_LESS !== 'true') { | ||
const mod = await import('less'); | ||
less = mod.default || mod; | ||
} | ||
|
||
if (!less) { | ||
throw new Error(`Please install less to compile "*.less" files:\n npm i -D less`); | ||
} | ||
} | ||
|
||
const lessOptions = { | ||
filename: id, | ||
plugins: [lessFileLoader(resolve, path.dirname(id))] | ||
}; | ||
if (sourcemap) lessOptions.sourceMap = {}; | ||
|
||
try { | ||
return await less.render(code, lessOptions); | ||
} catch (err) { | ||
if (err.extract && 'line' in err && 'column' in err) { | ||
const code = err.extract.filter(l => l !== undefined).join('\n'); | ||
err.codeFrame = createCodeFrame(code, err.line - 1, err.column); | ||
} | ||
|
||
throw err; | ||
} | ||
} | ||
|
||
/** | ||
* @param {object} options | ||
* @param {boolean} options.sourcemap | ||
* @param {Set<string>} options.mergedAssets | ||
* @param {Record<string, string>} options.alias | ||
* @returns {import('rollup').Plugin} | ||
*/ | ||
export function lessPlugin({ sourcemap, mergedAssets, alias }) { | ||
/** @type {Map<string, Set<string>>} */ | ||
const fileToBundles = new Map(); | ||
|
||
return { | ||
name: 'less', | ||
async transform(code, id) { | ||
if (!/\.less$/.test(id)) return; | ||
|
||
// Use absolute file paths, otherwise nested alias resolution | ||
// fails in less | ||
const file = resolveAlias(alias, id); | ||
const result = await renderLess(code, { resolve: this.resolve.bind(this), sourcemap, id: file }); | ||
|
||
for (let file of result.imports) { | ||
mergedAssets.add(file); | ||
|
||
if (!fileToBundles.has(file)) { | ||
fileToBundles.set(file, new Set()); | ||
} | ||
this.addWatchFile(file); | ||
// @ts-ignore | ||
fileToBundles.get(file).add(id); | ||
} | ||
|
||
return { | ||
code: result.css, | ||
map: result.map || null | ||
}; | ||
}, | ||
watchChange(id) { | ||
const bundle = fileToBundles.get(id); | ||
if (bundle) return Array.from(bundle); | ||
} | ||
}; | ||
} |
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
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 @@ | ||
@color: red; |
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 '/bar.less'; | ||
|
||
h1 { | ||
color: @color; | ||
} |
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,14 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Document</title> | ||
<link rel="stylesheet" href="./foo.less" /> | ||
</head> | ||
<body> | ||
<h1>hello</h1> | ||
<script type="module" src="index.js"></script> | ||
</body> | ||
</html> |
Empty file.
11 changes: 11 additions & 0 deletions
11
packages/wmr/test/fixtures/css-less-alias-relative/public/index.html
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,11 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title>Less</title> | ||
<link rel="stylesheet" href="style.less" /> | ||
</head> | ||
<body> | ||
<h1>Less</h1> | ||
</body> | ||
</html> |
1 change: 1 addition & 0 deletions
1
packages/wmr/test/fixtures/css-less-alias-relative/public/style.less
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 @@ | ||
@import '../src/foo.less'; |
1 change: 1 addition & 0 deletions
1
packages/wmr/test/fixtures/css-less-alias-relative/src/bar.less
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 @@ | ||
@color: red; |
5 changes: 5 additions & 0 deletions
5
packages/wmr/test/fixtures/css-less-alias-relative/src/foo.less
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 './bar.less'; | ||
|
||
h1 { | ||
color: @color; | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/wmr/test/fixtures/css-less-alias-relative/wmr.config.mjs
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 @@ | ||
export default { | ||
alias: { | ||
'src/*': '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 |
---|---|---|
@@ -0,0 +1 @@ | ||
@color: red; |
Oops, something went wrong.