From e3ec2c99ffa5e5fef64a8c7610a6c1657999741b Mon Sep 17 00:00:00 2001 From: meixg Date: Mon, 16 Aug 2021 21:56:57 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=B0=86=20store=20=E4=B8=8E=20compilat?= =?UTF-8?q?ion=20=E8=BF=9B=E8=A1=8C=E7=BB=91=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugin.ts | 11 +++++++++-- src/san-loader.ts | 4 +++- src/store.ts | 8 ++++---- src/style-loader.ts | 3 ++- src/types/global.d.ts | 2 +- test/san-loader.test.ts | 11 +++++++++-- test/store.test.ts | 4 ++-- 7 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/plugin.ts b/src/plugin.ts index 224aba9..7d254a3 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -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'; @@ -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); diff --git a/src/san-loader.ts b/src/san-loader.ts index 1799337..1245039 100644 --- a/src/san-loader.ts +++ b/src/san-loader.ts @@ -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(); diff --git a/src/store.ts b/src/store.ts index d7accff..b8c7dc8 100644 --- a/src/store.ts +++ b/src/store.ts @@ -1,6 +1,6 @@ -import type {ExtractedCssResult} from './types'; +import {ExtractedCssResult} from './types'; -class Store { +export class Store { store: Map; isArray: boolean = false; @@ -28,6 +28,6 @@ class Store { } export type TemplateResult = string; +export type StyleStore = Store; +export type TemplateStore = Store; -export const styleStore = new Store(); -export const templateStore = new Store(); diff --git a/src/style-loader.ts b/src/style-loader.ts index f88f107..d82fad9 100644 --- a/src/style-loader.ts +++ b/src/style-loader.ts @@ -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'; @@ -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], diff --git a/src/types/global.d.ts b/src/types/global.d.ts index 65d65ff..98cff6f 100644 --- a/src/types/global.d.ts +++ b/src/types/global.d.ts @@ -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'; \ No newline at end of file +declare module 'html-loader/dist/runtime/getUrl'; diff --git a/test/san-loader.test.ts b/test/san-loader.test.ts index d9f9247..cd187b3 100644 --- a/test/san-loader.test.ts +++ b/test/san-loader.test.ts @@ -1,5 +1,5 @@ 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 () => { @@ -7,11 +7,14 @@ test('styleStore', async () => { 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'); @@ -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, ` diff --git a/test/store.test.ts b/test/store.test.ts index 49c8d1c..04263d0 100644 --- a/test/store.test.ts +++ b/test/store.test.ts @@ -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',