Skip to content

Commit

Permalink
feat: allow the additionalData to be async (#902)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored Nov 11, 2020
1 parent e4afcd7 commit 9d925ff
Show file tree
Hide file tree
Showing 44 changed files with 3,026 additions and 2,397 deletions.
15 changes: 1 addition & 14 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,4 @@
module.exports = {
root: true,
extends: ['@webpack-contrib/eslint-config-webpack', 'prettier'],
overrides: [
{
files: [
'test/watch/**/*.js',
'test/hmr/**/*.js',
'test/extractText/**/*.js',
'test/helpers/testLoader.js',
],
rules: {
strict: 'off',
},
},
],
extends: ["@webpack-contrib/eslint-config-webpack", "prettier"],
};
1 change: 0 additions & 1 deletion .prettierrc.js

This file was deleted.

154 changes: 95 additions & 59 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Then add the loader to your Webpack configuration. For example:
**app.js**

```js
import './style.scss';
import "./style.scss";
```

**style.scss**
Expand All @@ -65,11 +65,11 @@ module.exports = {
test: /\.s[ac]ss$/i,
use: [
// Creates `style` nodes from JS strings
'style-loader',
"style-loader",
// Translates CSS into CommonJS
'css-loader',
"css-loader",
// Compiles Sass to CSS
'sass-loader',
"sass-loader",
],
},
],
Expand All @@ -86,7 +86,7 @@ Webpack provides an [advanced mechanism to resolve files](https://webpack.js.org
The `sass-loader` uses Sass's custom importer feature to pass all queries to the Webpack resolving engine. Thus you can import your Sass modules from `node_modules`. Just prepend them with a `~` to tell Webpack that this is not a relative import:

```scss
@import '~bootstrap';
@import "~bootstrap";
```

It's important to only prepend it with `~`, because `~/` resolves to the home directory.
Expand Down Expand Up @@ -167,13 +167,13 @@ module.exports = {
{
test: /\.s[ac]ss$/i,
use: [
'style-loader',
'css-loader',
"style-loader",
"css-loader",
{
loader: 'sass-loader',
loader: "sass-loader",
options: {
// Prefer `dart-sass`
implementation: require('sass'),
implementation: require("sass"),
},
},
],
Expand Down Expand Up @@ -211,12 +211,12 @@ module.exports = {
{
test: /\.s[ac]ss$/i,
use: [
'style-loader',
'css-loader',
"style-loader",
"css-loader",
{
loader: 'sass-loader',
loader: "sass-loader",
options: {
implementation: require('sass'),
implementation: require("sass"),
sassOptions: {
fiber: false,
},
Expand All @@ -240,14 +240,14 @@ module.exports = {
{
test: /\.s[ac]ss$/i,
use: [
'style-loader',
'css-loader',
"style-loader",
"css-loader",
{
loader: 'sass-loader',
loader: "sass-loader",
options: {
implementation: require('sass'),
implementation: require("sass"),
sassOptions: {
fiber: require('fibers'),
fiber: require("fibers"),
},
},
},
Expand Down Expand Up @@ -293,14 +293,14 @@ module.exports = {
{
test: /\.s[ac]ss$/i,
use: [
'style-loader',
'css-loader',
"style-loader",
"css-loader",
{
loader: 'sass-loader',
loader: "sass-loader",
options: {
sassOptions: {
indentWidth: 4,
includePaths: ['absolute/path/a', 'absolute/path/b'],
includePaths: ["absolute/path/a", "absolute/path/b"],
},
},
},
Expand All @@ -322,24 +322,24 @@ module.exports = {
{
test: /\.s[ac]ss$/i,
use: [
'style-loader',
'css-loader',
"style-loader",
"css-loader",
{
loader: 'sass-loader',
loader: "sass-loader",
options: {
sassOptions: (loaderContext) => {
// More information about available properties https://webpack.js.org/api/loaders/
const { resourcePath, rootContext } = loaderContext;
const relativePath = path.relative(rootContext, resourcePath);

if (relativePath === 'styles/foo.scss') {
if (relativePath === "styles/foo.scss") {
return {
includePaths: ['absolute/path/c', 'absolute/path/d'],
includePaths: ["absolute/path/c", "absolute/path/d"],
};
}

return {
includePaths: ['absolute/path/a', 'absolute/path/b'],
includePaths: ["absolute/path/a", "absolute/path/b"],
};
},
},
Expand Down Expand Up @@ -372,15 +372,15 @@ module.exports = {
{
test: /\.s[ac]ss$/i,
use: [
'style-loader',
"style-loader",
{
loader: 'css-loader',
loader: "css-loader",
options: {
sourceMap: true,
},
},
{
loader: 'sass-loader',
loader: "sass-loader",
options: {
sourceMap: true,
},
Expand All @@ -405,14 +405,14 @@ module.exports = {
{
test: /\.s[ac]ss$/i,
use: [
'style-loader',
'css-loader',
"style-loader",
"css-loader",
{
loader: 'sass-loader',
loader: "sass-loader",
options: {
sourceMap: true,
sassOptions: {
outputStyle: 'compressed',
outputStyle: "compressed",
},
},
},
Expand Down Expand Up @@ -442,12 +442,12 @@ module.exports = {
{
test: /\.s[ac]ss$/i,
use: [
'style-loader',
'css-loader',
"style-loader",
"css-loader",
{
loader: 'sass-loader',
loader: "sass-loader",
options: {
additionalData: '$env: ' + process.env.NODE_ENV + ';',
additionalData: "$env: " + process.env.NODE_ENV + ";",
},
},
],
Expand All @@ -459,28 +459,64 @@ module.exports = {

#### `Function`

##### Sync

```js
module.exports = {
module: {
rules: [
{
test: /\.s[ac]ss$/i,
use: [
'style-loader',
'css-loader',
"style-loader",
"css-loader",
{
loader: 'sass-loader',
loader: "sass-loader",
options: {
additionalData: (content, loaderContext) => {
// More information about available properties https://webpack.js.org/api/loaders/
const { resourcePath, rootContext } = loaderContext;
const relativePath = path.relative(rootContext, resourcePath);

if (relativePath === 'styles/foo.scss') {
return '$value: 100px;' + content;
if (relativePath === "styles/foo.scss") {
return "$value: 100px;" + content;
}

return "$value: 200px;" + content;
},
},
},
],
},
],
},
};
```

##### Async

```js
module.exports = {
module: {
rules: [
{
test: /\.s[ac]ss$/i,
use: [
"style-loader",
"css-loader",
{
loader: "sass-loader",
options: {
additionalData: async (content, loaderContext) => {
// More information about available properties https://webpack.js.org/api/loaders/
const { resourcePath, rootContext } = loaderContext;
const relativePath = path.relative(rootContext, resourcePath);

if (relativePath === "styles/foo.scss") {
return "$value: 100px;" + content;
}

return '$value: 200px;' + content;
return "$value: 200px;" + content;
},
},
},
Expand Down Expand Up @@ -510,10 +546,10 @@ module.exports = {
{
test: /\.s[ac]ss$/i,
use: [
'style-loader',
'css-loader',
"style-loader",
"css-loader",
{
loader: 'sass-loader',
loader: "sass-loader",
options: {
webpackImporter: false,
},
Expand All @@ -539,7 +575,7 @@ There are two possibilities to extract a style sheet from the bundle:
**webpack.config.js**

```js
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

module.exports = {
module: {
Expand All @@ -548,11 +584,11 @@ module.exports = {
test: /\.s[ac]ss$/i,
use: [
// fallback to style-loader in development
process.env.NODE_ENV !== 'production'
? 'style-loader'
process.env.NODE_ENV !== "production"
? "style-loader"
: MiniCssExtractPlugin.loader,
'css-loader',
'sass-loader',
"css-loader",
"sass-loader",
],
},
],
Expand All @@ -561,8 +597,8 @@ module.exports = {
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// both options are optional
filename: '[name].css',
chunkFilename: '[id].css',
filename: "[name].css",
chunkFilename: "[id].css",
}),
],
};
Expand All @@ -578,21 +614,21 @@ To enable CSS source maps, you'll need to pass the `sourceMap` option to the `sa

```javascript
module.exports = {
devtool: 'source-map', // any "source-map"-like devtool is possible
devtool: "source-map", // any "source-map"-like devtool is possible
module: {
rules: [
{
test: /\.s[ac]ss$/i,
use: [
'style-loader',
"style-loader",
{
loader: 'css-loader',
loader: "css-loader",
options: {
sourceMap: true,
},
},
{
loader: 'sass-loader',
loader: "sass-loader",
options: {
sourceMap: true,
},
Expand Down
4 changes: 2 additions & 2 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ module.exports = (api) => {
return {
presets: [
[
'@babel/preset-env',
"@babel/preset-env",
{
targets: {
node: '10.13.0',
node: "10.13.0",
},
},
],
Expand Down
2 changes: 1 addition & 1 deletion commitlint.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
extends: ['@commitlint/config-conventional'],
extends: ["@commitlint/config-conventional"],
};
4 changes: 2 additions & 2 deletions husky.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
hooks: {
'pre-commit': 'lint-staged',
'commit-msg': 'commitlint -E HUSKY_GIT_PARAMS',
"pre-commit": "lint-staged",
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
},
};
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
testEnvironment: 'node',
testEnvironment: "node",
};
4 changes: 2 additions & 2 deletions lint-staged.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
'*.js': ['prettier --write', 'eslint --fix'],
'*.{json,md,yml,css,ts}': ['prettier --write'],
"*.js": ["eslint --fix", "prettier --write"],
"*.{json,md,yml,css,ts}": ["prettier --write"],
};
Loading

0 comments on commit 9d925ff

Please sign in to comment.