diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index dd8d1350e1..9a94af22b4 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -39,6 +39,9 @@ jobs: - name: Lint scripts, type, style and docs run: pnpm lint + - name: Test build configuration + run: pnpm --filter @antv/s2 test:build + - name: Build run: pnpm build diff --git a/packages/s2-core/package.json b/packages/s2-core/package.json index 7e66624277..77c643b00d 100644 --- a/packages/s2-core/package.json +++ b/packages/s2-core/package.json @@ -64,6 +64,7 @@ "start": "pnpm test:live", "sync-event": "node ./scripts/sync-event.mjs", "test": "jest --passWithNoTests --detectOpenHandles", + "test:build": "cross-env FORMAT=umd node --test rollup.config.test.mjs", "test:ci": "pnpm test test:ci-coverage --verbose --detectOpenHandles --maxWorkers=3", "test:ci-coverage": "pnpm test:coverage --verbose --detectOpenHandles --maxWorkers=3", "test:coverage": "pnpm test -- --coverage", diff --git a/packages/s2-core/rollup.config.mjs b/packages/s2-core/rollup.config.mjs index a3d1d2046a..ccd6e3f8cb 100644 --- a/packages/s2-core/rollup.config.mjs +++ b/packages/s2-core/rollup.config.mjs @@ -31,10 +31,6 @@ const output = { const plugins = [ peerDepsExternal(), - replace({ - 'process.env.NODE_ENV': JSON.stringify('production'), - preventAssignment: true, - }), commonjs(), resolve(), typescript({ @@ -49,6 +45,15 @@ const plugins = [ }, }, }), + replace({ + // 类型检查后再替换,避免将测试环境判断变成不相交的字面量类型比较。 + // 方括号访问以非单词字符结尾,同时避免匹配其它标识符或对象成员。 + delimiters: ['(? plugin.name === 'replace'); + +function transform(code) { + return replace.transform(code, 'env-fixture.ts')?.code ?? code; +} + +const accessForms = [ + 'process.env.NODE_ENV', + "process.env['NODE_ENV']", + 'process.env["NODE_ENV"]', +]; + +for (const access of accessForms) { + test(`replaces ${access} with the production environment`, () => { + assert.equal( + transform(`const value = ${access};`), + 'const value = "production";', + ); + assert.equal(transform(access), '"production"'); + assert.equal( + transform(`${access} === 'test'`), + '"production" === \'test\'', + ); + }); + + test(`does not replace assignments to ${access}`, () => { + const code = `${access} = 'test';`; + + assert.equal(transform(code), code); + }); + + for (const prefix of ['other', '$', 'object.', 'object?.']) { + test(`does not replace ${access} after ${prefix}`, () => { + const code = `const value = ${prefix}${access};`; + + assert.equal(transform(code), code); + }); + } + + test(`does not replace nested property access after ${access}`, () => { + const code = `const value = ${access}.length;`; + + assert.equal(transform(code), code); + }); +} + +test('does not replace another environment variable', () => { + const code = [ + 'process.env.NODE_ENV_EXTRA', + 'process.env.NODE_ENV$other', + "process.env['NODE_ENV_EXTRA']", + 'process.env["NODE_ENV_EXTRA"]', + ].join(';'); + + assert.equal(transform(code), code); +}); + +test('replaces the environment check in the actual table export source', async () => { + const code = await readFile( + new URL('./src/utils/export/copy/table-copy.ts', import.meta.url), + 'utf8', + ); + const transformed = transform(code); + + assert.ok(transformed.includes('"production" === \'test\'')); + assert.ok(!transformed.includes("process.env['NODE_ENV']")); +});