From a4b851449a2533672dafabdeec13527d68bb974b Mon Sep 17 00:00:00 2001 From: ClarkXia Date: Wed, 24 May 2023 13:51:26 +0800 Subject: [PATCH 1/3] fix: compatible with code has import.meta (#6264) * fix: changelog * fix: test case --- .changeset/odd-colts-explain.md | 5 +++++ packages/webpack-config/src/utils/transformImport.ts | 3 ++- .../tests/fixtures/transformImport/importMeta.js | 1 + packages/webpack-config/tests/transformImport.test.ts | 5 +++++ 4 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 .changeset/odd-colts-explain.md create mode 100644 packages/webpack-config/tests/fixtures/transformImport/importMeta.js diff --git a/.changeset/odd-colts-explain.md b/.changeset/odd-colts-explain.md new file mode 100644 index 0000000000..25a83b3129 --- /dev/null +++ b/.changeset/odd-colts-explain.md @@ -0,0 +1,5 @@ +--- +'@ice/webpack-config': patch +--- + +fix: compatible with code has import.meta diff --git a/packages/webpack-config/src/utils/transformImport.ts b/packages/webpack-config/src/utils/transformImport.ts index 9c2c86d6c5..e05e4aeedc 100644 --- a/packages/webpack-config/src/utils/transformImport.ts +++ b/packages/webpack-config/src/utils/transformImport.ts @@ -12,7 +12,8 @@ const transformImport = async (source: string, coreJsPath: string) => { const str = () => s || (s = new MagicString(source)); const isESM = exports.length > 0 || imports.some((targetImport) => { const importString = targetImport.n; - return !importString.includes('core-js') && !importString.includes('@swc/helpers'); + // `targetImport.n` get undefined when code has `import.meta.*`. + return importString && !importString.includes('core-js') && !importString.includes('@swc/helpers'); }); imports.forEach((targetImport) => { if (!targetImport.n) { diff --git a/packages/webpack-config/tests/fixtures/transformImport/importMeta.js b/packages/webpack-config/tests/fixtures/transformImport/importMeta.js new file mode 100644 index 0000000000..776624f4eb --- /dev/null +++ b/packages/webpack-config/tests/fixtures/transformImport/importMeta.js @@ -0,0 +1 @@ +if (import.meta.rerender === 'client') console.log(true); \ No newline at end of file diff --git a/packages/webpack-config/tests/transformImport.test.ts b/packages/webpack-config/tests/transformImport.test.ts index 8cc39fdc61..69cb73027e 100644 --- a/packages/webpack-config/tests/transformImport.test.ts +++ b/packages/webpack-config/tests/transformImport.test.ts @@ -47,4 +47,9 @@ describe('transform core js path', () => { expect(await transformImport(orignalCode, coreJsPath)) .toBe('var _object_spread = require(\'@swc/helpers/cjs/_object_spread.cjs\')._;module.exports = {};'); }); + it('with import.meta', async () => { + const orignalCode = fs.readFileSync(path.join(__dirname, './fixtures/transformImport/importMeta.js'), 'utf-8'); + expect(await transformImport(orignalCode, coreJsPath)) + .toBe('if (import.meta.rerender === \'client\') console.log(true);'); + }); }); From f652be72b29895cad2f7d6771e702ddef708c6cd Mon Sep 17 00:00:00 2001 From: luhc228 Date: Wed, 24 May 2023 13:51:42 +0800 Subject: [PATCH 2/3] fix: import identifier is invalid in route config (#6265) * fix: import identifier is invalid in route config * chore: changeset --- .changeset/long-tomatoes-drum.md | 5 +++++ examples/routes-generate/ice.config.mts | 1 + examples/routes-generate/src/pages/about.tsx | 6 +++++- packages/ice/src/routes.ts | 8 ++++++-- 4 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 .changeset/long-tomatoes-drum.md diff --git a/.changeset/long-tomatoes-drum.md b/.changeset/long-tomatoes-drum.md new file mode 100644 index 0000000000..e1b7a6f3cf --- /dev/null +++ b/.changeset/long-tomatoes-drum.md @@ -0,0 +1,5 @@ +--- +'@ice/app': patch +--- + +fix: import identifier is invalid in route config diff --git a/examples/routes-generate/ice.config.mts b/examples/routes-generate/ice.config.mts index 4e84aa7555..5154e56ab2 100644 --- a/examples/routes-generate/ice.config.mts +++ b/examples/routes-generate/ice.config.mts @@ -6,6 +6,7 @@ export default defineConfig(() => ({ ignoreFiles: ['about.tsx', 'products.tsx'], defineRoutes: (route) => { route('/about-me', 'about.tsx'); + route('/about.html', 'about.tsx'); route('/', 'layout.tsx', () => { route('/product', 'products.tsx'); diff --git a/examples/routes-generate/src/pages/about.tsx b/examples/routes-generate/src/pages/about.tsx index 43bfb7bae5..0be794a990 100644 --- a/examples/routes-generate/src/pages/about.tsx +++ b/examples/routes-generate/src/pages/about.tsx @@ -1,6 +1,10 @@ import * as React from 'react'; -import { Link } from 'ice'; +import { Link, definePageConfig } from 'ice'; export default function About() { return <>

About

home; } + +export const pageConfig = definePageConfig(() => ({ + +})); \ No newline at end of file diff --git a/packages/ice/src/routes.ts b/packages/ice/src/routes.ts index f8be7cbbf9..7e95dbab05 100644 --- a/packages/ice/src/routes.ts +++ b/packages/ice/src/routes.ts @@ -66,7 +66,7 @@ export function getRoutesDefinition(nestRouteManifest: NestedRouteManifest[], la if (lazy) { loadStatement = `import(/* webpackChunkName: "p_${componentName}" */ '${formatPath(componentPath)}')`; } else { - const routeSpecifier = id.replace(/[./-]/g, '_').replace(/[:*]/g, '$'); + const routeSpecifier = formatRouteSpecifier(id); routeImports.push(`import * as ${routeSpecifier} from '${formatPath(componentPath)}';`); loadStatement = routeSpecifier; } @@ -145,7 +145,7 @@ function generateRouteConfig( const componentFile = file.replace(new RegExp(`${fileExtname}$`), ''); const componentPath = path.isAbsolute(componentFile) ? componentFile : `@/pages/${componentFile}`; - const loaderName = `${exportKey}_${id}`.replace(/[-/]/g, '_'); + const loaderName = formatRouteSpecifier(`${exportKey}_${id}`); const fullPath = path.join(parentPath, routePath); imports.push([id, loaderName, fullPath]); str = `import { ${exportKey} as ${loaderName} } from '${componentPath}';\n`; @@ -162,3 +162,7 @@ function generateRouteConfig( } return template(importConfig(routes, ''), imports); } + +function formatRouteSpecifier(routeId: string) { + return routeId.replace(/[./-]/g, '_').replace(/[:*]/g, '$'); +} From 4d19b912da05d15da57e5ac5cdeac9b773ffefd5 Mon Sep 17 00:00:00 2001 From: ClarkXia Date: Wed, 24 May 2023 15:53:55 +0800 Subject: [PATCH 3/3] chore: update versions (#6267) --- .changeset/long-tomatoes-drum.md | 5 ----- .changeset/odd-colts-explain.md | 5 ----- packages/ice/CHANGELOG.md | 8 ++++++++ packages/ice/package.json | 4 ++-- packages/plugin-i18n/package.json | 2 +- packages/webpack-config/CHANGELOG.md | 6 ++++++ packages/webpack-config/package.json | 2 +- pnpm-lock.yaml | 2 +- 8 files changed, 19 insertions(+), 15 deletions(-) delete mode 100644 .changeset/long-tomatoes-drum.md delete mode 100644 .changeset/odd-colts-explain.md diff --git a/.changeset/long-tomatoes-drum.md b/.changeset/long-tomatoes-drum.md deleted file mode 100644 index e1b7a6f3cf..0000000000 --- a/.changeset/long-tomatoes-drum.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -'@ice/app': patch ---- - -fix: import identifier is invalid in route config diff --git a/.changeset/odd-colts-explain.md b/.changeset/odd-colts-explain.md deleted file mode 100644 index 25a83b3129..0000000000 --- a/.changeset/odd-colts-explain.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -'@ice/webpack-config': patch ---- - -fix: compatible with code has import.meta diff --git a/packages/ice/CHANGELOG.md b/packages/ice/CHANGELOG.md index b3aa259c43..05b741009e 100644 --- a/packages/ice/CHANGELOG.md +++ b/packages/ice/CHANGELOG.md @@ -1,5 +1,13 @@ # Changelog +## 3.2.2 + +### Patch Changes + +- f652be72: fix: import identifier is invalid in route config +- Updated dependencies [a4b85144] + - @ice/webpack-config@1.0.15 + ## 3.2.1 ### Patch Changes diff --git a/packages/ice/package.json b/packages/ice/package.json index ca3b0cf61d..317631cbc1 100644 --- a/packages/ice/package.json +++ b/packages/ice/package.json @@ -1,6 +1,6 @@ { "name": "@ice/app", - "version": "3.2.1", + "version": "3.2.2", "description": "provide scripts and configuration used by web framework ice", "type": "module", "main": "./esm/index.js", @@ -39,7 +39,7 @@ "@ice/bundles": "0.1.10", "@ice/route-manifest": "1.2.0", "@ice/runtime": "^1.2.1", - "@ice/webpack-config": "1.0.14", + "@ice/webpack-config": "1.0.15", "@swc/helpers": "0.5.1", "@types/express": "^4.17.14", "address": "^1.1.2", diff --git a/packages/plugin-i18n/package.json b/packages/plugin-i18n/package.json index 7ca03a7c18..ab8f63e869 100644 --- a/packages/plugin-i18n/package.json +++ b/packages/plugin-i18n/package.json @@ -55,7 +55,7 @@ "webpack-dev-server": "^4.13.2" }, "peerDependencies": { - "@ice/app": "^3.2.1", + "@ice/app": "^3.2.2", "@ice/runtime": "^1.2.1" }, "publishConfig": { diff --git a/packages/webpack-config/CHANGELOG.md b/packages/webpack-config/CHANGELOG.md index b5e3294291..79228cb2b2 100644 --- a/packages/webpack-config/CHANGELOG.md +++ b/packages/webpack-config/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 1.0.15 + +### Patch Changes + +- a4b85144: fix: compatible with code has import.meta + ## 1.0.14 ### Patch Changes diff --git a/packages/webpack-config/package.json b/packages/webpack-config/package.json index aa398410c4..8e1d0c3fee 100644 --- a/packages/webpack-config/package.json +++ b/packages/webpack-config/package.json @@ -1,6 +1,6 @@ { "name": "@ice/webpack-config", - "version": "1.0.14", + "version": "1.0.15", "repository": "alibaba/ice", "bugs": "https://github.com/alibaba/ice/issues", "homepage": "https://v3.ice.work", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 54bfb7e1d5..b6a08e6220 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1058,7 +1058,7 @@ importers: '@ice/bundles': 0.1.10 '@ice/route-manifest': 1.2.0 '@ice/runtime': ^1.2.1 - '@ice/webpack-config': 1.0.14 + '@ice/webpack-config': 1.0.15 '@swc/helpers': 0.5.1 '@types/babel__generator': ^7.6.4 '@types/babel__traverse': ^7.17.1