From 652e02e17daeede955705e973c2e35f786c48d54 Mon Sep 17 00:00:00 2001 From: "gaoyuan.1226" Date: Mon, 15 May 2023 15:22:22 +0800 Subject: [PATCH 1/6] fix: support check-syntax in rspack --- .../src/config/defaults.ts | 3 +- .../src/shared/plugin.ts | 1 + .../src/types/config/security.ts | 6 +- packages/builder/builder-shared/package.json | 5 ++ .../helpers/generateError.ts | 0 .../helpers/generateHtmlScripts.ts | 0 .../helpers/getEcmaVersion.ts | 0 .../CheckSyntaxPlugin/helpers/index.ts | 0 .../CheckSyntaxPlugin/helpers/printErrors.ts | 2 +- .../CheckSyntaxPlugin/helpers/type.ts | 0 .../CheckSyntaxPlugin/helpers/utils.ts | 0 .../src/plugins}/CheckSyntaxPlugin/index.ts | 17 +++-- .../builder-shared/src/plugins/index.ts | 1 + .../builder-shared/src/test-stub/helper.ts | 1 + .../builder-shared/src/types/config/index.ts | 2 +- .../src/types/config/security.ts | 7 ++ .../builder-shared/src/types/plugin.ts | 1 + .../builder-webpack-provider/package.json | 5 -- .../src/shared/plugin.ts | 2 +- .../src/types/config/security.ts | 5 +- .../tests/plugins/checkSyntax.test.ts | 21 ------ .../src/plugins/checkSyntax.ts | 13 ++-- packages/builder/builder/src/plugins/index.ts | 2 + .../__snapshots__/checkSyntax.test.ts.snap | 5 +- .../builder/tests/plugins/checkSyntax.test.ts | 66 +++++++++++++++++++ pnpm-lock.yaml | 11 ++++ .../builder/cases/check-syntax/index.test.ts | 31 +++++++++ .../builder/cases/check-syntax/src/index.js | 3 + .../builder/cases/check-syntax/src/test.js | 4 ++ 29 files changed, 163 insertions(+), 51 deletions(-) rename packages/builder/{builder-webpack-provider/src/webpackPlugins => builder-shared/src/plugins}/CheckSyntaxPlugin/helpers/generateError.ts (100%) rename packages/builder/{builder-webpack-provider/src/webpackPlugins => builder-shared/src/plugins}/CheckSyntaxPlugin/helpers/generateHtmlScripts.ts (100%) rename packages/builder/{builder-webpack-provider/src/webpackPlugins => builder-shared/src/plugins}/CheckSyntaxPlugin/helpers/getEcmaVersion.ts (100%) rename packages/builder/{builder-webpack-provider/src/webpackPlugins => builder-shared/src/plugins}/CheckSyntaxPlugin/helpers/index.ts (100%) rename packages/builder/{builder-webpack-provider/src/webpackPlugins => builder-shared/src/plugins}/CheckSyntaxPlugin/helpers/printErrors.ts (97%) rename packages/builder/{builder-webpack-provider/src/webpackPlugins => builder-shared/src/plugins}/CheckSyntaxPlugin/helpers/type.ts (100%) rename packages/builder/{builder-webpack-provider/src/webpackPlugins => builder-shared/src/plugins}/CheckSyntaxPlugin/helpers/utils.ts (100%) rename packages/builder/{builder-webpack-provider/src/webpackPlugins => builder-shared/src/plugins}/CheckSyntaxPlugin/index.ts (83%) delete mode 100644 packages/builder/builder-webpack-provider/tests/plugins/checkSyntax.test.ts rename packages/builder/{builder-webpack-provider => builder}/src/plugins/checkSyntax.ts (79%) rename packages/builder/{builder-webpack-provider => builder}/tests/plugins/__snapshots__/checkSyntax.test.ts.snap (51%) create mode 100644 packages/builder/builder/tests/plugins/checkSyntax.test.ts create mode 100644 tests/e2e/builder/cases/check-syntax/index.test.ts create mode 100644 tests/e2e/builder/cases/check-syntax/src/index.js create mode 100644 tests/e2e/builder/cases/check-syntax/src/test.js diff --git a/packages/builder/builder-rspack-provider/src/config/defaults.ts b/packages/builder/builder-rspack-provider/src/config/defaults.ts index 78cb4136f217..ea78a64e561f 100644 --- a/packages/builder/builder-rspack-provider/src/config/defaults.ts +++ b/packages/builder/builder-rspack-provider/src/config/defaults.ts @@ -22,7 +22,8 @@ export const createDefaultConfig = () => output: getDefaultOutputConfig(), tools: {}, security: { - // sri: false + sri: false, + checkSyntax: false, }, performance: { // profile: false, diff --git a/packages/builder/builder-rspack-provider/src/shared/plugin.ts b/packages/builder/builder-rspack-provider/src/shared/plugin.ts index 3912d344d6fc..c94b2997cca4 100644 --- a/packages/builder/builder-rspack-provider/src/shared/plugin.ts +++ b/packages/builder/builder-rspack-provider/src/shared/plugin.ts @@ -42,5 +42,6 @@ export const applyDefaultPlugins = (plugins: Plugins) => plugins.inlineChunk(), plugins.bundleAnalyzer(), plugins.assetsRetry(), + plugins.checkSyntax(), import('../plugins/fallback').then(m => m.builderPluginFallback()), // fallback should be the last plugin ]); diff --git a/packages/builder/builder-rspack-provider/src/types/config/security.ts b/packages/builder/builder-rspack-provider/src/types/config/security.ts index c290c83a6467..da7684e41b09 100644 --- a/packages/builder/builder-rspack-provider/src/types/config/security.ts +++ b/packages/builder/builder-rspack-provider/src/types/config/security.ts @@ -1,5 +1,5 @@ -// TODO -// eslint-disable-next-line @typescript-eslint/no-empty-interface -export interface SecurityConfig {} +import type { SharedSecurityConfig } from '@modern-js/builder-shared'; + +export type SecurityConfig = SharedSecurityConfig; export type NormalizedSecurityConfig = Required; diff --git a/packages/builder/builder-shared/package.json b/packages/builder/builder-shared/package.json index 6dcf6feb8435..8ac6a374ce51 100644 --- a/packages/builder/builder-shared/package.json +++ b/packages/builder/builder-shared/package.json @@ -94,6 +94,10 @@ "@modern-js/types": "workspace:*", "@modern-js/utils": "workspace:*", "@babel/core": "7.18.0", + "acorn": "^8.8.1", + "caniuse-lite": "^1.0.30001451", + "cheerio": "1.0.0-rc.12", + "source-map": "^0.7.4", "webpack-sources": "^3.2.3", "zod": "^3.20.2", "zod-validation-error": "^0.3.0" @@ -103,6 +107,7 @@ "@scripts/vitest-config": "workspace:*", "@types/babel__core": "^7.1.19", "@types/babel__preset-env": "^7.9.2", + "@types/caniuse-lite": "^1.0.1", "@types/node": "^14", "html-webpack-plugin": "5.5.0", "terser": "^5.14.1", diff --git a/packages/builder/builder-webpack-provider/src/webpackPlugins/CheckSyntaxPlugin/helpers/generateError.ts b/packages/builder/builder-shared/src/plugins/CheckSyntaxPlugin/helpers/generateError.ts similarity index 100% rename from packages/builder/builder-webpack-provider/src/webpackPlugins/CheckSyntaxPlugin/helpers/generateError.ts rename to packages/builder/builder-shared/src/plugins/CheckSyntaxPlugin/helpers/generateError.ts diff --git a/packages/builder/builder-webpack-provider/src/webpackPlugins/CheckSyntaxPlugin/helpers/generateHtmlScripts.ts b/packages/builder/builder-shared/src/plugins/CheckSyntaxPlugin/helpers/generateHtmlScripts.ts similarity index 100% rename from packages/builder/builder-webpack-provider/src/webpackPlugins/CheckSyntaxPlugin/helpers/generateHtmlScripts.ts rename to packages/builder/builder-shared/src/plugins/CheckSyntaxPlugin/helpers/generateHtmlScripts.ts diff --git a/packages/builder/builder-webpack-provider/src/webpackPlugins/CheckSyntaxPlugin/helpers/getEcmaVersion.ts b/packages/builder/builder-shared/src/plugins/CheckSyntaxPlugin/helpers/getEcmaVersion.ts similarity index 100% rename from packages/builder/builder-webpack-provider/src/webpackPlugins/CheckSyntaxPlugin/helpers/getEcmaVersion.ts rename to packages/builder/builder-shared/src/plugins/CheckSyntaxPlugin/helpers/getEcmaVersion.ts diff --git a/packages/builder/builder-webpack-provider/src/webpackPlugins/CheckSyntaxPlugin/helpers/index.ts b/packages/builder/builder-shared/src/plugins/CheckSyntaxPlugin/helpers/index.ts similarity index 100% rename from packages/builder/builder-webpack-provider/src/webpackPlugins/CheckSyntaxPlugin/helpers/index.ts rename to packages/builder/builder-shared/src/plugins/CheckSyntaxPlugin/helpers/index.ts diff --git a/packages/builder/builder-webpack-provider/src/webpackPlugins/CheckSyntaxPlugin/helpers/printErrors.ts b/packages/builder/builder-shared/src/plugins/CheckSyntaxPlugin/helpers/printErrors.ts similarity index 97% rename from packages/builder/builder-webpack-provider/src/webpackPlugins/CheckSyntaxPlugin/helpers/printErrors.ts rename to packages/builder/builder-shared/src/plugins/CheckSyntaxPlugin/helpers/printErrors.ts index 1c1b28fb9fdf..4722d38c7890 100644 --- a/packages/builder/builder-webpack-provider/src/webpackPlugins/CheckSyntaxPlugin/helpers/printErrors.ts +++ b/packages/builder/builder-shared/src/plugins/CheckSyntaxPlugin/helpers/printErrors.ts @@ -1,6 +1,6 @@ import { SyntaxError } from './type'; import chalk from '@modern-js/utils/chalk'; -import { logger } from '@modern-js/builder-shared'; +import { logger } from '../../../logger'; type Error = { source: string; diff --git a/packages/builder/builder-webpack-provider/src/webpackPlugins/CheckSyntaxPlugin/helpers/type.ts b/packages/builder/builder-shared/src/plugins/CheckSyntaxPlugin/helpers/type.ts similarity index 100% rename from packages/builder/builder-webpack-provider/src/webpackPlugins/CheckSyntaxPlugin/helpers/type.ts rename to packages/builder/builder-shared/src/plugins/CheckSyntaxPlugin/helpers/type.ts diff --git a/packages/builder/builder-webpack-provider/src/webpackPlugins/CheckSyntaxPlugin/helpers/utils.ts b/packages/builder/builder-shared/src/plugins/CheckSyntaxPlugin/helpers/utils.ts similarity index 100% rename from packages/builder/builder-webpack-provider/src/webpackPlugins/CheckSyntaxPlugin/helpers/utils.ts rename to packages/builder/builder-shared/src/plugins/CheckSyntaxPlugin/helpers/utils.ts diff --git a/packages/builder/builder-webpack-provider/src/webpackPlugins/CheckSyntaxPlugin/index.ts b/packages/builder/builder-shared/src/plugins/CheckSyntaxPlugin/index.ts similarity index 83% rename from packages/builder/builder-webpack-provider/src/webpackPlugins/CheckSyntaxPlugin/index.ts rename to packages/builder/builder-shared/src/plugins/CheckSyntaxPlugin/index.ts index 6f8fea8ad52e..9f8b16fb3bb5 100644 --- a/packages/builder/builder-webpack-provider/src/webpackPlugins/CheckSyntaxPlugin/index.ts +++ b/packages/builder/builder-shared/src/plugins/CheckSyntaxPlugin/index.ts @@ -11,7 +11,8 @@ import { checkIsExcludeSource, CheckSyntaxExclude, } from './helpers'; -import { CheckSyntaxOptions, webpack } from '../../types'; +import { CheckSyntaxOptions } from '../../types'; +import type { Compiler, Compilation } from 'webpack'; const HTML_REGEX = /\.html$/; const JS_REGEX = /\.js$/; @@ -31,14 +32,18 @@ export class CheckSyntaxPlugin { this.ecmaVersion = getEcmaVersion(this.targets); } - apply(complier: webpack.Compiler) { + apply(complier: Compiler) { complier.hooks.afterEmit.tapPromise( CheckSyntaxPlugin.name, - async (compilation: webpack.Compilation) => { + async (compilation: Compilation) => { const outputPath = compilation.outputOptions.path || 'dist'; - const emittedAssets = Array.from(compilation.emittedAssets).map(p => - resolve(outputPath, p), - ); + // not support compilation.emittedAssets in Rspack + const emittedAssets = compilation + .getAssets() + .filter(a => a.source) + .map(a => a.name) + .map(p => resolve(outputPath, p)); + const files = emittedAssets.filter( assets => HTML_REGEX.test(assets) || JS_REGEX.test(assets), ); diff --git a/packages/builder/builder-shared/src/plugins/index.ts b/packages/builder/builder-shared/src/plugins/index.ts index 03ad47c9855c..976bc066d68a 100644 --- a/packages/builder/builder-shared/src/plugins/index.ts +++ b/packages/builder/builder-shared/src/plugins/index.ts @@ -6,3 +6,4 @@ export { HtmlAppIconPlugin } from './HtmlAppIconPlugin'; export { HtmlFaviconUrlPlugin, type FaviconUrls } from './HtmlFaviconUrlPlugin'; export { InlineChunkHtmlPlugin } from './InlineChunkHtmlPlugin'; export { AssetsRetryPlugin } from './AssetsRetryPlugin'; +export { CheckSyntaxPlugin } from './CheckSyntaxPlugin'; diff --git a/packages/builder/builder-shared/src/test-stub/helper.ts b/packages/builder/builder-shared/src/test-stub/helper.ts index fc5b424539b4..2b7a40a6aa1b 100644 --- a/packages/builder/builder-shared/src/test-stub/helper.ts +++ b/packages/builder/builder-shared/src/test-stub/helper.ts @@ -29,4 +29,5 @@ export const mockBuilderPlugins: Plugins = { svg: genMockPlugin('builder-plugin-svg'), html: genMockPlugin('builder-plugin-html'), antd: genMockPlugin('antd'), + checkSyntax: genMockPlugin('builder-plugin-check-syntax'), }; diff --git a/packages/builder/builder-shared/src/types/config/index.ts b/packages/builder/builder-shared/src/types/config/index.ts index 0b68e1f51c82..ea414d205ad8 100644 --- a/packages/builder/builder-shared/src/types/config/index.ts +++ b/packages/builder/builder-shared/src/types/config/index.ts @@ -37,7 +37,7 @@ export type SharedNormalizedConfig = DeepReadonly<{ html: NormalizedSharedHtmlConfig; source: NormalizedSharedSourceConfig; output: NormalizedSharedOutputConfig; - security?: SharedSecurityConfig; + security: Required; performance: NormalizedSharedPerformanceConfig; tools: SharedToolsConfig; }>; diff --git a/packages/builder/builder-shared/src/types/config/security.ts b/packages/builder/builder-shared/src/types/config/security.ts index 424323f3e253..155d5604cdae 100644 --- a/packages/builder/builder-shared/src/types/config/security.ts +++ b/packages/builder/builder-shared/src/types/config/security.ts @@ -4,10 +4,17 @@ export type SriOptions = { hashLoading?: 'eager' | 'lazy'; }; +export interface CheckSyntaxOptions { + targets: string[]; + exclude?: RegExp | Array; +} + export interface SharedSecurityConfig { /** * Adding an integrity attribute (`integrity`) to sub-resources introduced by HTML allows the browser to * verify the integrity of the introduced resource, thus preventing tampering with the downloaded resource. */ sri?: SriOptions | boolean; + /** Analyze the product for the presence of high-level syntax that is not compatible in the specified environment */ + checkSyntax?: boolean | CheckSyntaxOptions; } diff --git a/packages/builder/builder-shared/src/types/plugin.ts b/packages/builder/builder-shared/src/types/plugin.ts index 6da5fdc9902d..ebcdc68b8748 100644 --- a/packages/builder/builder-shared/src/types/plugin.ts +++ b/packages/builder/builder-shared/src/types/plugin.ts @@ -52,6 +52,7 @@ export type Plugins = { svg: PluginsFn; html: PluginsFn; antd: PluginsFn; + checkSyntax: PluginsFn; }; /** diff --git a/packages/builder/builder-webpack-provider/package.json b/packages/builder/builder-webpack-provider/package.json index 6a8283ccac63..02b44920f212 100644 --- a/packages/builder/builder-webpack-provider/package.json +++ b/packages/builder/builder-webpack-provider/package.json @@ -97,9 +97,6 @@ "@modern-js/utils": "workspace:*", "@pmmmwh/react-refresh-webpack-plugin": "0.5.9", "@babel/core": "7.18.0", - "acorn": "^8.8.1", - "caniuse-lite": "^1.0.30001451", - "cheerio": "1.0.0-rc.12", "css-minimizer-webpack-plugin": "5.0.0", "cssnano": "6.0.0", "fork-ts-checker-webpack-plugin": "8.0.0", @@ -107,7 +104,6 @@ "mini-css-extract-plugin": "2.7.0", "postcss": "8.4.21", "react-refresh": "0.14.0", - "source-map": "^0.7.4", "style-loader": "3.3.1", "terser-webpack-plugin": "5.3.6", "ts-loader": "9.4.1", @@ -119,7 +115,6 @@ "@scripts/vitest-config": "workspace:*", "@types/babel__core": "^7.1.19", "@types/babel__preset-env": "^7.9.2", - "@types/caniuse-lite": "^1.0.1", "@types/node": "^14", "antd": "4", "typescript": "^4", diff --git a/packages/builder/builder-webpack-provider/src/shared/plugin.ts b/packages/builder/builder-webpack-provider/src/shared/plugin.ts index 70dc04280400..02fae8e54d92 100644 --- a/packages/builder/builder-webpack-provider/src/shared/plugin.ts +++ b/packages/builder/builder-webpack-provider/src/shared/plugin.ts @@ -36,7 +36,7 @@ export const applyDefaultPlugins = (plugins: Plugins) => plugins.antd(), plugins.svg(), import('../plugins/pug').then(m => m.builderPluginPug()), - import('../plugins/checkSyntax').then(m => m.builderPluginCheckSyntax()), + plugins.checkSyntax(), import('../plugins/copy').then(m => m.builderPluginCopy()), plugins.font(), plugins.image(), diff --git a/packages/builder/builder-webpack-provider/src/types/config/security.ts b/packages/builder/builder-webpack-provider/src/types/config/security.ts index 239ae468c3a8..a9219f3ccdcd 100644 --- a/packages/builder/builder-webpack-provider/src/types/config/security.ts +++ b/packages/builder/builder-webpack-provider/src/types/config/security.ts @@ -5,9 +5,6 @@ export interface CheckSyntaxOptions { exclude?: RegExp | Array; } -export type SecurityConfig = SharedSecurityConfig & { - /** Analyze the product for the presence of high-level syntax that is not compatible in the specified environment */ - checkSyntax?: boolean | CheckSyntaxOptions; -}; +export type SecurityConfig = SharedSecurityConfig; export type NormalizedSecurityConfig = Required; diff --git a/packages/builder/builder-webpack-provider/tests/plugins/checkSyntax.test.ts b/packages/builder/builder-webpack-provider/tests/plugins/checkSyntax.test.ts deleted file mode 100644 index eaa8c8fd4a8c..000000000000 --- a/packages/builder/builder-webpack-provider/tests/plugins/checkSyntax.test.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { describe, expect, it } from 'vitest'; -import { builderPluginCheckSyntax } from '@/plugins/checkSyntax'; -import { createStubBuilder } from '@/stub/builder'; - -describe.skip('plugins/checkSyntax', () => { - it('should set WebpackCheckSyntax plugin', async () => { - process.env.NODE_ENV = 'production'; - const builder = await createStubBuilder({ - plugins: [builderPluginCheckSyntax()], - builderConfig: { - security: { - checkSyntax: true, - }, - }, - }); - - const config = await builder.unwrapWebpackConfig(); - expect(config).toMatchSnapshot(); - process.env.NODE_ENV = 'test'; - }); -}); diff --git a/packages/builder/builder-webpack-provider/src/plugins/checkSyntax.ts b/packages/builder/builder/src/plugins/checkSyntax.ts similarity index 79% rename from packages/builder/builder-webpack-provider/src/plugins/checkSyntax.ts rename to packages/builder/builder/src/plugins/checkSyntax.ts index b727223f856b..772e7d6a76bb 100644 --- a/packages/builder/builder-webpack-provider/src/plugins/checkSyntax.ts +++ b/packages/builder/builder/src/plugins/checkSyntax.ts @@ -3,15 +3,16 @@ import { BuilderTarget, DEFAULT_BROWSERSLIST, getBrowserslistWithDefault, + DefaultBuilderPlugin, + SharedNormalizedConfig, } from '@modern-js/builder-shared'; -import type { BuilderPlugin, NormalizedConfig } from '../types'; -export function builderPluginCheckSyntax(): BuilderPlugin { +export function builderPluginCheckSyntax(): DefaultBuilderPlugin { return { name: 'builder-plugin-check-syntax', setup(api) { - api.modifyWebpackChain(async (chain, { isProd, target }) => { + api.modifyBundlerChain(async (chain, { isProd, target }) => { const config = api.getNormalizedConfig(); const { checkSyntax } = config.security; @@ -29,9 +30,7 @@ export function builderPluginCheckSyntax(): BuilderPlugin { target, checkSyntax, ); - const { CheckSyntaxPlugin } = await import( - '../webpackPlugins/CheckSyntaxPlugin' - ); + const { CheckSyntaxPlugin } = await import('@modern-js/builder-shared'); chain.plugin(CheckSyntaxPlugin.name).use(CheckSyntaxPlugin, [ { targets, @@ -46,7 +45,7 @@ export function builderPluginCheckSyntax(): BuilderPlugin { async function getCheckTargets( builderContext: BuilderContext, - builderConfig: NormalizedConfig, + builderConfig: SharedNormalizedConfig, builderTarget: BuilderTarget, checkSyntax: { targets: string[] } | true, ) { diff --git a/packages/builder/builder/src/plugins/index.ts b/packages/builder/builder/src/plugins/index.ts index 53ac9177b5d0..b0cf42210e7f 100644 --- a/packages/builder/builder/src/plugins/index.ts +++ b/packages/builder/builder/src/plugins/index.ts @@ -36,4 +36,6 @@ export const plugins = { assetsRetry: () => import('./assetsRetry').then(m => m.builderPluginAssetsRetry()), antd: () => import('./antd').then(m => m.builderAntdPlugin()), + checkSyntax: () => + import('./checkSyntax').then(m => m.builderPluginCheckSyntax()), }; diff --git a/packages/builder/builder-webpack-provider/tests/plugins/__snapshots__/checkSyntax.test.ts.snap b/packages/builder/builder/tests/plugins/__snapshots__/checkSyntax.test.ts.snap similarity index 51% rename from packages/builder/builder-webpack-provider/tests/plugins/__snapshots__/checkSyntax.test.ts.snap rename to packages/builder/builder/tests/plugins/__snapshots__/checkSyntax.test.ts.snap index 5fbd96ae88e8..390ae7ea71ce 100644 --- a/packages/builder/builder-webpack-provider/tests/plugins/__snapshots__/checkSyntax.test.ts.snap +++ b/packages/builder/builder/tests/plugins/__snapshots__/checkSyntax.test.ts.snap @@ -1,11 +1,12 @@ // Vitest Snapshot v1 -exports[`plugins/checkSyntax > should set WebpackCheckSyntax plugin 1`] = ` +exports[`plugins/check-syntax > should add check-syntax plugin properly 1`] = ` { "plugins": [ CheckSyntaxPlugin { "ecmaVersion": 5, "errors": [], + "exclude": undefined, "targets": [ "> 0.01%", "not dead", @@ -15,3 +16,5 @@ exports[`plugins/checkSyntax > should set WebpackCheckSyntax plugin 1`] = ` ], } `; + +exports[`plugins/check-syntax > should not add check-syntax plugin when target node 1`] = `{}`; diff --git a/packages/builder/builder/tests/plugins/checkSyntax.test.ts b/packages/builder/builder/tests/plugins/checkSyntax.test.ts new file mode 100644 index 000000000000..936a2ce998b2 --- /dev/null +++ b/packages/builder/builder/tests/plugins/checkSyntax.test.ts @@ -0,0 +1,66 @@ +import { expect, describe, it } from 'vitest'; +import * as shared from '@modern-js/builder-shared'; +import { CHAIN_ID } from '@modern-js/utils'; +import { builderPluginCheckSyntax } from '@/plugins/checkSyntax'; + +describe('plugins/check-syntax', () => { + it('should add check-syntax plugin properly', async () => { + let modifyBundlerChainCb: any; + + const api: any = { + modifyBundlerChain: (fn: any) => { + modifyBundlerChainCb = fn; + }, + getNormalizedConfig: () => ({ + security: { + checkSyntax: true, + }, + }), + context: { + rootPath: __dirname, + }, + }; + + builderPluginCheckSyntax().setup(api); + + const chain = await shared.getBundlerChain(); + + await modifyBundlerChainCb(chain, { + CHAIN_ID, + isProd: true, + target: 'web', + }); + + expect(chain.toConfig()).toMatchSnapshot(); + }); + + it('should not add check-syntax plugin when target node', async () => { + let modifyBundlerChainCb: any; + + const api: any = { + modifyBundlerChain: (fn: any) => { + modifyBundlerChainCb = fn; + }, + getNormalizedConfig: () => ({ + security: { + checkSyntax: true, + }, + }), + context: { + rootPath: __dirname, + }, + }; + + builderPluginCheckSyntax().setup(api); + + const chain = await shared.getBundlerChain(); + + await modifyBundlerChainCb(chain, { + CHAIN_ID, + isProd: true, + target: 'node', + }); + + expect(chain.toConfig()).toMatchSnapshot(); + }); +}); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f3c09a68bfc0..6bdbe3bd5a42 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -132,8 +132,13 @@ importers: '@scripts/vitest-config': workspace:* '@types/babel__core': ^7.1.19 '@types/babel__preset-env': ^7.9.2 + '@types/caniuse-lite': ^1.0.1 '@types/node': ^14 + acorn: ^8.8.1 + caniuse-lite: ^1.0.30001451 + cheerio: 1.0.0-rc.12 html-webpack-plugin: 5.5.0 + source-map: ^0.7.4 terser: ^5.14.1 typescript: ^4 webpack: ^5.82.1 @@ -145,6 +150,10 @@ importers: '@modern-js/server': link:../../server/server '@modern-js/types': link:../../toolkit/types '@modern-js/utils': link:../../toolkit/utils + acorn: 8.8.1 + caniuse-lite: 1.0.30001451 + cheerio: 1.0.0-rc.12 + source-map: 0.7.4 webpack-sources: 3.2.3 zod: 3.20.2 zod-validation-error: 0.3.0_zod@3.20.2 @@ -153,6 +162,7 @@ importers: '@scripts/vitest-config': link:../../../scripts/vitest-config '@types/babel__core': 7.1.20 '@types/babel__preset-env': 7.9.2 + '@types/caniuse-lite': 1.0.1 '@types/node': 14.18.35 html-webpack-plugin: 5.5.0_webpack@5.82.1 terser: 5.15.0 @@ -15238,6 +15248,7 @@ packages: /acorn/7.4.1: resolution: {integrity: sha512-nQyp0o1/mNdbTO1PO6kHkwSrmgZ0MT/jCCpNiwbUjGoRN4dlBhqJtoQuCnEOKzgTVwg0ZWiCoQy6SxMebQVh8A==} engines: {node: '>=0.4.0'} + hasBin: true /acorn/8.8.1: resolution: {integrity: sha512-7zFpHzhnqYKrkYdUjF1HI1bzd0VygEGX8lFk4k5zVMqHEoES+P+7TKI+EvLO9WVMJ8eekdO0aDEK044xTXwPPA==} diff --git a/tests/e2e/builder/cases/check-syntax/index.test.ts b/tests/e2e/builder/cases/check-syntax/index.test.ts new file mode 100644 index 000000000000..e29c00e93318 --- /dev/null +++ b/tests/e2e/builder/cases/check-syntax/index.test.ts @@ -0,0 +1,31 @@ +import path from 'path'; +import { expect } from '@modern-js/e2e/playwright'; +import { allProviderTest } from '@scripts/helper'; +import { build } from '@scripts/shared'; + +allProviderTest('should throw error when exist syntax errors', async () => { + await expect( + build( + { + cwd: __dirname, + entry: { index: path.resolve(__dirname, './src/index.js') }, + }, + { + source: { + exclude: [path.resolve(__dirname, './src/test.js')], + }, + security: { + checkSyntax: true, + }, + tools: { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-expect-error + rspack: config => { + config.target = ['web']; + config.builtins.presetEnv = undefined; + }, + }, + }, + ), + ).rejects.toThrowError('[Syntax Checker]'); +}); diff --git a/tests/e2e/builder/cases/check-syntax/src/index.js b/tests/e2e/builder/cases/check-syntax/src/index.js new file mode 100644 index 000000000000..064e33ca66b0 --- /dev/null +++ b/tests/e2e/builder/cases/check-syntax/src/index.js @@ -0,0 +1,3 @@ +import { printLog } from './test'; + +printLog(); diff --git a/tests/e2e/builder/cases/check-syntax/src/test.js b/tests/e2e/builder/cases/check-syntax/src/test.js new file mode 100644 index 000000000000..bb01bfaf00dd --- /dev/null +++ b/tests/e2e/builder/cases/check-syntax/src/test.js @@ -0,0 +1,4 @@ +export const printLog = () => { + const arr = [1, 2, 3, 4, [5, 6, [7, 8]]]; + console.log(arr, arr.flat()); +}; From ec14b7e515771d78f2d4295e56caefaeb36a868e Mon Sep 17 00:00:00 2001 From: "gaoyuan.1226" Date: Mon, 15 May 2023 15:40:49 +0800 Subject: [PATCH 2/6] fix: lock --- pnpm-lock.yaml | 72 ++++++++++++++++++++++---------------------------- 1 file changed, 31 insertions(+), 41 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6bdbe3bd5a42..446186acc677 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -185,12 +185,8 @@ importers: '@scripts/vitest-config': workspace:* '@types/babel__core': ^7.1.19 '@types/babel__preset-env': ^7.9.2 - '@types/caniuse-lite': ^1.0.1 '@types/node': ^14 - acorn: ^8.8.1 antd: '4' - caniuse-lite: ^1.0.30001451 - cheerio: 1.0.0-rc.12 css-minimizer-webpack-plugin: 5.0.0 cssnano: 6.0.0 fork-ts-checker-webpack-plugin: 8.0.0 @@ -200,7 +196,6 @@ importers: react: ^18 react-dom: ^18 react-refresh: 0.14.0 - source-map: ^0.7.4 style-loader: 3.3.1 terser-webpack-plugin: 5.3.6 ts-loader: 9.4.1 @@ -216,9 +211,6 @@ importers: '@modern-js/types': link:../../toolkit/types '@modern-js/utils': link:../../toolkit/utils '@pmmmwh/react-refresh-webpack-plugin': 0.5.9_2sh2n464chklv3oeyrys3t3hwy - acorn: 8.8.1 - caniuse-lite: 1.0.30001451 - cheerio: 1.0.0-rc.12 css-minimizer-webpack-plugin: 5.0.0_webpack@5.82.1 cssnano: 6.0.0_postcss@8.4.21 fork-ts-checker-webpack-plugin: 8.0.0_7rk4b2hmmye4ay4nslib7apbti @@ -226,7 +218,6 @@ importers: mini-css-extract-plugin: 2.7.0_webpack@5.82.1 postcss: 8.4.21 react-refresh: 0.14.0 - source-map: 0.7.4 style-loader: 3.3.1_webpack@5.82.1 terser-webpack-plugin: 5.3.6_webpack@5.82.1 ts-loader: 9.4.1_7rk4b2hmmye4ay4nslib7apbti @@ -237,7 +228,6 @@ importers: '@scripts/vitest-config': link:../../../scripts/vitest-config '@types/babel__core': 7.1.20 '@types/babel__preset-env': 7.9.2 - '@types/caniuse-lite': 1.0.1 '@types/node': 14.18.35 antd: 4.21.4_biqbaboplfbrettd7655fr4n2y react: 18.2.0 @@ -567,7 +557,7 @@ importers: '@modern-js/builder': workspace:* '@modern-js/builder-rspack-provider': workspace:* '@modern-js/core': workspace:* - '@modern-js/mdx-rs-binding': ^0.1.6 + '@modern-js/mdx-rs-binding': ^0.1.8 '@modern-js/plugin-tailwindcss': workspace:* '@modern-js/plugin-testing': workspace:* '@modern-js/remark-container': workspace:* @@ -652,7 +642,7 @@ importers: '@mdx-js/react': 2.2.1_react@18.2.0 '@modern-js/builder': link:../../builder/builder '@modern-js/builder-rspack-provider': link:../../builder/builder-rspack-provider - '@modern-js/mdx-rs-binding': 0.1.6 + '@modern-js/mdx-rs-binding': 0.1.8 '@modern-js/remark-container': link:../../toolkit/remark-container '@modern-js/utils': link:../../toolkit/utils '@types/compression': 1.7.2 @@ -10961,8 +10951,8 @@ packages: sucrase: 3.29.0 terser: 5.15.0 - /@modern-js/mdx-rs-binding-darwin-arm64/0.1.6: - resolution: {integrity: sha512-BpkslS/zmr0VVrdf4nXBeM3cK0p6SnQjFQVKmFIM++IkWFJ2OjZoQL1fPij0xXgM+sZcFwPaLb3AnxIMPCTSRg==} + /@modern-js/mdx-rs-binding-darwin-arm64/0.1.8: + resolution: {integrity: sha512-2O/OyS0HQwgGpOJIcpxzWbUaxv8U9wqu7PdHTXqybqAcohlR0okbCWH8YDl3L/IsMnDxYJElWnquuEO0u5FZqw==} engines: {node: '>=14.12'} cpu: [arm64] os: [darwin] @@ -10970,8 +10960,8 @@ packages: dev: false optional: true - /@modern-js/mdx-rs-binding-darwin-x64/0.1.6: - resolution: {integrity: sha512-KjlzTcJ4OhfSwNZEPaNRPZnwKSnKAvlP4J3/EGni16Loa9cj3oXbYoone5PxJ+Uy6NvJA9A98x3NIlumcmcwMw==} + /@modern-js/mdx-rs-binding-darwin-x64/0.1.8: + resolution: {integrity: sha512-dOLyOR8prtzf9eyrWMDfPfxWNjcz/Wi6D+3db9UHawOGAXio5KGfPLqoGKJoKzTcrZw1RTvw9WaiA+1IUymOEw==} engines: {node: '>=14.12'} cpu: [x64] os: [darwin] @@ -10979,8 +10969,8 @@ packages: dev: false optional: true - /@modern-js/mdx-rs-binding-linux-arm-gnueabihf/0.1.6: - resolution: {integrity: sha512-O4SE2Ly0n0VhJqDV2jA/8rnfBA8XbD5+wiN3KLAXqZKH4/TrQ+HuLtlmAENNFIPOl3I9+pw/f+kX5lfneAk7CA==} + /@modern-js/mdx-rs-binding-linux-arm-gnueabihf/0.1.8: + resolution: {integrity: sha512-YrdhrNL/PcbDV3TOuXGmCNfABUqqwMG75zXyNop8ZVtw4d5BirtRHeYpKePhaAnhFNceLAdvgDmOsy8s25X04w==} engines: {node: '>=14.12'} cpu: [arm] os: [linux] @@ -10988,8 +10978,8 @@ packages: dev: false optional: true - /@modern-js/mdx-rs-binding-linux-arm64-gnu/0.1.6: - resolution: {integrity: sha512-34nfa6FpVF5HIADKSQuW2zylyvJhcVDMN402pc8Qm3dColU69+zBIyHe9Rwz4OkrFpulQDuFH0bssTzeuR05eg==} + /@modern-js/mdx-rs-binding-linux-arm64-gnu/0.1.8: + resolution: {integrity: sha512-Uxix5nTmyed6O9ffFL+y9Sq9nIifV4AxgFzS01I8lQyc56cgvlWEzbHJqMdyyIw29Yr5QcsCU+JI5RVo2uqPTA==} engines: {node: '>=14.12'} cpu: [arm64] os: [linux] @@ -10997,8 +10987,8 @@ packages: dev: false optional: true - /@modern-js/mdx-rs-binding-linux-arm64-musl/0.1.6: - resolution: {integrity: sha512-Td4IkknCEKTCkRRb5dAU8aI0fpptgLztD/23KP9olJUMlvses0hUdzaSUsGzM63eiUuqvVfBeRPm6nFTwwpcpg==} + /@modern-js/mdx-rs-binding-linux-arm64-musl/0.1.8: + resolution: {integrity: sha512-PufMmu5UFg2lFNbggeZpeQ0/eidoJUmAgs4oIEd/zARlHglH1eSo5wZbm+sAxbxM9yS5/pkQxGPI4c97BwSvgQ==} engines: {node: '>=14.12'} cpu: [arm64] os: [linux] @@ -11006,8 +10996,8 @@ packages: dev: false optional: true - /@modern-js/mdx-rs-binding-linux-x64-gnu/0.1.6: - resolution: {integrity: sha512-JEoQt01ObDUaY2/p1nnevmhOxcTtiVpB/5sn5NMh3vHXL94m88MkcnqeIdzcwYVwjGawLbe140uQn3cieomm1Q==} + /@modern-js/mdx-rs-binding-linux-x64-gnu/0.1.8: + resolution: {integrity: sha512-Jslw+MYZq7OnPXXHAl78P+vql6TLYqLzCvs6y5TzTV2sO3EfYbybH3IYsCa6jdALHWjnEeJj4tYiDKY5dhH2Eg==} engines: {node: '>=14.12'} cpu: [x64] os: [linux] @@ -11015,8 +11005,8 @@ packages: dev: false optional: true - /@modern-js/mdx-rs-binding-linux-x64-musl/0.1.6: - resolution: {integrity: sha512-Mdq1dOUcrtUMBI/zZzDlue9f790pgXX/iH0Mu8Co7y2KyAc/K6JHQ3UPBKXxPaTB+tulPG5KnGCnP1Hi1f6jTA==} + /@modern-js/mdx-rs-binding-linux-x64-musl/0.1.8: + resolution: {integrity: sha512-qyADWKxKSBMDNafBCuBSfeSYTiUkGyVPsTdPxvrScw4pfXaLM/gpnweuxIwa57mp0oszSoA69KYm6PoJWT1ZSA==} engines: {node: '>=14.12'} cpu: [x64] os: [linux] @@ -11024,8 +11014,8 @@ packages: dev: false optional: true - /@modern-js/mdx-rs-binding-win32-arm64-msvc/0.1.6: - resolution: {integrity: sha512-uJEi2CN15kFm5xm+S5CQxcBz1CW+fyOR4c9/d4CtU5POBagIfY2A95rciw2ySDau8fJ6EKWFuoiBA8XG1EwpwQ==} + /@modern-js/mdx-rs-binding-win32-arm64-msvc/0.1.8: + resolution: {integrity: sha512-oheysTyLO7oPlHVJfto4qKeXTgIMoBVmtd7eUMAd5AioKtEjGcj9aQyRIn2lptlhuc82L0sns1/ML9JswSWfsA==} engines: {node: '>=14.12'} cpu: [arm64] os: [win32] @@ -11033,8 +11023,8 @@ packages: dev: false optional: true - /@modern-js/mdx-rs-binding-win32-x64-msvc/0.1.6: - resolution: {integrity: sha512-UBbItICR7h3PPB6vupUBuGbQq7DXMMeBbSxgwh/vxpt3ESCFoIopkZAhnacWmLKco9H2yp6WTkbmuAdjLu7AWQ==} + /@modern-js/mdx-rs-binding-win32-x64-msvc/0.1.8: + resolution: {integrity: sha512-EXl4lmANIwzNm4hy59EswHd8JUsxGgpdJ6MEf3OShkUYpwwYinDUneh5ST9fERU1Ojl85WUmegzMcZGP19Gusg==} engines: {node: '>=14.12'} cpu: [x64] os: [win32] @@ -11042,19 +11032,19 @@ packages: dev: false optional: true - /@modern-js/mdx-rs-binding/0.1.6: - resolution: {integrity: sha512-V4uTqRdDTJTtL/otz0kENl1jzP6Lojs0SlR7yf2YJHOlu3AUa6nmL7r6Qgl+4zGVXETf3z8G+xjmAwuDCM77TQ==} + /@modern-js/mdx-rs-binding/0.1.8: + resolution: {integrity: sha512-tJBTnOE4qyK00N5ZDvMcmbbJbk++1BJLoQO3ZgP1aiGceDOx0VHyMB67BXgQ2CARFKD2nT9BjiiCe58KkzNN9Q==} engines: {node: '>= 10'} optionalDependencies: - '@modern-js/mdx-rs-binding-darwin-arm64': 0.1.6 - '@modern-js/mdx-rs-binding-darwin-x64': 0.1.6 - '@modern-js/mdx-rs-binding-linux-arm-gnueabihf': 0.1.6 - '@modern-js/mdx-rs-binding-linux-arm64-gnu': 0.1.6 - '@modern-js/mdx-rs-binding-linux-arm64-musl': 0.1.6 - '@modern-js/mdx-rs-binding-linux-x64-gnu': 0.1.6 - '@modern-js/mdx-rs-binding-linux-x64-musl': 0.1.6 - '@modern-js/mdx-rs-binding-win32-arm64-msvc': 0.1.6 - '@modern-js/mdx-rs-binding-win32-x64-msvc': 0.1.6 + '@modern-js/mdx-rs-binding-darwin-arm64': 0.1.8 + '@modern-js/mdx-rs-binding-darwin-x64': 0.1.8 + '@modern-js/mdx-rs-binding-linux-arm-gnueabihf': 0.1.8 + '@modern-js/mdx-rs-binding-linux-arm64-gnu': 0.1.8 + '@modern-js/mdx-rs-binding-linux-arm64-musl': 0.1.8 + '@modern-js/mdx-rs-binding-linux-x64-gnu': 0.1.8 + '@modern-js/mdx-rs-binding-linux-x64-musl': 0.1.8 + '@modern-js/mdx-rs-binding-win32-arm64-msvc': 0.1.8 + '@modern-js/mdx-rs-binding-win32-x64-msvc': 0.1.8 dev: false /@modern-js/plugin-i18n/2.18.0: From 9848c5c11febfcac99ddaa1fb37aa2ff65c1403b Mon Sep 17 00:00:00 2001 From: "gaoyuan.1226" Date: Mon, 15 May 2023 15:48:33 +0800 Subject: [PATCH 3/6] docs: update checkSyntax doc --- .changeset/gentle-adults-move.md | 9 +++++++++ .../builder-doc/docs/en/config/security/checkSyntax.md | 1 - .../builder-doc/docs/en/guide/advanced/rspack-start.mdx | 4 +++- .../builder-doc/docs/zh/config/security/checkSyntax.md | 1 - .../builder-doc/docs/zh/guide/advanced/rspack-start.mdx | 4 +++- 5 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 .changeset/gentle-adults-move.md diff --git a/.changeset/gentle-adults-move.md b/.changeset/gentle-adults-move.md new file mode 100644 index 000000000000..0a2ffac92d3d --- /dev/null +++ b/.changeset/gentle-adults-move.md @@ -0,0 +1,9 @@ +--- +'@modern-js/builder-rspack-provider': patch +'@modern-js/builder-shared': patch +'@modern-js/builder': patch +--- + +feat(rspack-provider): support security.checkSyntax in Rspack + +feat(rspack-provider): 在使用 Rspack 构建时支持 security.checkSyntax 配置项 diff --git a/packages/document/builder-doc/docs/en/config/security/checkSyntax.md b/packages/document/builder-doc/docs/en/config/security/checkSyntax.md index c809ee0add04..a8c5b04c576e 100644 --- a/packages/document/builder-doc/docs/en/config/security/checkSyntax.md +++ b/packages/document/builder-doc/docs/en/config/security/checkSyntax.md @@ -10,7 +10,6 @@ type CheckSyntax = ``` - **Default:** `false` -- **Bundler:** `only support webpack` Analyzes the build output files for the presence of high-level syntax that is incompatible with the current browserslist. If present, the details will be printed in the terminal. diff --git a/packages/document/builder-doc/docs/en/guide/advanced/rspack-start.mdx b/packages/document/builder-doc/docs/en/guide/advanced/rspack-start.mdx index ff997a60f50a..0a224e5f60f3 100644 --- a/packages/document/builder-doc/docs/en/guide/advanced/rspack-start.mdx +++ b/packages/document/builder-doc/docs/en/guide/advanced/rspack-start.mdx @@ -94,7 +94,9 @@ All configurations and capabilities under html are available within rspack. > Security related configurations in Builder. -All configurations and capabilities under security are **not supported** for use within rspack. +Unsupported configurations and capabilities include: + +- [source.sri](/api/config-security.html#securitysri) #### Dev Config diff --git a/packages/document/builder-doc/docs/zh/config/security/checkSyntax.md b/packages/document/builder-doc/docs/zh/config/security/checkSyntax.md index a882af0740e6..b9bc84187d5a 100644 --- a/packages/document/builder-doc/docs/zh/config/security/checkSyntax.md +++ b/packages/document/builder-doc/docs/zh/config/security/checkSyntax.md @@ -10,7 +10,6 @@ type CheckSyntax = ``` - **默认值:** `false` -- **打包工具:** `仅支持 webpack` 分析构建产物中是否存在当前浏览器范围下不兼容的高级语法。如果存在,会将详细信息打印在终端。 diff --git a/packages/document/builder-doc/docs/zh/guide/advanced/rspack-start.mdx b/packages/document/builder-doc/docs/zh/guide/advanced/rspack-start.mdx index 96eb0d0fb641..0bbc24f353f2 100644 --- a/packages/document/builder-doc/docs/zh/guide/advanced/rspack-start.mdx +++ b/packages/document/builder-doc/docs/zh/guide/advanced/rspack-start.mdx @@ -93,7 +93,9 @@ Builder 旨在消除不同打包工具之间的主要差异,帮助用户以较 > Builder 中与安全有关的配置。 -所有 security 下的配置项及能力**均不支持**在 Rspack 内使用。 +不支持的配置项及能力包括: + +- [source.sri](/api/config-security.html#securitysri) #### Dev Config From 2f4075a59abdf9e7b6020e195cfc6edfb4bf2b42 Mon Sep 17 00:00:00 2001 From: "gaoyuan.1226" Date: Tue, 23 May 2023 17:25:33 +0800 Subject: [PATCH 4/6] fix: move security.sri from shared to webpack-provider --- .../builder-shared/src/schema/security.ts | 10 +++++++- .../src/types/config/security.ts | 5 ---- .../src/config/validate/security.ts | 24 +++++++++++-------- .../src/types/config/security.ts | 18 ++++++++------ 4 files changed, 34 insertions(+), 23 deletions(-) diff --git a/packages/builder/builder-shared/src/schema/security.ts b/packages/builder/builder-shared/src/schema/security.ts index b5081f9e2272..a5df51fe1624 100644 --- a/packages/builder/builder-shared/src/schema/security.ts +++ b/packages/builder/builder-shared/src/schema/security.ts @@ -11,7 +11,15 @@ export const SriOptionsSchema: ZodType = z.partialObj({ }); export const sharedSecurityConfigSchema = z.partialObj({ - sri: z.union([SriOptionsSchema, z.boolean()]), + checkSyntax: z.union([ + z.boolean(), + z.object({ + targets: z.array(z.string()), + exclude: z.optional( + z.union([z.instanceof(RegExp), z.array(z.instanceof(RegExp))]), + ), + }), + ]), }); // eslint-disable-next-line @typescript-eslint/no-unused-vars diff --git a/packages/builder/builder-shared/src/types/config/security.ts b/packages/builder/builder-shared/src/types/config/security.ts index 155d5604cdae..2a4178b8f68c 100644 --- a/packages/builder/builder-shared/src/types/config/security.ts +++ b/packages/builder/builder-shared/src/types/config/security.ts @@ -10,11 +10,6 @@ export interface CheckSyntaxOptions { } export interface SharedSecurityConfig { - /** - * Adding an integrity attribute (`integrity`) to sub-resources introduced by HTML allows the browser to - * verify the integrity of the introduced resource, thus preventing tampering with the downloaded resource. - */ - sri?: SriOptions | boolean; /** Analyze the product for the presence of high-level syntax that is not compatible in the specified environment */ checkSyntax?: boolean | CheckSyntaxOptions; } diff --git a/packages/builder/builder-webpack-provider/src/config/validate/security.ts b/packages/builder/builder-webpack-provider/src/config/validate/security.ts index b969b7f83deb..964b6111449b 100644 --- a/packages/builder/builder-webpack-provider/src/config/validate/security.ts +++ b/packages/builder/builder-webpack-provider/src/config/validate/security.ts @@ -1,17 +1,21 @@ -import { sharedSecurityConfigSchema, z } from '@modern-js/builder-shared'; +import { + sharedSecurityConfigSchema, + z, + SriOptions, +} from '@modern-js/builder-shared'; import type { SecurityConfig } from '../../types'; +export const SriOptionsSchema: z.ZodType = z.partialObj({ + hashFuncNames: z.array(z.string()).min(1) as unknown as z.ZodType< + [string, ...string[]] + >, + enabled: z.literals(['auto', true, false]), + hashLoading: z.literals(['eager', 'lazy']), +}); + export const securityConfigSchema: z.ZodType = sharedSecurityConfigSchema .extend({ - checkSyntax: z.union([ - z.boolean(), - z.object({ - targets: z.array(z.string()), - exclude: z.optional( - z.union([z.instanceof(RegExp), z.array(z.instanceof(RegExp))]), - ), - }), - ]), + sri: z.union([SriOptionsSchema, z.boolean()]), }) .partial(); diff --git a/packages/builder/builder-webpack-provider/src/types/config/security.ts b/packages/builder/builder-webpack-provider/src/types/config/security.ts index a9219f3ccdcd..9290844f0817 100644 --- a/packages/builder/builder-webpack-provider/src/types/config/security.ts +++ b/packages/builder/builder-webpack-provider/src/types/config/security.ts @@ -1,10 +1,14 @@ -import type { SharedSecurityConfig } from '@modern-js/builder-shared'; +import type { + SriOptions, + SharedSecurityConfig, +} from '@modern-js/builder-shared'; -export interface CheckSyntaxOptions { - targets: string[]; - exclude?: RegExp | Array; -} - -export type SecurityConfig = SharedSecurityConfig; +export type SecurityConfig = SharedSecurityConfig & { + /** + * Adding an integrity attribute (`integrity`) to sub-resources introduced by HTML allows the browser to + * verify the integrity of the introduced resource, thus preventing tampering with the downloaded resource. + */ + sri?: SriOptions | boolean; +}; export type NormalizedSecurityConfig = Required; From e8c70f698029a37b2a91cfdfd7bad5276d0c2738 Mon Sep 17 00:00:00 2001 From: "gaoyuan.1226" Date: Tue, 23 May 2023 19:16:45 +0800 Subject: [PATCH 5/6] fix: pure deps --- packages/builder/builder-shared/package.json | 1 - .../builder-webpack-provider/package.json | 3 - pnpm-lock.yaml | 96 +------------------ 3 files changed, 4 insertions(+), 96 deletions(-) diff --git a/packages/builder/builder-shared/package.json b/packages/builder/builder-shared/package.json index 737ce913fd7e..c0d0ffe6d2a7 100644 --- a/packages/builder/builder-shared/package.json +++ b/packages/builder/builder-shared/package.json @@ -108,7 +108,6 @@ "@babel/preset-env": "^7.21.5", "@scripts/vitest-config": "workspace:*", "@types/babel__core": "^7.20.0", - "@types/babel__preset-env": "^7.9.2", "@types/caniuse-lite": "^1.0.1", "@types/node": "^14", "html-webpack-plugin": "5.5.0", diff --git a/packages/builder/builder-webpack-provider/package.json b/packages/builder/builder-webpack-provider/package.json index 8e1b0e082c44..9b0b329d0d0e 100644 --- a/packages/builder/builder-webpack-provider/package.json +++ b/packages/builder/builder-webpack-provider/package.json @@ -96,7 +96,6 @@ "@modern-js/types": "workspace:*", "@modern-js/utils": "workspace:*", "@pmmmwh/react-refresh-webpack-plugin": "0.5.9", - "@babel/core": "7.18.0", "css-minimizer-webpack-plugin": "5.0.0", "cssnano": "6.0.0", "fork-ts-checker-webpack-plugin": "8.0.0", @@ -112,8 +111,6 @@ "devDependencies": { "@modern-js/e2e": "workspace:*", "@scripts/vitest-config": "workspace:*", - "@types/babel__core": "^7.20.0", - "@types/babel__preset-env": "^7.9.2", "@types/caniuse-lite": "^1.0.1", "@types/node": "^14", "antd": "4", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8099e0e8692d..c44acbd9f1c0 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -131,7 +131,6 @@ importers: '@modern-js/utils': workspace:* '@scripts/vitest-config': workspace:* '@types/babel__core': ^7.20.0 - '@types/babel__preset-env': ^7.9.2 '@types/caniuse-lite': ^1.0.1 '@types/node': ^14 acorn: ^8.8.1 @@ -163,7 +162,6 @@ importers: '@babel/preset-env': 7.21.5_@babel+core@7.21.8 '@scripts/vitest-config': link:../../../scripts/vitest-config '@types/babel__core': 7.20.0 - '@types/babel__preset-env': 7.9.2 '@types/caniuse-lite': 1.0.1 '@types/node': 14.18.35 html-webpack-plugin: 5.5.0_webpack@5.82.1 @@ -173,7 +171,6 @@ importers: packages/builder/builder-webpack-provider: specifiers: - '@babel/core': 7.18.0 '@modern-js/babel-preset-app': workspace:* '@modern-js/babel-preset-base': workspace:* '@modern-js/builder-shared': workspace:* @@ -184,8 +181,6 @@ importers: '@modern-js/utils': workspace:* '@pmmmwh/react-refresh-webpack-plugin': 0.5.9 '@scripts/vitest-config': workspace:* - '@types/babel__core': ^7.20.0 - '@types/babel__preset-env': ^7.9.2 '@types/caniuse-lite': ^1.0.1 '@types/node': ^14 antd: '4' @@ -204,7 +199,6 @@ importers: typescript: ^5 webpack: ^5.82.1 dependencies: - '@babel/core': 7.18.0 '@modern-js/babel-preset-app': link:../../cli/babel-preset-app '@modern-js/babel-preset-base': link:../../cli/babel-preset-base '@modern-js/builder-shared': link:../builder-shared @@ -227,8 +221,6 @@ importers: devDependencies: '@modern-js/e2e': link:../../toolkit/e2e '@scripts/vitest-config': link:../../../scripts/vitest-config - '@types/babel__core': 7.20.0 - '@types/babel__preset-env': 7.9.2 '@types/caniuse-lite': 1.0.1 '@types/node': 14.18.35 antd: 4.21.4_biqbaboplfbrettd7655fr4n2y @@ -6101,29 +6093,6 @@ packages: - supports-color dev: false - /@babel/core/7.18.0: - resolution: {integrity: sha512-Xyw74OlJwDijToNi0+6BBI5mLLR5+5R3bcSH80LXzjzEGEUlvNzujEE71BaD/ApEZHAvFI/Mlmp4M5lIkdeeWw==} - engines: {node: '>=6.9.0'} - dependencies: - '@ampproject/remapping': 2.2.0 - '@babel/code-frame': 7.18.6 - '@babel/generator': 7.20.5 - '@babel/helper-compilation-targets': 7.20.0_@babel+core@7.18.0 - '@babel/helper-module-transforms': 7.20.2 - '@babel/helpers': 7.20.6 - '@babel/parser': 7.21.8 - '@babel/template': 7.18.10 - '@babel/traverse': 7.20.5 - '@babel/types': 7.21.5 - convert-source-map: 1.8.0 - debug: 4.3.4 - gensync: 1.0.0-beta.2 - json5: 2.2.3 - semver: 6.3.0 - transitivePeerDependencies: - - supports-color - dev: false - /@babel/core/7.20.5: resolution: {integrity: sha512-UdOWmk4pNWTm/4DlPUl/Pt4Gz4rcEMb7CY0Y3eJl5Yz1vI8ZJGmHWaVE55LoxRjdpx0z259GE9U5STA9atUinQ==} engines: {node: '>=6.9.0'} @@ -6195,15 +6164,6 @@ packages: eslint-rule-composer: 0.3.0 dev: false - /@babel/generator/7.20.5: - resolution: {integrity: sha512-jl7JY2Ykn9S0yj4DQP82sYvPU+T3g0HFcWTqDLqiuA9tGRNIj9VfbtXGAYTTkyNEnQk1jkMGOdYka8aG/lulCA==} - engines: {node: '>=6.9.0'} - dependencies: - '@babel/types': 7.21.5 - '@jridgewell/gen-mapping': 0.3.2 - jsesc: 2.5.2 - dev: false - /@babel/generator/7.21.5: resolution: {integrity: sha512-SrKK/sRv8GesIW1bDagf9cCG38IOMYZusoe1dfg0D8aiUe3Amvoj1QtjTPAWcfrZFvIwlleLb0gxzQidL9w14w==} engines: {node: '>=6.9.0'} @@ -6238,19 +6198,6 @@ packages: semver: 6.3.0 dev: false - /@babel/helper-compilation-targets/7.20.0_@babel+core@7.18.0: - resolution: {integrity: sha512-0jp//vDGp9e8hZzBc6N/KwA5ZK3Wsm/pfm4CrY7vzegkVxc65SgSn6wYOnwHe9Js9HRQ1YTCKLGPzDtaS3RoLQ==} - engines: {node: '>=6.9.0'} - peerDependencies: - '@babel/core': ^7.0.0 - dependencies: - '@babel/compat-data': 7.20.1 - '@babel/core': 7.18.0 - '@babel/helper-validator-option': 7.18.6 - browserslist: 4.21.4 - semver: 6.3.0 - dev: false - /@babel/helper-compilation-targets/7.20.0_@babel+core@7.20.5: resolution: {integrity: sha512-0jp//vDGp9e8hZzBc6N/KwA5ZK3Wsm/pfm4CrY7vzegkVxc65SgSn6wYOnwHe9Js9HRQ1YTCKLGPzDtaS3RoLQ==} engines: {node: '>=6.9.0'} @@ -6413,6 +6360,7 @@ packages: /@babel/helper-environment-visitor/7.18.9: resolution: {integrity: sha512-3r/aACDJ3fhQ/EVgFy0hpj8oHyHpQc+LPtJoY9SzTThAsStm4Ptegq92vqKoE3vD706ZVFWITnMnxucw+S9Ipg==} engines: {node: '>=6.9.0'} + dev: true /@babel/helper-environment-visitor/7.21.5: resolution: {integrity: sha512-IYl4gZ3ETsWocUWgsFZLM5i1BYx9SoemminVEXadgLBa9TdeorzgLKm8wWLA6J1N/kT3Kch8XIk1laNzYoHKvQ==} @@ -6424,14 +6372,6 @@ packages: dependencies: '@babel/types': 7.21.5 - /@babel/helper-function-name/7.19.0: - resolution: {integrity: sha512-WAwHBINyrpqywkUH0nTnNgI5ina5TFn85HKS0pbPDfxFfhyR/aNQEn4hGi1P1JyT//I0t4OgXUlofzWILRvS5w==} - engines: {node: '>=6.9.0'} - dependencies: - '@babel/template': 7.20.7 - '@babel/types': 7.21.5 - dev: false - /@babel/helper-function-name/7.21.0: resolution: {integrity: sha512-HfK1aMRanKHpxemaY2gqBmL04iAPOPRj7DxtNbiDOrJK+gdwkiNRVpCpUJYbUT+aZyemKN8brqTOxzCaG6ExRg==} engines: {node: '>=6.9.0'} @@ -6478,6 +6418,7 @@ packages: '@babel/types': 7.21.5 transitivePeerDependencies: - supports-color + dev: true /@babel/helper-module-transforms/7.21.5: resolution: {integrity: sha512-bI2Z9zBGY2q5yMHoBvJ2a9iX3ZOAzJPm7Q8Yz6YeoUjU/Cvhmi2G4QyTNyPBqqXSgTjUxRg3L0xV45HvkNWWBw==} @@ -6545,6 +6486,7 @@ packages: engines: {node: '>=6.9.0'} dependencies: '@babel/types': 7.21.5 + dev: true /@babel/helper-simple-access/7.21.5: resolution: {integrity: sha512-ENPDAMC1wAjR0uaCUwliBdiSl1KBJAVnMTzXqi64c2MG8MPR6ii4qf7bSXDqSFbr4W6W028/rf5ivoHop5/mkg==} @@ -6600,6 +6542,7 @@ packages: '@babel/types': 7.21.5 transitivePeerDependencies: - supports-color + dev: true /@babel/helpers/7.21.5: resolution: {integrity: sha512-BSY+JSlHxOmGsPTydUkPf1MdMQ3M81x5xGCOVgWM3G8XH77sJ292Y2oqcp0CbbgxhqBuI46iUz1tT7hqP7EfgA==} @@ -7914,15 +7857,6 @@ packages: dependencies: regenerator-runtime: 0.13.11 - /@babel/template/7.18.10: - resolution: {integrity: sha512-TI+rCtooWHr3QJ27kJxfjutghu44DLnasDMwpDqCXVTal9RLp3RSYNh4NdBrRP2cQAoG9A8juOQl6P6oZG4JxA==} - engines: {node: '>=6.9.0'} - dependencies: - '@babel/code-frame': 7.18.6 - '@babel/parser': 7.21.8 - '@babel/types': 7.21.5 - dev: false - /@babel/template/7.20.7: resolution: {integrity: sha512-8SegXApWe6VoNw0r9JHpSteLKTpTiLZ4rMlGIm9JQ18KiCtyQiAMEazujAHrUS5flrcqYZa75ukev3P6QmUwUw==} engines: {node: '>=6.9.0'} @@ -7931,24 +7865,6 @@ packages: '@babel/parser': 7.21.8 '@babel/types': 7.21.5 - /@babel/traverse/7.20.5: - resolution: {integrity: sha512-WM5ZNN3JITQIq9tFZaw1ojLU3WgWdtkxnhM1AegMS+PvHjkM5IXjmYEGY7yukz5XS4sJyEf2VzWjI8uAavhxBQ==} - engines: {node: '>=6.9.0'} - dependencies: - '@babel/code-frame': 7.18.6 - '@babel/generator': 7.21.5 - '@babel/helper-environment-visitor': 7.18.9 - '@babel/helper-function-name': 7.19.0 - '@babel/helper-hoist-variables': 7.18.6 - '@babel/helper-split-export-declaration': 7.18.6 - '@babel/parser': 7.21.8 - '@babel/types': 7.21.5 - debug: 4.3.4 - globals: 11.12.0 - transitivePeerDependencies: - - supports-color - dev: false - /@babel/traverse/7.21.5: resolution: {integrity: sha512-AhQoI3YjWi6u/y/ntv7k48mcrCXmus0t79J9qPNlk/lAsFlCiJ047RmbfMOawySTHtywXhbXgpx/8nXMYd+oFw==} engines: {node: '>=6.9.0'} @@ -12893,10 +12809,6 @@ packages: dependencies: '@babel/types': 7.21.5 - /@types/babel__preset-env/7.9.2: - resolution: {integrity: sha512-epEgKQiqTDZdPgYwtriYK1GVAGcyVZVvvw2UatX3+95mogKGimebApcMEWLF12uhUbNIvX284CSQEavnV/OIgw==} - dev: true - /@types/babel__template/7.4.1: resolution: {integrity: sha512-azBFKemX6kMg5Io+/rdGT0dkGreboUVR0Cdm3fz9QJWpaQGJRQXl7C+6hOTCZcMll7KFyEQpgbYI2lHdsS4U7g==} dependencies: From a558145a1fe058eaf1318101ec494c07d87478a4 Mon Sep 17 00:00:00 2001 From: "gaoyuan.1226" Date: Wed, 24 May 2023 10:27:38 +0800 Subject: [PATCH 6/6] fix: type SharedSecurityConfig --- .../src/types/config/security.ts | 11 ++++++----- .../builder/builder-shared/src/schema/security.ts | 1 + .../builder/builder-shared/src/types/config/index.ts | 7 +++++-- .../builder-shared/src/types/config/security.ts | 2 ++ .../src/types/config/security.ts | 4 +++- 5 files changed, 17 insertions(+), 8 deletions(-) diff --git a/packages/builder/builder-rspack-provider/src/types/config/security.ts b/packages/builder/builder-rspack-provider/src/types/config/security.ts index 68e2dec6e37a..9b35a8611f85 100644 --- a/packages/builder/builder-rspack-provider/src/types/config/security.ts +++ b/packages/builder/builder-rspack-provider/src/types/config/security.ts @@ -1,7 +1,8 @@ -import type { SharedSecurityConfig } from '@modern-js/builder-shared'; +import type { + SharedSecurityConfig, + NormalizedSharedSecurityConfig, +} from '@modern-js/builder-shared'; -export type SecurityConfig = SharedSecurityConfig & { - nonce?: string; -}; +export type SecurityConfig = SharedSecurityConfig; -export type NormalizedSecurityConfig = Required; +export type NormalizedSecurityConfig = NormalizedSharedSecurityConfig; diff --git a/packages/builder/builder-shared/src/schema/security.ts b/packages/builder/builder-shared/src/schema/security.ts index a5df51fe1624..787b275b2a3a 100644 --- a/packages/builder/builder-shared/src/schema/security.ts +++ b/packages/builder/builder-shared/src/schema/security.ts @@ -11,6 +11,7 @@ export const SriOptionsSchema: ZodType = z.partialObj({ }); export const sharedSecurityConfigSchema = z.partialObj({ + nonce: z.string(), checkSyntax: z.union([ z.boolean(), z.object({ diff --git a/packages/builder/builder-shared/src/types/config/index.ts b/packages/builder/builder-shared/src/types/config/index.ts index ea414d205ad8..6004f986c888 100644 --- a/packages/builder/builder-shared/src/types/config/index.ts +++ b/packages/builder/builder-shared/src/types/config/index.ts @@ -8,7 +8,10 @@ import type { SharedSourceConfig, NormalizedSharedSourceConfig, } from './source'; -import type { SharedSecurityConfig } from './security'; +import type { + SharedSecurityConfig, + NormalizedSharedSecurityConfig, +} from './security'; import type { SharedPerformanceConfig, NormalizedSharedPerformanceConfig, @@ -37,7 +40,7 @@ export type SharedNormalizedConfig = DeepReadonly<{ html: NormalizedSharedHtmlConfig; source: NormalizedSharedSourceConfig; output: NormalizedSharedOutputConfig; - security: Required; + security: NormalizedSharedSecurityConfig; performance: NormalizedSharedPerformanceConfig; tools: SharedToolsConfig; }>; diff --git a/packages/builder/builder-shared/src/types/config/security.ts b/packages/builder/builder-shared/src/types/config/security.ts index 5655d1ea9588..d956b189756c 100644 --- a/packages/builder/builder-shared/src/types/config/security.ts +++ b/packages/builder/builder-shared/src/types/config/security.ts @@ -19,3 +19,5 @@ export interface SharedSecurityConfig { */ nonce?: string; } + +export type NormalizedSharedSecurityConfig = Required; diff --git a/packages/builder/builder-webpack-provider/src/types/config/security.ts b/packages/builder/builder-webpack-provider/src/types/config/security.ts index 9290844f0817..a2789473dba4 100644 --- a/packages/builder/builder-webpack-provider/src/types/config/security.ts +++ b/packages/builder/builder-webpack-provider/src/types/config/security.ts @@ -1,6 +1,7 @@ import type { SriOptions, SharedSecurityConfig, + NormalizedSharedSecurityConfig, } from '@modern-js/builder-shared'; export type SecurityConfig = SharedSecurityConfig & { @@ -11,4 +12,5 @@ export type SecurityConfig = SharedSecurityConfig & { sri?: SriOptions | boolean; }; -export type NormalizedSecurityConfig = Required; +export type NormalizedSecurityConfig = NormalizedSharedSecurityConfig & + Required>;