-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack-json-compact-loader.test.js
44 lines (39 loc) · 2.42 KB
/
webpack-json-compact-loader.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { deepStrictEqual } from 'node:assert'
import { resolve } from 'node:path'
import { resetDirectory } from '@dr-js/core/module/node/fs/Directory.js'
import { modifyDelete } from '@dr-js/core/module/node/fs/Modify.js'
import { runStdout } from '@dr-js/core/module/node/run.js'
import { getKit } from '@dr-js/core/module/node/kit.js'
import { compileWithWebpack, commonFlag } from './webpack.js'
import { getWebpackBabelConfig } from './babel.js'
const { describe, it, before, after, info = console.log } = globalThis
const PATH_TEST_ROOT = resolve(__dirname, 'test-webpack-json-compact-loader-gitignore/')
before(() => resetDirectory(PATH_TEST_ROOT))
after(() => modifyDelete(PATH_TEST_ROOT))
describe('webpack-json-compact-loader', () => {
it('test webpack build', async () => {
const kit = getKit({ PATH_TEMP: PATH_TEST_ROOT, logFunc: info })
const { mode, isWatch, isProduction, profileOutput, getCommonWebpackConfig } = await commonFlag({ kit })
const config = getCommonWebpackConfig({
babelOption: null, // do not use default
output: { path: kit.fromTemp(), filename: 'index.js', libraryTarget: 'commonjs2' },
entry: { 'index': kit.fromRoot('script/webpack-json-compact-loader.test/index.js') },
extraModuleRuleList: [
{ test: /\.js$/, exclude: /\.@json\.js$/, use: { loader: 'babel-loader', options: getWebpackBabelConfig({ isProduction }) } },
{ test: /\.@json\.js$/, use: { loader: resolve(__dirname, './webpack-json-compact-loader.js'), options: { useConst: true } } }
]
})
kit.padLog(`compile with webpack mode: ${mode}, isWatch: ${Boolean(isWatch)}`)
await compileWithWebpack({ config, isWatch, profileOutput, kit })
const runNodeForStdout = async (...argList) => {
const stdoutString = String(await runStdout([ ...argList ], { cwd: kit.fromRoot() }))
kit.log(stdoutString)
return stdoutString
}
kit.padLog('run test output')
const testOutputString = await runNodeForStdout(process.execPath, '-p', 'JSON.stringify(require(process.argv[1]))', kit.fromTemp('index.js'))
kit.padLog('run test source')
const testSourceString = await runNodeForStdout(process.execPath, '-r', '@babel/register', '-p', 'JSON.stringify(require(process.argv[1]))', kit.fromRoot('script/webpack-json-compact-loader.test/index.js'))
deepStrictEqual(JSON.parse(testOutputString), JSON.parse(testSourceString), 'the data should be the same through webpack')
})
})