-
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for compiling
.less
stylesheets
- Loading branch information
1 parent
cb09f9b
commit a49e4f9
Showing
56 changed files
with
723 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import path from 'path'; | ||
|
||
/** @type {import('less') | undefined} */ | ||
let less; | ||
|
||
const lessFileLoader = resolve => | ||
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'; | ||
|
||
// Supply fake importer for relative resolution | ||
const importer = path.join(currentDirectory, 'fake.less'); | ||
const resolved = await resolve(file, importer, { skipSelf: true }); | ||
let resolvedId = resolved.id; | ||
|
||
// 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)] | ||
}; | ||
if (sourcemap) lessOptions.sourceMap = {}; | ||
|
||
return await less.render(code, lessOptions); | ||
} | ||
|
||
/** | ||
* @param {object} options | ||
* @param {boolean} options.sourcemap | ||
* @param {Set<string>} options.mergedAssets | ||
* @returns {import('rollup').Plugin} | ||
*/ | ||
export function lessPlugin({ sourcemap, mergedAssets }) { | ||
/** @type {Map<string, Set<string>>} */ | ||
const fileToBundles = new Map(); | ||
|
||
return { | ||
name: 'less', | ||
async transform(code, id) { | ||
if (!/\.less$/.test(id)) return; | ||
|
||
const result = await renderLess(code, { resolve: this.resolve.bind(this), sourcemap, id }); | ||
|
||
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; |
11 changes: 11 additions & 0 deletions
11
packages/wmr/test/fixtures/css-less-alias/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> |
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 '~/foo.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,5 @@ | ||
export default { | ||
alias: { | ||
'~/*': 'foo' | ||
} | ||
}; |
11 changes: 11 additions & 0 deletions
11
packages/wmr/test/fixtures/css-less-file-error/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-file-error/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 'non-existing.less'; |
Oops, something went wrong.