Skip to content

Commit

Permalink
feat: optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
luhc228 committed Oct 12, 2023
1 parent 43e0c49 commit 0511b17
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
4 changes: 2 additions & 2 deletions packages/pkg/src/rollupPlugins/babel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import * as babel from '@babel/core';
import type { ParserPlugin } from '@babel/parser';
import { Plugin } from 'rollup';
import { createScriptsFilter, formatCnpmDepFilepath, getExcludeNodeModules } from '../utils.js';
import { createScriptsFilter, formatCnpmDepFilepath, getIncludeNodeModules } from '../utils.js';
import type { BundleTaskConfig } from '../types.js';
import { TransformOptions } from '@babel/core';
import getBabelOptions from '../helpers/getBabelOptions.js';
Expand Down Expand Up @@ -43,7 +43,7 @@ const babelPlugin = (
): Plugin => {
// https://babeljs.io/docs/en/babel-preset-react#usage
const babelOptions = getBabelOptions(plugins, options, modifyBabelOptions);
const scriptsFilter = createScriptsFilter([], getExcludeNodeModules(compileDependencies));
const scriptsFilter = createScriptsFilter(getIncludeNodeModules(compileDependencies));
return {
name: 'ice-pkg:babel',

Expand Down
4 changes: 2 additions & 2 deletions packages/pkg/src/rollupPlugins/swc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { extname, basename, relative, sep } from 'path';
import * as swc from '@swc/core';
import deepmerge from 'deepmerge';
import { isTypescriptOnly } from '../helpers/suffix.js';
import { checkDependencyExists, createScriptsFilter, formatCnpmDepFilepath, getExcludeNodeModules } from '../utils.js';
import { checkDependencyExists, createScriptsFilter, formatCnpmDepFilepath, getIncludeNodeModules } from '../utils.js';

import type { Options as swcCompileOptions, Config, TsParserConfig, EsParserConfig } from '@swc/core';
import type { TaskConfig, OutputFile, BundleTaskConfig } from '../types.js';
Expand Down Expand Up @@ -65,7 +65,7 @@ const swcPlugin = (
extraSwcOptions?: Config,
compileDependencies?: BundleTaskConfig['compileDependencies'],
): Plugin => {
const scriptsFilter = createScriptsFilter([], getExcludeNodeModules(compileDependencies));
const scriptsFilter = createScriptsFilter(getIncludeNodeModules(compileDependencies));
return {
name: 'ice-pkg:swc',

Expand Down
21 changes: 10 additions & 11 deletions packages/pkg/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,17 +287,15 @@ export const stringifyObject = (obj: PlainObject) => {
};

// @ref: It will pass to createScriptFilter function
export function getExcludeNodeModules(compileDependencies: boolean | RegExp[] | string[]): RegExp[] {
if (!compileDependencies) {
// not compile deps in node_modules
export function getIncludeNodeModules(compileDependencies: boolean | RegExp[] | string[]): RegExp[] {
if (compileDependencies === true || (Array.isArray(compileDependencies) && compileDependencies.length === 0)) {
return [/node_modules/];
}
if (Array.isArray(compileDependencies) && compileDependencies.length > 0) {
// compile all deps in node_modules except compileDependencies

return [new RegExp(`node_modules/(?!(${compileDependencies.map((dep: string | RegExp) => (`${typeof dep === 'string' ? dep : dep.source}`)).join('|')}))`)];
return [new RegExp(`node_modules/(${compileDependencies.map((dep: string | RegExp) => (`${typeof dep === 'string' ? dep : dep.source}`)).join('|')})/(?!node_modules).*`)];
}
// compile all deps in node_modules
// default
return [];
}

Expand All @@ -315,20 +313,21 @@ export function formatCnpmDepFilepath(filepath: string) {
}

/**
* default include src/**.m?[jt]sx? but exclude .d.ts file
*
* @param extraInclude include other file types
* @param extraExclude exclude other file types
*
* @example exclude node_modules createScriptsFilter([], [/node_modules/])
*/
export const createScriptsFilter = (
extraInclude: RegExp[] = [],
extraExclude: RegExp[] = [],
extraIncludes: RegExp[] = [],
extraExcludes: RegExp[] = [],
) => {
const exclude = [/\.d\.ts$/].concat(extraExclude);
const include = [/\.m?[jt]sx?$/].concat(extraInclude);
const includes = [/src\/.*\.m?[jt]sx?$/].concat(extraIncludes);
const excludes = [/\.d\.ts$/].concat(extraExcludes);

return createFilter(include, exclude);
return createFilter(includes, excludes);
};

export const cwd = process.cwd();
Expand Down

0 comments on commit 0511b17

Please sign in to comment.