Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[loader-load-themed-styles] Remove the namedExport option. #3657

Merged
merged 2 commits into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@microsoft/loader-load-themed-styles",
"comment": "Remove the namedExport option.",
"type": "major"
}
],
"packageName": "@microsoft/loader-load-themed-styles"
}
17 changes: 0 additions & 17 deletions webpack/loader-load-themed-styles/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ var css = require("@microsoft/loader-load-themed-styles!css!./file.css");
{
loader: "@microsoft/loader-load-themed-styles", // creates style nodes from JS strings
options: {
namedExport: 'default',
async: false
}
},
Expand Down Expand Up @@ -60,22 +59,6 @@ var css = require("@microsoft/loader-load-themed-styles!css!./file.css");

## Options

### `namedExport` (string, defaults to `undefined`)

By default, css modules will be exported as a commonjs export:

```js
module.exports = { ... };
```

To override this, you may provide a named export to export to a specifically named thing. This
is useful in exporting as the default in es6 module import scenarios. For example, providing
"default" for the named export will output this:

```js
module.exports.default = { ... };
```

### `async` (boolean, defaults to `false`)

By default, `@microsoft/load-themed-styles` loads styles synchronously. This can have adverse performance effects
Expand Down
18 changes: 6 additions & 12 deletions webpack/loader-load-themed-styles/src/LoadThemedStylesLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ const loadedThemedStylesPath: string = require.resolve('@microsoft/load-themed-s
* @public
*/
export interface ILoadThemedStylesLoaderOptions {
/**
* If this parameter is specified, override the name of the value exported from this loader. This is useful in
* exporting as the default in es6 module import scenarios. See the README for more information.
*/
namedExport?: string;

/**
* If this parameter is set to "true," the "loadAsync" parameter is set to true in the call to loadStyles.
* Defaults to false.
Expand Down Expand Up @@ -63,13 +57,13 @@ export class LoadThemedStylesLoader {
}

public static pitch(this: loader.LoaderContext, remainingRequest: string): string {
const { namedExport, async = false }: ILoadThemedStylesLoaderOptions = loaderUtils.getOptions(this) || {};

let exportName: string = 'module.exports';
if (namedExport) {
exportName += `.${namedExport}`;
const options: ILoadThemedStylesLoaderOptions = loaderUtils.getOptions(this) || {};
if ((options as Record<string, unknown>).namedExport) {
throw new Error('The "namedExport" option has been removed.');
}

const { async = false } = options;

return [
`var content = require(${loaderUtils.stringifyRequest(this, '!!' + remainingRequest)});`,
`var loader = require(${JSON.stringify(LoadThemedStylesLoader._loadedThemedStylesPath)});`,
Expand All @@ -79,7 +73,7 @@ export class LoadThemedStylesLoader {
'// add the styles to the DOM',
`for (var i = 0; i < content.length; i++) loader.loadStyles(content[i][1], ${async === true});`,
'',
`if(content.locals) ${exportName} = content.locals;`
'if(content.locals) module.exports = content.locals;'
].join('\n');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,28 +90,6 @@ describe(LoadThemedStylesLoader.name, () => {
expect(returnedModule.exports).toEqual({});
});

it('correctly handles the namedExport option', () => {
LoadThemedStylesLoader.loadedThemedStylesPath = './testData/LoadThemedStylesMock';

const query: {} = { namedExport: 'default' };
let loaderResult: string = LoadThemedStylesLoader.pitch.call(
{ query } as webpack.loader.LoaderContext,
'./testData/MockStyle1'
);
loaderResult = loaderResult.replace(/require\(\"!!/, 'require("');
loaderResult = wrapResult(loaderResult);

const returnedModule: { exports: string } = eval(loaderResult); // eslint-disable-line no-eval

expect(LoadThemedStylesMock.loadedData.indexOf('STYLE 1') !== -1).toEqual(true);
expect(LoadThemedStylesMock.loadedData.indexOf('STYLE 2') !== -1).toEqual(true);
expect(LoadThemedStylesMock.loadedData).toHaveLength(2);
expect(LoadThemedStylesMock.calledWithAsync[0]).toEqual(false);
expect(LoadThemedStylesMock.calledWithAsync[1]).toEqual(false);
expect(LoadThemedStylesMock.calledWithAsync).toHaveLength(2);
expect(returnedModule.exports).toEqual({ default: 'locals' });
});

it('correctly handles the async option set to "true"', () => {
LoadThemedStylesLoader.loadedThemedStylesPath = './testData/LoadThemedStylesMock';

Expand Down