Skip to content

Commit

Permalink
fix: 将 store 与 compilation 进行绑定
Browse files Browse the repository at this point in the history
  • Loading branch information
meixg committed Aug 16, 2021
1 parent b400975 commit e3ec2c9
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 13 deletions.
11 changes: 9 additions & 2 deletions src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {styleStore, templateStore} from './store';
import {Store} from './store';
import type {Compiler, RuleSetRule} from 'webpack';
import {promises as fsPromise} from 'fs';
import {parseComponent} from './lib/parseComponent';
Expand Down Expand Up @@ -67,7 +67,14 @@ export default class SanSSRLoaderPlugin {
}

compiler.hooks.compilation.tap(id, compilation => {
styleStore.clear();
// @ts-ignore
compilation._styleStore = new Store();
// @ts-ignore
compilation._templateStore = new Store();
// @ts-ignore
const styleStore = compilation._styleStore;
// @ts-ignore
const templateStore = compilation._templateStore;

const reportError = (err: Error) => compilation.errors.push(err);

Expand Down
4 changes: 3 additions & 1 deletion src/san-loader.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {styleStore, templateStore, TemplateResult} from './store';
import {StyleStore, TemplateResult, TemplateStore} from './store';
import {noop, extractRequire, getFileLoaderExportPromise} from './lib/utils';

import type {loader} from 'webpack';
import ___HTML_LOADER_GET_URL_IMPORT___ from 'html-loader/dist/runtime/getUrl';

export default function (this: loader.LoaderContext, content: string) {
const styleStore = this._compilation._styleStore as StyleStore;
const templateStore = this._compilation._templateStore as TemplateStore;
styleStore.set(this.resourcePath);

const callback = this.async();
Expand Down
8 changes: 4 additions & 4 deletions src/store.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {ExtractedCssResult} from './types';
import {ExtractedCssResult} from './types';

class Store<T> {
export class Store<T> {
store: Map<string, T[]>;
isArray: boolean = false;

Expand Down Expand Up @@ -28,6 +28,6 @@ class Store<T> {
}

export type TemplateResult = string;
export type StyleStore = Store<ExtractedCssResult>;
export type TemplateStore = Store<TemplateResult>;

export const styleStore = new Store<ExtractedCssResult>();
export const templateStore = new Store<TemplateResult>();
3 changes: 2 additions & 1 deletion src/style-loader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {styleStore} from './store';
import {StyleStore} from './store';

import type {loader} from 'webpack';
import ___CSS_LOADER_GET_URL_IMPORT___ from 'css-loader/dist/runtime/getUrl';
Expand All @@ -16,6 +16,7 @@ export default function (this: loader.LoaderContext, content: string) {
checkIsAfterCssLoader(this);

extractCssResult(content, this, cssRes => {
const styleStore = this._compilation._styleStore as StyleStore;
styleStore.set(this.resourcePath, {
locals: cssRes.exports.locals,
cssCode: cssRes.exports[0][1],
Expand Down
2 changes: 1 addition & 1 deletion src/types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ declare module 'de-indent';
declare module 'san-loader/lib/plugin';
declare module 'webpack/lib/RuleSet';
declare module 'css-loader/dist/runtime/getUrl';
declare module 'html-loader/dist/runtime/getUrl';
declare module 'html-loader/dist/runtime/getUrl';
11 changes: 9 additions & 2 deletions test/san-loader.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import sanLoader from '../src/san-loader';
import {styleStore} from '../src/store';
import {Store, StyleStore, TemplateStore} from '../src/store';
import type {loader} from 'webpack';

test('styleStore', async () => {
const mockAsyncCallback = jest.fn();
const env = {
async: () => (...args: any[]) => mockAsyncCallback(...args),
resourcePath: '/mock/path',
_compilation: {
_styleStore: new Store() as StyleStore
}
};

sanLoader.call(env as unknown as loader.LoaderContext, 'mock content');

expect(styleStore.get('/mock/path')).toStrictEqual([]);
expect(env._compilation._styleStore.get('/mock/path')).toStrictEqual([]);

expect(mockAsyncCallback).toHaveBeenCalledTimes(1);
expect(mockAsyncCallback.mock.calls[0][1]).toBe('mock content');
Expand All @@ -25,6 +28,10 @@ test('templateStore', () => {
async: jest.fn(),
resourcePath: '/path/to/foo.san',
loadModule: (...args: any[]) => mockLoadModule(...args),
_compilation: {
_templateStore: new Store() as TemplateStore,
_styleStore: new Store() as StyleStore
}
} as unknown as loader.LoaderContext;

sanLoader.call(mockLoaderContext, `
Expand Down
4 changes: 2 additions & 2 deletions test/store.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {styleStore} from '../src/store';
import {Store, StyleStore} from '../src/store';

test('styleStore', async () => {

const styleStore = new Store() as StyleStore;

styleStore.set('styleA', {
cssCode: 'aaa',
Expand Down

0 comments on commit e3ec2c9

Please sign in to comment.