From 49f35084f2bedd38e6286a9016eba3bea91e240a Mon Sep 17 00:00:00 2001 From: owjs3901 Date: Mon, 17 Feb 2025 22:33:57 +0900 Subject: [PATCH] Add include option --- .changeset/smooth-dots-carry.md | 7 ++++ packages/next-plugin/package.json | 1 + packages/vite-plugin/package.json | 3 +- packages/webpack-plugin/package.json | 3 ++ .../src/__tests__/loader.test.ts | 40 ------------------- .../src/__tests__/plugin.test.ts | 38 ++++++++++++++++++ packages/webpack-plugin/src/loader.ts | 8 ---- packages/webpack-plugin/src/plugin.ts | 9 ++++- 8 files changed, 59 insertions(+), 50 deletions(-) create mode 100644 .changeset/smooth-dots-carry.md diff --git a/.changeset/smooth-dots-carry.md b/.changeset/smooth-dots-carry.md new file mode 100644 index 00000000..8d3e9f02 --- /dev/null +++ b/.changeset/smooth-dots-carry.md @@ -0,0 +1,7 @@ +--- +"@devup-ui/webpack-plugin": patch +"@devup-ui/next-plugin": patch +"@devup-ui/vite-plugin": patch +--- + +Add include option diff --git a/packages/next-plugin/package.json b/packages/next-plugin/package.json index f5c7ac70..8c54c397 100644 --- a/packages/next-plugin/package.json +++ b/packages/next-plugin/package.json @@ -33,6 +33,7 @@ "typescript": "^5.7.3" }, "peerDependencies": { + "@devup-ui/webpack-plugin": "*", "next": "*" } } diff --git a/packages/vite-plugin/package.json b/packages/vite-plugin/package.json index c9f0a309..b0e91618 100644 --- a/packages/vite-plugin/package.json +++ b/packages/vite-plugin/package.json @@ -30,6 +30,7 @@ "typescript": "^5.7.3" }, "peerDependencies": { - "vite": "*" + "vite": "*", + "@devup-ui/wasm": "*" } } diff --git a/packages/webpack-plugin/package.json b/packages/webpack-plugin/package.json index 543f9344..fcaf3f3c 100644 --- a/packages/webpack-plugin/package.json +++ b/packages/webpack-plugin/package.json @@ -34,6 +34,9 @@ "dependencies": { "@devup-ui/wasm": "workspace:*" }, + "peerDependencies": { + "@devup-ui/wasm": "*" + }, "devDependencies": { "vite": "^6.1.0", "@types/webpack": "^5.28.5", diff --git a/packages/webpack-plugin/src/__tests__/loader.test.ts b/packages/webpack-plugin/src/__tests__/loader.test.ts index ea2bf9bc..04748ae1 100644 --- a/packages/webpack-plugin/src/__tests__/loader.test.ts +++ b/packages/webpack-plugin/src/__tests__/loader.test.ts @@ -13,46 +13,6 @@ beforeEach(() => { }) describe('devupUILoader', () => { - it('should ignore lib files', () => { - const t = { - getOptions: () => ({ - package: 'package', - cssFile: 'cssFile', - watch: false, - }), - addDependency: vi.fn(), - async: vi.fn().mockReturnValue(vi.fn()), - resourcePath: 'node_modules/package/index.ts', - } - devupUILoader.bind(t as any)( - Buffer.from('code'), - 'node_modules/package/index.ts', - ) - - expect(t.async).toHaveBeenCalled() - expect(t.async()).toHaveBeenCalledWith(null, Buffer.from('code')) - }) - - it('should ignore wrong files', () => { - const t = { - getOptions: () => ({ - package: 'package', - cssFile: 'cssFile', - watch: false, - }), - async: vi.fn().mockReturnValue(vi.fn()), - resourcePath: 'node_modules/package/index.css', - addDependency: vi.fn(), - } - devupUILoader.bind(t as any)( - Buffer.from('code'), - 'node_modules/package/index.css', - ) - - expect(t.async).toHaveBeenCalled() - expect(t.async()).toHaveBeenCalledWith(null, Buffer.from('code')) - }) - it('should extract code with css', async () => { const _compiler = { __DEVUP_CACHE: '', diff --git a/packages/webpack-plugin/src/__tests__/plugin.test.ts b/packages/webpack-plugin/src/__tests__/plugin.test.ts index fe47ed96..02731716 100644 --- a/packages/webpack-plugin/src/__tests__/plugin.test.ts +++ b/packages/webpack-plugin/src/__tests__/plugin.test.ts @@ -28,6 +28,7 @@ describe('devupUIPlugin', () => { it('should apply default options', () => { import.meta.resolve = vi.fn().mockReturnValue('resolved') expect(new DevupUIWebpackPlugin({}).options).toEqual({ + include: [], package: '@devup-ui/react', cssFile: resolve('.df', 'devup-ui.css'), devupPath: 'devup.json', @@ -48,6 +49,7 @@ describe('devupUIPlugin', () => { watch: false, }).options, ).toEqual({ + include: [], package: 'new-package', cssFile: 'new-css-file', devupPath: 'new-devup-path', @@ -265,4 +267,40 @@ describe('devupUIPlugin', () => { ) }) }) + + it('should include lib', () => { + vi.mocked(readFileSync).mockReturnValue('{"theme": "theme"}') + vi.mocked(getThemeInterface).mockReturnValue('interfaceCode') + vi.mocked(getCss).mockReturnValue('css') + vi.mocked(writeFileSync).mockReturnValue() + vi.mocked(mkdirSync) + + const plugin = new DevupUIWebpackPlugin({ + include: ['lib'], + }) + const compiler = { + options: { + module: { + rules: [], + }, + }, + hooks: { + afterCompile: { + tap: vi.fn(), + }, + watchRun: { + tapAsync: vi.fn(), + }, + }, + } as any + plugin.apply(compiler) + + expect(writeFileSync).toHaveBeenCalledWith( + resolve('.df', 'devup-ui.css'), + '', + { + encoding: 'utf-8', + }, + ) + }) }) diff --git a/packages/webpack-plugin/src/loader.ts b/packages/webpack-plugin/src/loader.ts index 0655cad8..3761c9ac 100644 --- a/packages/webpack-plugin/src/loader.ts +++ b/packages/webpack-plugin/src/loader.ts @@ -23,14 +23,6 @@ const devupUILoader: RawLoaderDefinitionFunction = } = this.getOptions() const callback = this.async() const id = this.resourcePath - if ( - id.includes('node_modules/') || - id.includes('@devup-ui/react') || - !/\.[tj](s|sx)?$/.test(id) - ) { - callback(null, source) - return - } try { const { code, css } = codeExtract( diff --git a/packages/webpack-plugin/src/plugin.ts b/packages/webpack-plugin/src/plugin.ts index 031008f3..953969c4 100644 --- a/packages/webpack-plugin/src/plugin.ts +++ b/packages/webpack-plugin/src/plugin.ts @@ -24,6 +24,7 @@ export interface DevupUIWebpackPluginOptions { interfacePath: string watch: boolean debug: boolean + include: string[] } export class DevupUIWebpackPlugin { @@ -36,6 +37,7 @@ export class DevupUIWebpackPlugin { cssFile = resolve(interfacePath, 'devup-ui.css'), watch = false, debug = false, + include = [], }: Partial = {}) { this.options = { package: libPackage, @@ -44,6 +46,7 @@ export class DevupUIWebpackPlugin { interfacePath, watch, debug, + include, } } @@ -132,7 +135,11 @@ export class DevupUIWebpackPlugin { compiler.options.module.rules.push( { test: /\.(tsx|ts|js|mjs|jsx)$/, - exclude: /node_modules/, + exclude: new RegExp( + this.options.include.length + ? `node_modules(?!(.*${this.options.include.join('|').replaceAll('/', '[\\/\\\\]')})([\\/\\\\]|$))` + : 'node_modules', + ), enforce: 'pre', use: [ {