-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Gary Wu
committed
Oct 9, 2024
1 parent
42bf5d7
commit 46383dc
Showing
10 changed files
with
193 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
packages/nx-plugin/src/utils/create-rspack-config.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { ExecutorContext } from '@nx/devkit'; | ||
import { createRspackConfig } from './create-rspack-config'; | ||
import { BuildExecutorSchema } from '../executors/build/schema'; | ||
import { Compiler, RspackOptionsNormalized, RuleSetRule } from '@rspack/core'; | ||
import { NgRspackPlugin } from 'packages/build/src/lib/plugins/ng-rspack'; | ||
|
||
describe('Create rspack config', () => { | ||
let executorContext: ExecutorContext = null; | ||
beforeEach(() => { | ||
executorContext = { | ||
root: '/<workspace-root>', | ||
cwd: '/<workspace-root>', | ||
isVerbose: false, | ||
projectName: 'the-project', | ||
projectGraph: { | ||
nodes: { | ||
'the-project': { | ||
type: 'app', | ||
name: 'the-project', | ||
data: { | ||
root: 'apps/the-project', | ||
name: 'the-project', | ||
} | ||
} | ||
}, | ||
dependencies: {}, | ||
} | ||
}; | ||
}); | ||
|
||
it('resolve paths relative to project root', async () => { | ||
const options: BuildExecutorSchema = { | ||
outputPath: 'dist', | ||
main: 'src/main.ts', | ||
index: 'src/index.html', | ||
tsConfig: 'ts.config.json', | ||
stylePreprocessorOptions: { | ||
includePaths: [ | ||
'src/styles', | ||
'../../packages/common/styles', | ||
], | ||
}, | ||
polyfills: ['zone.js'], | ||
scripts: ['src/scripts/base.js'], | ||
styles: ['src/styles/base.scss'], | ||
assets: ['src/assets/base.png'], | ||
}; | ||
|
||
process.env['NODE_ENV'] = 'development'; | ||
process.env['WEBPACK_SERVE'] = 'true'; | ||
|
||
const rspackConfig = createRspackConfig(options, executorContext); | ||
|
||
expect(rspackConfig.context).toBe( | ||
'/<workspace-root>/apps/the-project' | ||
); | ||
expect(rspackConfig.entry['main'].import).toEqual( | ||
['src/main.ts'] | ||
); | ||
expect(rspackConfig.output.uniqueName).toBe('the-project'); | ||
expect(rspackConfig.output.path).toBe( | ||
'/<workspace-root>/apps/the-project/dist' | ||
); | ||
expect((rspackConfig.resolve.tsConfig as { configFile: string }).configFile).toBe( | ||
'/<workspace-root>/apps/the-project/ts.config.json' | ||
); | ||
|
||
const hmrLoaderRule = rspackConfig.module.rules.find((rule: RuleSetRule) => rule.loader.endsWith('/build/src/lib/loaders/hmr/hmr-loader.ts')) as RuleSetRule; | ||
expect(hmrLoaderRule.include).toEqual( | ||
['/<workspace-root>/apps/the-project/src/main.ts'] | ||
); | ||
|
||
const sassLoaderRule = rspackConfig.module.rules.find((rule: RuleSetRule) => (rule.test as RegExp)?.test('.scss')) as RuleSetRule; | ||
expect(sassLoaderRule.use[0].options.sassOptions.loadPaths).toEqual( | ||
[ | ||
'/<workspace-root>/apps/the-project/src/styles', | ||
'/<workspace-root>/packages/common/styles', | ||
] | ||
); | ||
|
||
expect(options.polyfills).toEqual(['zone.js']); | ||
expect(options.scripts).toEqual(['src/scripts/base.js']); | ||
expect(options.styles).toEqual(['src/styles/base.scss']); | ||
expect(options.assets).toEqual(['src/assets/base.png']); | ||
|
||
const compiler = new Compiler('context', { output: {}, resolve: rspackConfig.resolve } as RspackOptionsNormalized); | ||
(rspackConfig.plugins[0] as NgRspackPlugin).apply(compiler); | ||
|
||
// TODO remove the following tricky way to check the correctness of plugin options | ||
expect(compiler.__internal__builtinPlugins.filter((plugin) => plugin.name === 'EntryPlugin') | ||
.map(({ options: { entry } }: any) => entry)) | ||
.toEqual(['zone.js', 'src/styles/base.scss', 'src/scripts/base.js']); | ||
expect(compiler.__internal__builtinPlugins.filter((plugin) => plugin.name === 'CopyRspackPlugin') | ||
.map(({ options: { patterns }}: any) => patterns.map((pattern) => pattern.from))) | ||
.toEqual([['/<workspace-root>/apps/the-project/src/assets/base.png']]); | ||
expect(compiler.__internal__builtinPlugins.filter((plugin) => plugin.name === 'HtmlRspackPlugin') | ||
.map(({ options }: any) => options.template)) | ||
.toEqual(['/<workspace-root>/apps/the-project/src/index.html']) | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,5 +10,5 @@ | |
"src/**/*.test.ts", | ||
"src/**/*.spec.ts", | ||
"src/**/*.d.ts" | ||
] | ||
, "../build/src/lib/utils/create-config.spec.ts" ] | ||
} |