Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions packages/s2-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 9 additions & 4 deletions packages/s2-core/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ const output = {

const plugins = [
peerDepsExternal(),
replace({
'process.env.NODE_ENV': JSON.stringify('production'),
preventAssignment: true,
}),
commonjs(),
resolve(),
typescript({
Expand All @@ -49,6 +45,15 @@ const plugins = [
},
},
}),
replace({
// 类型检查后再替换,避免将测试环境判断变成不相交的字面量类型比较。
// 方括号访问以非单词字符结尾,同时避免匹配其它标识符或对象成员。
delimiters: ['(?<![\\w$.])', '(?![\\w$.])'],
'process.env.NODE_ENV': JSON.stringify('production'),
"process.env['NODE_ENV']": JSON.stringify('production'),
'process.env["NODE_ENV"]': JSON.stringify('production'),
preventAssignment: true,
}),
postcss({
exclude: ['**/styles/theme/*.less'],
minimize: isUmdFormat,
Expand Down
72 changes: 72 additions & 0 deletions packages/s2-core/rollup.config.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises';
import test from 'node:test';
import configs from './rollup.config.mjs';

const replace = configs[0].plugins.find((plugin) => 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']"));
});
Loading