Skip to content

Commit

Permalink
make module configurable martonlederer#14
Browse files Browse the repository at this point in the history
  • Loading branch information
gsouf committed Dec 27, 2021
1 parent 32d928a commit a10b12c
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/esbuild-plugin-postcss2.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ postCssPlugin.default({
});
```

As per standard any file having `module` before the extension (ie `somefile.module.css`) will be treated as a module.
The option `fileIsModule` allows to override this behavior.

```js
postCssPlugin.default({
// pass a custom `fileIsModule` option to tell whether a file should be treated as a module
// in this example we want everything to be a module except file finishing with `global.css`
fileIsModule: (filepath) => !filepath.endsWith(".global.css")
});
```

### Preprocessors

To use preprocessors (`sass`, `scss`, `stylus`, `less`), just add the desired preprocessor as a `devDependency`:
Expand Down
11 changes: 8 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ interface PostCSSPluginOptions {
lessOptions?: Less.Options;
stylusOptions?: StylusRenderOptions;
writeToFile?: boolean;
fileIsModule?: (filename: string) => boolean;
}

interface CSSModule {
Expand All @@ -47,7 +48,8 @@ export const defaultOptions: PostCSSPluginOptions = {
sassOptions: {},
lessOptions: {},
stylusOptions: {},
writeToFile: true
writeToFile: true,
fileIsModule: null
};

const postCSSPlugin = ({
Expand All @@ -57,7 +59,8 @@ const postCSSPlugin = ({
sassOptions,
lessOptions,
stylusOptions,
writeToFile
writeToFile,
fileIsModule
}: PostCSSPluginOptions = defaultOptions): Plugin => ({
name: "postcss2",
setup(build) {
Expand Down Expand Up @@ -101,7 +104,9 @@ const postCSSPlugin = ({

const sourceExt = path.extname(sourceFullPath);
const sourceBaseName = path.basename(sourceFullPath, sourceExt);
const isModule = sourceBaseName.match(/\.module$/);
const isModule = fileIsModule
? fileIsModule(sourceFullPath)
: sourceBaseName.match(/\.module$/);
const sourceDir = path.dirname(sourceFullPath);

let tmpFilePath: string;
Expand Down
27 changes: 27 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,33 @@ describe("PostCSS esbuild tests", () => {
})
.catch(() => process.exit(1));
});

it("Works with custom module function", (done) => {
let testFilename = null;
build({
entryPoints: ["tests/basic.ts"],
bundle: true,
outdir: "dist",
plugins: [
postCSS.default({
plugins: [autoprefixer, postCssImport],
modules: true,
fileIsModule: (filename) => {
testFilename = filename;
return false;
}
})
]
})
.then(() => {
// ensure the proper filename was passed
assert.match(testFilename, /styles\/basic\.css/);
})
.catch((e) => {
console.error(e);
process.exit(1);
});
});
});

function test(entryPoint) {
Expand Down

0 comments on commit a10b12c

Please sign in to comment.