Skip to content
Draft
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
16 changes: 16 additions & 0 deletions .github/workflows/node-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,19 @@ jobs:
run: npm ci
- name: Run lint
run: npm run lint
run-build:
name: Run build
runs-on: ubuntu-latest
defaults:
run:
working-directory: node
steps:
- uses: actions/setup-node@v7
with:
node-version: '26'
- name: Check out Git repository
uses: actions/checkout@v7
- name: Install dependencies
run: npm ci
- name: Run build
run: npm run build
131 changes: 76 additions & 55 deletions node/build/build.test.js

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's hard to identify all of the changes here from the diff. Is it entirely just moving some behaviour into functions and not changing any of the actual implementation?

Original file line number Diff line number Diff line change
Expand Up @@ -18,66 +18,87 @@ import { fileURLToPath } from 'node:url'
const nodeRoot = resolve(dirname(fileURLToPath(import.meta.url)), '..')
const projectRoot = resolve(nodeRoot, '..')

describe('Django asset build', () => {
it('builds the complete asset set from the Node package without root npm tooling', () => {
const fixture = mkdtempSync(join(tmpdir(), 'direct asset build-'))
const expectedBuildFiles = [
'css/theme.css',
'css/theme.css.map',
'css/theme.min.css',
'css/theme.min.css.map',
'css/vendor.bundle.css',
'js/main.js',
'js/main.js.map',
'js/main.min.js',
'js/main.min.js.map',
'js/vendor.bundle.js',
'js/vendor.bundle.js.map',
]

try {
cpSync(nodeRoot, join(fixture, 'node'), {
recursive: true,
filter: (source) =>
!['node_modules', 'storybook-static'].includes(
relative(nodeRoot, source).split(sep)[0]
),
})
cpSync(join(projectRoot, '.editorconfig'), join(fixture, '.editorconfig'))
symlinkSync(
join(nodeRoot, 'node_modules'),
join(fixture, 'node/node_modules'),
'junction'
)
function createBuildFixture() {
const fixture = mkdtempSync(join(tmpdir(), 'direct asset build-'))

cpSync(nodeRoot, join(fixture, 'node'), {
recursive: true,
filter: (source) =>
!['node_modules', 'storybook-static'].includes(
relative(nodeRoot, source).split(sep)[0]
),
})
cpSync(join(projectRoot, '.editorconfig'), join(fixture, '.editorconfig'))
symlinkSync(
join(nodeRoot, 'node_modules'),
join(fixture, 'node/node_modules'),
'junction'
)

const assets = join(fixture, 'main/static/assets')
mkdirSync(join(assets, 'css'), { recursive: true })
mkdirSync(join(assets, 'js'), { recursive: true })

return { fixture, assets }
}

function runAssetBuild(fixture) {
execFileSync('npm', ['--prefix', join(fixture, 'node'), 'run', 'build'], {
cwd: fixture,
stdio: 'pipe',
timeout: 120000,
})
}

const assets = join(fixture, 'main/static/assets')
mkdirSync(join(assets, 'css'), { recursive: true })
mkdirSync(join(assets, 'js'), { recursive: true })
function expectSourceMapSourcesToExist(assets, file) {
const map = JSON.parse(readFileSync(join(assets, file), 'utf8'))
expect(map.version).toBe(3)
expect(map.sources.length).toBeGreaterThan(0)

execFileSync('npm', ['--prefix', join(fixture, 'node'), 'run', 'build'], {
cwd: fixture,
stdio: 'pipe',
timeout: 120000,
})
for (const source of map.sources) {
const sourcePath = source.startsWith('file:')
? fileURLToPath(source)
: resolve(dirname(join(assets, file)), source)
expect(existsSync(sourcePath), source).toBe(true)
}
}

const files = readdirSync(assets, { recursive: true }).filter((file) =>
/\.(css|js|map)$/.test(file)
)
expect(files.sort()).toEqual([
'css/theme.css',
'css/theme.css.map',
'css/theme.min.css',
'css/theme.min.css.map',
'css/vendor.bundle.css',
'js/main.js',
'js/main.js.map',
'js/main.min.js',
'js/main.min.js.map',
'js/vendor.bundle.js',
'js/vendor.bundle.js.map',
])
function expectBuildOutputs(assets) {
const files = readdirSync(assets, { recursive: true }).filter((file) =>
/\.(css|js|map)$/.test(file)
)
expect(files.sort()).toEqual(expectedBuildFiles)

for (const file of files) {
expect(readFileSync(join(assets, file)).length).toBeGreaterThan(0)
if (file.endsWith('.map')) {
const map = JSON.parse(readFileSync(join(assets, file), 'utf8'))
expect(map.version).toBe(3)
expect(map.sources.length).toBeGreaterThan(0)
for (const source of map.sources) {
const sourcePath = source.startsWith('file:')
? fileURLToPath(source)
: resolve(dirname(join(assets, file)), source)
expect(existsSync(sourcePath), source).toBe(true)
}
}
}
for (const file of files) {
expect(readFileSync(join(assets, file)).length).toBeGreaterThan(0)
if (file.endsWith('.map')) {
expectSourceMapSourcesToExist(assets, file)
}
}
}

describe('Django asset build', () => {
it('builds the complete asset set from the Node package without root npm tooling', () => {
// Keep the build isolated from the repository's generated assets.
const { fixture, assets } = createBuildFixture()

try {
runAssetBuild(fixture)
expectBuildOutputs(assets)
} finally {
rmSync(fixture, { recursive: true, force: true })
}
Expand Down